Skip to content

Commit 53236ab

Browse files
authored
Adjust primary and secondary buttons to make them customizable (Apps-2213) (#247)
1 parent 515199d commit 53236ab

File tree

7 files changed

+85
-18
lines changed

7 files changed

+85
-18
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file.
44
## UNRELEASED
55
### Added
66
### Changed
7+
* ui: add new style and theme manager to make primary and secondary buttons customizable (APPS-2213)
78
### Removed
89
### Fixed
910

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package io.snabble.sdk.ui
2+
3+
object ThemeManager {
4+
var primaryButtonConfig: PrimaryConfig = PrimaryConfig()
5+
var secondaryButtonConfig: SecondaryConfig = SecondaryConfig()
6+
}
7+
8+
9+
data class PrimaryConfig(
10+
val textSize: Int = 16,
11+
val minHeight: Int = 40,
12+
)
13+
14+
data class SecondaryConfig(
15+
val textSize: Int = 16,
16+
val minHeight: Int = 40,
17+
val useOutlinedButton: Boolean = false
18+
)

ui/src/main/java/io/snabble/sdk/ui/payment/creditcard/shared/CustomerInfoInputScreen.kt

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package io.snabble.sdk.ui.payment.creditcard.shared
22

33
import androidx.compose.animation.AnimatedVisibility
4+
import androidx.compose.foundation.BorderStroke
45
import androidx.compose.foundation.layout.Arrangement
56
import androidx.compose.foundation.layout.Column
7+
import androidx.compose.foundation.layout.defaultMinSize
68
import androidx.compose.foundation.layout.fillMaxWidth
79
import androidx.compose.foundation.layout.padding
810
import androidx.compose.foundation.rememberScrollState
@@ -11,6 +13,7 @@ import androidx.compose.foundation.text.KeyboardOptions
1113
import androidx.compose.foundation.verticalScroll
1214
import androidx.compose.material3.Button
1315
import androidx.compose.material3.MaterialTheme
16+
import androidx.compose.material3.OutlinedButton
1417
import androidx.compose.material3.Text
1518
import androidx.compose.material3.TextButton
1619
import androidx.compose.runtime.Composable
@@ -24,8 +27,10 @@ import androidx.compose.ui.res.stringResource
2427
import androidx.compose.ui.text.input.ImeAction
2528
import androidx.compose.ui.text.input.KeyboardCapitalization
2629
import androidx.compose.ui.unit.dp
30+
import androidx.compose.ui.unit.sp
2731
import io.snabble.sdk.Snabble
2832
import io.snabble.sdk.ui.R
33+
import io.snabble.sdk.ui.ThemeManager
2934
import io.snabble.sdk.ui.cart.shoppingcart.utils.rememberTextFieldManager
3035
import io.snabble.sdk.ui.payment.creditcard.shared.country.displayName
3136
import io.snabble.sdk.ui.payment.creditcard.shared.country.domain.models.Address
@@ -46,6 +51,8 @@ internal fun CustomerInfoInputScreen(
4651
onBackNavigationClick: () -> Unit,
4752
) {
4853
val preFilledData = Snabble.formPrefillData
54+
val primaryButtonConfig = ThemeManager.primaryButtonConfig
55+
val secondaryButtonConfig = ThemeManager.secondaryButtonConfig
4956

5057
var name by remember { mutableStateOf(preFilledData?.name.orEmpty()) }
5158
var intCallingCode by remember { mutableStateOf("") }
@@ -193,11 +200,16 @@ internal fun CustomerInfoInputScreen(
193200
verticalArrangement = Arrangement.spacedBy(4.dp),
194201
) {
195202
Button(
196-
modifier = Modifier.fillMaxWidth(),
203+
modifier = Modifier
204+
.fillMaxWidth()
205+
.defaultMinSize(minHeight = primaryButtonConfig.minHeight.dp),
197206
onClick = { onSendAction(createCustomerInfo()) },
198207
enabled = !isLoading && areRequiredFieldsSet
199208
) {
200-
Text(stringResource(R.string.Snabble_Payment_CustomerInfo_next))
209+
Text(
210+
stringResource(R.string.Snabble_Payment_CustomerInfo_next),
211+
fontSize = primaryButtonConfig.textSize.sp
212+
)
201213
}
202214
AnimatedVisibility(visible = showError) {
203215
Text(
@@ -208,11 +220,26 @@ internal fun CustomerInfoInputScreen(
208220
)
209221
}
210222
}
211-
TextButton(
212-
modifier = Modifier.fillMaxWidth(),
213-
onClick = onBackNavigationClick
214-
) {
215-
Text(text = stringResource(R.string.Snabble_cancel))
223+
if (!secondaryButtonConfig.useOutlinedButton) {
224+
TextButton(
225+
modifier = Modifier.fillMaxWidth(),
226+
onClick = onBackNavigationClick
227+
) {
228+
Text(text = stringResource(R.string.Snabble_cancel))
229+
}
230+
} else {
231+
OutlinedButton(
232+
modifier = Modifier
233+
.fillMaxWidth()
234+
.defaultMinSize(minHeight = secondaryButtonConfig.minHeight.dp),
235+
onClick = onBackNavigationClick,
236+
border = BorderStroke(width = 1.dp, color = MaterialTheme.colorScheme.primary)
237+
) {
238+
Text(
239+
text = stringResource(R.string.Snabble_cancel),
240+
fontSize = secondaryButtonConfig.textSize.sp
241+
)
242+
}
216243
}
217244
}
218245
}

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

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ package io.snabble.sdk.ui.remotetheme
22

33
import android.content.Context
44
import android.content.res.ColorStateList
5+
import android.graphics.Color
56
import android.util.AttributeSet
7+
import androidx.core.graphics.ColorUtils
68
import com.google.android.material.button.MaterialButton
9+
import com.google.android.material.color.MaterialColors
710
import io.snabble.sdk.Project
811
import io.snabble.sdk.Snabble
912
import io.snabble.sdk.ui.R
@@ -26,28 +29,43 @@ class SnabbleSecondaryButton @JvmOverloads constructor(
2629

2730
private fun setProjectAppTheme() {
2831
val project = Snabble.checkedInProject.value
32+
setRippleColor(project)
2933
setTextColorFor(project)
34+
setStrokeColor(project)
3035
}
3136

32-
private fun setTextColorFor(project: Project?) {
33-
val defaultTextColorStateList = textColors
37+
private fun setStrokeColor(project: Project?) {
38+
strokeColor = ColorStateList.valueOf(context.primaryColorForProject(project))
39+
}
3440

35-
// Extract the default disabled color
36-
val defaultDisabledTextColor = defaultTextColorStateList.getColorForState(
37-
intArrayOf(-android.R.attr.state_enabled),
38-
currentTextColor
41+
private fun setRippleColor(project: Project?) {
42+
val highlightColor = MaterialColors.getColor(
43+
context,
44+
com.google.android.material.R.attr.colorControlHighlight,
45+
Color.TRANSPARENT
46+
)
47+
48+
val rippleColorStateList = ColorStateList.valueOf(
49+
ColorUtils.setAlphaComponent(
50+
context.primaryColorForProject(project), Color.alpha(highlightColor)
3951
)
52+
)
4053

54+
rippleColor = rippleColorStateList
55+
}
56+
57+
private fun setTextColorFor(project: Project?) {
4158
val states = arrayOf(
4259
intArrayOf(-android.R.attr.state_enabled),
43-
intArrayOf(android.R.attr.state_enabled)
60+
intArrayOf(android.R.attr.state_enabled),
4461
)
4562

4663
val colors = intArrayOf(
47-
defaultDisabledTextColor,
48-
context.primaryColorForProject(project)
64+
context.primaryColorForProject(project),
65+
context.primaryColorForProject(project),
4966
)
5067

5168
setTextColor(ColorStateList(states, colors))
5269
}
70+
5371
}

ui/src/main/res/layout/snabble_view_payment_status.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@
285285

286286
<io.snabble.sdk.ui.remotetheme.SnabblePrimaryButton
287287
android:id="@+id/send_feedback"
288-
style="@style/Widget.Material3.Button"
288+
style="@style/Snabble.Widget.MaterialComponents.Button"
289289
android:layout_width="match_parent"
290290
android:layout_height="wrap_content"
291291
android:layout_marginTop="16dp"
@@ -325,7 +325,7 @@
325325

326326
<io.snabble.sdk.ui.remotetheme.SnabblePrimaryButton
327327
android:id="@+id/back"
328-
style="@style/Widget.Material3.Button"
328+
style="@style/Snabble.Widget.MaterialComponents.Button"
329329
android:layout_width="match_parent"
330330
android:layout_height="wrap_content"
331331
android:layout_margin="16dp"

ui/src/main/res/layout/snabble_view_search_product.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
android:layout_centerHorizontal="true"
2727
android:layout_marginTop="56dp"
2828
android:gravity="center"
29+
android:visibility="gone"
2930
android:padding="16dp"
3031
tools:text="Add 2623237002494 as is" />
3132
</RelativeLayout>

ui/src/main/res/values/styles.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949

5050
<style name="ThemeOverlay.Snabble.CouponDetail.ActivateButtonStyle" parent="Snabble.CouponDetail.ActivateButtonStyle" />
5151

52+
<style name="ThemeOverlay.Snabble.SecondaryButton" parent="Widget.Material3.Button.TextButton"/>
53+
5254
<style name="Snabble.CouponDetail.AppliedButtonStyle" parent="Widget.Material3.Button">
5355
<item name="android:layout_width">match_parent</item>
5456
<item name="android:layout_height">wrap_content</item>

0 commit comments

Comments
 (0)