Skip to content

Commit

Permalink
Merge pull request #1821 from danishjamal104/lang-setting-fix
Browse files Browse the repository at this point in the history
Language setting fixed, code reformatted and optimised
  • Loading branch information
iamsh4shank authored Jul 22, 2021
2 parents 14d4345 + 929e1cb commit 6c15132
Showing 1 changed file with 60 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("DEPRECATION")

package com.mifos.mifosxdroid

import android.content.Intent
Expand Down Expand Up @@ -34,80 +36,100 @@ class SettingsFragment : PreferenceFragment(), SharedPreferences.OnSharedPrefere

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setUpPreferences()

when (preference?.key) {
getString(R.string.password) -> {
// TODO("create changePasswordActivity and implement the logic for password change")
}
getString(R.string.passcode) -> {
activity?.let {
val passCodePreferencesHelper = PasscodePreferencesHelper(activity)
val currPassCode = passCodePreferencesHelper.passCode
passCodePreferencesHelper.savePassCode("")
val intent = Intent(it, PassCodeActivity::class.java).apply {
putExtra(Constants.CURR_PASSWORD, currPassCode)
putExtra(Constants.IS_TO_UPDATE_PASS_CODE, true)
}
preferenceScreen.sharedPreferences.registerOnSharedPreferenceChangeListener(this)
startActivity(intent)
}
}
}
}

fun setUpPreferences() {
addPreferencesFromResource(R.xml.preferences)
languages = activity.resources.getStringArray(R.array.language_option)

initSurveyPreferences()
initInstanceUrlPreferences()
initLanguagePreferences()
initThemePreferences()
}

private fun initSurveyPreferences() {
mEnableSyncSurvey = findPreference(resources.getString(R.string.sync_survey)) as SwitchPreference
mEnableSyncSurvey!!.onPreferenceChangeListener = OnPreferenceChangeListener { preference, newValue ->
mEnableSyncSurvey!!.onPreferenceChangeListener = OnPreferenceChangeListener { _, newValue ->
if (newValue as Boolean) {
val syncSurveysDialogFragment = SyncSurveysDialogFragment.newInstance()
val fragmentTransaction = fragmentManager.beginTransaction()
fragmentTransaction.addToBackStack(FragmentConstants.FRAG_SURVEYS_SYNC)
syncSurveysDialogFragment.isCancelable = false
syncSurveysDialogFragment.show(fragmentTransaction,
resources.getString(R.string.sync_clients))
resources.getString(R.string.sync_clients))
}
true
}
}

private fun initInstanceUrlPreferences() {
mInstanceUrlPref = findPreference(
getString(R.string.hint_instance_url)) as EditTextPreference
getString(R.string.hint_instance_url)
) as EditTextPreference
val instanceUrl = PrefManager.getInstanceUrl()
mInstanceUrlPref!!.text = instanceUrl
mInstanceUrlPref!!.isSelectable = true
mInstanceUrlPref!!.dialogTitle = "Edit Instance Url"
mInstanceUrlPref!!.setDialogIcon(R.drawable.ic_baseline_edit_24)
mInstanceUrlPref!!.onPreferenceChangeListener = OnPreferenceChangeListener { preference, o ->
val newUrl = o.toString()
if (newUrl != instanceUrl) {
PrefManager.setInstanceUrl(newUrl)
Toast.makeText(activity, newUrl, Toast.LENGTH_SHORT).show()
startActivity(Intent(activity, DashboardActivity::class.java))
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
activity.finishAffinity()
} else {
activity.finish()
mInstanceUrlPref!!.onPreferenceChangeListener =
OnPreferenceChangeListener { _, o ->
val newUrl = o.toString()
if (newUrl != instanceUrl) {
PrefManager.setInstanceUrl(newUrl)
Toast.makeText(activity, newUrl, Toast.LENGTH_SHORT).show()
startActivity(Intent(activity, DashboardActivity::class.java))
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
activity.finishAffinity()
} else {
activity.finish()
}
}
true
}
true
}
}

private fun initLanguagePreferences() {
val langPref = findPreference("language_type") as ListPreference
langPref.onPreferenceChangeListener = OnPreferenceChangeListener {preference, newValue ->
langPref.onPreferenceChangeListener = OnPreferenceChangeListener { _, newValue ->
LanguageHelper.setLocale(this.activity, newValue.toString())
startActivity(Intent(activity, activity.javaClass))
preferenceScreen = null
addPreferencesFromResource(R.xml.preferences)
preferenceScreen.sharedPreferences.registerOnSharedPreferenceChangeListener(this)
true
}
}

private fun initThemePreferences() {
val themePreference = findPreference(resources.getString(R.string.mode_key)) as ListPreference
themePreference.onPreferenceChangeListener = OnPreferenceChangeListener { preference, newValue ->
themePreference.onPreferenceChangeListener = OnPreferenceChangeListener { _, newValue ->
val themeOption = newValue as String
ThemeHelper.applyTheme(themeOption)
startActivity(Intent(activity, activity.javaClass))
Toast.makeText(activity, "Switched to ${themeOption.toString()} Mode", Toast.LENGTH_SHORT).show()
Toast.makeText(activity, "Switched to $themeOption Mode", Toast.LENGTH_SHORT).show()
true
}

when (preference?.key) {
getString(R.string.password) -> {
// TODO("create changePasswordActivity and implement the logic for password change")
}
getString(R.string.passcode) -> {
activity?.let {
val passCodePreferencesHelper = PasscodePreferencesHelper(activity)
val currPassCode = passCodePreferencesHelper.passCode
passCodePreferencesHelper.savePassCode("")
val intent = Intent(it, PassCodeActivity::class.java).apply {
putExtra(Constants.CURR_PASSWORD, currPassCode)
putExtra(Constants.IS_TO_UPDATE_PASS_CODE, true)
}
preferenceScreen.sharedPreferences.registerOnSharedPreferenceChangeListener(this)
startActivity(intent)
}
}
}
}

override fun onPause() {
Expand All @@ -124,9 +146,7 @@ class SettingsFragment : PreferenceFragment(), SharedPreferences.OnSharedPrefere
LanguageHelper.setLocale(this.activity, preference.value)
}

interface LanguageCallback {
fun updateNavDrawer()
}
interface LanguageCallback

override fun onPreferenceTreeClick(preferenceScreen: PreferenceScreen?, preference: Preference?): Boolean {
when (preference?.key) {
Expand Down

0 comments on commit 6c15132

Please sign in to comment.