|
1 | 1 | package io.snabble.sdk.ui.checkout |
2 | 2 |
|
| 3 | +import android.annotation.SuppressLint |
3 | 4 | import android.content.Context |
4 | 5 | import android.content.Intent |
| 6 | +import android.graphics.Color |
5 | 7 | import android.os.Bundle |
| 8 | +import android.view.View |
| 9 | +import android.view.ViewGroup |
| 10 | +import android.view.WindowManager |
6 | 11 | import androidx.appcompat.app.AlertDialog |
| 12 | +import androidx.core.view.* |
7 | 13 | import androidx.fragment.app.FragmentActivity |
8 | 14 | import androidx.lifecycle.Observer |
9 | 15 | import androidx.navigation.NavController |
@@ -38,8 +44,8 @@ class CheckoutActivity : FragmentActivity() { |
38 | 44 | @JvmStatic |
39 | 45 | fun restoreCheckoutIfNeeded(context: Context) { |
40 | 46 | Snabble.initializationState.observeForever(object : Observer<InitializationState> { |
41 | | - override fun onChanged(t: InitializationState) { |
42 | | - if (t == InitializationState.INITIALIZED) { |
| 47 | + override fun onChanged(value: InitializationState) { |
| 48 | + if (value == InitializationState.INITIALIZED) { |
43 | 49 | Snabble.initializationState.removeObserver(this) |
44 | 50 | val project = Snabble.checkedInProject.value |
45 | 51 | if (project?.checkout?.state?.value?.isCheckoutState == true) { |
@@ -74,6 +80,7 @@ class CheckoutActivity : FragmentActivity() { |
74 | 80 | val graphInflater = navHostFragment.navController.navInflater |
75 | 81 | navGraph = graphInflater.inflate(R.navigation.snabble_nav_checkout) |
76 | 82 | navController = navHostFragment.navController |
| 83 | + setUpToolBarAndStatusBar() |
77 | 84 |
|
78 | 85 | if (project == null) { |
79 | 86 | finishWithError("Project not set") |
@@ -113,6 +120,17 @@ class CheckoutActivity : FragmentActivity() { |
113 | 120 | } |
114 | 121 | } |
115 | 122 |
|
| 123 | + private fun setUpToolBarAndStatusBar() { |
| 124 | + val showToolBar = resources.getBoolean(R.bool.showToolbarInCheckout) |
| 125 | + findViewById<View>(R.id.checkout_toolbar_spacer)?.isVisible = showToolBar |
| 126 | + navController.addOnDestinationChangedListener { _, destination, arguments -> |
| 127 | + findViewById<View>(R.id.checkout_toolbar)?.isVisible = arguments?.getBoolean("showToolbar", false) == true |
| 128 | + } |
| 129 | + if (showToolBar) { |
| 130 | + applyInsets() |
| 131 | + } |
| 132 | + } |
| 133 | + |
116 | 134 | private fun finishWithError(error: String) { |
117 | 135 | Logger.e(error) |
118 | 136 | finish() |
@@ -176,6 +194,37 @@ class CheckoutActivity : FragmentActivity() { |
176 | 194 | } |
177 | 195 | } |
178 | 196 |
|
| 197 | + private fun applyInsets() { |
| 198 | + val root = findViewById<View>(R.id.root) ?: return |
| 199 | + |
| 200 | + window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS) |
| 201 | + WindowCompat.setDecorFitsSystemWindows(window, false) |
| 202 | + val windowInsetsController = WindowInsetsControllerCompat(window, root) |
| 203 | + |
| 204 | + ViewCompat.setOnApplyWindowInsetsListener(root) { view, windowInsets -> |
| 205 | + window.statusBarColor = Color.parseColor("#22000000") |
| 206 | + windowInsetsController.isAppearanceLightStatusBars = false |
| 207 | + |
| 208 | + val currentInsetTypeMask = listOf( |
| 209 | + WindowInsetsCompat.Type.navigationBars(), |
| 210 | + WindowInsetsCompat.Type.ime(), |
| 211 | + WindowInsetsCompat.Type.systemBars(), |
| 212 | + WindowInsetsCompat.Type.statusBars() |
| 213 | + ).fold(0) { accumulator, type -> accumulator or type } |
| 214 | + |
| 215 | + @SuppressLint("WrongConstant") |
| 216 | + val insets = windowInsets.getInsets(currentInsetTypeMask) |
| 217 | + view.updateLayoutParams<ViewGroup.MarginLayoutParams> { |
| 218 | + updateMargins(insets.left, 0, insets.right, insets.bottom) |
| 219 | + } |
| 220 | + findViewById<View>(R.id.checkout_toolbar_spacer)?.apply { |
| 221 | + setPadding(0, insets.top, 0, 0) |
| 222 | + } |
| 223 | + |
| 224 | + windowInsets.inset(insets.left, insets.top, insets.right, insets.bottom) |
| 225 | + } |
| 226 | + } |
| 227 | + |
179 | 228 | override fun onBackPressed() { |
180 | 229 | // prevent user from backing out while checkout is in progress |
181 | 230 | if (checkout?.state?.value == CheckoutState.PAYMENT_APPROVED) { |
|
0 commit comments