1
1
package com.mifos.feature.settings.settings
2
2
3
+ import android.content.ComponentName
3
4
import android.content.Context
5
+ import android.content.Intent
6
+ import android.content.pm.PackageManager
7
+ import android.os.CountDownTimer
8
+ import android.widget.Toast
4
9
import androidx.compose.foundation.layout.Column
5
10
import androidx.compose.foundation.layout.Row
6
11
import androidx.compose.foundation.layout.Spacer
12
+ import androidx.compose.foundation.layout.fillMaxSize
7
13
import androidx.compose.foundation.layout.fillMaxWidth
8
14
import androidx.compose.foundation.layout.padding
9
15
import androidx.compose.foundation.lazy.LazyColumn
10
16
import androidx.compose.foundation.lazy.items
11
17
import androidx.compose.foundation.shape.RoundedCornerShape
12
18
import androidx.compose.material3.Card
13
19
import androidx.compose.material3.CardDefaults
20
+ import androidx.compose.material3.ExperimentalMaterial3Api
14
21
import androidx.compose.material3.Icon
15
22
import androidx.compose.material3.MaterialTheme
23
+ import androidx.compose.material3.ModalBottomSheet
16
24
import androidx.compose.material3.SnackbarHostState
17
25
import androidx.compose.material3.Text
26
+ import androidx.compose.material3.rememberModalBottomSheetState
18
27
import androidx.compose.runtime.Composable
19
28
import androidx.compose.runtime.getValue
20
29
import androidx.compose.runtime.mutableStateOf
@@ -25,11 +34,13 @@ import androidx.compose.ui.Alignment
25
34
import androidx.compose.ui.Modifier
26
35
import androidx.compose.ui.graphics.Color
27
36
import androidx.compose.ui.graphics.vector.ImageVector
37
+ import androidx.compose.ui.platform.LocalAutofill
28
38
import androidx.compose.ui.platform.LocalContext
29
39
import androidx.compose.ui.res.stringArrayResource
30
40
import androidx.compose.ui.res.stringResource
31
41
import androidx.compose.ui.tooling.preview.Preview
32
42
import androidx.compose.ui.unit.dp
43
+ import androidx.core.content.ContentProviderCompat.requireContext
33
44
import androidx.hilt.navigation.compose.hiltViewModel
34
45
import androidx.lifecycle.compose.collectAsStateWithLifecycle
35
46
import com.mifos.core.common.enums.MifosAppLanguage
@@ -40,6 +51,7 @@ import com.mifos.core.designsystem.component.UpdateEndpointDialogScreen
40
51
import com.mifos.core.designsystem.icon.MifosIcons
41
52
import com.mifos.feature.settings.R
42
53
import com.mifos.feature.settings.syncSurvey.SyncSurveysDialog
54
+ import com.mifos.feature.settings.updateServer.UpdateServerConfigScreenRoute
43
55
import java.util.Locale
44
56
45
57
@Composable
@@ -48,7 +60,6 @@ fun SettingsScreen(
48
60
navigateToLoginScreen : () -> Unit ,
49
61
changePasscode : (String ) -> Unit ,
50
62
languageChanged : () -> Unit ,
51
- serverConfig : () -> Unit
52
63
) {
53
64
val viewModel: SettingsViewModel = hiltViewModel()
54
65
val baseURL by viewModel.baseUrl.collectAsStateWithLifecycle()
@@ -82,11 +93,11 @@ fun SettingsScreen(
82
93
)
83
94
languageChanged()
84
95
},
85
- serverConfig = serverConfig
86
96
)
87
97
}
88
98
89
99
100
+ @OptIn(ExperimentalMaterial3Api ::class )
90
101
@Composable
91
102
fun SettingsScreen (
92
103
onBackPressed : () -> Unit ,
@@ -98,14 +109,17 @@ fun SettingsScreen(
98
109
handleEndpointUpdate : (baseURL: String , tenant: String ) -> Unit ,
99
110
updateTheme : (theme: AppTheme ) -> Unit ,
100
111
updateLanguage : (language: MifosAppLanguage ) -> Unit ,
101
- serverConfig : () -> Unit
102
112
) {
103
113
104
114
val snackbarHostState = remember { SnackbarHostState () }
105
115
var showLanguageUpdateDialog by rememberSaveable { mutableStateOf(false ) }
106
116
var showEndpointUpdateDialog by rememberSaveable { mutableStateOf(false ) }
107
117
var showThemeUpdateDialog by rememberSaveable { mutableStateOf(false ) }
108
118
var showSyncSurveyDialog by rememberSaveable { mutableStateOf(false ) }
119
+ var showServerConfig by rememberSaveable { mutableStateOf(false ) }
120
+
121
+ val sheetState = rememberModalBottomSheetState()
122
+ val context = LocalContext .current
109
123
110
124
MifosScaffold (
111
125
icon = MifosIcons .arrowBack,
@@ -129,24 +143,36 @@ fun SettingsScreen(
129
143
130
144
SettingsCardItem .ENDPOINT -> showEndpointUpdateDialog = true
131
145
132
- SettingsCardItem .SERVER_CONFIG -> {
133
- serverConfig()
134
-
135
- }
146
+ SettingsCardItem .SERVER_CONFIG -> showServerConfig = true
136
147
}
137
148
}
138
149
)
139
150
}
140
151
}
141
152
142
- if ( showSyncSurveyDialog ) {
153
+ if ( showSyncSurveyDialog) {
143
154
SyncSurveysDialog (
144
155
closeDialog = {
145
156
showSyncSurveyDialog = false
146
157
}
147
158
)
148
159
}
149
160
161
+ if (showServerConfig) {
162
+ ModalBottomSheet (
163
+ onDismissRequest = { showServerConfig = false },
164
+ sheetState = sheetState,
165
+ ) {
166
+ UpdateServerConfigScreenRoute (
167
+ onCloseClick = { showServerConfig = false },
168
+ onSuccessful = {
169
+ showServerConfig = false
170
+ showRestartCountdownToast(context, 2 )
171
+ }
172
+ )
173
+ }
174
+ }
175
+
150
176
if (showLanguageUpdateDialog) {
151
177
MifosRadioButtonDialog (
152
178
titleResId = R .string.feature_settings_choose_language,
@@ -254,6 +280,33 @@ fun updateLanguageLocale(context: Context, language: String, isSystemLanguage: B
254
280
}
255
281
}
256
282
283
+ private fun showRestartCountdownToast (context : Context , seconds : Int ) {
284
+ val countDownTimer = object : CountDownTimer ((seconds * 1000 ).toLong(), 1000 ) {
285
+ override fun onTick (millisUntilFinished : Long ) {
286
+ val secondsRemaining = millisUntilFinished / 1000
287
+ Toast .makeText(
288
+ context,
289
+ " Restarting app in $secondsRemaining seconds" ,
290
+ Toast .LENGTH_SHORT
291
+ ).show()
292
+ }
293
+
294
+ override fun onFinish () {
295
+ context.restartApplication()
296
+ }
297
+ }
298
+ countDownTimer.start()
299
+ }
300
+
301
+ fun Context.restartApplication () {
302
+ val packageManager: PackageManager = this .packageManager
303
+ val intent: Intent = packageManager.getLaunchIntentForPackage(this .packageName)!!
304
+ val componentName: ComponentName = intent.component!!
305
+ val restartIntent: Intent = Intent .makeRestartActivityTask(componentName)
306
+ this .startActivity(restartIntent)
307
+ Runtime .getRuntime().exit(0 )
308
+ }
309
+
257
310
@Composable
258
311
@Preview(showSystemUi = true , showBackground = true )
259
312
fun PreviewSettingsScreen () {
@@ -267,7 +320,6 @@ fun PreviewSettingsScreen() {
267
320
updateLanguage = {},
268
321
updateTheme = {},
269
322
changePasscode = {},
270
- serverConfig = {}
271
323
)
272
324
273
325
}
0 commit comments