Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //viewmodel
- fun handleLogin() {
- hideKeyBoard.setValue(true)
- // skip fields syntax validation
- // var hasSyntaxErrors = false
- // validationList.asSequence().forEach {
- // hasSyntaxErrors = hasSyntaxErrors or it.syntaxError()
- // }
- // if (!hasSyntaxErrors)
- rx.completable(
- singInUseCase.onSingIn(
- emailField.input.get().orEmpty(),
- passwordField.input.get().orEmpty()
- )
- .subscribeOn(schedulerProvider.io())
- .observeOn(schedulerProvider.main())
- .compose(applyProgressCompletable()),
- onComplete = { routing.getRouter().newRootScreen(Route.AppScheme.AppEntranceRoute) })
- }
- //usecase
- interface SignInUseCase {
- fun onSingIn(email: String, password: String): Completable
- }
- class SignInUseCaseImpl @Inject constructor(
- private val appConfigProvider: AppConfigProvider,
- private val authRepository: AuthRepository,
- private val userRepository: UserRepository,
- private val createSessionUseCase: CreateSessionUseCase,
- private val settingsProvider: SettingsProvider
- ) : SignInUseCase {
- override fun onSingIn(email: String, password: String): Completable =
- authRepository.signIn(
- email = email,
- password = password,
- clientId = appConfigProvider.clientId(),
- clientSecret = appConfigProvider.clientSecret())
- .doOnSuccess { settingsProvider.tokenInfo = it }
- .flatMap { userRepository.onGetUserInfo() }
- .flatMapCompletable(createSessionUseCase::initSession)
- }
- interface CreateSessionUseCase {
- fun initSession(userModel: UserModel): Completable
- }
- class CreateSessionUseCaseImpl @Inject constructor(
- private val settingsProvider: SettingsProvider
- ) : CreateSessionUseCase {
- override fun initSession(userModel: UserModel): Completable =
- Completable.fromAction { settingsProvider.userModel = userModel }
- .andThen(BaseApp.session.createSession())
- }
- //repo
- interface AuthRepository {
- fun signIn(email: String, password: String, clientId: String, clientSecret: String): Single<AccessToken>
- fun signUp(userPojo: User, clientId: String, clientSecret: String): Single<AccessToken>
- fun resetPassword(clientId: String, clientSecret: String, resetPassword: ResetPassword): Completable
- fun newPassword(clientId: String, clientSecret: String, password: NewPassword): Completable
- }
- class AuthRepositoryImpl @Inject constructor(
- private val authApi: AuthApi
- ) : AuthRepository {
- override fun signIn(email: String, password: String, clientId: String, clientSecret: String): Single<AccessToken> =
- authApi.singIn(email, password, clientId, clientSecret)
- override fun signUp(userPojo: User, clientId: String, clientSecret: String): Single<AccessToken> =
- authApi.singUp(userPojo, clientId, clientSecret)
- override fun resetPassword(clientId: String, clientSecret: String, resetPassword: ResetPassword): Completable =
- authApi.resetPassword(clientId, clientSecret, resetPassword)
- override fun newPassword(clientId: String, clientSecret: String, password: NewPassword): Completable =
- authApi.newPassword(clientId, clientSecret, password)
- }
- //api
- interface AuthApi {
- /* auth */
- @POST("/$PREFIX/v$VERSION/$USERS")
- fun singUp(@Body user: User,
- @Header("clientId") clientId: String,
- @Header("clientSecret") clientSecret: String): Single<AccessToken>
- @POST("/$PREFIX/v$VERSION/$USERS/$TOKEN")
- fun singIn(@Header("username") email: String,
- @Header("password") password: String,
- @Header("clientId") clientId: String,
- @Header("clientSecret") clientSecret: String): Single<AccessToken>
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement