Skip to content

Commit

Permalink
reducer, sideEffectHandler를 생성자에서 추상 프로퍼티로 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
kimdohun0104 committed Dec 20, 2020
1 parent 26ebac3 commit f47379e
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 57 deletions.
12 changes: 6 additions & 6 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -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<CountState, CountEvent, CountSideEffect>(
initialState = CountState(),
reducer = CountReducer(),
sideEffectHandler = CountSideEffectHandler()
)
initialState = CountState()
) {
override val reducer: KindaReducer<CountState, CountEvent, CountSideEffect>
get() = CountReducer()

override val sideEffectHandler: KindaSideEffectHandler<CountState, CountEvent, CountSideEffect>
get() = CountSideEffectHandler()
}
Original file line number Diff line number Diff line change
@@ -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<LoginState, LoginEvent, LoginSideEffect>(
initialState = LoginState(),
reducer = LoginReducer(),
sideEffectHandler = LoginSideEffectHandler()
initialState = LoginState()
) {
override val reducer: KindaReducer<LoginState, LoginEvent, LoginSideEffect>
get() = LoginReducer()
override val sideEffectHandler: KindaSideEffectHandler<LoginState, LoginEvent, LoginSideEffect>
get() = LoginSideEffectHandler()
}
Original file line number Diff line number Diff line change
@@ -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<CountState, CountEvent, CountSideEffect>(
initialState = CountState(),
reducer = buildReducer {
whenEvent<CountEvent.AttemptMagic> {
dispatch(CountSideEffect.Magic)
}
initialState = CountState()
) {

whenEvent<CountEvent.Decrease> {
next(copy(count = count - 1))
}
override val reducer: KindaReducer<CountState, CountEvent, CountSideEffect>
get() = buildReducer {
whenEvent<CountEvent.AttemptMagic> {
dispatch(CountSideEffect.Magic)
}

whenEvent<CountEvent.Increase> {
next(copy(count = count + 1))
}
whenEvent<CountEvent.Decrease> {
next(copy(count = count - 1))
}

whenEvent<CountEvent.Increase1000> {
next(copy(count = count + 1000))
}
},
sideEffectHandler = buildSideEffectHandler {
whenSideEffect<CountSideEffect.Magic> {
delay(1000)
CountEvent.Increase1000
whenEvent<CountEvent.Increase> {
next(copy(count = count + 1))
}

whenEvent<CountEvent.Increase1000> {
next(copy(count = count + 1000))
}
}
}
)

override val sideEffectHandler: KindaSideEffectHandler<CountState, CountEvent, CountSideEffect>
get() = buildSideEffectHandler {
whenSideEffect<CountSideEffect.Magic> {
delay(1000)
CountEvent.Increase1000
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,42 @@ 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

class GithubViewModel @ViewModelInject constructor(
private val githubRepository: GithubRepository
) : KindaViewModel<GithubState, GithubEvent, GithubSideEffect>(
initialState = GithubState(),
reducer = buildReducer {
whenEvent<GithubEvent.AttemptGetUsers> {
next(copy(isLoading = true), GithubSideEffect.GetUsers)
}
) {
override val reducer: KindaReducer<GithubState, GithubEvent, GithubSideEffect>
get() = buildReducer {
whenEvent<GithubEvent.AttemptGetUsers> {
next(copy(isLoading = true), GithubSideEffect.GetUsers)
}

whenEvent<GithubEvent.GetUsersSucceed> {
next(copy(users = it.users, isLoading = false))
}
whenEvent<GithubEvent.GetUsersSucceed> {
next(copy(users = it.users, isLoading = false))
}

whenEvent<GithubEvent.ToastEvent> {
next(copy(toastEvent = Event(it.message), isLoading = false))
whenEvent<GithubEvent.ToastEvent> {
next(copy(toastEvent = Event(it.message), isLoading = false))
}
}
},
sideEffectHandler = buildSideEffectHandler {
whenSideEffect<GithubSideEffect> {
try {
val users = githubRepository.getUsers()
GithubEvent.GetUsersSucceed(users)
} catch (e: InternalErrorException) {
GithubEvent.ToastEvent("서버 문제가 발생했습니다.")
} catch (e: ForbiddenException) {
GithubEvent.ToastEvent("잠시 후 다시 시도해주세요.")

override val sideEffectHandler: KindaSideEffectHandler<GithubState, GithubEvent, GithubSideEffect>
get() = buildSideEffectHandler {
whenSideEffect<GithubSideEffect> {
try {
val users = githubRepository.getUsers()
GithubEvent.GetUsersSucceed(users)
} catch (e: InternalErrorException) {
GithubEvent.ToastEvent("서버 문제가 발생했습니다.")
} catch (e: ForbiddenException) {
GithubEvent.ToastEvent("잠시 후 다시 시도해주세요.")
}
}
}
}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import dohun.kim.kinda.kinda_core.interceptor.Interceptor

abstract class KindaViewModel<S : KindaState, E : KindaEvent, SE : KindaSideEffect>(
private val initialState: S,
reducer: KindaReducer<S, E, SE>,
sideEffectHandler: KindaSideEffectHandler<S, E, SE>,
interceptors: Set<Interceptor<S, E, SE>> = emptySet()
) : ViewModel() {

val stateClass: Class<out S>
get() = initialState::class.java

abstract val reducer: KindaReducer<S, E, SE>
abstract val sideEffectHandler: KindaSideEffectHandler<S, E, SE>

private val kinda: Kinda<S, E, SE> = Kinda.Builder<S, E, SE>()
.coroutineScope(viewModelScope)
.initialState(initialState)
Expand Down

0 comments on commit f47379e

Please sign in to comment.