From f47379eb722950f73c2b0915006db87ef49ef891 Mon Sep 17 00:00:00 2001 From: kimdohun Date: Sun, 20 Dec 2020 12:48:40 +0900 Subject: [PATCH] =?UTF-8?q?reducer,=20sideEffectHandler=EB=A5=BC=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=EC=9E=90=EC=97=90=EC=84=9C=20=EC=B6=94?= =?UTF-8?q?=EC=83=81=20=ED=94=84=EB=A1=9C=ED=8D=BC=ED=8B=B0=EB=A1=9C=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/compiler.xml | 12 ++--- .../example_android/count/CountViewModel.kt | 14 ++++-- .../example_android/login/LoginViewModel.kt | 10 ++-- .../kinda/androidwithdsl/CountViewModel.kt | 49 +++++++++++-------- .../github/GithubViewModel.kt | 48 ++++++++++-------- .../kim/kinda/kinda_android/KindaViewModel.kt | 5 +- 6 files changed, 81 insertions(+), 57 deletions(-) diff --git a/.idea/compiler.xml b/.idea/compiler.xml index 91634f4..6e0b3b9 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -5,14 +5,14 @@ - + - - + + - - - + + + diff --git a/example/android/src/main/java/dohun/kim/kinda/example_android/count/CountViewModel.kt b/example/android/src/main/java/dohun/kim/kinda/example_android/count/CountViewModel.kt index 74edf67..6445bca 100644 --- a/example/android/src/main/java/dohun/kim/kinda/example_android/count/CountViewModel.kt +++ b/example/android/src/main/java/dohun/kim/kinda/example_android/count/CountViewModel.kt @@ -1,9 +1,15 @@ package dohun.kim.kinda.example_android.count import dohun.kim.kinda.kinda_android.KindaViewModel +import dohun.kim.kinda.kinda_core.KindaReducer +import dohun.kim.kinda.kinda_core.KindaSideEffectHandler class CountViewModel : KindaViewModel( - initialState = CountState(), - reducer = CountReducer(), - sideEffectHandler = CountSideEffectHandler() -) \ No newline at end of file + initialState = CountState() +) { + override val reducer: KindaReducer + get() = CountReducer() + + override val sideEffectHandler: KindaSideEffectHandler + get() = CountSideEffectHandler() +} \ No newline at end of file diff --git a/example/android/src/main/java/dohun/kim/kinda/example_android/login/LoginViewModel.kt b/example/android/src/main/java/dohun/kim/kinda/example_android/login/LoginViewModel.kt index 00902b7..7d7312a 100644 --- a/example/android/src/main/java/dohun/kim/kinda/example_android/login/LoginViewModel.kt +++ b/example/android/src/main/java/dohun/kim/kinda/example_android/login/LoginViewModel.kt @@ -1,10 +1,14 @@ package dohun.kim.kinda.example_android.login import dohun.kim.kinda.kinda_android.KindaViewModel +import dohun.kim.kinda.kinda_core.KindaReducer +import dohun.kim.kinda.kinda_core.KindaSideEffectHandler class LoginViewModel : KindaViewModel( - initialState = LoginState(), - reducer = LoginReducer(), - sideEffectHandler = LoginSideEffectHandler() + initialState = LoginState() ) { + override val reducer: KindaReducer + get() = LoginReducer() + override val sideEffectHandler: KindaSideEffectHandler + get() = LoginSideEffectHandler() } \ No newline at end of file diff --git a/example/androidwithdsl/src/main/java/dohun/kim/kinda/androidwithdsl/CountViewModel.kt b/example/androidwithdsl/src/main/java/dohun/kim/kinda/androidwithdsl/CountViewModel.kt index d6d1e2e..525b8bb 100644 --- a/example/androidwithdsl/src/main/java/dohun/kim/kinda/androidwithdsl/CountViewModel.kt +++ b/example/androidwithdsl/src/main/java/dohun/kim/kinda/androidwithdsl/CountViewModel.kt @@ -1,33 +1,40 @@ package dohun.kim.kinda.androidwithdsl import dohun.kim.kinda.kinda_android.KindaViewModel +import dohun.kim.kinda.kinda_core.KindaReducer +import dohun.kim.kinda.kinda_core.KindaSideEffectHandler import dohun.kim.kinda.kinda_dsl.buildReducer import dohun.kim.kinda.kinda_dsl.buildSideEffectHandler import kotlinx.coroutines.delay class CountViewModel : KindaViewModel( - initialState = CountState(), - reducer = buildReducer { - whenEvent { - dispatch(CountSideEffect.Magic) - } + initialState = CountState() +) { - whenEvent { - next(copy(count = count - 1)) - } + override val reducer: KindaReducer + get() = buildReducer { + whenEvent { + dispatch(CountSideEffect.Magic) + } - whenEvent { - next(copy(count = count + 1)) - } + whenEvent { + next(copy(count = count - 1)) + } - whenEvent { - next(copy(count = count + 1000)) - } - }, - sideEffectHandler = buildSideEffectHandler { - whenSideEffect { - delay(1000) - CountEvent.Increase1000 + whenEvent { + next(copy(count = count + 1)) + } + + whenEvent { + next(copy(count = count + 1000)) + } } - } -) \ No newline at end of file + + override val sideEffectHandler: KindaSideEffectHandler + get() = buildSideEffectHandler { + whenSideEffect { + delay(1000) + CountEvent.Increase1000 + } + } +} \ No newline at end of file diff --git a/example/hilt-retrofit-test/src/main/java/dohun/kim/kinda/hilt_retrofit_test/github/GithubViewModel.kt b/example/hilt-retrofit-test/src/main/java/dohun/kim/kinda/hilt_retrofit_test/github/GithubViewModel.kt index 281149b..dfc2bc3 100644 --- a/example/hilt-retrofit-test/src/main/java/dohun/kim/kinda/hilt_retrofit_test/github/GithubViewModel.kt +++ b/example/hilt-retrofit-test/src/main/java/dohun/kim/kinda/hilt_retrofit_test/github/GithubViewModel.kt @@ -6,6 +6,8 @@ import dohun.kim.kinda.hilt_retrofit_test.data.exception.ForbiddenException import dohun.kim.kinda.hilt_retrofit_test.data.exception.InternalErrorException import dohun.kim.kinda.kinda_android.KindaViewModel import dohun.kim.kinda.kinda_core.Event +import dohun.kim.kinda.kinda_core.KindaReducer +import dohun.kim.kinda.kinda_core.KindaSideEffectHandler import dohun.kim.kinda.kinda_dsl.buildReducer import dohun.kim.kinda.kinda_dsl.buildSideEffectHandler @@ -13,29 +15,33 @@ class GithubViewModel @ViewModelInject constructor( private val githubRepository: GithubRepository ) : KindaViewModel( initialState = GithubState(), - reducer = buildReducer { - whenEvent { - next(copy(isLoading = true), GithubSideEffect.GetUsers) - } +) { + override val reducer: KindaReducer + get() = buildReducer { + whenEvent { + next(copy(isLoading = true), GithubSideEffect.GetUsers) + } - whenEvent { - next(copy(users = it.users, isLoading = false)) - } + whenEvent { + next(copy(users = it.users, isLoading = false)) + } - whenEvent { - next(copy(toastEvent = Event(it.message), isLoading = false)) + whenEvent { + next(copy(toastEvent = Event(it.message), isLoading = false)) + } } - }, - sideEffectHandler = buildSideEffectHandler { - whenSideEffect { - try { - val users = githubRepository.getUsers() - GithubEvent.GetUsersSucceed(users) - } catch (e: InternalErrorException) { - GithubEvent.ToastEvent("서버 문제가 발생했습니다.") - } catch (e: ForbiddenException) { - GithubEvent.ToastEvent("잠시 후 다시 시도해주세요.") + + override val sideEffectHandler: KindaSideEffectHandler + get() = buildSideEffectHandler { + whenSideEffect { + try { + val users = githubRepository.getUsers() + GithubEvent.GetUsersSucceed(users) + } catch (e: InternalErrorException) { + GithubEvent.ToastEvent("서버 문제가 발생했습니다.") + } catch (e: ForbiddenException) { + GithubEvent.ToastEvent("잠시 후 다시 시도해주세요.") + } } } - } -) \ No newline at end of file +} \ No newline at end of file diff --git a/kinda-android/src/main/java/dohun/kim/kinda/kinda_android/KindaViewModel.kt b/kinda-android/src/main/java/dohun/kim/kinda/kinda_android/KindaViewModel.kt index 1cba6bc..ad26b49 100644 --- a/kinda-android/src/main/java/dohun/kim/kinda/kinda_android/KindaViewModel.kt +++ b/kinda-android/src/main/java/dohun/kim/kinda/kinda_android/KindaViewModel.kt @@ -9,14 +9,15 @@ import dohun.kim.kinda.kinda_core.interceptor.Interceptor abstract class KindaViewModel( private val initialState: S, - reducer: KindaReducer, - sideEffectHandler: KindaSideEffectHandler, interceptors: Set> = emptySet() ) : ViewModel() { val stateClass: Class get() = initialState::class.java + abstract val reducer: KindaReducer + abstract val sideEffectHandler: KindaSideEffectHandler + private val kinda: Kinda = Kinda.Builder() .coroutineScope(viewModelScope) .initialState(initialState)