Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
This is an example of Android development with MVP + Interactor in Kotlin
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 = null
private fun onLoginButtonClicked() {
presenter?.onLoginButtonClicked(usernameEditText.text, passwordEditText.text)
fun presentHomeScreen(user: User) {
val intent = Intent(view,
intent.putExtra(Constants.IntentExtras.USER, user)
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 = null
fun onLoginButtonPressed(username: String, password: String) {
interactor?.login(username, password)
fun onLoginSuccess(user: User) {
fun onLoginError(message: String) {
class LoginInteractor(var output: LoginContract.InteractorOutput?): LoginContract.Interactor {
fun unregister() {
output = null
fun login(username: String, password: String) {
LoginApiManager.login(username, password)
?.subscribe({ user ->
//does something with the user, like saving it or the token
{ throwable -> output?.onLoginError(throwable.message ?: "Error!") })

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 as presenter?.onLoginButtonClicked(usernameEditText.text, passwordEditText.text)
Again, awesome example!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment