Skip to content

Instantly share code, notes, and snippets.

@tqlong
Created August 10, 2020 16:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tqlong/8320e37904887763a1fec00a8673bd3a to your computer and use it in GitHub Desktop.
Save tqlong/8320e37904887763a1fec00a8673bd3a to your computer and use it in GitHub Desktop.
/*
* Copyright 2020 UET-AILAB
*
* 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.ailab.aicardio.annotatescreen
import android.widget.Toast
import androidx.lifecycle.viewModelScope
import com.ailab.aicardio.repository.AnnotateStateEffectObject
import com.ailab.aicardio.toast
import kotlinx.android.synthetic.main.activity_annotate.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
class UniDirectionMVI {
companion object {
// For Singleton instantiation
@Volatile
private var instance: UniDirectionMVI? = null
fun getInstance() =
instance ?: synchronized(this) {
instance
?: UniDirectionMVI()
.also { instance = it }
}
fun process(annotateActVM: AnnotateActVM, annotateViewEvent: AnnotateViewEvent) {
getInstance().process(annotateActVM, annotateViewEvent)
}
fun renderViewState(annotateActivity: AnnotateActivity, viewState: AnnotateViewState) {
getInstance().renderViewState(annotateActivity, viewState)
}
fun renderViewEffect(annotateActivity: AnnotateActivity, viewEffect: AnnotateViewEffect) {
getInstance().renderViewEffect(annotateActivity, viewEffect)
}
}
private fun renderViewEffect(annotateActivity: AnnotateActivity, viewEffect: AnnotateViewEffect) {
when (viewEffect) {
is AnnotateViewEffect.Test -> {
Toast.makeText(annotateActivity, viewEffect.message, Toast.LENGTH_SHORT).show()
}
is AnnotateViewEffect.TestAsync -> {
Toast.makeText(annotateActivity, viewEffect.message, Toast.LENGTH_SHORT).show()
}
}
}
private fun renderViewState(annotateActivity: AnnotateActivity, viewState: AnnotateViewState) {
when (viewState.annotateViewStatus) {
is AnnotateViewStatus.Test -> {
val newText = "-- ${viewState.annotateViewStatus.message} --"
annotateActivity.bt_test.text = newText
}
is AnnotateViewStatus.TestAsync -> {
val newText = "++ ${viewState.annotateViewStatus.message} ++"
annotateActivity.bt_test.text = newText
}
}
}
fun process(annotateActVM: AnnotateActVM, annotateViewEvent: AnnotateViewEvent) {
if (annotateViewEvent is AnnotateViewEvent.Test) {
annotateActVM.viewStates().value?.let {
annotateActVM.reduce(Reducer(annotateActVM, it, annotateViewEvent))
}
}
}
inner class Reducer(viewModel: AnnotateActVM, viewState: AnnotateViewState, val viewEvent: AnnotateViewEvent.Test)
: AnnotateActReducer(viewModel, viewState, viewEvent) {
suspend fun asyncRun() : Int = withContext(Dispatchers.IO) {
repeat(100000000) {
100*100
}
return@withContext 1234
}
override fun reduce(): AnnotateStateEffectObject {
val viewState = viewState.copy(annotateViewStatus = AnnotateViewStatus.Test(message = viewEvent.message))
val viewEffect = AnnotateViewEffect.Test(message = viewEvent.message)
viewModel.viewModelScope.launch {
val result = asyncRun()
viewModel.viewStates().value?.let {
viewModel.reduce(ReducerAsync(viewModel, it, AnnotateViewEvent.TestAsync(message = "Test Async $result")))
}
}
return AnnotateStateEffectObject(viewState, viewEffect)
}
}
inner class ReducerAsync(viewModel: AnnotateActVM, viewState: AnnotateViewState, val viewEvent: AnnotateViewEvent.TestAsync) : AnnotateActReducer(viewModel, viewState, viewEvent) {
override fun reduce(): AnnotateStateEffectObject {
val viewState = viewState.copy(annotateViewStatus = AnnotateViewStatus.TestAsync(message = viewEvent.message))
val viewEffect = AnnotateViewEffect.TestAsync(message = viewEvent.message)
return AnnotateStateEffectObject(viewState, viewEffect)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment