45. class StopwatchUtil {
private val _state = MutableStateFlow<StopwatchState>(StopwatchState.STOP)
val state = _state.asStateFlow()
private val _time = MutableStateFlow(0)
val time = _time.asStateFlow()
private val coroutineScope = CoroutineScope(Dispatchers.IO)
private var job: Job? = null
val formattedTimeString = time.map {
val hour = time.value / 3600
val minute = (time.value % 3600) / 60
val second = time.value % 60
"%02d:%02d:%02d".format(hour, minute, second)
}.stateIn(coroutineScope, SharingStarted.Lazily, "00:00:00")
fun start() {...}
fun pause() {...}
...
}
StopwatchUtil.kt
52. Version
@Composable
fun SettingsScreen(modifier: Modifier = Modifier) {
var value by remember { mutableStateOf(0f) }
var checked by remember { mutableStateOf(false) }
Column(...) {
...
VersionText(version = getVersionName())
}
}
@Composable
fun SettingsScreen(modifier: Modifier = Modifier) {
var value by remember { mutableStateOf(0f) }
var checked by remember { mutableStateOf(false) }
Column(...) {
...
VersionText(version = "1.0.0")
}
}
60. ViewModel
class AppViewModel(private val stopwatchUtil: StopwatchUtil = StopwatchUtil()) : ViewModel()
{
{
val stopwatchState = stopwatchUtil.state
val formattedTimeString = stopwatchUtil.formattedTimeString
fun start() = stopwatchUtil.start()
fun pause() = stopwatchUtil.pause()
fun resume() = stopwatchUtil.resume()
fun reset() = stopwatchUtil.reset()
fun destroy() = stopwatchUtil.destroy()
}
61. class AppViewModel(private val stopwatchUtil: StopwatchUtil = StopwatchUtil()) : ViewModel()
{
{
val stopwatchState = stopwatchUtil.state
val formattedTimeString = stopwatchUtil.formattedTimeString
fun start() = stopwatchUtil.start()
fun pause() = stopwatchUtil.pause()
fun resume() = stopwatchUtil.resume()
fun reset() = stopwatchUtil.reset()
fun destroy() = stopwatchUtil.destroy()
}
ViewModel
ViewModel ?
62. ViewModel
class AppViewModel(private val stopwatchUtil: StopwatchUtil = StopwatchUtil()) : ViewModel() {
val stopwatchState = stopwatchUtil.state
val formattedTimeString = stopwatchUtil.formattedTimeString
fun start() = stopwatchUtil.start()
fun pause() = stopwatchUtil.pause()
fun resume() = stopwatchUtil.resume()
fun reset() = stopwatchUtil.reset()
fun destroy() = stopwatchUtil.destroy()
}
ViewModel Android Common
class AppViewModel(private val stopwatchUtil: StopwatchUtil = StopwatchUtil()) : ViewModel()
{
{
val stopwatchState = stopwatchUtil.state
val formattedTimeString = stopwatchUtil.formattedTimeString
fun start() = stopwatchUtil.start()
fun pause() = stopwatchUtil.pause()
fun resume() = stopwatchUtil.resume()
fun reset() = stopwatchUtil.reset()
fun destroy() = stopwatchUtil.destroy()
}
67. ViewModel
class AppViewModel(private val stopwatchUtil: StopwatchUtil = StopwatchUtil()) : BaseViewModel() {
val stopwatchState = stopwatchUtil.state
val formattedTimeString = stopwatchUtil.formattedTimeString
fun start() = stopwatchUtil.start() // 혹은 suspend로 구현 후 viewModelScope 사용
fun pause() = stopwatchUtil.pause()
fun resume() = stopwatchUtil.resume()
fun reset() = stopwatchUtil.reset()
override fun destroy() {
super.destroy()
stopwatchUtil.destroy()
}
}