Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. interface LoginContracts {
  2. interface View {
  3. fun showError(message: String)
  4. }
  5.  
  6. interface Presenter {
  7. fun onDestroy()
  8. fun onLoginButtonPressed(username: String, password: String)
  9. }
  10.  
  11. interface Interactor {
  12. fun unregister()
  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. interface Router {
  22. fun unregister()
  23. fun presentHomeScreen(user: User)
  24. }
  25. }
  26.  
  27. class LoginActivity: BaseActivity, LoginContracts.View {
  28.  
  29. var presenter: LoginContracts.Presenter? = null
  30.  
  31. //other fields
  32.  
  33. override fun onCreate() {
  34. //...
  35. presenter = LoginPresenter(this)
  36. loginButton.setOnClickListener { onLoginButtonClicked() }
  37. //...
  38. }
  39.  
  40. override fun onDestroy() {
  41. presenter?.onDestroy()
  42. presenter = null
  43. super.onDestroy()
  44. }
  45.  
  46. private fun onLoginButtonClicked() {
  47. presenter?.onLoginButtonClicked(usernameEditText.text, passwordEditText.text)
  48. }
  49.  
  50. fun showError(message: String) {
  51. //shows the error on a dialog
  52. }
  53. }
  54.  
  55. class LoginPresenter(var view: LoginContracts.View?): LoginContracts.Presenter, LoginContracts.InteractorOutput {
  56. var interactor: LoginContracts.Interactor? = LoginInteractor(this)
  57. var router: LoginContracts.Router? = LoginRouter(view as? Activity)
  58.  
  59. fun onDestroy() {
  60. view = null
  61. interactor?.unregister()
  62. interactor = null
  63. router?.unregister()
  64. router = null
  65. }
  66.  
  67. fun onLoginButtonPressed(username: String, password: String) {
  68. interactor?.login(username, password)
  69. }
  70.  
  71. fun onLoginSuccess(user: User) {
  72. router?.goToNextScreen(user)
  73. }
  74.  
  75. fun onLoginError(message: String) {
  76. view?.showError(message)
  77. }
  78. }
  79.  
  80. class LoginInteractor(var output: LoginContracts.InteractorOutput?): LoginContracts.Interactor {
  81.  
  82. fun unregister() {
  83. output = null
  84. }
  85.  
  86. fun login(username: String, password: String) {
  87. LoginApiManager.login(username, password)
  88. ?.subscribeOn(Schedulers.io())
  89. ?.observeOn(AndroidSchedulers.mainThread())
  90. ?.subscribe({
  91. //does something with the user, like saving it or the token
  92. output?.onLoginSuccess(it)
  93. },
  94. { output?.onLoginError(it.message ?: "Error!") })
  95. }
  96. }
  97.  
  98. class LoginRouter(var activity: Activity?): LoginContracts.Router {
  99.  
  100. fun unregister() {
  101. activity = null
  102. }
  103.  
  104. fun presentHomeScreen(user: User) {
  105. val intent = Intent(view, HomeActivity::class.java)
  106. intent.putExtra(Constants.IntentExtras.USER, user)
  107. activity?.startActivity(intent)
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement