/
DayCoffee.kt
65 lines (57 loc) · 2.01 KB
/
DayCoffee.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
package ru.beryukhov.coffeegram.data
import androidx.compose.Composable
import androidx.compose.State
import androidx.compose.collectAsState
import androidx.ui.graphics.vector.VectorAsset
import androidx.ui.material.icons.Icons
import androidx.ui.material.icons.filled.Settings
import androidx.ui.res.vectorResource
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.map
import org.threeten.bp.LocalDate
import ru.beryukhov.coffeegram.R
typealias DaysCoffeesFlow = MutableStateFlow<DaysCoffees>
typealias DaysCoffees = Map<LocalDate, DayCoffee>
data class DayCoffee(
val coffeeCountMap: Map<CoffeeType, Int> = mapOf(Cappucino to 0, Latte to 0)
){
@Composable
fun getVector(): VectorAsset? {
//todo normal logic
if (coffeeCountMap[Cappucino]==0 && coffeeCountMap[Latte]!=0) return Latte.icon()
if (coffeeCountMap[Cappucino]!=0 && coffeeCountMap[Latte]==0) return Cappucino.icon()
if (coffeeCountMap[Cappucino]!=0 && coffeeCountMap[Latte]!=0) return vectorResource(R.drawable.coffee)
return null
}
}
data class DayCoffeeFlow(
val parentFlow: DaysCoffeesFlow,
val date: LocalDate
) {
fun set(value: DayCoffee) {
parentFlow.value = parentFlow.value.toMutableMap().also { it[date] = value }
}
@Composable
fun getState(): State<DayCoffee> {
return parentFlow.map { it[date] ?: DayCoffee() }.collectAsState(initial = DayCoffee())
}
val value: DayCoffee get() = parentFlow.value[date] ?: DayCoffee()
}
data class IntFlow(
val parentFlow: DayCoffeeFlow,
val coffeeType: CoffeeType
) {
fun set(value: Int) {
parentFlow.set(
DayCoffee(
parentFlow.value.coffeeCountMap.toMutableMap().also { it[coffeeType] = value })
)
}
@Composable
fun getState(): State<Int> {
return parentFlow.parentFlow.map { it[parentFlow.date] ?: DayCoffee() }
.map { it.coffeeCountMap[coffeeType] ?: 0 }.collectAsState(
initial = 0
)
}
}