Skip to content

Commit e5cb97f

Browse files
authored
Add remote theming toolbar colors (Apps-2062) (#248)
1 parent 53236ab commit e5cb97f

File tree

4 files changed

+62
-1
lines changed

4 files changed

+62
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file.
55
### Added
66
### Changed
77
* ui: add new style and theme manager to make primary and secondary buttons customizable (APPS-2213)
8+
* ui: add new remote colors for toolbar and adjust it accordingly in the checkout activity (APPS-2062)
89
### Removed
910
### Fixed
1011

ui/src/main/java/io/snabble/sdk/ui/checkout/CheckoutActivity.kt

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import android.os.Bundle
88
import android.view.View
99
import android.view.ViewGroup
1010
import android.view.WindowManager
11+
import android.widget.FrameLayout
1112
import androidx.activity.addCallback
1213
import androidx.appcompat.app.AlertDialog
1314
import androidx.core.view.ViewCompat
@@ -23,12 +24,15 @@ import androidx.navigation.NavController
2324
import androidx.navigation.NavGraph
2425
import androidx.navigation.NavOptions
2526
import androidx.navigation.fragment.NavHostFragment
27+
import com.google.android.material.appbar.MaterialToolbar
2628
import io.snabble.sdk.InitializationState
2729
import io.snabble.sdk.PaymentMethod
2830
import io.snabble.sdk.Snabble
2931
import io.snabble.sdk.checkout.Checkout
3032
import io.snabble.sdk.checkout.CheckoutState
3133
import io.snabble.sdk.ui.R
34+
import io.snabble.sdk.ui.remotetheme.onToolBarColorForProject
35+
import io.snabble.sdk.ui.remotetheme.toolBarColorForProject
3236
import io.snabble.sdk.utils.Logger
3337

3438
class CheckoutActivity : FragmentActivity() {
@@ -136,9 +140,24 @@ class CheckoutActivity : FragmentActivity() {
136140
private fun setUpToolBarAndStatusBar() {
137141
val showToolBar = resources.getBoolean(R.bool.showToolbarInCheckout)
138142
findViewById<View>(R.id.checkout_toolbar_spacer)?.isVisible = showToolBar
143+
139144
navController.addOnDestinationChangedListener { _, _, arguments ->
140-
findViewById<View>(R.id.checkout_toolbar)?.isVisible = arguments?.getBoolean("showToolbar", false) == true
145+
findViewById<View>(R.id.checkout_toolbar)?.isVisible =
146+
arguments?.getBoolean("showToolbar", false) == true
147+
148+
val toolBarColor =
149+
this@CheckoutActivity.toolBarColorForProject(Snabble.checkedInProject.value)
150+
val onToolBarColor =
151+
this@CheckoutActivity.onToolBarColorForProject(Snabble.checkedInProject.value)
152+
this.findViewById<MaterialToolbar>(R.id.checkout_toolbar).apply {
153+
toolBarColor?.let(::setBackgroundColor)
154+
onToolBarColor?.let(::setTitleTextColor)
155+
}
156+
this.findViewById<FrameLayout>(R.id.checkout_toolbar_spacer).apply {
157+
toolBarColor?.let(::setBackgroundColor)
158+
}
141159
}
160+
142161
if (showToolBar) {
143162
applyInsets()
144163
}

ui/src/main/java/io/snabble/sdk/ui/remotetheme/RemoteThemingExtensions.kt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ package io.snabble.sdk.ui.remotetheme
55
import android.content.Context
66
import android.graphics.Color
77
import androidx.appcompat.app.AlertDialog
8+
import com.google.gson.annotations.SerializedName
89
import io.snabble.sdk.Project
910
import io.snabble.sdk.ui.R
11+
import io.snabble.sdk.utils.GsonHolder
1012
import io.snabble.sdk.utils.getColorByAttribute
1113

1214
fun Context.getPrimaryColorForProject(project: Project?): Int {
@@ -72,3 +74,30 @@ fun AlertDialog.changeButtonColorFor(project: Project?): AlertDialog {
7274
}
7375
return this
7476
}
77+
78+
private data class ToolbarColors(
79+
@SerializedName("colorAppBar_light") val lightToolbarColor: String?,
80+
@SerializedName("colorAppBar_dark") val darkToolbarColor: String?,
81+
@SerializedName("colorOnAppBar_light") val lightOnToolbarColor: String?,
82+
@SerializedName("colorOnAppBar_dark") val darkOnToolbarColor: String?,
83+
)
84+
85+
fun Context.toolBarColorForProject(project: Project?): Int? =
86+
GsonHolder.get().fromJson(project?.customizationConfig, ToolbarColors::class.java)?.let {
87+
val lightColor = it.lightToolbarColor?.asColor()
88+
val darkColor = it.darkToolbarColor?.asColor()
89+
when {
90+
isDarkMode() -> darkColor ?: lightColor
91+
else -> lightColor
92+
}
93+
}
94+
95+
fun Context.onToolBarColorForProject(project: Project?): Int? =
96+
GsonHolder.get().fromJson(project?.customizationConfig, ToolbarColors::class.java)?.let {
97+
val lightColor = it.lightOnToolbarColor?.asColor()
98+
val darkColor = it.darkOnToolbarColor?.asColor()
99+
when {
100+
isDarkMode() -> darkColor ?: lightColor
101+
else -> lightColor
102+
}
103+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:autoMirrored="true"
5+
android:tint="#000000"
6+
android:viewportWidth="24"
7+
android:viewportHeight="24">
8+
9+
<path
10+
android:fillColor="@android:color/white"
11+
android:pathData="M20,11H7.83l5.59,-5.59L12,4l-8,8 8,8 1.41,-1.41L7.83,13H20v-2z" />
12+
</vector>

0 commit comments

Comments
 (0)