Skip to content

Commit

Permalink
refactor: refactor Settings fragment to compose
Browse files Browse the repository at this point in the history
  • Loading branch information
Aditya-gupta99 committed Jul 12, 2024
1 parent 5705fb7 commit eef9e47
Show file tree
Hide file tree
Showing 15 changed files with 820 additions and 140 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.mifos.core.common.enums

enum class MifosAppLanguage(val code: String, val displayName: String) {

SYSTEM_LANGUAGE("System_Language", "System Language"),
ENGLISH("en", "English"),
HINDI("hi", "हिंदी"),
ARABIC("ar", "عربى"),
URDU("ur", "اُردُو"),
BENGALI("bn", "বাঙালি"),
SPANISH("es", "Español"),
FRENCH("fr", "français"),
INDONESIAN("in", "bahasa Indonesia"),
KHMER("km", "ភាសាខ្មែរ"),
KANNADA("kn", "ಕನ್ನಡ"),
TELUGU("te", "తెలుగు"),
BURMESE("my", "မြန်မာ"),
POLISH("pl", "Polski"),
PORTUGUESE("pt", "Português"),
RUSSIAN("ru", "русский"),
SWAHILI("sw", "Kiswahili"),
FARSI("fa", "فارسی");

companion object {
fun fromCode(code: String): MifosAppLanguage {
return entries.find { it.code.equals(code, ignoreCase = true) } ?: ENGLISH
}
}

override fun toString(): String {
return displayName
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,11 @@ object Constants {
const val CURR_PASSWORD = "currentPassword"
const val IS_TO_UPDATE_PASS_CODE = "updatePassCode"
const val HAS_SETTING_CHANGED = "hasSettingsChanged"


const val TENANT = "tenant"
const val BASE_URL = "base_url"
const val PASSCODE = "preferences_mifos_passcode_string"
const val THEME = "theme"
const val LANGUAGE = "language_type"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.mifos.core.common.utils

import android.content.Context
import android.os.Build
import android.preference.PreferenceManager
import com.mifos.core.common.R
import java.util.Locale

object LanguageHelper {
// https://gunhansancar.com/change-language-programmatically-in-android/
fun onAttach(context: Context): Context? {
val preferences = PreferenceManager.getDefaultSharedPreferences(context)
return if (preferences.getBoolean(
context.getString(R.string.core_common_default_system_language),
true
)
) {
if (!context.resources.getStringArray(R.array.core_common_languages_value)
.contains(Locale.getDefault().language)
) {
setLocale(context, "en")
} else {
setLocale(context, Locale.getDefault().language)
}
} else {
val lang = getPersistedData(context, Locale.getDefault().language)
lang?.let { setLocale(context, it) }
}
}

@JvmStatic
fun onAttach(context: Context, defaultLanguage: String): Context? {
val lang = getPersistedData(context, defaultLanguage)
return lang?.let { setLocale(context, it) }
}

fun setLocale(context: Context?, language: String): Context? {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
updateResources(context!!, language)
} else {
updateResourcesLegacy(context, language)
}
}

private fun getPersistedData(context: Context, defaultLanguage: String): String? {
val preferences = PreferenceManager.getDefaultSharedPreferences(context)
return preferences.getString(
context.getString(R.string.core_common_language_type),
defaultLanguage
)
}

private fun updateResources(context: Context, language: String): Context {
val locale = Locale(language)
Locale.setDefault(locale)
val configuration = context.resources.configuration
configuration.setLocale(locale)
configuration.setLayoutDirection(locale)
return context.createConfigurationContext(configuration)
}

private fun updateResourcesLegacy(context: Context?, language: String): Context? {
val locale = Locale(language)
Locale.setDefault(locale)
val resources = context?.resources
val configuration = resources?.configuration
configuration?.locale = locale
configuration?.setLayoutDirection(locale)
resources?.updateConfiguration(configuration, resources.displayMetrics)
return context
}
}
28 changes: 28 additions & 0 deletions core/common/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="core_common_default_system_language" translatable="false">default_system_language</string>
<string name="core_common_language_type" translatable="false">language_type</string>

<string-array name="core_common_languages_value" translatable="false">
<item>System_Language</item>
<item>en</item>
<item>hi</item>
<item>ar</item>
<item>ur</item>
<item>bn</item>
<item>es</item>
<item>fr</item>
<item>in</item>
<item>km</item>
<item>kn</item>
<item>te</item>
<item>my</item>
<item>pl</item>
<item>pt</item>
<item>ru</item>
<item>sw</item>
<item>fa</item>
</string-array>

</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import com.mifos.core.common.utils.Constants
import com.mifos.core.common.utils.asServerConfig
import com.mifos.core.model.ServerConfig
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import org.apache.fineract.client.models.PostAuthenticationResponse
import org.mifos.core.sharedpreference.Key
import org.mifos.core.sharedpreference.UserPreferences
Expand Down Expand Up @@ -75,4 +77,12 @@ class PrefManager @Inject constructor(
fun updateServerConfig(config: ServerConfig?) {
this.put(serverConfigKey, config)
}

fun getStringValue(key: String): Flow<String?> = flow {
emit(preference.getString(key, ""))
}

fun setStringValue(key: String,value: String) {
preference.edit().putString(key, value).apply()
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,33 @@
package com.mifos.core.designsystem.component

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Card
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.RadioButton
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import com.mifos.core.designsystem.R

@Composable
fun MifosDialogBox(
Expand Down Expand Up @@ -42,3 +65,116 @@ fun MifosDialogBox(
)
}
}


@Composable
fun MifosRadioButtonDialog(
titleResId: Int,
selectedItem: String,
items: Array<String>,
selectItem: (item: String, index: Int) -> Unit,
onDismissRequest: () -> Unit,
) {
Dialog(
onDismissRequest = { onDismissRequest.invoke() }
) {
Card {
Column(modifier = Modifier.padding(20.dp)) {
Text(text = stringResource(id = titleResId))
LazyColumn(
modifier = Modifier
.fillMaxWidth()
.heightIn(max = 500.dp)
) {
itemsIndexed(items = items) { index, item ->
Row(
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier
.clickable {
onDismissRequest.invoke()
selectItem.invoke(item, index)
}
.fillMaxWidth()
) {
RadioButton(
selected = (item == selectedItem),
onClick = {
onDismissRequest.invoke()
selectItem.invoke(item, index)
}
)
Text(
text = item,
modifier = Modifier.padding(start = 4.dp)
)
}
}
}
}
}
}
}

@Composable
fun UpdateEndpointDialogScreen(
initialBaseURL: String?,
initialTenant: String?,
onDismissRequest: () -> Unit,
handleEndpointUpdate: (baseURL: String, tenant: String) -> Unit
) {
var baseURL by rememberSaveable { mutableStateOf(initialBaseURL) }
var tenant by rememberSaveable { mutableStateOf(initialTenant) }

Dialog(
onDismissRequest = { onDismissRequest.invoke() }
) {
Card {
Column(
modifier = Modifier
.fillMaxWidth()
.padding(20.dp),
) {
Text(text = stringResource(id = R.string.core_designsystem_pref_base_url_title))
Spacer(modifier = Modifier.height(8.dp))

baseURL?.let {
OutlinedTextField(
value = it,
onValueChange = { baseURL = it },
label = { Text(text = stringResource(id = R.string.core_designsystem_enter_base_url)) }
)
}

Spacer(modifier = Modifier.height(8.dp))

tenant?.let {
OutlinedTextField(
value = it,
onValueChange = { tenant = it },
label = { Text(text = stringResource(id = R.string.core_designsystem_enter_tenant)) }
)
}

Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.End
) {
TextButton(
onClick = { onDismissRequest.invoke() }) {
Text(text = stringResource(id = R.string.core_designsystem_cancel))
}
TextButton(
onClick = {
if (baseURL != null && tenant != null) {
handleEndpointUpdate.invoke(baseURL ?: "", tenant ?: "")
}
}
)
{
Text(text = stringResource(id = R.string.core_designsystem_dialog_action_ok))
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@ import androidx.compose.material.icons.outlined.EventRepeat
import androidx.compose.material.icons.outlined.Group
import androidx.compose.material.icons.rounded.Add
import androidx.compose.material.icons.rounded.ArrowBackIosNew
import androidx.compose.material.icons.rounded.Bedtime
import androidx.compose.material.icons.rounded.Check
import androidx.compose.material.icons.rounded.Close
import androidx.compose.material.icons.rounded.Delete
import androidx.compose.material.icons.rounded.FilterList
import androidx.compose.material.icons.rounded.KeyboardArrowDown
import androidx.compose.material.icons.rounded.KeyboardArrowUp
import androidx.compose.material.icons.rounded.Lock
import androidx.compose.material.icons.rounded.MoreVert
import androidx.compose.material.icons.rounded.PersonOutline
import androidx.compose.material.icons.rounded.Search
import androidx.compose.material.icons.rounded.Sync
import androidx.compose.material.icons.rounded.Translate

object MifosIcons {
val Add = Icons.Rounded.Add
Expand All @@ -37,4 +40,7 @@ object MifosIcons {
val moreVert = Icons.Rounded.MoreVert
val fileTask = Icons.Default.AssignmentTurnedIn
val cloudDownload = Icons.Default.CloudDownload
val password = Icons.Rounded.Lock
val theme = Icons.Rounded.Bedtime
val language = Icons.Rounded.Translate
}
6 changes: 6 additions & 0 deletions core/designsystem/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@
<resources>
<string name="core_designsystem_unable_to_load">Sorry we weren\'t able to load</string>
<string name="core_designsystem_try_again">Try Again</string>
<string name="core_designsystem_pref_base_url_title">Please enter a base URL</string>
<string name="core_designsystem_enter_base_url">Enter a base URL</string>
<string name="core_designsystem_enter_tenant">Enter a tenant</string>
<string name="core_designsystem_cancel">Cancel</string>
<string name="core_designsystem_dialog_action_ok">Save</string>

</resources>
Loading

0 comments on commit eef9e47

Please sign in to comment.