-
Notifications
You must be signed in to change notification settings - Fork 629
/
Setting.kt
51 lines (40 loc) · 1.8 KB
/
Setting.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
package com.stripe.android.financialconnections.example.settings
import com.stripe.android.financialconnections.example.Flow
import com.stripe.android.financialconnections.example.Merchant
import com.stripe.android.financialconnections.example.data.model.LinkAccountSessionBody
import com.stripe.android.financialconnections.example.data.model.PaymentIntentBody
sealed class Setting<T> {
abstract fun lasRequest(body: LinkAccountSessionBody): LinkAccountSessionBody
abstract fun paymentIntentRequest(body: PaymentIntentBody): PaymentIntentBody
abstract fun valueUpdated(currentSettings: List<Setting<*>>, value: T): List<Setting<*>>
fun replace(currentSettings: List<Setting<*>>, newSetting: Setting<*>): List<Setting<*>> {
val settingsReplaced = currentSettings.map { if (it::class == newSetting::class) newSetting else it }
return if (settingsReplaced.contains(newSetting)) settingsReplaced else settingsReplaced + newSetting
}
fun saveable(): Saveable<T>? {
@Suppress("UNCHECKED_CAST")
return this as? Saveable<T>?
}
open fun shouldDisplay(merchant: Merchant, flow: Flow): Boolean = true
abstract val displayName: String
abstract val selectedOption: T
}
interface Saveable<T> {
val key: String
fun convertToString(value: T): String?
fun convertToValue(value: String): T
}
// Single choice settings
sealed class SingleChoiceSetting<T>(
override val displayName: String,
override val selectedOption: T,
open val options: List<Option<T>>,
) : Setting<T>()
// Multiple choice settings
sealed class MultipleChoiceSetting<T>(
override val displayName: String,
override val selectedOption: List<T>,
open val options: List<Option<T>>
) : Setting<List<T>>()
// Define the option
data class Option<T>(val name: String, val value: T)