-
Notifications
You must be signed in to change notification settings - Fork 32
/
ProfileScreen.kt
79 lines (65 loc) · 2.66 KB
/
ProfileScreen.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
/*
* Copyright 2019 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/
package com.icerockdev.library.universal
import dev.icerock.moko.mvvm.dispatcher.EventsDispatcher
import dev.icerock.moko.mvvm.dispatcher.EventsDispatcherOwner
import dev.icerock.moko.mvvm.livedata.LiveData
import dev.icerock.moko.mvvm.livedata.MutableLiveData
import dev.icerock.moko.mvvm.viewmodel.ViewModel
import dev.icerock.moko.parcelize.Parcelable
import dev.icerock.moko.parcelize.Parcelize
import dev.icerock.moko.resources.desc.StringDesc
import dev.icerock.moko.resources.desc.desc
import dev.icerock.moko.widgets.core.screen.Args
import dev.icerock.moko.widgets.core.screen.Screen
import dev.icerock.moko.widgets.core.screen.getArgument
import dev.icerock.moko.widgets.core.screen.getViewModel
import dev.icerock.moko.widgets.core.screen.listen
import dev.icerock.moko.widgets.core.screen.navigation.NavigationBar
import dev.icerock.moko.widgets.core.screen.navigation.NavigationItem
import dev.icerock.moko.widgets.core.screen.navigation.Route
import dev.icerock.moko.widgets.core.screen.showToast
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
abstract class ProfileScreen : Screen<Args.Parcel<ProfileScreen.Arg>>(),
ProfileViewModel.EventsListener, NavigationItem {
protected abstract val backRoute: Route<Unit>
override val navigationBar: NavigationBar = NavigationBar.Normal(title = "Profile".desc())
protected val profileViewModel: ProfileViewModel by lazy {
val arg = getArgument()
val viewModel = getViewModel {
ProfileViewModel(
eventsDispatcher = createEventsDispatcher(),
userId = arg.userId
).apply { start() }
}
viewModel.eventsDispatcher.listen(this, this)
return@lazy viewModel
}
override fun showTimedMessage(message: StringDesc) {
showToast(message)
}
@Parcelize
data class Arg(
val userId: Int
) : Parcelable
}
expect class PlatformProfileScreen(backRoute: Route<Unit>) : ProfileScreen
class ProfileViewModel(
override val eventsDispatcher: EventsDispatcher<EventsListener>,
val userId: Int
) : ViewModel(), EventsDispatcherOwner<ProfileViewModel.EventsListener> {
val text: LiveData<StringDesc> = MutableLiveData(initialValue = "User $userId".desc())
@Suppress("MagicNumber")
fun start() {
viewModelScope.launch {
delay(2000)
val message = "Hello Profile $userId".desc()
eventsDispatcher.dispatchEvent { showTimedMessage(message) }
}
}
interface EventsListener {
fun showTimedMessage(message: StringDesc)
}
}