/
DurationState.kt
170 lines (149 loc) · 6.15 KB
/
DurationState.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
* Copyright (C) 2022-2024. Maximilian Keppeler (https://www.maxkeppeler.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.maxkeppeler.sheets.duration
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.Saver
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import com.maxkeppeker.sheets.core.views.BaseTypeState
import com.maxkeppeler.sheets.duration.models.DurationConfig
import com.maxkeppeler.sheets.duration.models.DurationSelection
import com.maxkeppeler.sheets.duration.utils.*
import java.io.Serializable
/**
* Handles the duration state.
* @param selection The selection configuration for the dialog view.
* @param config The general configuration for the dialog view.
* @param stateData The data of the state when the state is required to be restored.
*/
internal class DurationState(
val selection: DurationSelection,
val config: DurationConfig,
stateData: DurationStateData? = null
) : BaseTypeState() {
var timeTextValue by mutableStateOf(stateData?.timeTextValue ?: getInitTimeTextValue())
var timeInfoInSeconds by mutableStateOf(getInitTimeInfoInSeconds())
var values by mutableStateOf(getInitValuePairs())
var indexOfFirstValue by mutableStateOf(getInitIndexOfFirstValue())
val keys by mutableStateOf(getInputKeys(config))
var valid by mutableStateOf(isValid())
init {
checkSetup()
}
private fun checkSetup() {
if((config.currentTime ?: 0) < 0L) throw IllegalStateException("Please correct your setup. The current time must be greater than or equals 0.")
if(config.minTime < 0L) throw IllegalStateException("Please correct your setup. The min time must be greater than or equals 0.")
if(config.maxTime < 0L) throw IllegalStateException("Please correct your setup. The max time must be greater than or equals 0.")
if(config.minTime > config.maxTime) throw IllegalStateException("Please correct your setup. The min time must be less than or equals the max time.")
}
private fun getInitTimeTextValue(): StringBuilder {
return parseCurrentTime(config.timeFormat, config.currentTime)
}
private fun getInitTimeInfoInSeconds(): Triple<Long, Long?, Long?> {
val value = parseToSeconds(timeTextValue, config.timeFormat)
val minTime = if (value != 0L && value < config.minTime) config.minTime else null
val maxTime = if (value != 0L && value > config.maxTime) config.maxTime else null
return Triple(value, minTime, maxTime)
}
private fun getInitValuePairs(): List<Pair<String, Label>> {
return getValuePairs(timeTextValue, config)
}
private fun getInitIndexOfFirstValue(): Int? {
return values
.indexOfFirst { runCatching { it.first.toInt() != 0 }.getOrNull() ?: false }
.takeUnless { it == -1 }
}
private fun checkValid() {
valid = isValid()
}
private fun refreshTime() {
timeInfoInSeconds = getInitTimeInfoInSeconds()
values = getInitValuePairs()
indexOfFirstValue = getInitIndexOfFirstValue()
checkValid()
}
private fun isValid(): Boolean = timeInfoInSeconds.first in config.minTime..config.maxTime
&& timeInfoInSeconds.second == null
&& timeInfoInSeconds.third == null
fun onEnterValue(value: String) {
val newTimeBuilder = timeTextValue.apply {
if (length >= config.timeFormat.length) {
repeat(value.length) { deleteCharAt(0) }
}
append(value)
}
timeTextValue = StringBuilder(newTimeBuilder.toString())
refreshTime()
}
fun onBackspaceAction() {
val newTimeBuilder = timeTextValue.apply {
deleteCharAt(lastIndex)
insert(0, 0.toString())
}
timeTextValue = StringBuilder(newTimeBuilder)
refreshTime()
}
fun onEmptyAction() {
timeTextValue = StringBuilder(parseCurrentTime(config.timeFormat))
refreshTime()
}
fun onFinish() {
selection.onPositiveClick(timeInfoInSeconds.first)
}
override fun reset() {
timeTextValue = getInitTimeTextValue()
timeInfoInSeconds = getInitTimeInfoInSeconds()
values = getInitValuePairs()
indexOfFirstValue = getInitIndexOfFirstValue()
}
companion object {
/**
* [Saver] implementation.
* @param selection The selection configuration for the dialog view.
* @param config The general configuration for the dialog view.
*/
fun Saver(
selection: DurationSelection,
config: DurationConfig
): Saver<DurationState, *> = Saver(
save = { state -> DurationStateData(state.timeTextValue) },
restore = { data -> DurationState(selection, config, data) }
)
}
/**
* Data class that stores the important information of the current state
* and can be used by the [Saver] to save and restore the state.
*/
data class DurationStateData(
val timeTextValue: StringBuilder,
) : Serializable
}
/**
* Create a DurationState and remember it.
* @param selection The selection configuration for the dialog view.
* @param config The general configuration for the dialog view.
*/
@Composable
internal fun rememberDurationState(
selection: DurationSelection,
config: DurationConfig,
): DurationState = rememberSaveable(
inputs = arrayOf(selection, config),
saver = DurationState.Saver(selection, config),
init = { DurationState(selection, config) }
)