Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Interactor<State>(
- private val dispatcher: ObservableTransformer<Any, Any>,
- private val reducer: BiFunction<State, Any, State>,
- private val initialState: State,
- private val logger: Logger? = null
- ) : ObservableTransformer<Any, State> {
- override fun apply(upstream: Observable<Any>): Observable<State> =
- upstream
- .doOnNext { logger?.info(":↑: $it") }
- .compose(dispatcher)
- .doOnNext { logger?.debug(":↓: $it") }
- .scan(initialState, reducer)
- .distinctUntilChanged()
- .doOnNext { logger?.info(":↪: $it") }
- .doOnDispose { logger?.info(":x: disposed") }
- //.retryWhen(IOExceptionResolver()) //TODO
- }
- class LoginActionDispatcher(
- private val loadLatestSelectedUserIdentification: LoadLatestSelectedUserIdentification,
- private val observeLatestSelectedUserIdentification: ObserveLatestSelectedUserIdentification,
- private val selectUserIdentification: SelectUserIdentification,
- private val selectPassword: SelectPassword,
- private val executeAuthorizationWithBasic: ExecuteAuthorizationWithBasic,
- private val executeAuthorizationWithFacebook: ExecuteAuthorizationWithFacebook
- ) : ObservableTransformer<Any, Any> {
- override fun apply(upstream: Observable<Any>): ObservableSource<Any> =
- upstream.publish { shared ->
- Observable.merge(
- listOf(
- loadLatestSelectedUserIdentification(),
- observeLatestSelectedUserIdentification(),
- shared.ofType(SelectUserIdentificationAction::class.java)
- .compose(selectUserIdentification),
- shared.ofType(SelectPasswordAction::class.java)
- .compose(selectPassword),
- shared.ofType(ExecuteAuthorizationWithBasicAction::class.java)
- .compose(executeAuthorizationWithBasic),
- shared.ofType(ExecuteAuthorizationWithFacebookAction::class.java)
- .compose(executeAuthorizationWithFacebook)
- )
- )
- }
- }
- class LoginStateReducer(
- private val basicStateReducer: AuthorizationWithBasicStateReducer,
- private val facebookStateReducer: AuthorizationWithFacebookStateReducer
- ) : BiFunction<LoginState, Any, LoginState> {
- override fun apply(
- currentState: LoginState,
- event: Any
- ): LoginState =
- currentState.copy(
- basic = basicStateReducer.apply(currentState.basic, event),
- facebook = facebookStateReducer.apply(currentState.facebook, event)
- )
- }
- class AuthorizationWithBasicStateReducer : BiFunction<AuthorizationWithBasicState, Any, AuthorizationWithBasicState> {
- override fun apply(
- currentState: AuthorizationWithBasicState,
- event: Any
- ): AuthorizationWithBasicState =
- when (event) {
- is LoadLatestSelectedUserIdentificationEvent ->
- applyForLoadLatestSelectedUserIdentificationEvent(currentState, event)
- is ObserveLatestSelectedUserIdentificationEvent ->
- applyForLatestSelectedUserIdentificationEven(currentState, event)
- is SelectUserIdentificationEvent ->
- applyForSelectUserIdentificationEvent(currentState, event)
- is SelectPasswordEvent ->
- applyForValidatePassword(currentState, event)
- is ExecuteAuthorizationWithBasicEvent ->
- applyForExecuteLogin(currentState, event)
- else -> currentState
- }
- private fun applyForLoadLatestSelectedUserIdentificationEvent(
- currentState: AuthorizationWithBasicState,
- event: LoadLatestSelectedUserIdentificationEvent
- ): AuthorizationWithBasicState =
- when (event) {
- LoadLatestSelectedUserIdentificationEvent.InFlight -> currentState
- is LoadLatestSelectedUserIdentificationEvent.Success -> currentState.copy(
- selectedUserIdentification = event.userIdentification
- )
- }
- private fun applyForLatestSelectedUserIdentificationEven(
- currentState: AuthorizationWithBasicState,
- event: ObserveLatestSelectedUserIdentificationEvent
- ): AuthorizationWithBasicState =
- when (event) {
- is LatestSelectedUserIdentificationChanged -> currentState.copy(
- selectedUserIdentification = event.userIdentificationSelection.userIdentification
- )
- }
- private fun applyForExecuteLogin(
- currentState: AuthorizationWithBasicState,
- event: ExecuteAuthorizationWithBasicEvent
- ): AuthorizationWithBasicState =
- when (event) {
- ExecuteAuthorizationWithBasicEvent.Message.InFlight -> currentState.copy(
- loginInFlight = true,
- loginInFlightEvents = currentState.loginInFlightEvents + 1,
- loginFailureReasons = emptyList()
- )
- ExecuteAuthorizationWithBasicEvent.Result.Success -> currentState.copy(
- loginInFlight = false,
- loginSuccess = true,
- loginFailureReasons = emptyList()
- )
- is ExecuteAuthorizationWithBasicEvent.Result.Failed -> currentState.copy(
- loginInFlight = false,
- loginSuccess = false,
- loginFailureReasons = event.authorizationWithBasicFactoryFailureReasons,
- emailInvalidReasons = event.emailInValidReasons,
- phoneInvalidReasons = event.phoneInValidReasons,
- passwordInvalidReasons = event.passwordInValidReasons,
- userIdentificationInvalid = event.emailInValidReasons.isNotEmpty() && event.passwordInValidReasons.isNotEmpty(),
- passwordInvalid = event.passwordInValidReasons.isNotEmpty()
- )
- }
- private fun applyForSelectUserIdentificationEvent(
- currentState: AuthorizationWithBasicState,
- event: SelectUserIdentificationEvent
- ): AuthorizationWithBasicState =
- when (event) {
- is SelectUserIdentificationEvent.Message.InFlight -> currentState.copy(
- emailInvalidReasons = emptyList(),
- phoneInvalidReasons = emptyList()
- )
- SelectUserIdentificationEvent.Message.PreInvalid -> currentState.copy(
- userIdentificationInvalid = true
- )
- SelectUserIdentificationEvent.Result.Valid -> currentState.copy(
- emailInvalidReasons = emptyList(),
- phoneInvalidReasons = emptyList(),
- userIdentificationInvalid = false
- )
- is SelectUserIdentificationEvent.Result.Invalid -> currentState.copy(
- emailInvalidReasons = if (currentState.selectedUserIdentification.isEmpty()) {
- emptyList()
- } else {
- event.emailInvalidReasons
- },
- phoneInvalidReasons = if (currentState.selectedUserIdentification.isEmpty()) {
- emptyList()
- } else {
- event.phoneInvalidReasons
- }
- )
- }
- private fun applyForValidatePassword(
- currentState: AuthorizationWithBasicState,
- event: SelectPasswordEvent
- ): AuthorizationWithBasicState =
- when (event) {
- SelectPasswordEvent.Message.InFlight -> currentState.copy(
- passwordInvalidReasons = emptyList(),
- passwordValidationInFlight = true
- )
- SelectPasswordEvent.Message.PreInvalid -> currentState.copy(
- passwordInvalid = true
- )
- SelectPasswordEvent.Result.Valid -> currentState.copy(
- passwordInvalidReasons = emptyList(),
- passwordValidationInFlight = false,
- passwordInvalid = false
- )
- is SelectPasswordEvent.Result.Invalid -> currentState.copy(
- passwordInvalidReasons = event.reasons,
- passwordValidationInFlight = false
- )
- }
- }
Add Comment
Please, Sign In to add comment