-
-
Notifications
You must be signed in to change notification settings - Fork 632
feat(Tabs, Android): handle colorScheme (dark mode)
#3723
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
68fc6a9
adjust test to Android
kligarski d328eeb
move prop to general section
kligarski 2b27596
PoC
kligarski 6740f79
add missing import in test
kligarski d4c8a88
implement ColorSchemeProviding for TabsHost
kligarski fb0df01
refactor to remove reference to host view
kligarski a1a4e59
make ColorSchemeCoordinator internal
kligarski a4f99f3
make naming less ambiguous
kligarski 110c8c4
Merge branch 'main' into @kligarski/tabs-color-scheme-android
kligarski c8817b4
fix SFT after merge
kligarski d82adfa
change resolvedUiNightMode computed property to function
kligarski aa5866e
add doc for onUiNightModeResolved callback
kligarski e2b72e1
rename onAttached/DetachedToWindow to setup/teardown; add callback as…
kligarski 3f1774b
reset lastAppliedUiNightMode in setup to ensure that callback is called
kligarski 3f91fba
make sure that setup method runs only once
kligarski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
android/src/main/java/com/swmansion/rnscreens/gamma/common/colorscheme/ColorScheme.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package com.swmansion.rnscreens.gamma.common.colorscheme | ||
|
|
||
| internal enum class ColorScheme { | ||
| INHERIT, | ||
| LIGHT, | ||
| DARK, | ||
| } |
120 changes: 120 additions & 0 deletions
120
.../src/main/java/com/swmansion/rnscreens/gamma/common/colorscheme/ColorSchemeCoordinator.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| package com.swmansion.rnscreens.gamma.common.colorscheme | ||
|
|
||
| import android.content.res.Configuration | ||
| import android.view.View | ||
| import android.view.ViewParent | ||
| import kotlin.properties.Delegates | ||
|
|
||
| internal typealias OnUiNightModeResolvedCallback = (nightMode: Int) -> Unit | ||
|
|
||
| internal class ColorSchemeCoordinator : | ||
| ColorSchemeProviding, | ||
| ColorSchemeListener { | ||
| internal var colorScheme: ColorScheme by Delegates.observable(ColorScheme.INHERIT) { _, oldValue, newValue -> | ||
| if (oldValue != newValue) { | ||
| applyResolvedColorScheme() | ||
| } | ||
| } | ||
| private var parentProvider: ColorSchemeProviding? = null | ||
| private var systemUiNightMode: Int = Configuration.UI_MODE_NIGHT_NO | ||
| private var lastAppliedUiNightMode: Int? = null | ||
| private val childListeners = mutableListOf<ColorSchemeListener>() | ||
| private var isSetUp = false | ||
|
|
||
| /** | ||
| * Callback invoked when color scheme changes. It should be used to adapt | ||
| * view's appearance to the current mode. | ||
| * | ||
| * The [Int] parameter represents the resolved night mode value. It can be | ||
| * one of [Configuration.UI_MODE_NIGHT_UNDEFINED], [Configuration.UI_MODE_NIGHT_NO] | ||
| * or [Configuration.UI_MODE_NIGHT_YES]. | ||
| * | ||
| * This callback is invoked only if the value has changed. The change is propagated | ||
| * in top-down order. | ||
| */ | ||
| internal var onUiNightModeResolved: OnUiNightModeResolvedCallback? = null | ||
|
|
||
| override fun getResolvedUiNightMode(): Int = | ||
| when (colorScheme) { | ||
| ColorScheme.LIGHT -> Configuration.UI_MODE_NIGHT_NO | ||
| ColorScheme.DARK -> Configuration.UI_MODE_NIGHT_YES | ||
| ColorScheme.INHERIT -> | ||
| parentProvider?.getResolvedUiNightMode() | ||
| ?: systemUiNightMode | ||
| } | ||
|
|
||
| /** | ||
| * Initializes the color scheme resolution for the given [hostView] | ||
| * with [onUiNightModeResolvedCallback]. | ||
| * | ||
| * Must be called after the view is attached to the window. | ||
| * Must not be called again without calling [teardown] first. | ||
| */ | ||
| internal fun setup( | ||
| hostView: View, | ||
| onUiNightModeResolvedCallback: OnUiNightModeResolvedCallback?, | ||
| ) { | ||
| check(!isSetUp) { | ||
| "[RNScreens] ColorSchemeCoordinator's setup method must not be called again without calling teardown() first." | ||
| } | ||
|
|
||
| systemUiNightMode = | ||
| hostView.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK | ||
| parentProvider = findParentColorSchemeProvider(hostView) | ||
| parentProvider?.addColorSchemeListener(this) | ||
| onUiNightModeResolved = onUiNightModeResolvedCallback | ||
kligarski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| isSetUp = true | ||
|
|
||
| // Reset last applied value so the initial callback is always invoked after setup. | ||
| // This is necessary because `colorScheme` could've been set before `setup` method | ||
| // is called. | ||
| lastAppliedUiNightMode = null | ||
|
|
||
| applyResolvedColorScheme() | ||
| } | ||
|
|
||
| internal fun teardown() { | ||
| parentProvider?.removeColorSchemeListener(this) | ||
| onUiNightModeResolved = null | ||
| parentProvider = null | ||
| lastAppliedUiNightMode = null | ||
| isSetUp = false | ||
| } | ||
|
|
||
| internal fun onConfigurationChanged(configuration: Configuration?) { | ||
| systemUiNightMode = configuration?.uiMode?.and(Configuration.UI_MODE_NIGHT_MASK) ?: Configuration.UI_MODE_NIGHT_UNDEFINED | ||
| applyResolvedColorScheme() | ||
| } | ||
|
|
||
| override fun onParentUiNightModeChanged() { | ||
| if (colorScheme == ColorScheme.INHERIT) { | ||
| applyResolvedColorScheme() | ||
| } | ||
| } | ||
|
|
||
| override fun addColorSchemeListener(listener: ColorSchemeListener) { | ||
| childListeners.add(listener) | ||
| } | ||
|
|
||
| override fun removeColorSchemeListener(listener: ColorSchemeListener) { | ||
| childListeners.remove(listener) | ||
| } | ||
|
|
||
| private fun applyResolvedColorScheme() { | ||
| val resolved = getResolvedUiNightMode() | ||
| if (resolved == lastAppliedUiNightMode) return | ||
| lastAppliedUiNightMode = resolved | ||
|
|
||
| onUiNightModeResolved?.invoke(resolved) | ||
| childListeners.forEach { it.onParentUiNightModeChanged() } | ||
| } | ||
|
|
||
| private fun findParentColorSchemeProvider(hostView: View): ColorSchemeProviding? { | ||
| var current: ViewParent? = hostView.parent | ||
| while (current != null) { | ||
| if (current is ColorSchemeProviding) return current | ||
| current = current.parent | ||
| } | ||
| return null | ||
| } | ||
| } | ||
5 changes: 5 additions & 0 deletions
5
...oid/src/main/java/com/swmansion/rnscreens/gamma/common/colorscheme/ColorSchemeListener.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package com.swmansion.rnscreens.gamma.common.colorscheme | ||
|
|
||
| fun interface ColorSchemeListener { | ||
| fun onParentUiNightModeChanged() | ||
| } |
9 changes: 9 additions & 0 deletions
9
...id/src/main/java/com/swmansion/rnscreens/gamma/common/colorscheme/ColorSchemeProviding.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package com.swmansion.rnscreens.gamma.common.colorscheme | ||
|
|
||
| interface ColorSchemeProviding { | ||
| fun getResolvedUiNightMode(): Int // UI_MODE_NIGHT_YES, UI_MODE_NIGHT_NO or UI_MODE_NIGHT_UNDEFINED | ||
|
|
||
| fun addColorSchemeListener(listener: ColorSchemeListener) | ||
|
|
||
| fun removeColorSchemeListener(listener: ColorSchemeListener) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe
mutableSet?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that we usually used list instead of a set for a small amount of entities but I guess that after we renamed
onAttachedToWindowtosetupthe intention might not be as clear and somebody might callsetupmultiple times and create duplicates. We can use a set or maybe just throw an error ifsetupis called whenparentProvideris not null? Not sure what's better here.cc @kkafar
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that
setuplooks risky withinternalaccess modifier, so as you already mentioned, maybe it would be possible to handle it when we don't expect it to be called subsequent timesThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's throw an error in case setup is called multiple times before teardown is called
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used separate property because I can't determine in other way whether setup has been run (callback might be nullable, parent might also be null if we're top-level provider).
3f91fba