Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. class LoginContracts {
  2. interface View {
  3. fun goToHomeScreen(user: User)
  4. fun showError(message: String)
  5. }
  6.  
  7. interface Presenter {
  8. fun onDestroy()
  9. fun onLoginButtonPressed(username: String, password: String)
  10. }
  11.  
  12. interface Interactor {
  13. fun login(username: String, password: String)
  14. }
  15.  
  16. interface InteractorOutput {
  17. fun onLoginSuccess(user: User)
  18. fun onLoginError(message: String)
  19. }
  20. }
  21.  
  22. class LoginActivity: LoginContracts.View{
  23.  
  24. var presenter: LoginContracts.Presenter? = LoginPresenter(this)
  25.  
  26. //other fields
  27.  
  28. override fun onCreate() {
  29. //...
  30. loginButton.setOnClickListener { onLoginButtonClicked() }
  31. //...
  32. }
  33.  
  34. override fun onDestroy() {
  35. presenter?.onDestroy()
  36. presenter = null
  37. super.onDestroy()
  38. }
  39.  
  40. private fun onLoginButtonClicked() {
  41. presenter?.onLoginButtonClicked(usernameEditText.text, passwordEditText.text)
  42. }
  43.  
  44. fun goToHomeScreen(user: User) {
  45. val intent = Intent(view, HomeActivity::class.java)
  46. intent.putExtra(Constants.IntentExtras.USER, user)
  47. startActivity(intent)
  48. }
  49.  
  50. fun showError(message: String) {
  51. //shows the error on a dialog
  52. }
  53. }
  54.  
  55. class LoginPresenter(var view: LoginContract.View?): LoginContract.Presenter, LoginContract.InteractorOutput {
  56. var interactor: LoginContract.Interactor? = LoginInteractor(this)
  57.  
  58. fun onDestroy() {
  59. view = null
  60. interactor = null
  61. }
  62.  
  63. fun onLoginButtonPressed(username: String, password: String) {
  64. interactor?.login(username, password)
  65. }
  66.  
  67. fun onLoginSuccess(user: User) {
  68. view?.goToNextScreen(user)
  69. }
  70.  
  71. fun onLoginError(message: String) {
  72. view?.showError(message)
  73. }
  74. }
  75.  
  76. class LoginInteractor(var output: LoginContract.InteractorOutput?): LoginContract.Interactor {
  77. fun login(username: String, password: String) {
  78. LoginApiManager.login(username, password)
  79. ?.subscribeOn(Schedulers.newThread())
  80. ?.observeOn(AndroidSchedulers.mainThread())
  81. ?.subscribe({
  82. //does something with the user, like saving it or the token
  83. output?.onLoginSuccess(it)
  84. },
  85. { output?.onLoginError(it.message ?: "Error!") })
  86. }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement