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

MOB-363: Android: "System" theme setting not working #32

Merged
merged 2 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
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
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)
Comment on lines +167 to +168
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't need to track this ourselves - isSystemInDarkTheme() handles this for us. It is backed by a CompositionLocal of the Configuration:

@Composable
@ReadOnlyComposable
internal actual fun _isSystemInDarkTheme(): Boolean {
    val uiMode = LocalConfiguration.current.uiMode
    return (uiMode and Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES
}


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
Loading