Guest User

Untitled

a guest
Jan 24th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.21 KB | None | 0 0
  1. class Interactor<State>(
  2. private val dispatcher: ObservableTransformer<Any, Any>,
  3. private val reducer: BiFunction<State, Any, State>,
  4. private val initialState: State,
  5. private val logger: Logger? = null
  6. ) : ObservableTransformer<Any, State> {
  7.  
  8. override fun apply(upstream: Observable<Any>): Observable<State> =
  9. upstream
  10. .doOnNext { logger?.info(":↑: $it") }
  11. .compose(dispatcher)
  12. .doOnNext { logger?.debug(":↓: $it") }
  13. .scan(initialState, reducer)
  14. .distinctUntilChanged()
  15. .doOnNext { logger?.info(":↪: $it") }
  16. .doOnDispose { logger?.info(":x: disposed") }
  17. //.retryWhen(IOExceptionResolver()) //TODO
  18.  
  19. }
  20.  
  21. class LoginActionDispatcher(
  22. private val loadLatestSelectedUserIdentification: LoadLatestSelectedUserIdentification,
  23. private val observeLatestSelectedUserIdentification: ObserveLatestSelectedUserIdentification,
  24. private val selectUserIdentification: SelectUserIdentification,
  25. private val selectPassword: SelectPassword,
  26. private val executeAuthorizationWithBasic: ExecuteAuthorizationWithBasic,
  27. private val executeAuthorizationWithFacebook: ExecuteAuthorizationWithFacebook
  28. ) : ObservableTransformer<Any, Any> {
  29.  
  30. override fun apply(upstream: Observable<Any>): ObservableSource<Any> =
  31. upstream.publish { shared ->
  32. Observable.merge(
  33. listOf(
  34. loadLatestSelectedUserIdentification(),
  35. observeLatestSelectedUserIdentification(),
  36. shared.ofType(SelectUserIdentificationAction::class.java)
  37. .compose(selectUserIdentification),
  38. shared.ofType(SelectPasswordAction::class.java)
  39. .compose(selectPassword),
  40. shared.ofType(ExecuteAuthorizationWithBasicAction::class.java)
  41. .compose(executeAuthorizationWithBasic),
  42. shared.ofType(ExecuteAuthorizationWithFacebookAction::class.java)
  43. .compose(executeAuthorizationWithFacebook)
  44. )
  45. )
  46. }
  47.  
  48. }
  49.  
  50. class LoginStateReducer(
  51. private val basicStateReducer: AuthorizationWithBasicStateReducer,
  52. private val facebookStateReducer: AuthorizationWithFacebookStateReducer
  53. ) : BiFunction<LoginState, Any, LoginState> {
  54.  
  55. override fun apply(
  56. currentState: LoginState,
  57. event: Any
  58. ): LoginState =
  59. currentState.copy(
  60. basic = basicStateReducer.apply(currentState.basic, event),
  61. facebook = facebookStateReducer.apply(currentState.facebook, event)
  62. )
  63.  
  64. }
  65.  
  66. class AuthorizationWithBasicStateReducer : BiFunction<AuthorizationWithBasicState, Any, AuthorizationWithBasicState> {
  67.  
  68. override fun apply(
  69. currentState: AuthorizationWithBasicState,
  70. event: Any
  71. ): AuthorizationWithBasicState =
  72. when (event) {
  73. is LoadLatestSelectedUserIdentificationEvent ->
  74. applyForLoadLatestSelectedUserIdentificationEvent(currentState, event)
  75. is ObserveLatestSelectedUserIdentificationEvent ->
  76. applyForLatestSelectedUserIdentificationEven(currentState, event)
  77. is SelectUserIdentificationEvent ->
  78. applyForSelectUserIdentificationEvent(currentState, event)
  79. is SelectPasswordEvent ->
  80. applyForValidatePassword(currentState, event)
  81. is ExecuteAuthorizationWithBasicEvent ->
  82. applyForExecuteLogin(currentState, event)
  83. else -> currentState
  84. }
  85.  
  86. private fun applyForLoadLatestSelectedUserIdentificationEvent(
  87. currentState: AuthorizationWithBasicState,
  88. event: LoadLatestSelectedUserIdentificationEvent
  89. ): AuthorizationWithBasicState =
  90. when (event) {
  91. LoadLatestSelectedUserIdentificationEvent.InFlight -> currentState
  92. is LoadLatestSelectedUserIdentificationEvent.Success -> currentState.copy(
  93. selectedUserIdentification = event.userIdentification
  94. )
  95. }
  96.  
  97. private fun applyForLatestSelectedUserIdentificationEven(
  98. currentState: AuthorizationWithBasicState,
  99. event: ObserveLatestSelectedUserIdentificationEvent
  100. ): AuthorizationWithBasicState =
  101. when (event) {
  102. is LatestSelectedUserIdentificationChanged -> currentState.copy(
  103. selectedUserIdentification = event.userIdentificationSelection.userIdentification
  104. )
  105. }
  106.  
  107. private fun applyForExecuteLogin(
  108. currentState: AuthorizationWithBasicState,
  109. event: ExecuteAuthorizationWithBasicEvent
  110. ): AuthorizationWithBasicState =
  111. when (event) {
  112. ExecuteAuthorizationWithBasicEvent.Message.InFlight -> currentState.copy(
  113. loginInFlight = true,
  114. loginInFlightEvents = currentState.loginInFlightEvents + 1,
  115. loginFailureReasons = emptyList()
  116. )
  117. ExecuteAuthorizationWithBasicEvent.Result.Success -> currentState.copy(
  118. loginInFlight = false,
  119. loginSuccess = true,
  120. loginFailureReasons = emptyList()
  121. )
  122. is ExecuteAuthorizationWithBasicEvent.Result.Failed -> currentState.copy(
  123. loginInFlight = false,
  124. loginSuccess = false,
  125. loginFailureReasons = event.authorizationWithBasicFactoryFailureReasons,
  126. emailInvalidReasons = event.emailInValidReasons,
  127. phoneInvalidReasons = event.phoneInValidReasons,
  128. passwordInvalidReasons = event.passwordInValidReasons,
  129. userIdentificationInvalid = event.emailInValidReasons.isNotEmpty() && event.passwordInValidReasons.isNotEmpty(),
  130. passwordInvalid = event.passwordInValidReasons.isNotEmpty()
  131. )
  132. }
  133.  
  134. private fun applyForSelectUserIdentificationEvent(
  135. currentState: AuthorizationWithBasicState,
  136. event: SelectUserIdentificationEvent
  137. ): AuthorizationWithBasicState =
  138. when (event) {
  139. is SelectUserIdentificationEvent.Message.InFlight -> currentState.copy(
  140. emailInvalidReasons = emptyList(),
  141. phoneInvalidReasons = emptyList()
  142. )
  143. SelectUserIdentificationEvent.Message.PreInvalid -> currentState.copy(
  144. userIdentificationInvalid = true
  145. )
  146. SelectUserIdentificationEvent.Result.Valid -> currentState.copy(
  147. emailInvalidReasons = emptyList(),
  148. phoneInvalidReasons = emptyList(),
  149. userIdentificationInvalid = false
  150. )
  151. is SelectUserIdentificationEvent.Result.Invalid -> currentState.copy(
  152. emailInvalidReasons = if (currentState.selectedUserIdentification.isEmpty()) {
  153. emptyList()
  154. } else {
  155. event.emailInvalidReasons
  156. },
  157. phoneInvalidReasons = if (currentState.selectedUserIdentification.isEmpty()) {
  158. emptyList()
  159. } else {
  160. event.phoneInvalidReasons
  161. }
  162. )
  163. }
  164.  
  165. private fun applyForValidatePassword(
  166. currentState: AuthorizationWithBasicState,
  167. event: SelectPasswordEvent
  168. ): AuthorizationWithBasicState =
  169. when (event) {
  170. SelectPasswordEvent.Message.InFlight -> currentState.copy(
  171. passwordInvalidReasons = emptyList(),
  172. passwordValidationInFlight = true
  173. )
  174. SelectPasswordEvent.Message.PreInvalid -> currentState.copy(
  175. passwordInvalid = true
  176. )
  177. SelectPasswordEvent.Result.Valid -> currentState.copy(
  178. passwordInvalidReasons = emptyList(),
  179. passwordValidationInFlight = false,
  180. passwordInvalid = false
  181. )
  182. is SelectPasswordEvent.Result.Invalid -> currentState.copy(
  183. passwordInvalidReasons = event.reasons,
  184. passwordValidationInFlight = false
  185. )
  186. }
  187.  
  188. }
Add Comment
Please, Sign In to add comment