-
-
Save marciogranzotto/412c11947a311b65d75a8df5ad46b55d to your computer and use it in GitHub Desktop.
| interface LoginContracts { | |
| interface View { | |
| fun presentHomeScreen(user: User) | |
| fun showError(message: String) | |
| } | |
| interface Presenter { | |
| fun onDestroy() | |
| fun onLoginButtonPressed(username: String, password: String) | |
| } | |
| interface Interactor { | |
| fun unregister() | |
| fun login(username: String, password: String) | |
| } | |
| interface InteractorOutput { | |
| fun onLoginSuccess(user: User) | |
| fun onLoginError(message: String) | |
| } | |
| } | |
| class LoginActivity: BaseActivity, LoginContracts.View { | |
| var presenter: LoginContracts.Presenter? = null | |
| //other fields | |
| override fun onCreate() { | |
| //... | |
| presenter = LoginPresenter(this) | |
| loginButton.setOnClickListener { onLoginButtonClicked() } | |
| //... | |
| } | |
| override fun onDestroy() { | |
| presenter?.onDestroy() | |
| presenter = null | |
| super.onDestroy() | |
| } | |
| private fun onLoginButtonClicked() { | |
| presenter?.onLoginButtonClicked(usernameEditText.text, passwordEditText.text) | |
| } | |
| fun presentHomeScreen(user: User) { | |
| val intent = Intent(view, HomeActivity::class.java) | |
| intent.putExtra(Constants.IntentExtras.USER, user) | |
| startActivity(intent) | |
| } | |
| fun showError(message: String) { | |
| //shows the error on a dialog | |
| } | |
| } | |
| class LoginPresenter(var view: LoginContract.View?): LoginContract.Presenter, LoginContract.InteractorOutput { | |
| var interactor: LoginContract.Interactor? = LoginInteractor(this) | |
| fun onDestroy() { | |
| view = null | |
| interactor?.unregister() | |
| interactor = null | |
| } | |
| fun onLoginButtonPressed(username: String, password: String) { | |
| interactor?.login(username, password) | |
| } | |
| fun onLoginSuccess(user: User) { | |
| view?.presentNextScreen(user) | |
| } | |
| fun onLoginError(message: String) { | |
| view?.showError(message) | |
| } | |
| } | |
| class LoginInteractor(var output: LoginContract.InteractorOutput?): LoginContract.Interactor { | |
| fun unregister() { | |
| output = null | |
| } | |
| fun login(username: String, password: String) { | |
| LoginApiManager.login(username, password) | |
| ?.subscribeOn(Schedulers.newThread()) | |
| ?.observeOn(AndroidSchedulers.mainThread()) | |
| ?.subscribe({ user -> | |
| //does something with the user, like saving it or the token | |
| output?.onLoginSuccess(user) | |
| }, | |
| { throwable -> output?.onLoginError(throwable.message ?: "Error!") }) | |
| } | |
| } |
Nice example !!!
I want to ask a question. How will you show the login success in the activity? I mean when the login API call is successful, how do you notify the user about it? Will you create an interface and implement that in the LoginActivity?
@shubham171294
in Interactor class, on output?.onLoginSuccess(user) will execute. and this onLoginSuccess(user) is implemented in LoginPresenter class as fun onLoginSuccess(user: User) { view?.presentNextScreen(user) } and now here in LoginPresenter, view?.presentNextScreen(user) would be called and it's implemented in LoginActivity as fun presentHomeScreen(user: User) { val intent = Intent(view, HomeActivity::class.java) intent.putExtra(Constants.IntentExtras.USER, user) startActivity(intent) } and user will navigate from LoginActivity to HomeActivity so that's the way how callbacks would work...
Great example! Thank you very much for this. Just a little fix will be that the LoginPresenter has the function
fun onLoginButtonPressed(username: String, password: String)but is called in the LoginActivity aspresenter?.onLoginButtonClicked(usernameEditText.text, passwordEditText.text)Again, awesome example!