Skip to content

Commit 33ca8fe

Browse files
committed
Refactor color scheme handling in CustomTabsIntentFactory
Consolidates color parameter resolution into a new ColorSet data class and updates the logic to build and apply color schemes for toolbar, secondary toolbar, navigation bar, and divider colors. This improves maintainability and ensures color options are handled consistently across different Android versions.
1 parent 66804de commit 33ca8fe

File tree

1 file changed

+70
-36
lines changed

1 file changed

+70
-36
lines changed

android/src/main/java/com/inappbrowsernitro/browser/CustomTabsIntentFactory.kt

Lines changed: 70 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import androidx.browser.customtabs.CustomTabsSession
1616
import com.margelo.nitro.inappbrowsernitro.BrowserAnimations
1717
import com.margelo.nitro.inappbrowsernitro.BrowserColorScheme
1818
import com.margelo.nitro.inappbrowsernitro.BrowserShareState
19-
import com.margelo.nitro.inappbrowsernitro.DynamicColor
2019
import com.margelo.nitro.inappbrowsernitro.InAppBrowserOptions
2120

2221
internal class CustomTabsIntentFactory(
@@ -41,27 +40,20 @@ internal class CustomTabsIntentFactory(
4140
}
4241

4342
private fun applyColors(builder: CustomTabsIntent.Builder, options: InAppBrowserOptions?) {
44-
val toolbarParams = buildColorParams(options?.toolbarColor)
45-
if (toolbarParams != null) {
46-
builder.setDefaultColorSchemeParams(toolbarParams.system)
47-
builder.setColorSchemeParams(CustomTabsIntent.COLOR_SCHEME_LIGHT, toolbarParams.light)
48-
builder.setColorSchemeParams(CustomTabsIntent.COLOR_SCHEME_DARK, toolbarParams.dark)
49-
}
43+
val colorSchemes = buildColorParams(options)
5044

51-
buildColorParams(options?.secondaryToolbarColor)?.systemColor?.let {
52-
builder.setSecondaryToolbarColor(it)
45+
when {
46+
colorSchemes?.system != null -> builder.setDefaultColorSchemeParams(colorSchemes.system)
47+
colorSchemes?.light != null -> builder.setDefaultColorSchemeParams(colorSchemes.light)
48+
colorSchemes?.dark != null -> builder.setDefaultColorSchemeParams(colorSchemes.dark)
5349
}
5450

55-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
56-
buildColorParams(options?.navigationBarColor)?.systemColor?.let { color ->
57-
builder.setNavigationBarColor(color)
58-
}
51+
colorSchemes?.light?.let {
52+
builder.setColorSchemeParams(CustomTabsIntent.COLOR_SCHEME_LIGHT, it)
5953
}
6054

61-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
62-
buildColorParams(options?.navigationBarDividerColor)?.systemColor?.let { color ->
63-
builder.setNavigationBarDividerColor(color)
64-
}
55+
colorSchemes?.dark?.let {
56+
builder.setColorSchemeParams(CustomTabsIntent.COLOR_SCHEME_DARK, it)
6557
}
6658

6759
options?.colorScheme?.let { scheme ->
@@ -135,26 +127,45 @@ internal class CustomTabsIntentFactory(
135127
}
136128
}
137129

138-
private fun buildColorParams(color: DynamicColor?): ColorSchemeParams? {
139-
val system = DynamicColorResolver.resolveForScheme(color, DynamicColorResolver.DynamicScheme.SYSTEM)
140-
val light = DynamicColorResolver.resolveForScheme(color, DynamicColorResolver.DynamicScheme.LIGHT)
141-
val dark = DynamicColorResolver.resolveForScheme(color, DynamicColorResolver.DynamicScheme.DARK)
130+
private fun buildColorParams(options: InAppBrowserOptions?): ColorSchemeParams? {
131+
val system = resolveColorSet(options, DynamicColorResolver.DynamicScheme.SYSTEM)
132+
val light = resolveColorSet(options, DynamicColorResolver.DynamicScheme.LIGHT)
133+
val dark = resolveColorSet(options, DynamicColorResolver.DynamicScheme.DARK)
134+
135+
val systemParams = system.toCustomTabParams()
136+
val lightParams = light.toCustomTabParams()
137+
val darkParams = dark.toCustomTabParams()
142138

143-
if (system == null && light == null && dark == null) {
139+
if (systemParams == null && lightParams == null && darkParams == null) {
144140
return null
145141
}
146142

147143
return ColorSchemeParams(
148-
system = CustomTabColorSchemeParams.Builder().apply {
149-
system?.let { setToolbarColor(it) }
150-
}.build(),
151-
light = CustomTabColorSchemeParams.Builder().apply {
152-
light?.let { setToolbarColor(it) }
153-
}.build(),
154-
dark = CustomTabColorSchemeParams.Builder().apply {
155-
dark?.let { setToolbarColor(it) }
156-
}.build(),
157-
systemColor = system
144+
system = systemParams,
145+
light = lightParams,
146+
dark = darkParams,
147+
)
148+
}
149+
150+
private fun resolveColorSet(options: InAppBrowserOptions?, scheme: DynamicColorResolver.DynamicScheme): ColorSet {
151+
val toolbar = DynamicColorResolver.resolveForScheme(options?.toolbarColor, scheme)
152+
val secondaryToolbar = DynamicColorResolver.resolveForScheme(options?.secondaryToolbarColor, scheme)
153+
val navigationBar = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
154+
DynamicColorResolver.resolveForScheme(options?.navigationBarColor, scheme)
155+
} else {
156+
null
157+
}
158+
val navigationDivider = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
159+
DynamicColorResolver.resolveForScheme(options?.navigationBarDividerColor, scheme)
160+
} else {
161+
null
162+
}
163+
164+
return ColorSet(
165+
toolbar = toolbar,
166+
secondaryToolbar = secondaryToolbar,
167+
navigationBar = navigationBar,
168+
navigationBarDivider = navigationDivider,
158169
)
159170
}
160171

@@ -191,12 +202,35 @@ internal class CustomTabsIntentFactory(
191202
}
192203

193204
private data class ColorSchemeParams(
194-
val system: CustomTabColorSchemeParams,
195-
val light: CustomTabColorSchemeParams,
196-
val dark: CustomTabColorSchemeParams,
197-
val systemColor: Int?,
205+
val system: CustomTabColorSchemeParams?,
206+
val light: CustomTabColorSchemeParams?,
207+
val dark: CustomTabColorSchemeParams?,
198208
)
199209

210+
private data class ColorSet(
211+
val toolbar: Int?,
212+
val secondaryToolbar: Int?,
213+
val navigationBar: Int?,
214+
val navigationBarDivider: Int?,
215+
) {
216+
fun hasAny(): Boolean {
217+
return toolbar != null || secondaryToolbar != null || navigationBar != null || navigationBarDivider != null
218+
}
219+
220+
fun toCustomTabParams(): CustomTabColorSchemeParams? {
221+
if (!hasAny()) {
222+
return null
223+
}
224+
225+
return CustomTabColorSchemeParams.Builder().apply {
226+
toolbar?.let(::setToolbarColor)
227+
secondaryToolbar?.let(::setSecondaryToolbarColor)
228+
navigationBar?.let(::setNavigationBarColor)
229+
navigationBarDivider?.let(::setNavigationBarDividerColor)
230+
}.build()
231+
}
232+
}
233+
200234
private fun BrowserColorScheme.toCustomTabsScheme(): Int {
201235
return when (this) {
202236
BrowserColorScheme.LIGHT -> CustomTabsIntent.COLOR_SCHEME_LIGHT

0 commit comments

Comments
 (0)