Skip to content

Commit

Permalink
Settings Module CleanUp
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkeye14 committed Nov 13, 2024
1 parent 87b5d66 commit 49e755f
Show file tree
Hide file tree
Showing 24 changed files with 389 additions and 636 deletions.
9 changes: 9 additions & 0 deletions feature/settings/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/*
* Copyright 2024 Mifos Initiative
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* See https://github.com/openMF/android-client/blob/master/LICENSE.md
*/
plugins {
alias(libs.plugins.mifos.android.feature)
alias(libs.plugins.mifos.android.library.compose)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
/*
* Copyright 2024 Mifos Initiative
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* See https://github.com/openMF/android-client/blob/master/LICENSE.md
*/
package com.mifos.feature.settings

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import androidx.test.platform.app.InstrumentationRegistry
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
Expand All @@ -21,4 +28,4 @@ class ExampleInstrumentedTest {
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.mifos.feature.settings", appContext.packageName)
}
}
}
9 changes: 9 additions & 0 deletions feature/settings/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2024 Mifos Initiative
This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
If a copy of the MPL was not distributed with this file,
You can obtain one at https://mozilla.org/MPL/2.0/.
See https://github.com/openMF/android-client/blob/master/LICENSE.md
-->
<manifest>


Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
/*
* Copyright 2024 Mifos Initiative
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* See https://github.com/openMF/android-client/blob/master/LICENSE.md
*/
package com.mifos.feature.settings.navigation

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)
Expand All @@ -16,13 +23,13 @@ fun NavGraphBuilder.settingsScreen(
languageChanged: () -> Unit,
) {
composable(
route = SettingsScreens.SettingsScreen.route
route = SettingsScreens.SettingsScreen.route,
) {
SettingsScreen(
onBackPressed = navigateBack,
navigateToLoginScreen = navigateToLoginScreen,
languageChanged = languageChanged,
changePasscode = changePasscode
changePasscode = changePasscode,
)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
/*
* Copyright 2024 Mifos Initiative
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* See https://github.com/openMF/android-client/blob/master/LICENSE.md
*/
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,3 +1,12 @@
/*
* Copyright 2024 Mifos Initiative
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* See https://github.com/openMF/android-client/blob/master/LICENSE.md
*/
package com.mifos.feature.settings.settings

import android.content.ComponentName
Expand All @@ -9,7 +18,6 @@ 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
Expand All @@ -34,13 +42,11 @@ 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 @@ -55,7 +61,7 @@ import com.mifos.feature.settings.updateServer.UpdateServerConfigScreenRoute
import java.util.Locale

@Composable
fun SettingsScreen(
internal fun SettingsScreen(
onBackPressed: () -> Unit,
navigateToLoginScreen: () -> Unit,
changePasscode: (String) -> Unit,
Expand Down Expand Up @@ -89,17 +95,16 @@ fun SettingsScreen(
updateLanguageLocale(
context = context,
language = it.code,
isSystemLanguage = isSystemLanguage
isSystemLanguage = isSystemLanguage,
)
languageChanged()
},
)
}


@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun SettingsScreen(
internal fun SettingsScreen(
onBackPressed: () -> Unit,
selectedLanguage: String,
selectedTheme: String,
Expand All @@ -110,7 +115,6 @@ fun SettingsScreen(
updateTheme: (theme: AppTheme) -> Unit,
updateLanguage: (language: MifosAppLanguage) -> Unit,
) {

val snackbarHostState = remember { SnackbarHostState() }
var showLanguageUpdateDialog by rememberSaveable { mutableStateOf(false) }
var showEndpointUpdateDialog by rememberSaveable { mutableStateOf(false) }
Expand All @@ -128,7 +132,7 @@ fun SettingsScreen(
snackbarHostState = snackbarHostState,
) { paddingValues ->
Column(
Modifier.padding(paddingValues)
Modifier.padding(paddingValues),
) {
SettingsCards(
settingsCardClicked = { item ->
Expand All @@ -145,7 +149,7 @@ fun SettingsScreen(

SettingsCardItem.SERVER_CONFIG -> showServerConfig = true
}
}
},
)
}
}
Expand All @@ -154,7 +158,7 @@ fun SettingsScreen(
SyncSurveysDialog(
closeDialog = {
showSyncSurveyDialog = false
}
},
)
}

Expand All @@ -168,7 +172,7 @@ fun SettingsScreen(
onSuccessful = {
showServerConfig = false
showRestartCountdownToast(context, 2)
}
},
)
}
}
Expand All @@ -179,7 +183,7 @@ fun SettingsScreen(
items = stringArrayResource(R.array.feature_settings_languages),
selectItem = { _, index -> updateLanguage(MifosAppLanguage.entries[index]) },
onDismissRequest = { showLanguageUpdateDialog = false },
selectedItem = MifosAppLanguage.fromCode(selectedLanguage).displayName
selectedItem = MifosAppLanguage.fromCode(selectedLanguage).displayName,
)
}

Expand All @@ -189,7 +193,7 @@ fun SettingsScreen(
items = AppTheme.entries.map { it.themeName }.toTypedArray(),
selectItem = { _, index -> updateTheme(AppTheme.entries[index]) },
onDismissRequest = { showThemeUpdateDialog = false },
selectedItem = selectedTheme
selectedItem = selectedTheme,
)
}

Expand All @@ -198,13 +202,13 @@ fun SettingsScreen(
initialBaseURL = baseURL,
initialTenant = tenant,
onDismissRequest = { showEndpointUpdateDialog = false },
handleEndpointUpdate = handleEndpointUpdate
handleEndpointUpdate = handleEndpointUpdate,
)
}
}

@Composable
fun SettingsCards(
private fun SettingsCards(
settingsCardClicked: (SettingsCardItem) -> Unit,
) {
LazyColumn {
Expand All @@ -215,24 +219,24 @@ fun SettingsCards(
icon = card.icon,
onclick = {
settingsCardClicked(card)
}
},
)
}
}
}

@Composable
fun SettingsCardItem(
private fun SettingsCardItem(
title: Int,
details: Int,
icon: ImageVector?,
onclick: () -> Unit
onclick: () -> Unit,
) {
Card(
modifier = Modifier.fillMaxWidth(),
colors = CardDefaults.cardColors(containerColor = Color.Transparent),
shape = RoundedCornerShape(0.dp),
onClick = { onclick.invoke() }
onClick = { onclick.invoke() },
) {
Row(
verticalAlignment = Alignment.CenterVertically,
Expand All @@ -242,18 +246,18 @@ fun SettingsCardItem(
Icon(
imageVector = it,
contentDescription = null,
modifier = Modifier.weight(0.2f)
modifier = Modifier.weight(0.2f),
)
}
if (icon == null) {
Spacer(modifier = Modifier.weight(0.2f))
}
Column(
modifier = Modifier.weight(0.8f)
modifier = Modifier.weight(0.8f),
) {
Text(
text = stringResource(id = title),
style = MaterialTheme.typography.bodyMedium
style = MaterialTheme.typography.bodyMedium,
)
Text(
modifier = Modifier.padding(end = 16.dp),
Expand All @@ -266,8 +270,7 @@ fun SettingsCardItem(
}
}


fun updateLanguageLocale(context: Context, language: String, isSystemLanguage: Boolean) {
private fun updateLanguageLocale(context: Context, language: String, isSystemLanguage: Boolean) {
if (isSystemLanguage) {
LanguageHelper.setLocale(context, language)
} else {
Expand All @@ -287,7 +290,7 @@ private fun showRestartCountdownToast(context: Context, seconds: Int) {
Toast.makeText(
context,
"Restarting app in $secondsRemaining seconds",
Toast.LENGTH_SHORT
Toast.LENGTH_SHORT,
).show()
}

Expand All @@ -298,7 +301,7 @@ private fun showRestartCountdownToast(context: Context, seconds: Int) {
countDownTimer.start()
}

fun Context.restartApplication() {
private fun Context.restartApplication() {
val packageManager: PackageManager = this.packageManager
val intent: Intent = packageManager.getLaunchIntentForPackage(this.packageName)!!
val componentName: ComponentName = intent.component!!
Expand All @@ -309,7 +312,7 @@ fun Context.restartApplication() {

@Composable
@Preview(showSystemUi = true, showBackground = true)
fun PreviewSettingsScreen() {
private fun PreviewSettingsScreen() {
SettingsScreen(
onBackPressed = {},
selectedLanguage = "",
Expand All @@ -321,5 +324,4 @@ fun PreviewSettingsScreen() {
updateTheme = {},
changePasscode = {},
)

}
}
Loading

0 comments on commit 49e755f

Please sign in to comment.