52 lines
1.4 KiB
Kotlin
52 lines
1.4 KiB
Kotlin
package example
|
|
|
|
import com.vanced.manager.core.mvi.MviRenderView
|
|
import example.ExampleContainer.Action
|
|
import example.ExampleContainer.SideEffect
|
|
import example.ExampleContainer.State
|
|
import kotlinx.coroutines.CoroutineScope
|
|
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
|
import kotlinx.coroutines.Job
|
|
import kotlinx.coroutines.channels.awaitClose
|
|
import kotlinx.coroutines.flow.Flow
|
|
import kotlinx.coroutines.flow.callbackFlow
|
|
import kotlinx.coroutines.launch
|
|
|
|
|
|
class ExampleFragment : MviRenderView<State, Action, SideEffect> {
|
|
|
|
val lifecycleScope = CoroutineScope(Job())
|
|
|
|
val viewModel = ExampleViewModel()
|
|
|
|
private fun onCreate() {
|
|
lifecycleScope.launch {
|
|
viewModel.mvi.bindView(view = this@ExampleFragment, scope = this)
|
|
}
|
|
lifecycleScope.launch {
|
|
viewModel.mvi.bindSideEffects(view = this@ExampleFragment, scope = this)
|
|
}
|
|
}
|
|
|
|
override fun render(state: State) {
|
|
when (state) {
|
|
State.Default -> {
|
|
// render view
|
|
}
|
|
}
|
|
}
|
|
|
|
@ExperimentalCoroutinesApi
|
|
override fun actionsFlow(): Flow<Action> = callbackFlow {
|
|
//generate actions click and other
|
|
awaitClose()
|
|
}
|
|
|
|
override fun sideEffects(sideEffect: SideEffect) { // single events
|
|
when (sideEffect) {
|
|
is SideEffect.ShowToast -> {
|
|
// Toast.show
|
|
}
|
|
}
|
|
}
|
|
} |