Guest User

Untitled

a guest
Jul 16th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. class LoginActivity: AppCompatActivity(), LoginContract.View {
  2.  
  3. var mPresenter: LoginContract.Presenter? = LoginPresenter(this)
  4.  
  5. override fun onCreate() {
  6. loginBtn.setOnClickListener { onLoginButtonClicked() }
  7. }
  8.  
  9. override fun onLoginButtonClicked() {
  10. mPresenter?.onLoginBtnPressed(etUsername.text, etPassword.text)
  11. }
  12.  
  13. // Move to Router
  14. // override fun goToOtpPage(userId: String) {
  15. // val intent = Intent(view, OtpActivity::class.java)
  16. // intent.putExtra(Constants.IntentExtras.USERID, userId)
  17. // startActivity(intent)
  18. // }
  19.  
  20. override fun showError(message: String) {
  21. toast("Error: {$message}")
  22. }
  23. }
  24.  
  25. class LoginPresenter(var mView: LoginContract.View?): LoginContract.Presenter, LoginContract.InteractorOutput {
  26. // Initialized Router to the Presenter and passes view as activity
  27. var mRouter: LoginContract.Router? = LoginRouter(mView as? Activity)
  28.  
  29. var mInteractor: LoginContract.Interactor? = LoginInteractor(this)
  30.  
  31. override fun onLoginBtnPressed(username: String, password: String) {
  32. mInteractor?.login(username, password)
  33. }
  34.  
  35. override fun onLoginSuccess(userid: String) {
  36. mRouter?.goToOtpPage(user)
  37. }
  38.  
  39. override fun onLoginError(message: String) {
  40. mView?.showError(message)
  41. }
  42. }
  43.  
  44. class LoginInteractor(var mOutput: LoginContract.InteractorOutput?): LoginContract.Interactor {
  45. override fun login(username: String, password: String) {
  46. mRepository.login(username, password)
  47. ?.subscribeOn(Schedulers.io())
  48. ?.observeOn(AndroidSchedulers.mainThread())
  49. ?.subscribe({
  50. //do something here, like save it to db
  51. mOutput?.onLoginSuccess(it)
  52. },{
  53. mOutput?.onLoginError(it.message ?: "Error!")
  54. })
  55. }
  56. }
  57.  
  58. // Separated from the view
  59. class LoginRouter(var mActivity: Activity?): LoginContract.Router {
  60. override fun goToOtpPage(userid: String) {
  61. val intent = Intent(view, OtpActivity::class.java)
  62. intent.putExtra(Constants.IntentExtras.USERID, userid)
  63. mActivity?.startActivity(intent)
  64. }
  65. }
Add Comment
Please, Sign In to add comment