SHARE
TWEET

Untitled

a guest Apr 2nd, 2019 102 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //viewmodel
  2.  
  3.  fun handleLogin() {
  4.         hideKeyBoard.setValue(true)
  5. //        skip fields syntax validation
  6. //        var hasSyntaxErrors = false
  7. //        validationList.asSequence().forEach {
  8. //            hasSyntaxErrors = hasSyntaxErrors or it.syntaxError()
  9. //        }
  10. //        if (!hasSyntaxErrors)
  11.         rx.completable(
  12.                 singInUseCase.onSingIn(
  13.                         emailField.input.get().orEmpty(),
  14.                         passwordField.input.get().orEmpty()
  15.                 )
  16.                         .subscribeOn(schedulerProvider.io())
  17.                         .observeOn(schedulerProvider.main())
  18.                         .compose(applyProgressCompletable()),
  19.                 onComplete = { routing.getRouter().newRootScreen(Route.AppScheme.AppEntranceRoute) })
  20.     }
  21.  
  22.  
  23. //usecase
  24. interface SignInUseCase {
  25.  
  26.     fun onSingIn(email: String, password: String): Completable
  27. }
  28.  
  29. class SignInUseCaseImpl @Inject constructor(
  30.         private val appConfigProvider: AppConfigProvider,
  31.         private val authRepository: AuthRepository,
  32.         private val userRepository: UserRepository,
  33.         private val createSessionUseCase: CreateSessionUseCase,
  34.         private val settingsProvider: SettingsProvider
  35. ) : SignInUseCase {
  36.  
  37.     override fun onSingIn(email: String, password: String): Completable =
  38.             authRepository.signIn(
  39.                     email = email,
  40.                     password = password,
  41.                     clientId = appConfigProvider.clientId(),
  42.                     clientSecret = appConfigProvider.clientSecret())
  43.                     .doOnSuccess { settingsProvider.tokenInfo = it }
  44.                     .flatMap { userRepository.onGetUserInfo() }
  45.                     .flatMapCompletable(createSessionUseCase::initSession)
  46.  
  47. }
  48.  
  49. interface CreateSessionUseCase {
  50.     fun initSession(userModel: UserModel): Completable
  51. }
  52.  
  53. class CreateSessionUseCaseImpl @Inject constructor(
  54.         private val settingsProvider: SettingsProvider
  55. ) : CreateSessionUseCase {
  56.  
  57.     override fun initSession(userModel: UserModel): Completable =
  58.             Completable.fromAction { settingsProvider.userModel = userModel }
  59.                     .andThen(BaseApp.session.createSession())
  60.  
  61. }
  62.  
  63. //repo
  64. interface AuthRepository {
  65.  
  66.     fun signIn(email: String, password: String, clientId: String, clientSecret: String): Single<AccessToken>
  67.  
  68.     fun signUp(userPojo: User, clientId: String, clientSecret: String): Single<AccessToken>
  69.  
  70.     fun resetPassword(clientId: String, clientSecret: String, resetPassword: ResetPassword): Completable
  71.  
  72.     fun newPassword(clientId: String, clientSecret: String, password: NewPassword): Completable
  73. }
  74.  
  75. class AuthRepositoryImpl @Inject constructor(
  76.         private val authApi: AuthApi
  77. ) : AuthRepository {
  78.  
  79.     override fun signIn(email: String, password: String, clientId: String, clientSecret: String): Single<AccessToken> =
  80.             authApi.singIn(email, password, clientId, clientSecret)
  81.  
  82.     override fun signUp(userPojo: User, clientId: String, clientSecret: String): Single<AccessToken> =
  83.             authApi.singUp(userPojo, clientId, clientSecret)
  84.  
  85.     override fun resetPassword(clientId: String, clientSecret: String, resetPassword: ResetPassword): Completable =
  86.             authApi.resetPassword(clientId, clientSecret, resetPassword)
  87.  
  88.     override fun newPassword(clientId: String, clientSecret: String, password: NewPassword): Completable =
  89.             authApi.newPassword(clientId, clientSecret, password)
  90.  
  91. }
  92.  
  93. //api
  94. interface AuthApi {
  95.  
  96.     /* auth */
  97.  
  98.     @POST("/$PREFIX/v$VERSION/$USERS")
  99.     fun singUp(@Body user: User,
  100.                @Header("clientId") clientId: String,
  101.                @Header("clientSecret") clientSecret: String): Single<AccessToken>
  102.  
  103.     @POST("/$PREFIX/v$VERSION/$USERS/$TOKEN")
  104.     fun singIn(@Header("username") email: String,
  105.                @Header("password") password: String,
  106.                @Header("clientId") clientId: String,
  107.                @Header("clientSecret") clientSecret: String): Single<AccessToken>
  108. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top