Skip to content

Commit

Permalink
onConfigChanged better logic
Browse files Browse the repository at this point in the history
  • Loading branch information
deeppp15 committed Dec 12, 2024
1 parent f3b9bc2 commit f43f1c2
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions app/src/main/java/org/ole/planet/myplanet/MainApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class MainApplication : Application(), Application.ActivityLifecycleCallbacks {
private const val AUTO_SYNC_WORK_TAG = "autoSyncWork"
private const val STAY_ONLINE_WORK_TAG = "stayOnlineWork"
private const val TASK_NOTIFICATION_WORK_TAG = "taskNotificationWork"
lateinit var context: Context
lateinit var context: Context
lateinit var mRealm: Realm
lateinit var service: DatabaseService
var preferences: SharedPreferences? = null
Expand Down Expand Up @@ -218,10 +218,8 @@ class MainApplication : Application(), Application.ActivityLifecycleCallbacks {
registerActivityLifecycleCallbacks(this)
onAppStarted()

val sharedPreferences = getSharedPreferences("app_preferences", MODE_PRIVATE)
val themeMode = sharedPreferences.getString("theme_mode", ThemeMode.FOLLOW_SYSTEM)

applyThemeMode(themeMode)
val savedThemeMode = getCurrentThemeMode()
applyThemeMode(savedThemeMode)

isNetworkConnectedFlow.onEach { isConnected ->
if (isConnected) {
Expand Down Expand Up @@ -274,7 +272,12 @@ class MainApplication : Application(), Application.ActivityLifecycleCallbacks {

override fun onConfigurationChanged(newConfig: android.content.res.Configuration) {
super.onConfigurationChanged(newConfig)
LocaleHelper.onAttach(this)

val savedThemeMode = getCurrentThemeMode()
if (savedThemeMode != ThemeMode.FOLLOW_SYSTEM) {
return
}

val currentNightMode = newConfig.uiMode and android.content.res.Configuration.UI_MODE_NIGHT_MASK
when (currentNightMode) {
android.content.res.Configuration.UI_MODE_NIGHT_NO -> {
Expand All @@ -286,6 +289,11 @@ class MainApplication : Application(), Application.ActivityLifecycleCallbacks {
}
}

private fun getCurrentThemeMode(): String {
val sharedPreferences = context.getSharedPreferences(PREFS_NAME, MODE_PRIVATE)
return sharedPreferences.getString("theme_mode", ThemeMode.FOLLOW_SYSTEM) ?: ThemeMode.FOLLOW_SYSTEM
}

override fun onActivityCreated(activity: Activity, bundle: Bundle?) {}

override fun onActivityStarted(activity: Activity) {
Expand Down Expand Up @@ -320,7 +328,7 @@ class MainApplication : Application(), Application.ActivityLifecycleCallbacks {
}
}
}

private fun onAppBackgrounded() {}

private fun onAppStarted() {
Expand Down

0 comments on commit f43f1c2

Please sign in to comment.