Skip to content

Instantly share code, notes, and snippets.

@deniszink
Created September 11, 2017 13:07
Show Gist options
  • Save deniszink/5218f662e9f24e0d4b26ff154dad94e1 to your computer and use it in GitHub Desktop.
Save deniszink/5218f662e9f24e0d4b26ff154dad94e1 to your computer and use it in GitHub Desktop.
Example of Presenter class
package com.wellaapp.presentation.screens.authorization.presenter
import android.content.Context
import com.facebook.AccessToken
import com.hannesdorfmann.mosby.mvp.MvpBasePresenter
import com.wellaapp.data.entities.User
import com.wellaapp.data.error_handler.RetrofitException
import com.wellaapp.data.model.LoginResponseModel
import com.wellaapp.domain.action.sessions.AnonymousLogin
import com.wellaapp.domain.action.sessions.AutoLogin
import com.wellaapp.domain.action.sessions.Login
import com.wellaapp.domain.action.sessions.LoginFacebook
import com.wellaapp.presentation.base.BaseDisposableObserver
import com.wellaapp.presentation.extensions.regExpEmail
import com.wellaapp.presentation.extensions.regExpPassword
import com.wellaapp.presentation.quickblox.ChatHelper
import com.wellaapp.presentation.quickblox.holders.UsersHolder
import io.reactivex.Observable
import javax.inject.Inject
/**
* @author Denis_Zinkovskiy at 1/16/17.
*/
class SignInPresenter @Inject constructor(private var action: Login,
private var facebookLogin: LoginFacebook,
private val autoLogin: AutoLogin,
private val anonymousLogin: AnonymousLogin) :
AuthorizationMVP.SignInPresenter, MvpBasePresenter<AuthorizationMVP.View>() {
override fun <T> doOperation(context: Context, actionType: AuthorizationPresenterAdapter.OperationType, model: T) = when (actionType) {
AuthorizationPresenterAdapter.OperationType.SIGN_IN -> doSignIn(context, model as User)
AuthorizationPresenterAdapter.OperationType.ANONYMOUS -> doAnonymousLogin(context)
else -> doSignFacebook(context, model as AccessToken)
}
/**
* Fires signIn request described in sessions_actions.kt
*/
fun doSignIn(context: Context, model: User) {
val isPassValid = model.password.regExpPassword()
val isEmailValid = model.email!!.regExpEmail()
var observable: Observable<LoginResponseModel>? = null
if (isPassValid && isEmailValid) {
view?.validationEvent(isEmailValid, isPassValid)
view?.showProgress(true)
observable = action.fireAction(context, null, null, model)
.doOnComplete { view?.onSuccess() }
.doOnError {
when (it) { //checkPermissionAndCall is it our RetrofitException class with parsed error message,
// if so, show message to the user, otherwise show the simple message of Throwable
//pass observable ot onError to retry the action
is RetrofitException -> view?.onError(it.description, observable)
else -> view?.onError(it.message!!, observable)
}
}
observable.subscribe(BaseDisposableObserver<LoginResponseModel>())
} else {
view?.validationEvent(isEmailValid, isPassValid)
}
}
/**
* Fires signIn via Facebook request described in sessions_actions.kt
*/
fun doSignFacebook(context: Context, token: AccessToken) {
view?.showProgress(true)
var observable: Observable<LoginResponseModel>? = null
observable = facebookLogin.fireAction(context, null, null, token.token)
.doOnComplete { view?.onSuccess() }
.doOnError {
when (it) { //checkPermissionAndCall is it our RetrofitException class with parsed error message,
// if so, show message to the user, otherwise show the simple message of Throwable
//pass observable ot onError to retry the action
is RetrofitException -> view?.onError(it.description, observable)
else -> view?.onError(it.message!!, observable)
}
}
observable.subscribe(BaseDisposableObserver<LoginResponseModel>())
}
fun doAnonymousLogin(context: Context) {
var observable: Observable<LoginResponseModel>? = null
view?.showProgress(true)
observable = anonymousLogin.fireAction(context, null, null, null)
.doOnComplete { view?.showProgress(false); view?.anonymousLogin() }
.doOnNext { UsersHolder.putUser(it.user) }
.doOnError {
when (it) { //checkPermissionAndCall is it our RetrofitException class with parsed error message,
// if so, show message to the user, otherwise show the simple message of Throwable
//pass observable ot onError to retry the action
is RetrofitException -> view?.onError(it.description, observable)
else -> view?.onError(it.message!!, observable)
}
}
observable.subscribe(BaseDisposableObserver<LoginResponseModel>())
}
override fun checkAutoLogin(context: Context) {
autoLogin
.fireAction(context, null, null, null)
.doOnNext { if (it) view?.doAutoLogin() }
.subscribe(BaseDisposableObserver())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment