Skip to content

Commit

Permalink
MIFOSAC-269 compose navigation in settings module (#2203)
Browse files Browse the repository at this point in the history
  • Loading branch information
itsPronay authored Aug 22, 2024
1 parent bde6483 commit 01c8e45
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,25 @@ import androidx.navigation.NavController
import androidx.navigation.NavGraphBuilder
import androidx.navigation.compose.composable
import com.mifos.feature.settings.settings.SettingsScreen
import com.mifos.feature.settings.updateServer.UpdateServerConfigScreenRoute

/**
* Created by Pronay Sarker on 10/08/2024 (7:52 AM)
*/
const val SETTINGS_SCREEN_ROUTE = "settings_screen_route"

fun NavController.navigateToSettingsScreen() {
this.navigate(SETTINGS_SCREEN_ROUTE)
}

fun NavGraphBuilder.settingsScreen(
navigateBack: () -> Unit,
navigateToLoginScreen: () -> Unit,
changePasscode: (String) -> Unit,
languageChanged: () -> Unit,
serverConfig: () -> Unit
) {
composable(SETTINGS_SCREEN_ROUTE) {
composable(
route = SettingsScreens.SettingsScreen.route
) {
SettingsScreen(
onBackPressed = navigateBack,
navigateToLoginScreen = navigateToLoginScreen,
languageChanged = languageChanged,
serverConfig = serverConfig,
changePasscode = { }
changePasscode = changePasscode
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.mifos.feature.settings.navigation

/**
* Created by Pronay Sarker on 22/08/2024 (4:31 PM)
*/
sealed class SettingsScreens(val route: String) {
data object SettingsScreen : SettingsScreens("settings_screen")
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
package com.mifos.feature.settings.settings

import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.CountDownTimer
import android.widget.Toast
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.ModalBottomSheet
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Text
import androidx.compose.material3.rememberModalBottomSheetState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
Expand All @@ -25,11 +34,13 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.platform.LocalAutofill
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringArrayResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.core.content.ContentProviderCompat.requireContext
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.mifos.core.common.enums.MifosAppLanguage
Expand All @@ -40,6 +51,7 @@ import com.mifos.core.designsystem.component.UpdateEndpointDialogScreen
import com.mifos.core.designsystem.icon.MifosIcons
import com.mifos.feature.settings.R
import com.mifos.feature.settings.syncSurvey.SyncSurveysDialog
import com.mifos.feature.settings.updateServer.UpdateServerConfigScreenRoute
import java.util.Locale

@Composable
Expand All @@ -48,7 +60,6 @@ fun SettingsScreen(
navigateToLoginScreen: () -> Unit,
changePasscode: (String) -> Unit,
languageChanged: () -> Unit,
serverConfig: () -> Unit
) {
val viewModel: SettingsViewModel = hiltViewModel()
val baseURL by viewModel.baseUrl.collectAsStateWithLifecycle()
Expand Down Expand Up @@ -82,11 +93,11 @@ fun SettingsScreen(
)
languageChanged()
},
serverConfig = serverConfig
)
}


@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun SettingsScreen(
onBackPressed: () -> Unit,
Expand All @@ -98,14 +109,17 @@ fun SettingsScreen(
handleEndpointUpdate: (baseURL: String, tenant: String) -> Unit,
updateTheme: (theme: AppTheme) -> Unit,
updateLanguage: (language: MifosAppLanguage) -> Unit,
serverConfig: () -> Unit
) {

val snackbarHostState = remember { SnackbarHostState() }
var showLanguageUpdateDialog by rememberSaveable { mutableStateOf(false) }
var showEndpointUpdateDialog by rememberSaveable { mutableStateOf(false) }
var showThemeUpdateDialog by rememberSaveable { mutableStateOf(false) }
var showSyncSurveyDialog by rememberSaveable { mutableStateOf(false) }
var showServerConfig by rememberSaveable { mutableStateOf(false) }

val sheetState = rememberModalBottomSheetState()
val context = LocalContext.current

MifosScaffold(
icon = MifosIcons.arrowBack,
Expand All @@ -129,24 +143,36 @@ fun SettingsScreen(

SettingsCardItem.ENDPOINT -> showEndpointUpdateDialog = true

SettingsCardItem.SERVER_CONFIG -> {
serverConfig()

}
SettingsCardItem.SERVER_CONFIG -> showServerConfig = true
}
}
)
}
}

if( showSyncSurveyDialog ) {
if (showSyncSurveyDialog) {
SyncSurveysDialog(
closeDialog = {
showSyncSurveyDialog = false
}
)
}

if (showServerConfig) {
ModalBottomSheet(
onDismissRequest = { showServerConfig = false },
sheetState = sheetState,
) {
UpdateServerConfigScreenRoute(
onCloseClick = { showServerConfig = false },
onSuccessful = {
showServerConfig = false
showRestartCountdownToast(context, 2)
}
)
}
}

if (showLanguageUpdateDialog) {
MifosRadioButtonDialog(
titleResId = R.string.feature_settings_choose_language,
Expand Down Expand Up @@ -254,6 +280,33 @@ fun updateLanguageLocale(context: Context, language: String, isSystemLanguage: B
}
}

private fun showRestartCountdownToast(context: Context, seconds: Int) {
val countDownTimer = object : CountDownTimer((seconds * 1000).toLong(), 1000) {
override fun onTick(millisUntilFinished: Long) {
val secondsRemaining = millisUntilFinished / 1000
Toast.makeText(
context,
"Restarting app in $secondsRemaining seconds",
Toast.LENGTH_SHORT
).show()
}

override fun onFinish() {
context.restartApplication()
}
}
countDownTimer.start()
}

fun Context.restartApplication() {
val packageManager: PackageManager = this.packageManager
val intent: Intent = packageManager.getLaunchIntentForPackage(this.packageName)!!
val componentName: ComponentName = intent.component!!
val restartIntent: Intent = Intent.makeRestartActivityTask(componentName)
this.startActivity(restartIntent)
Runtime.getRuntime().exit(0)
}

@Composable
@Preview(showSystemUi = true, showBackground = true)
fun PreviewSettingsScreen() {
Expand All @@ -267,7 +320,6 @@ fun PreviewSettingsScreen() {
updateLanguage = {},
updateTheme = {},
changePasscode = {},
serverConfig = {}
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import com.mifos.feature.checker_inbox_task.navigation.CheckerInboxTaskScreens
import com.mifos.feature.groups.navigation.GroupScreen
import com.mifos.feature.path_tracking.navigation.PathTrackingScreens
import com.mifos.feature.search.navigation.SearchScreens
import com.mifos.feature.settings.navigation.SETTINGS_SCREEN_ROUTE
import com.mifos.feature.settings.navigation.SettingsScreens

sealed class HomeDestinationsScreen(
val title: String = "",
Expand Down Expand Up @@ -80,7 +80,7 @@ sealed class HomeDestinationsScreen(

data object SettingsScreen : HomeDestinationsScreen(
title = "Settings",
route = SETTINGS_SCREEN_ROUTE,
route = SettingsScreens.SettingsScreen.route,
icon = Icons.Rounded.Settings
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ class SettingsFragment : Fragment() {
startActivity(intent)
activity?.finish()
},
serverConfig = {
findNavController().navigate(R.id.updateServerConfigFragment)
}
// serverConfig = {
// findNavController().navigate(R.id.updateServerConfigFragment)
// }
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,37 +39,11 @@ class UpdateServerConfigFragment : BottomSheetDialogFragment() {
onSuccessful = {
this@UpdateServerConfigFragment.dismiss()

showRestartCountdownToast(requireContext(), 2)
// showRestartCountdownToast(requireContext(), 2)
}
)
}
}
}
}

private fun showRestartCountdownToast(context: Context, seconds: Int) {
val countDownTimer = object : CountDownTimer((seconds * 1000).toLong(), 1000) {
override fun onTick(millisUntilFinished: Long) {
val secondsRemaining = millisUntilFinished / 1000
Toast.makeText(
context,
"Restarting app in $secondsRemaining seconds",
Toast.LENGTH_SHORT
).show()
}

override fun onFinish() {
context.restartApplication()
}
}
countDownTimer.start()
}

fun Context.restartApplication() {
val packageManager: PackageManager = this.packageManager
val intent: Intent = packageManager.getLaunchIntentForPackage(this.packageName)!!
val componentName: ComponentName = intent.component!!
val restartIntent: Intent = Intent.makeRestartActivityTask(componentName)
this.startActivity(restartIntent)
Runtime.getRuntime().exit(0)
}
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,10 @@ fun Navigation(
)

settingsScreen(
navigateBack = { navController.popBackStack() },
navigateBack = navController::popBackStack,
navigateToLoginScreen = {},
changePasscode = {},
languageChanged = { },
serverConfig = {}
)

aboutScreen(
Expand Down

0 comments on commit 01c8e45

Please sign in to comment.