Skip to content

Commit 26f982f

Browse files
committed
add support for toolbar in activities
1 parent 1f0a40d commit 26f982f

19 files changed

+85
-56
lines changed

docs/docs/ui-api.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,23 @@ Exceptions are View's for entering payment credentials.
3232
The SDK does need to switch to different Screens at different times (For example, when starting a Checkout).
3333

3434
If you do not register any ui actions, activities will be launched. If you want to integrate the SDK more deeply into
35-
your App, you need to Handlers to Events.
35+
your App, you need to add Handlers to Events.
3636

3737
Todo that, call **SnabbleUI.setUiAction**, which will then be called instead of opening an Activity.
3838

3939
In some cases Activities will still be launched, which will then not invoke the ui action handler.
4040

41+
## Styling
42+
43+
The SDK uses Material 3 theme styling where possible.
44+
45+
Activities can also use a MaterialToolbar, to enable using a Toolbar set the Theme attribute
46+
**snabbleToolbarStyle**.
47+
48+
## Night mode
49+
50+
The SDK supports Material 3 DayNight themes.
51+
4152
## List of UI Actions
4253

4354
- SHOW_CHECKOUT,

java-sample/src/main/java/io/snabble/testapp/App.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@ public interface InitCallback {
3232
public void onCreate() {
3333
super.onCreate();
3434
instance = this;
35-
36-
// if you are using a light mode theme, disable night mode resources
37-
// this seems like a bug in android
38-
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
3935
}
4036

4137
public void initBlocking() {

java-sample/src/main/res/values/styles.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<resources>
3-
<style name="SnabbleTheme" parent="@style/Theme.Material3.DayNight">
3+
<style name="SnabbleTheme" parent="@style/Theme.Material3.DayNight.NoActionBar">
44
<item name="colorPrimary">#0077BB</item>
55
<item name="colorOnPrimary">#ffffff</item>
66
<item name="colorPrimaryContainer">#0077BB</item>
@@ -16,5 +16,8 @@
1616
<item name="colorOnTertiary">#ffffff</item>
1717
<item name="colorTertiaryContainer">#0077BB</item>
1818
<item name="colorOnTertiaryContainer">#ffffff</item>
19+
20+
<!-- You can enable a Toolbar inside Activities, by setting a style -->
21+
<item name="snabbleToolbarStyle">@style/SnabbleTheme</item>
1922
</style>
2023
</resources>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package io.snabble.sdk.ui
2+
3+
import android.os.Bundle
4+
import android.util.TypedValue
5+
import androidx.appcompat.app.AppCompatActivity
6+
import androidx.fragment.app.Fragment
7+
import com.google.android.material.appbar.MaterialToolbar
8+
9+
abstract class BaseFragmentActivity : AppCompatActivity() {
10+
override fun onCreate(savedInstanceState: Bundle?) {
11+
super.onCreate(savedInstanceState)
12+
13+
val typedValue = TypedValue()
14+
if (getTheme().resolveAttribute(R.attr.snabbleToolbarStyle, typedValue, true)) {
15+
setContentView(R.layout.snabble_activity_simple_fragment_with_toolbar)
16+
val toolbar = findViewById<MaterialToolbar>(R.id.toolbar)
17+
setSupportActionBar(toolbar)
18+
} else {
19+
setContentView(R.layout.snabble_activity_simple_fragment)
20+
}
21+
22+
supportFragmentManager
23+
.beginTransaction()
24+
.replace(R.id.content, onCreateFragment())
25+
.commitAllowingStateLoss()
26+
}
27+
28+
abstract fun onCreateFragment(): Fragment
29+
}

ui/src/main/java/io/snabble/sdk/ui/SimpleFragmentActivity.kt

Lines changed: 0 additions & 23 deletions
This file was deleted.

ui/src/main/java/io/snabble/sdk/ui/SnabbleUI.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ object SnabbleUI {
8181
@JvmOverloads
8282
fun executeAction(context: Context, event: Event?, args: Bundle? = null) {
8383
val activity = UIUtils.getHostFragmentActivity(context)
84-
if (event == GO_BACK && activity is SimpleFragmentActivity) {
84+
if (event == GO_BACK && activity is BaseFragmentActivity) {
8585
activity.finish()
8686
return
8787
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package io.snabble.sdk.ui.cart
22

33
import androidx.fragment.app.Fragment
4-
import io.snabble.sdk.ui.SimpleFragmentActivity
4+
import io.snabble.sdk.ui.BaseFragmentActivity
55

6-
class ShoppingCartActivity : SimpleFragmentActivity() {
6+
class ShoppingCartActivity : BaseFragmentActivity() {
77
override fun onCreateFragment(): Fragment = ShoppingCartFragment()
88
}

ui/src/main/java/io/snabble/sdk/ui/cart/ShoppingCartFragment.kt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,6 @@ open class ShoppingCartFragment : Fragment() {
3131

3232
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
3333
inflater.inflate(R.menu.snabble_menu_shopping_cart, menu)
34-
for (i in 0 until menu.size()) {
35-
val menuItem = menu.getItem(i)
36-
val color = UIUtils.getColorByAttribute(requireContext(), R.attr.colorOnActionBar)
37-
MenuItemCompat.setIconTintList(menuItem, ColorStateList.valueOf(color))
38-
}
3934
}
4035

4136
override fun onOptionsItemSelected(item: MenuItem): Boolean {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package io.snabble.sdk.ui.payment
22

33
import androidx.fragment.app.Fragment
4-
import io.snabble.sdk.ui.SimpleFragmentActivity
4+
import io.snabble.sdk.ui.BaseFragmentActivity
55

6-
class AgeVerificationInputActivity : SimpleFragmentActivity() {
6+
class AgeVerificationInputActivity : BaseFragmentActivity() {
77
override fun onCreateFragment(): Fragment = AgeVerificationInputFragment()
88
}

ui/src/main/java/io/snabble/sdk/ui/payment/CreditCardInputActivity.kt

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

33
import androidx.fragment.app.Fragment
4-
import io.snabble.sdk.ui.SimpleFragmentActivity
4+
import io.snabble.sdk.ui.BaseFragmentActivity
55

6-
class CreditCardInputActivity : SimpleFragmentActivity() {
6+
class CreditCardInputActivity : BaseFragmentActivity() {
77
companion object {
88
const val ARG_PROJECT_ID = CreditCardInputView.ARG_PROJECT_ID
99
const val ARG_PAYMENT_TYPE = CreditCardInputView.ARG_PAYMENT_TYPE

0 commit comments

Comments
 (0)