Skip to content

feat: settings migrate to cmp #2366

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class CMPFeatureConventionPlugin : Plugin<Project> {
dependencies {
add("commonMainImplementation", project(":core:ui"))
add("commonMainImplementation", project(":core:designsystem"))
add("commonMainImplementation", project(":core:testing"))
add("commonMainImplementation", project(":core:data"))
//add("commonMainImplementation", project(":core:testing"))
//add("commonMainImplementation", project(":core:data"))

add("commonMainImplementation", libs.findLibrary("koin.compose").get())
add("commonMainImplementation", libs.findLibrary("koin.compose.viewmodel").get())
Expand Down
34 changes: 16 additions & 18 deletions feature/settings/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,28 @@
* 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)
alias(libs.plugins.mifos.android.library.jacoco)
alias(libs.plugins.mifos.cmp.feature)
}

android {
namespace = "com.mifos.feature.settings"
}

dependencies {

implementation(projects.core.datastore)
implementation(projects.core.designsystem)
implementation(projects.core.domain)
implementation(projects.core.common)
implementation(projects.core.ui)
implementation(libs.appcompat)
kotlin {
sourceSets {
commonMain.dependencies {
implementation(compose.material3)
implementation(compose.components.resources)
implementation(compose.ui)
api(projects.core.common)
api(projects.core.model)
api(projects.core.network)
api(projects.core.datastore)
api(projects.core.designsystem)
// api(projects.core.domain)

androidTestImplementation(libs.androidx.compose.ui.test)
debugApi(libs.androidx.compose.ui.test.manifest)

testImplementation(libs.hilt.android.testing)
testImplementation(projects.core.testing)

androidTestImplementation(projects.core.testing)
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<string name="feature_settings_close_bottomsheet">CloseBottomSheetIcon</string>
<string name="feature_settings_update_config_btn_text">Update Config</string>
<string name="feature_settings_note_text">The application will restart after a successful update.</string>

<string name="feature_settings_title">Update Server Configuration</string>
<string name="feature_settings">Settings</string>
<string name="feature_settings_sync_survey">Sync Survey</string>
<string name="feature_settings_sync_survey_desc">Click to Sync Survey</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@
*/
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.util.Log
import android.widget.Toast
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
Expand All @@ -41,21 +34,18 @@ 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.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.lifecycle.compose.collectAsStateWithLifecycle
import com.mifos.core.common.enums.MifosAppLanguage
import com.mifos.core.datastore.model.AppTheme
import com.mifos.core.designsystem.component.MifosRadioButtonDialog
import com.mifos.core.designsystem.component.MifosScaffold
import com.mifos.core.designsystem.component.UpdateEndpointDialogScreen
import com.mifos.feature.settings.R
import com.mifos.feature.settings.syncSurvey.SyncSurveysDialog
import com.mifos.feature.settings.updateServer.UpdateServerConfigScreenRoute
import org.koin.androidx.compose.koinViewModel
import core.designsystem.generated.resources.Res
import org.jetbrains.compose.resources.stringResource
import org.koin.compose.viewmodel.koinViewModel

@Composable
internal fun SettingsScreen(
Expand All @@ -66,7 +56,6 @@ internal fun SettingsScreen(
viewModel: SettingsViewModel = koinViewModel(),
) {
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
val context = LocalContext.current

SettingsScreen(
onBackPressed = onBackPressed,
Expand All @@ -85,11 +74,6 @@ internal fun SettingsScreen(
},
updateLanguage = {
val isSystemLanguage = viewModel.updateLanguage(it.code)
updateLanguageLocale(
context = context,
language = it.code,
isSystemLanguage = isSystemLanguage,
)
languageChanged()
},
)
Expand All @@ -115,11 +99,10 @@ internal fun SettingsScreen(
var showServerConfig by rememberSaveable { mutableStateOf(false) }

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

MifosScaffold(
onBackPressed = onBackPressed,
title = stringResource(R.string.feature_settings),
title = stringResource(Res.string.feature_settings),
) { paddingValues ->
Column(
Modifier.padding(paddingValues),
Expand Down Expand Up @@ -161,16 +144,15 @@ internal fun SettingsScreen(
onCloseClick = { showServerConfig = false },
onSuccessful = {
showServerConfig = false
showRestartCountdownToast(context, 2)
},
)
}
}

if (showLanguageUpdateDialog) {
MifosRadioButtonDialog(
title = stringResource(R.string.feature_settings_choose_language),
items = stringArrayResource(R.array.feature_settings_languages),
title = stringResource(Res.string.feature_settings_choose_language),
items = MifosAppLanguage.entries.map { it.displayName }.toTypedArray(),
selectItem = { _, index -> updateLanguage(MifosAppLanguage.entries[index]) },
onDismissRequest = { showLanguageUpdateDialog = false },
selectedItem = MifosAppLanguage.fromCode(selectedLanguage).displayName,
Expand All @@ -179,7 +161,7 @@ internal fun SettingsScreen(

if (showThemeUpdateDialog) {
MifosRadioButtonDialog(
title = stringResource(R.string.feature_settings_change_app_theme),
title = stringResource(Res.string.feature_settings_change_app_theme),
items = AppTheme.entries.map { it.themeName }.toTypedArray(),
selectItem = { _, index -> updateTheme(AppTheme.entries[index]) },
onDismissRequest = { showThemeUpdateDialog = false },
Expand Down Expand Up @@ -246,12 +228,12 @@ private fun SettingsCardItem(
modifier = Modifier.weight(0.8f),
) {
Text(
text = stringResource(id = title),
text = stringResource(title),
style = MaterialTheme.typography.bodyMedium,
)
Text(
modifier = Modifier.padding(end = 16.dp),
text = stringResource(id = details),
text = stringResource(details),
color = MaterialTheme.colorScheme.onBackground.copy(alpha = 0.8f),
style = MaterialTheme.typography.bodyMedium,
)
Expand All @@ -260,62 +242,4 @@ private fun SettingsCardItem(
}
}

private fun updateLanguageLocale(context: Context, language: String, isSystemLanguage: Boolean) {
Log.d(
"SettingsScreen",
"updateLanguageLocale: $language" + context.packageName.toString() + isSystemLanguage,
)
// if (isSystemLanguage) {
// LanguageHelper.setLocale(context, language)
// } else {
// val systemLanguageCode = Locale.getDefault().language
// if (MifosAppLanguage.entries.find { it.code == systemLanguageCode } == null) {
// LanguageHelper.setLocale(context, MifosAppLanguage.ENGLISH.code)
// } else {
// LanguageHelper.setLocale(context, language)
// }
// }
}

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()
}

private 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)
private fun PreviewSettingsScreen() {
SettingsScreen(
onBackPressed = {},
selectedLanguage = "",
selectedTheme = "",
baseURL = "",
tenant = "",
handleEndpointUpdate = { _, _ -> },
updateLanguage = {},
updateTheme = {},
changePasscode = {},
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import com.mifos.core.common.enums.MifosAppLanguage
import com.mifos.core.datastore.UserPreferencesRepository
import com.mifos.core.datastore.model.AppTheme
import com.mifos.core.designsystem.icon.MifosIcons
import com.mifos.feature.settings.R
import core.designsystem.generated.resources.Res
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.first
Expand Down Expand Up @@ -73,33 +73,33 @@ enum class SettingsCardItem(
val icon: ImageVector?,
) {
SYNC_SURVEY(
title = R.string.feature_settings_sync_survey,
details = R.string.feature_settings_sync_survey_desc,
title = Res.string.feature_settings_sync_survey,
details = Res.string.feature_settings_sync_survey_desc,
icon = null,
),
LANGUAGE(
title = R.string.feature_settings_language,
details = R.string.feature_settings_language_desc,
title = Res.string.feature_settings_language,
details = Res.string.feature_settings_language_desc,
icon = MifosIcons.Language,
),
THEME(
title = R.string.feature_settings_theme,
details = R.string.feature_settings_theme_desc,
title = Res.string.feature_settings_theme,
details = Res.string.feature_settings_theme_desc,
icon = MifosIcons.Theme,
),
PASSCODE(
title = R.string.feature_settings_change_passcode,
details = R.string.feature_settings_change_passcode_desc,
title = Res.string.feature_settings_change_passcode,
details = Res.string.feature_settings_change_passcode_desc,
icon = MifosIcons.Password,
),
ENDPOINT(
title = R.string.feature_settings_instance_url,
details = R.string.feature_settings_instance_url_desc,
title = Res.string.feature_settings_instance_url,
details = Res.string.feature_settings_instance_url_desc,
icon = null,
),
SERVER_CONFIG(
title = R.string.feature_settings_server_config,
details = R.string.feature_settings_server_config_desc,
title = Res.string.feature_settings_server_config,
details = Res.string.feature_settings_server_config_desc,
icon = null,
),
}
Expand Down
Loading
Loading