1-
21import androidx.compose.desktop.ui.tooling.preview.Preview
32import androidx.compose.foundation.layout.Column
43import androidx.compose.foundation.layout.Row
54import androidx.compose.foundation.layout.fillMaxHeight
65import androidx.compose.foundation.layout.fillMaxSize
6+ import androidx.compose.foundation.layout.height
77import androidx.compose.foundation.layout.padding
88import androidx.compose.material.MaterialTheme
99import androidx.compose.runtime.Composable
@@ -17,26 +17,19 @@ import components.CustomButton
1717import components.CustomTextField
1818import components.SelectCountries
1919import components.Toast
20+ import components.translateDialog
2021import kotlinx.coroutines.CoroutineScope
2122import kotlinx.coroutines.Dispatchers
2223import kotlinx.coroutines.delay
2324import kotlinx.coroutines.launch
24- import translate.translateHttpURLConnection
25- import utils.createOutputMainFolder
26- import utils.createStringFolder
27- import utils.defaultLanguageCode
28- import utils.endResourcesWrite
25+ import utils.WindowState
2926import utils.languageList
30- import utils.readAllStrings
31- import utils.startResourcesWrite
32- import utils.stringWrite
33- import java.net.URLEncoder
3427
3528@Composable
3629@Preview
3730fun App () {
3831
39- var isShowWindowState by remember { mutableStateOf(false ) }
32+ var isWindowShow by remember { mutableStateOf(WindowState . NO_STATE ) }
4033 var stringState by remember {
4134 mutableStateOf(
4235 """
@@ -48,15 +41,7 @@ fun App() {
4841 """ .trimIndent()
4942 )
5043 }
51- // <string name="create_thumbnail">Save thumbnail</string>
52- // <string name="settings">Settings</string>
53- // <string name="download_settings_desc" translatable="false">General, format, custom command</string>
54- // <string name="download">Download</string>
55- // <string name="url_empty">The link cannot be empty</string>
56- // <string name="extract_audio_summary">Download and save audio, instead of video</string>
57- // <string name="create_thumbnail_summary" translatable="false">Save video thumbnail as a file</string>
58- // <string name="yt_dlp_up_to_date">Using the latest version of yt-dlp</string>
59-
44+ var folderState by remember { mutableStateOf(" String Translator App" ) }
6045 var countryListState by remember { mutableStateOf(languageList) }
6146 var isShowToast by remember { mutableStateOf(false ) }
6247 var toastMessage by remember { mutableStateOf(" " ) }
@@ -74,59 +59,50 @@ fun App() {
7459 Modifier .weight(0.2f , true ).padding(start = 10 .dp)
7560 ) {
7661 CustomButton (" Select Languages" , onClick = {
77- isShowWindowState = true
62+ isWindowShow = WindowState . SELECT_COUNTRY
7863 })
64+ CustomTextField (
65+ folderState,
66+ " Enter the Folder Name" ,
67+ Modifier .height(80 .dp),
68+ ) { folderState = it }
7969 CustomButton (" Convert" ,
80- isEnable = stringState.isNotEmpty() && countryListState.any { it.isChecked },
70+ isEnable = stringState.isNotEmpty() && folderState.isNotEmpty() && countryListState.any { it.isChecked },
8171 onClick = {
82- createOutputMainFolder()
83- try {
84- val allStrings = readAllStrings(stringState)
85-
86- println (countryListState.filter { it.isChecked })
87- countryListState.filter { it.isChecked }
88- .forEachIndexed { index, language ->
89- val stringFile =
90- createStringFolder(language.code)
91- stringFile.startResourcesWrite()
92- for ((name, isTranslatable, textContent) in allStrings) {
93- println (" Name: $name , isTranslatable: $isTranslatable , Text Content: $textContent " )
94- if (isTranslatable) {
95-
96- // stringFile.stringWrite(name, someConvertText.random())
97- val convertedString = translateHttpURLConnection(
98- URLEncoder .encode(textContent, " UTF-8" ),
99- URLEncoder .encode(defaultLanguageCode, " UTF-8" ),
100- URLEncoder .encode(language.code, " UTF-8" ),
101- ) {
102- isShowToast = true
103- toastMessage = it
104- CoroutineScope (Dispatchers .IO ).launch {
105- delay(3000 )
106- isShowToast = false
107- }
108- }
109- stringFile.stringWrite(name, convertedString)
110- } else {
111- stringFile.stringWrite(name, textContent, false )
112- }
113- }
114- stringFile.endResourcesWrite()
115- }
116- } catch (e: Exception ) {
117- e.printStackTrace()
118- }
119- })
72+ isWindowShow = WindowState .CONVERT_TRANSLATE
73+ }
74+ )
12075 }
12176 }
122- if (isShowWindowState) {
123- SelectCountries (countryListState) {
124- countryListState = it
125- isShowWindowState = false
77+ when (isWindowShow) {
78+ WindowState .SELECT_COUNTRY -> {
79+ SelectCountries (countryListState) {
80+ countryListState = it
81+ isWindowShow = WindowState .NO_STATE
82+ }
83+ }
84+
85+ WindowState .CONVERT_TRANSLATE -> {
86+ translateDialog(countryListState.filter { it.isChecked }, folderState,stringState, { errorMsg ->
87+ isShowToast = true
88+ toastMessage = errorMsg
89+ CoroutineScope (Dispatchers .IO ).launch {
90+ delay(3000 )
91+ isShowToast = false
92+ }
93+ }, {
94+ isWindowShow = WindowState .NO_STATE
95+ })
96+ }
97+
98+ else -> {
99+
126100 }
127101 }
128102 if (isShowToast) {
129- Toast (toastMessage) {}
103+ Toast (toastMessage) {
104+ isShowToast = false
105+ }
130106 }
131107
132108 }
0 commit comments