Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Language setting fixed, code reformatted and optimised #1821

Merged
merged 1 commit into from
Jul 22, 2021
Merged
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
@@ -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