Skip to content

Commit

Permalink
all: smoother orientation change (fixes #4859) (#4871)
Browse files Browse the repository at this point in the history
Co-authored-by: dogi <[email protected]>
  • Loading branch information
deeppp15 and dogi authored Dec 18, 2024
1 parent d23d288 commit 626dd23
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ android {
applicationId "org.ole.planet.myplanet"
minSdkVersion 26
targetSdkVersion 34
versionCode 2142
versionName "0.21.42"
versionCode 2143
versionName "0.21.43"
ndkVersion '21.3.6528147'
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
Expand Down
33 changes: 23 additions & 10 deletions app/src/main/java/org/ole/planet/myplanet/MainApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.app.Application
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.content.res.Configuration
import android.os.Bundle
import android.os.StrictMode
import android.os.StrictMode.VmPolicy
Expand Down Expand Up @@ -55,7 +56,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 @@ -111,10 +112,10 @@ class MainApplication : Application(), Application.ActivityLifecycleCallbacks {
}

fun setThemeMode(themeMode: String) {
val sharedPreferences = context.getSharedPreferences("app_preferences", MODE_PRIVATE)
val sharedPreferences = context.getSharedPreferences(PREFS_NAME, MODE_PRIVATE)
with(sharedPreferences.edit()) {
putString("theme_mode", themeMode)
apply()
commit()
}
applyThemeMode(themeMode)
}
Expand Down Expand Up @@ -218,10 +219,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,8 +273,17 @@ class MainApplication : Application(), Application.ActivityLifecycleCallbacks {

override fun onConfigurationChanged(newConfig: android.content.res.Configuration) {
super.onConfigurationChanged(newConfig)
LocaleHelper.onAttach(this)
val currentNightMode = newConfig.uiMode and android.content.res.Configuration.UI_MODE_NIGHT_MASK
val currentNightMode = context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
val isSystemNight= when (currentNightMode) {
Configuration.UI_MODE_NIGHT_YES -> true
Configuration.UI_MODE_NIGHT_NO -> false
else -> false
}
val savedThemeMode = getCurrentThemeMode()
if (savedThemeMode != ThemeMode.FOLLOW_SYSTEM) {
return
}

when (currentNightMode) {
android.content.res.Configuration.UI_MODE_NIGHT_NO -> {
applyThemeMode(ThemeMode.LIGHT)
Expand All @@ -286,6 +294,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 +333,7 @@ class MainApplication : Application(), Application.ActivityLifecycleCallbacks {
}
}
}

private fun onAppBackgrounded() {}

private fun onAppStarted() {
Expand Down

0 comments on commit 626dd23

Please sign in to comment.