Skip to content

Commit 01c8e45

Browse files
authored
MIFOSAC-269 compose navigation in settings module (#2203)
1 parent bde6483 commit 01c8e45

File tree

7 files changed

+81
-53
lines changed

7 files changed

+81
-53
lines changed

feature/settings/src/main/java/com/mifos/feature/settings/navigation/SettingsNavigation.kt

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,25 @@ import androidx.navigation.NavController
44
import androidx.navigation.NavGraphBuilder
55
import androidx.navigation.compose.composable
66
import com.mifos.feature.settings.settings.SettingsScreen
7+
import com.mifos.feature.settings.updateServer.UpdateServerConfigScreenRoute
78

89
/**
910
* Created by Pronay Sarker on 10/08/2024 (7:52 AM)
1011
*/
11-
const val SETTINGS_SCREEN_ROUTE = "settings_screen_route"
12-
13-
fun NavController.navigateToSettingsScreen() {
14-
this.navigate(SETTINGS_SCREEN_ROUTE)
15-
}
16-
1712
fun NavGraphBuilder.settingsScreen(
1813
navigateBack: () -> Unit,
1914
navigateToLoginScreen: () -> Unit,
2015
changePasscode: (String) -> Unit,
2116
languageChanged: () -> Unit,
22-
serverConfig: () -> Unit
2317
) {
24-
composable(SETTINGS_SCREEN_ROUTE) {
18+
composable(
19+
route = SettingsScreens.SettingsScreen.route
20+
) {
2521
SettingsScreen(
2622
onBackPressed = navigateBack,
2723
navigateToLoginScreen = navigateToLoginScreen,
2824
languageChanged = languageChanged,
29-
serverConfig = serverConfig,
30-
changePasscode = { }
25+
changePasscode = changePasscode
3126
)
3227
}
3328
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.mifos.feature.settings.navigation
2+
3+
/**
4+
* Created by Pronay Sarker on 22/08/2024 (4:31 PM)
5+
*/
6+
sealed class SettingsScreens(val route: String) {
7+
data object SettingsScreen : SettingsScreens("settings_screen")
8+
}

feature/settings/src/main/java/com/mifos/feature/settings/settings/SettingsScreen.kt

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
11
package com.mifos.feature.settings.settings
22

3+
import android.content.ComponentName
34
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
49
import androidx.compose.foundation.layout.Column
510
import androidx.compose.foundation.layout.Row
611
import androidx.compose.foundation.layout.Spacer
12+
import androidx.compose.foundation.layout.fillMaxSize
713
import androidx.compose.foundation.layout.fillMaxWidth
814
import androidx.compose.foundation.layout.padding
915
import androidx.compose.foundation.lazy.LazyColumn
1016
import androidx.compose.foundation.lazy.items
1117
import androidx.compose.foundation.shape.RoundedCornerShape
1218
import androidx.compose.material3.Card
1319
import androidx.compose.material3.CardDefaults
20+
import androidx.compose.material3.ExperimentalMaterial3Api
1421
import androidx.compose.material3.Icon
1522
import androidx.compose.material3.MaterialTheme
23+
import androidx.compose.material3.ModalBottomSheet
1624
import androidx.compose.material3.SnackbarHostState
1725
import androidx.compose.material3.Text
26+
import androidx.compose.material3.rememberModalBottomSheetState
1827
import androidx.compose.runtime.Composable
1928
import androidx.compose.runtime.getValue
2029
import androidx.compose.runtime.mutableStateOf
@@ -25,11 +34,13 @@ import androidx.compose.ui.Alignment
2534
import androidx.compose.ui.Modifier
2635
import androidx.compose.ui.graphics.Color
2736
import androidx.compose.ui.graphics.vector.ImageVector
37+
import androidx.compose.ui.platform.LocalAutofill
2838
import androidx.compose.ui.platform.LocalContext
2939
import androidx.compose.ui.res.stringArrayResource
3040
import androidx.compose.ui.res.stringResource
3141
import androidx.compose.ui.tooling.preview.Preview
3242
import androidx.compose.ui.unit.dp
43+
import androidx.core.content.ContentProviderCompat.requireContext
3344
import androidx.hilt.navigation.compose.hiltViewModel
3445
import androidx.lifecycle.compose.collectAsStateWithLifecycle
3546
import com.mifos.core.common.enums.MifosAppLanguage
@@ -40,6 +51,7 @@ import com.mifos.core.designsystem.component.UpdateEndpointDialogScreen
4051
import com.mifos.core.designsystem.icon.MifosIcons
4152
import com.mifos.feature.settings.R
4253
import com.mifos.feature.settings.syncSurvey.SyncSurveysDialog
54+
import com.mifos.feature.settings.updateServer.UpdateServerConfigScreenRoute
4355
import java.util.Locale
4456

4557
@Composable
@@ -48,7 +60,6 @@ fun SettingsScreen(
4860
navigateToLoginScreen: () -> Unit,
4961
changePasscode: (String) -> Unit,
5062
languageChanged: () -> Unit,
51-
serverConfig: () -> Unit
5263
) {
5364
val viewModel: SettingsViewModel = hiltViewModel()
5465
val baseURL by viewModel.baseUrl.collectAsStateWithLifecycle()
@@ -82,11 +93,11 @@ fun SettingsScreen(
8293
)
8394
languageChanged()
8495
},
85-
serverConfig = serverConfig
8696
)
8797
}
8898

8999

100+
@OptIn(ExperimentalMaterial3Api::class)
90101
@Composable
91102
fun SettingsScreen(
92103
onBackPressed: () -> Unit,
@@ -98,14 +109,17 @@ fun SettingsScreen(
98109
handleEndpointUpdate: (baseURL: String, tenant: String) -> Unit,
99110
updateTheme: (theme: AppTheme) -> Unit,
100111
updateLanguage: (language: MifosAppLanguage) -> Unit,
101-
serverConfig: () -> Unit
102112
) {
103113

104114
val snackbarHostState = remember { SnackbarHostState() }
105115
var showLanguageUpdateDialog by rememberSaveable { mutableStateOf(false) }
106116
var showEndpointUpdateDialog by rememberSaveable { mutableStateOf(false) }
107117
var showThemeUpdateDialog by rememberSaveable { mutableStateOf(false) }
108118
var showSyncSurveyDialog by rememberSaveable { mutableStateOf(false) }
119+
var showServerConfig by rememberSaveable { mutableStateOf(false) }
120+
121+
val sheetState = rememberModalBottomSheetState()
122+
val context = LocalContext.current
109123

110124
MifosScaffold(
111125
icon = MifosIcons.arrowBack,
@@ -129,24 +143,36 @@ fun SettingsScreen(
129143

130144
SettingsCardItem.ENDPOINT -> showEndpointUpdateDialog = true
131145

132-
SettingsCardItem.SERVER_CONFIG -> {
133-
serverConfig()
134-
135-
}
146+
SettingsCardItem.SERVER_CONFIG -> showServerConfig = true
136147
}
137148
}
138149
)
139150
}
140151
}
141152

142-
if( showSyncSurveyDialog ) {
153+
if (showSyncSurveyDialog) {
143154
SyncSurveysDialog(
144155
closeDialog = {
145156
showSyncSurveyDialog = false
146157
}
147158
)
148159
}
149160

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+
150176
if (showLanguageUpdateDialog) {
151177
MifosRadioButtonDialog(
152178
titleResId = R.string.feature_settings_choose_language,
@@ -254,6 +280,33 @@ fun updateLanguageLocale(context: Context, language: String, isSystemLanguage: B
254280
}
255281
}
256282

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+
257310
@Composable
258311
@Preview(showSystemUi = true, showBackground = true)
259312
fun PreviewSettingsScreen() {
@@ -267,7 +320,6 @@ fun PreviewSettingsScreen() {
267320
updateLanguage = {},
268321
updateTheme = {},
269322
changePasscode = {},
270-
serverConfig = {}
271323
)
272324

273325
}

mifosng-android/src/main/java/com/mifos/mifosxdroid/HomeDestinationsScreen.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import com.mifos.feature.checker_inbox_task.navigation.CheckerInboxTaskScreens
1717
import com.mifos.feature.groups.navigation.GroupScreen
1818
import com.mifos.feature.path_tracking.navigation.PathTrackingScreens
1919
import com.mifos.feature.search.navigation.SearchScreens
20-
import com.mifos.feature.settings.navigation.SETTINGS_SCREEN_ROUTE
20+
import com.mifos.feature.settings.navigation.SettingsScreens
2121

2222
sealed class HomeDestinationsScreen(
2323
val title: String = "",
@@ -80,7 +80,7 @@ sealed class HomeDestinationsScreen(
8080

8181
data object SettingsScreen : HomeDestinationsScreen(
8282
title = "Settings",
83-
route = SETTINGS_SCREEN_ROUTE,
83+
route = SettingsScreens.SettingsScreen.route,
8484
icon = Icons.Rounded.Settings
8585
)
8686

mifosng-android/src/main/java/com/mifos/mifosxdroid/activity/setting/SettingsFragment.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ class SettingsFragment : Fragment() {
5555
startActivity(intent)
5656
activity?.finish()
5757
},
58-
serverConfig = {
59-
findNavController().navigate(R.id.updateServerConfigFragment)
60-
}
58+
// serverConfig = {
59+
// findNavController().navigate(R.id.updateServerConfigFragment)
60+
// }
6161
)
6262
}
6363
}

mifosng-android/src/main/java/com/mifos/mifosxdroid/activity/setting/UpdateServerConfigFragment.kt

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -39,37 +39,11 @@ class UpdateServerConfigFragment : BottomSheetDialogFragment() {
3939
onSuccessful = {
4040
this@UpdateServerConfigFragment.dismiss()
4141

42-
showRestartCountdownToast(requireContext(), 2)
42+
// showRestartCountdownToast(requireContext(), 2)
4343
}
4444
)
4545
}
4646
}
4747
}
4848
}
4949

50-
private fun showRestartCountdownToast(context: Context, seconds: Int) {
51-
val countDownTimer = object : CountDownTimer((seconds * 1000).toLong(), 1000) {
52-
override fun onTick(millisUntilFinished: Long) {
53-
val secondsRemaining = millisUntilFinished / 1000
54-
Toast.makeText(
55-
context,
56-
"Restarting app in $secondsRemaining seconds",
57-
Toast.LENGTH_SHORT
58-
).show()
59-
}
60-
61-
override fun onFinish() {
62-
context.restartApplication()
63-
}
64-
}
65-
countDownTimer.start()
66-
}
67-
68-
fun Context.restartApplication() {
69-
val packageManager: PackageManager = this.packageManager
70-
val intent: Intent = packageManager.getLaunchIntentForPackage(this.packageName)!!
71-
val componentName: ComponentName = intent.component!!
72-
val restartIntent: Intent = Intent.makeRestartActivityTask(componentName)
73-
this.startActivity(restartIntent)
74-
Runtime.getRuntime().exit(0)
75-
}

mifosng-android/src/main/java/com/mifos/mifosxdroid/components/Navigation.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,10 @@ fun Navigation(
177177
)
178178

179179
settingsScreen(
180-
navigateBack = { navController.popBackStack() },
180+
navigateBack = navController::popBackStack,
181181
navigateToLoginScreen = {},
182182
changePasscode = {},
183183
languageChanged = { },
184-
serverConfig = {}
185184
)
186185

187186
aboutScreen(

0 commit comments

Comments
 (0)