-
Notifications
You must be signed in to change notification settings - Fork 629
/
InputAddressScreen.kt
144 lines (139 loc) · 5.09 KB
/
InputAddressScreen.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package com.stripe.android.paymentsheet.addresselement
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.CircularProgressIndicator
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.stripe.android.paymentsheet.R
import com.stripe.android.paymentsheet.ui.AddressOptionsAppBar
import com.stripe.android.ui.core.FormUI
import com.stripe.android.ui.core.elements.CheckboxElementUI
import com.stripe.android.ui.core.injection.NonFallbackInjector
@Composable
internal fun InputAddressScreen(
primaryButtonEnabled: Boolean,
primaryButtonText: String,
title: String,
onPrimaryButtonClick: () -> Unit,
onCloseClick: () -> Unit,
formContent: @Composable ColumnScope.() -> Unit,
checkboxContent: @Composable ColumnScope.() -> Unit
) {
val focusManager = LocalFocusManager.current
ScrollableColumn(
modifier = Modifier
.fillMaxHeight()
.background(MaterialTheme.colors.surface)
) {
AddressOptionsAppBar(
isRootScreen = true,
onButtonClick = {
focusManager.clearFocus()
onCloseClick()
}
)
Column(
Modifier
.padding(horizontal = 20.dp)
) {
Text(
title,
style = MaterialTheme.typography.h4,
modifier = Modifier.padding(bottom = 8.dp)
)
formContent()
checkboxContent()
AddressElementPrimaryButton(
isEnabled = primaryButtonEnabled,
text = primaryButtonText
) {
focusManager.clearFocus()
onPrimaryButtonClick()
}
}
}
}
@Composable
internal fun InputAddressScreen(
injector: NonFallbackInjector
) {
val viewModel: InputAddressViewModel = viewModel(
factory = InputAddressViewModel.Factory(
injector
)
)
val formController by viewModel.formController.collectAsState()
if (formController == null) {
Box(
contentAlignment = Alignment.Center,
modifier = Modifier.fillMaxSize()
) {
CircularProgressIndicator()
}
} else {
formController?.let {
val completeValues by it.completeFormValues.collectAsState(null)
val buttonText = viewModel.args.config?.buttonTitle ?: stringResource(
R.string.stripe_paymentsheet_address_element_primary_button
)
val titleText = viewModel.args.config?.title ?: stringResource(
R.string.stripe_paymentsheet_address_element_shipping_address
)
val formEnabled by viewModel.formEnabled.collectAsState(initial = true)
val checkboxChecked by viewModel.checkboxChecked.collectAsState(false)
InputAddressScreen(
primaryButtonEnabled = completeValues != null,
primaryButtonText = buttonText,
title = titleText,
onPrimaryButtonClick = {
viewModel.clickPrimaryButton(
completeValues,
checkboxChecked
)
},
onCloseClick = { viewModel.navigator.dismiss() },
formContent = {
FormUI(
it.hiddenIdentifiers,
viewModel.formEnabled,
it.elements,
it.lastTextFieldIdentifier
) {
Box(
contentAlignment = Alignment.Center,
modifier = Modifier.fillMaxSize()
) {
CircularProgressIndicator()
}
}
},
checkboxContent = {
viewModel.args.config?.additionalFields?.checkboxLabel?.let { label ->
CheckboxElementUI(
isChecked = checkboxChecked,
label = label,
isEnabled = formEnabled,
onValueChange = {
viewModel.clickCheckbox(!checkboxChecked)
}
)
}
}
)
}
}
}