Skip to content

Commit

Permalink
MOB-363: Android: "System" theme setting not working (#32)
Browse files Browse the repository at this point in the history
* Observe system theme

* Recompose when theme changes
  • Loading branch information
ruixhuang authored Mar 18, 2024
1 parent 1ceea81 commit 885b278
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 17 deletions.
58 changes: 41 additions & 17 deletions v4/core/src/main/java/exchange/dydx/trading/TradingActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.key
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.setValue
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.Modifier
import androidx.core.view.WindowCompat
Expand All @@ -26,8 +30,11 @@ import exchange.dydx.trading.core.CoreViewModel
import exchange.dydx.trading.core.DydxNavGraph
import exchange.dydx.trading.core.biometric.DydxBiometricPrompt
import exchange.dydx.trading.core.biometric.DydxBiometricView
import exchange.dydx.trading.feature.shared.PreferenceKeys
import exchange.dydx.utilities.utils.SharedPreferencesStore
import kotlinx.coroutines.launch
import timber.log.Timber
import javax.inject.Inject

private const val TAG = "TradingActivity"

Expand All @@ -44,6 +51,9 @@ class TradingActivity : FragmentActivity() {
// This is the main ViewModel that the Activity will use to communicate with Compose-scoped code.
private val viewModel: CoreViewModel by viewModels()

@Inject
lateinit var preferencesStore: SharedPreferencesStore

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

Expand Down Expand Up @@ -121,14 +131,16 @@ class TradingActivity : FragmentActivity() {

@Composable
private fun MainContent() {
PlatformInfoScaffold(
modifier = Modifier,
platformInfo = viewModel.platformInfo,
) {
DydxNavGraph(
appRouter = viewModel.router,
key(themeChangedState) {
PlatformInfoScaffold(
modifier = Modifier,
)
platformInfo = viewModel.platformInfo,
) {
DydxNavGraph(
appRouter = viewModel.router,
modifier = Modifier,
)
}
}
}

Expand All @@ -152,20 +164,32 @@ class TradingActivity : FragmentActivity() {
}
}

// This is a state that is used to force a recomposition when the theme changes.
private var themeChangedState by mutableIntStateOf(0)

override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)

when (newConfig.uiMode and Configuration.UI_MODE_NIGHT_MASK) {
Configuration.UI_MODE_NIGHT_NO -> {
// Night mode is not active, we're using the light theme
ThemeSettings.shared.themeConfig.value = ThemeConfig.light(this)
ThemeSettings.shared.colorMap = mapOf()
}
val theme = preferencesStore.read(key = PreferenceKeys.Theme)
if (theme == "system") {
when (newConfig.uiMode and Configuration.UI_MODE_NIGHT_MASK) {
Configuration.UI_MODE_NIGHT_NO -> {
// Night mode is not active, we're using the light theme
if (ThemeSettings.shared.themeConfig.value != ThemeConfig.light(this)) {
ThemeSettings.shared.themeConfig.value = ThemeConfig.light(this)
ThemeSettings.shared.colorMap = mapOf()
themeChangedState++
}
}

Configuration.UI_MODE_NIGHT_YES -> {
// Night mode is active, we're using dark theme
ThemeSettings.shared.themeConfig.value = ThemeConfig.dark(this)
ThemeSettings.shared.colorMap = mapOf()
Configuration.UI_MODE_NIGHT_YES -> {
// Night mode is active, we're using dark theme
if (ThemeSettings.shared.themeConfig.value != ThemeConfig.dark(this)) {
ThemeSettings.shared.themeConfig.value = ThemeConfig.dark(this)
ThemeSettings.shared.colorMap = mapOf()
themeChangedState++
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package exchange.dydx.platformui.designSystem.theme

import android.content.Context
import android.content.res.Configuration
import android.content.res.Configuration.UI_MODE_NIGHT_YES
import android.util.Log
import exchange.dydx.utilities.utils.JsonUtils
import exchange.dydx.utilities.utils.SharedPreferencesStore
Expand Down Expand Up @@ -47,6 +49,7 @@ data class ThemeConfig(
"dark" -> dark(context)
"light" -> light(context)
"classic_dark" -> classicDark(context)
"system" -> if (context.isDarkThemeOn()) dark(context) else light(context)
else -> null
}

Expand All @@ -57,6 +60,11 @@ data class ThemeConfig(
config
}
}

private fun Context.isDarkThemeOn(): Boolean {
return resources.configuration.uiMode and
Configuration.UI_MODE_NIGHT_MASK == UI_MODE_NIGHT_YES
}
}
}

Expand Down

0 comments on commit 885b278

Please sign in to comment.