Advertisement
Guest User

Untitled

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