Skip to content

Commit a122655

Browse files
authored
Serializable data classes and reusable external billing screen (#196)
1 parent 8b13da5 commit a122655

File tree

4 files changed

+76
-59
lines changed

4 files changed

+76
-59
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ import io.snabble.sdk.utils.Dispatch
1515
import io.snabble.sdk.utils.Logger
1616
import io.snabble.sdk.utils.SimpleJsonCallback
1717
import kotlinx.parcelize.Parcelize
18+
import kotlinx.serialization.Serializable
1819
import okhttp3.Callback
1920
import okhttp3.Request
2021

2122
object Payone {
2223

24+
@Serializable
2325
@Parcelize
2426
data class PayoneTokenizationData(
2527
val merchantID: String,
@@ -31,12 +33,14 @@ object Payone {
3133
val links: Map<String, Link>
3234
) : Parcelable
3335

36+
@Serializable
3437
@Parcelize
3538
data class PreAuthInfo(
3639
val amount: Int?,
3740
val currency: String?
3841
) : Parcelable
3942

43+
@Serializable
4044
@Parcelize
4145
data class Link(
4246
val href: String?

ui/src/main/java/io/snabble/sdk/ui/payment/externalbilling/ExternalBillingFragment.kt

Lines changed: 3 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,81 +2,28 @@ package io.snabble.sdk.ui.payment.externalbilling
22

33
import android.os.Bundle
44
import android.view.View
5-
import androidx.compose.runtime.mutableStateOf
6-
import androidx.compose.runtime.remember
75
import androidx.compose.ui.platform.ComposeView
86
import androidx.compose.ui.platform.ViewCompositionStrategy
9-
import androidx.compose.ui.res.stringResource
107
import androidx.core.os.bundleOf
11-
import androidx.fragment.app.viewModels
12-
import androidx.lifecycle.compose.collectAsStateWithLifecycle
8+
import io.snabble.sdk.config.ProjectId
139
import io.snabble.sdk.ui.BaseFragment
1410
import io.snabble.sdk.ui.R
15-
import io.snabble.sdk.ui.SnabbleUI
16-
import io.snabble.sdk.ui.payment.externalbilling.ui.ExternalBillingLoginScreen
17-
import io.snabble.sdk.ui.payment.externalbilling.viewmodel.Error
18-
import io.snabble.sdk.ui.payment.externalbilling.viewmodel.ExternalBillingViewModel
19-
import io.snabble.sdk.ui.payment.externalbilling.viewmodel.Processing
20-
import io.snabble.sdk.ui.payment.externalbilling.viewmodel.Success
21-
import io.snabble.sdk.ui.utils.ThemeWrapper
2211

2312
open class ExternalBillingFragment : BaseFragment(
2413
layoutResId = R.layout.snabble_fragment_external_billing,
2514
waitForProject = false,
2615
) {
2716

28-
private val viewModel: ExternalBillingViewModel by viewModels()
29-
3017
override fun onActualViewCreated(view: View, savedInstanceState: Bundle?) {
3118
super.onActualViewCreated(view, savedInstanceState)
3219

3320
view.findViewById<ComposeView>(R.id.external_billing_compose_view).apply {
3421
setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
3522

36-
val projectId: String? = arguments?.getString(ARG_PROJECT_ID)
23+
val projectId: ProjectId? = arguments?.getString(ARG_PROJECT_ID)?.let(::ProjectId)
3724

3825
setContent {
39-
val state = viewModel.uiState.collectAsStateWithLifecycle()
40-
val errorMessage = remember {
41-
mutableStateOf("")
42-
}
43-
44-
when (val it = state.value) {
45-
is Error -> {
46-
if (it.message == "400" || it.message == "null") {
47-
errorMessage.value =
48-
stringResource(id = R.string.Snabble_Payment_ExternalBilling_Error_badRequest)
49-
} else {
50-
errorMessage.value =
51-
stringResource(id = R.string.Snabble_Payment_ExternalBilling_Error_wrongCredentials)
52-
}
53-
}
54-
55-
Processing -> {
56-
errorMessage.value = ""
57-
}
58-
59-
Success -> {
60-
SnabbleUI.executeAction(context, SnabbleUI.Event.GO_BACK)
61-
}
62-
}
63-
64-
val idDescriptor = stringResource(id = R.string.Snabble_Payment_ExternalBilling_username)
65-
66-
ThemeWrapper {
67-
ExternalBillingLoginScreen(
68-
onSaveClick = { username, password ->
69-
projectId?.let {
70-
viewModel.login(idDescriptor, username, password, it)
71-
}
72-
},
73-
isInputValid = false,
74-
errorMessage = errorMessage.value,
75-
onFocusChanged = {
76-
if (state.value != Processing) viewModel.typing()
77-
}
78-
)
79-
}
26+
PaymentExternalBillingScreen(projectId = projectId)
8027
}
8128
}
8229
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package io.snabble.sdk.ui.payment.externalbilling
2+
3+
import androidx.compose.runtime.Composable
4+
import androidx.compose.runtime.mutableStateOf
5+
import androidx.compose.runtime.remember
6+
import androidx.compose.ui.Modifier
7+
import androidx.compose.ui.platform.LocalContext
8+
import androidx.compose.ui.res.stringResource
9+
import androidx.lifecycle.compose.collectAsStateWithLifecycle
10+
import androidx.lifecycle.viewmodel.compose.viewModel
11+
import io.snabble.sdk.config.ProjectId
12+
import io.snabble.sdk.ui.R
13+
import io.snabble.sdk.ui.SnabbleUI
14+
import io.snabble.sdk.ui.payment.externalbilling.ui.ExternalBillingLoginScreen
15+
import io.snabble.sdk.ui.payment.externalbilling.viewmodel.Error
16+
import io.snabble.sdk.ui.payment.externalbilling.viewmodel.ExternalBillingViewModel
17+
import io.snabble.sdk.ui.payment.externalbilling.viewmodel.Processing
18+
import io.snabble.sdk.ui.payment.externalbilling.viewmodel.Success
19+
import io.snabble.sdk.ui.utils.ThemeWrapper
20+
21+
@Composable
22+
fun PaymentExternalBillingScreen(
23+
modifier: Modifier = Modifier,
24+
projectId: ProjectId?,
25+
viewModel: ExternalBillingViewModel = viewModel()
26+
) {
27+
val state = viewModel.uiState.collectAsStateWithLifecycle()
28+
val errorMessage = remember {
29+
mutableStateOf("")
30+
}
31+
32+
when (val it = state.value) {
33+
is Error -> {
34+
if (it.message == "400" || it.message == "null") {
35+
errorMessage.value =
36+
stringResource(id = R.string.Snabble_Payment_ExternalBilling_Error_badRequest)
37+
} else {
38+
errorMessage.value =
39+
stringResource(id = R.string.Snabble_Payment_ExternalBilling_Error_wrongCredentials)
40+
}
41+
}
42+
43+
Processing -> {
44+
errorMessage.value = ""
45+
}
46+
47+
Success -> {
48+
SnabbleUI.executeAction(LocalContext.current, SnabbleUI.Event.GO_BACK)
49+
}
50+
}
51+
52+
val idDescriptor = stringResource(id = R.string.Snabble_Payment_ExternalBilling_username)
53+
54+
ThemeWrapper {
55+
ExternalBillingLoginScreen(
56+
onSaveClick = { username, password ->
57+
projectId?.let {
58+
viewModel.login(idDescriptor, username, password, it.id)
59+
}
60+
},
61+
isInputValid = false,
62+
errorMessage = errorMessage.value,
63+
onFocusChanged = {
64+
if (state.value != Processing) viewModel.typing()
65+
}
66+
)
67+
}
68+
}

ui/src/main/java/io/snabble/sdk/ui/payment/externalbilling/ui/ExternalBillingLoginScreen.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import androidx.compose.runtime.Composable
1717
import androidx.compose.runtime.mutableStateOf
1818
import androidx.compose.runtime.saveable.rememberSaveable
1919
import androidx.compose.ui.Alignment
20-
import androidx.compose.ui.ExperimentalComposeUiApi
2120
import androidx.compose.ui.Modifier
2221
import androidx.compose.ui.platform.LocalFocusManager
2322
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
@@ -29,9 +28,8 @@ import io.snabble.sdk.ui.R
2928
import io.snabble.sdk.ui.payment.externalbilling.ui.widgets.PasswordField
3029
import io.snabble.sdk.ui.payment.payone.sepa.form.ui.widget.TextFieldWidget
3130

32-
@OptIn(ExperimentalComposeUiApi::class)
3331
@Composable
34-
fun ExternalBillingLoginScreen(
32+
internal fun ExternalBillingLoginScreen(
3533
onSaveClick: (username: String, password: String) -> Unit,
3634
onFocusChanged: () -> Unit,
3735
isInputValid: Boolean,

0 commit comments

Comments
 (0)