├── app ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── values │ │ │ │ ├── dimens.xml │ │ │ │ ├── colors.xml │ │ │ │ ├── styles.xml │ │ │ │ └── strings.xml │ │ │ ├── drawable-hdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── drawable-mdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── drawable-xhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── drawable-xxhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── values-w820dp │ │ │ │ └── dimens.xml │ │ │ ├── layout │ │ │ │ ├── view_main_item.xml │ │ │ │ ├── activity_main.xml │ │ │ │ └── activity_login.xml │ │ │ ├── menu │ │ │ │ ├── main.xml │ │ │ │ └── login.xml │ │ │ └── drawable │ │ │ │ ├── ic_username.xml │ │ │ │ └── ic_password.xml │ │ ├── AndroidManifest.xml │ │ └── java │ │ │ └── com │ │ │ └── antonioleiva │ │ │ └── mvpexample │ │ │ └── app │ │ │ ├── login │ │ │ ├── LoginInteractor.java │ │ │ ├── LoginView.java │ │ │ ├── LoginPresenter.java │ │ │ └── LoginActivity.java │ │ │ └── main │ │ │ ├── MainView.java │ │ │ ├── FindItemsInteractor.java │ │ │ ├── MainAdapter.java │ │ │ ├── MainPresenter.java │ │ │ └── MainActivity.java │ └── test │ │ └── java │ │ └── com │ │ └── antonioleiva │ │ └── mvpexample │ │ └── app │ │ └── main │ │ └── MainPresenterTest.java ├── proguard-rules.txt └── build.gradle ├── appkotlin ├── .gitignore ├── src │ └── main │ │ ├── res │ │ ├── values │ │ │ ├── dimens.xml │ │ │ ├── colors.xml │ │ │ ├── styles.xml │ │ │ └── strings.xml │ │ ├── mipmap-hdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ ├── mipmap-anydpi-v26 │ │ │ ├── ic_launcher.xml │ │ │ └── ic_launcher_round.xml │ │ ├── layout │ │ │ ├── view_main_item.xml │ │ │ ├── activity_main.xml │ │ │ └── activity_login.xml │ │ ├── drawable │ │ │ ├── ic_username.xml │ │ │ ├── ic_password.xml │ │ │ └── ic_launcher_background.xml │ │ └── drawable-v24 │ │ │ └── ic_launcher_foreground.xml │ │ ├── java │ │ └── antonioleiva │ │ │ └── com │ │ │ └── appkotlin │ │ │ ├── Extensions.kt │ │ │ ├── main │ │ │ ├── MainView.kt │ │ │ ├── FindItemsInteractor.kt │ │ │ ├── MainPresenter.kt │ │ │ ├── MainAdapter.kt │ │ │ └── MainActivity.kt │ │ │ └── login │ │ │ ├── LoginView.kt │ │ │ ├── LoginInteractor.kt │ │ │ ├── LoginPresenter.kt │ │ │ └── LoginActivity.kt │ │ └── AndroidManifest.xml ├── proguard-rules.pro └── build.gradle ├── settings.gradle ├── login-classes.png ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── README.md ├── .gitignore ├── HOW-IT-WORKS.md ├── gradle.properties ├── gradlew.bat └── gradlew /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /appkotlin/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':appkotlin' 2 | -------------------------------------------------------------------------------- /login-classes.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/antoniolg/androidmvp/HEAD/login-classes.png -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 16dp 3 | 4 | -------------------------------------------------------------------------------- /appkotlin/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 16dp 3 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/antoniolg/androidmvp/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/antoniolg/androidmvp/HEAD/app/src/main/res/drawable-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/antoniolg/androidmvp/HEAD/app/src/main/res/drawable-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/antoniolg/androidmvp/HEAD/app/src/main/res/drawable-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/antoniolg/androidmvp/HEAD/app/src/main/res/drawable-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /appkotlin/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/antoniolg/androidmvp/HEAD/appkotlin/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /appkotlin/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/antoniolg/androidmvp/HEAD/appkotlin/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /appkotlin/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/antoniolg/androidmvp/HEAD/appkotlin/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /appkotlin/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/antoniolg/androidmvp/HEAD/appkotlin/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /appkotlin/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/antoniolg/androidmvp/HEAD/appkotlin/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /appkotlin/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/antoniolg/androidmvp/HEAD/appkotlin/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /appkotlin/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/antoniolg/androidmvp/HEAD/appkotlin/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /appkotlin/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/antoniolg/androidmvp/HEAD/appkotlin/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /appkotlin/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/antoniolg/androidmvp/HEAD/appkotlin/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /appkotlin/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/antoniolg/androidmvp/HEAD/appkotlin/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /appkotlin/src/main/java/antonioleiva/com/appkotlin/Extensions.kt: -------------------------------------------------------------------------------- 1 | package antonioleiva.com.appkotlin 2 | 3 | import android.os.Handler 4 | 5 | fun postDelayed(delayMillis: Long, task: () -> Unit) { 6 | Handler().postDelayed(task, delayMillis) 7 | } -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /appkotlin/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /appkotlin/src/main/java/antonioleiva/com/appkotlin/main/MainView.kt: -------------------------------------------------------------------------------- 1 | package antonioleiva.com.appkotlin.main 2 | 3 | interface MainView { 4 | fun showProgress() 5 | fun hideProgress() 6 | fun setItems(items: List) 7 | fun showMessage(message: String) 8 | } -------------------------------------------------------------------------------- /appkotlin/src/main/java/antonioleiva/com/appkotlin/login/LoginView.kt: -------------------------------------------------------------------------------- 1 | package antonioleiva.com.appkotlin.login 2 | 3 | interface LoginView { 4 | fun showProgress() 5 | fun hideProgress() 6 | fun setUsernameError() 7 | fun setPasswordError() 8 | fun navigateToHome() 9 | } -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sat Jan 13 09:12:34 PST 2018 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.4.1-all.zip -------------------------------------------------------------------------------- /appkotlin/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /appkotlin/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | androidmvp 2 | ========== 3 | 4 | MVP Android Example used to explain how to use this pattern in our Android apps. This code was created to support an article explanation: 5 | 6 | [Android MVP @ antonioleiva.com (English)](http://antonioleiva.com/mvp-android) 7 | 8 | [![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-androidmvp-brightgreen.svg?style=flat)](https://android-arsenal.com/details/3/1514) 9 | -------------------------------------------------------------------------------- /app/src/main/res/layout/view_main_item.xml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /app/src/main/res/menu/main.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 9 | 10 | -------------------------------------------------------------------------------- /appkotlin/src/main/java/antonioleiva/com/appkotlin/main/FindItemsInteractor.kt: -------------------------------------------------------------------------------- 1 | package antonioleiva.com.appkotlin.main 2 | 3 | import antonioleiva.com.appkotlin.postDelayed 4 | 5 | class FindItemsInteractor { 6 | 7 | fun findItems(callback: (List) -> Unit) { 8 | postDelayed(2000) { callback(createArrayList()) } 9 | } 10 | 11 | private fun createArrayList(): List = (1..10).map { "Item $it" } 12 | } -------------------------------------------------------------------------------- /appkotlin/src/main/res/layout/view_main_item.xml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /app/src/main/res/menu/login.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_username.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /appkotlin/src/main/res/drawable/ic_username.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /appkotlin/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_password.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /appkotlin/src/main/res/drawable/ic_password.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | 15 | # Gradle files 16 | .gradle/ 17 | ./gradlew.bat 18 | ./gradlew 19 | build/ 20 | 21 | # Mirror files 22 | mirror/ 23 | 24 | # Local configuration file (sdk path, etc) 25 | local.properties 26 | 27 | # Proguard folder generated by Eclipse 28 | proguard/ 29 | 30 | # Intellij project files 31 | *.iws 32 | .idea/workspace.xml 33 | .idea/tasks.xml 34 | .idea 35 | 36 | *.iml 37 | 38 | # OS 39 | .DS_Store -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | MVP Example 5 | Hello world! 6 | Settings 7 | username 8 | password 9 | Log in 10 | MVP Example 11 | Username cannot be empty 12 | Password cannot be empty 13 | 14 | 15 | -------------------------------------------------------------------------------- /appkotlin/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | MVP Example Kotlin 5 | Hello world! 6 | Settings 7 | username 8 | password 9 | Log in 10 | MVP Example 11 | Username cannot be empty 12 | Password cannot be empty 13 | 14 | 15 | -------------------------------------------------------------------------------- /appkotlin/src/main/java/antonioleiva/com/appkotlin/main/MainPresenter.kt: -------------------------------------------------------------------------------- 1 | package antonioleiva.com.appkotlin.main 2 | 3 | class MainPresenter(var mainView: MainView?, val findItemsInteractor: FindItemsInteractor) { 4 | 5 | fun onResume() { 6 | mainView?.showProgress() 7 | findItemsInteractor.findItems(::onItemsLoaded) 8 | } 9 | 10 | private fun onItemsLoaded(items: List) { 11 | mainView?.apply { 12 | setItems(items) 13 | hideProgress() 14 | } 15 | } 16 | 17 | fun onItemClicked(item: String) { 18 | mainView?.showMessage(item) 19 | } 20 | 21 | fun onDestroy() { 22 | mainView = null 23 | } 24 | } -------------------------------------------------------------------------------- /HOW-IT-WORKS.md: -------------------------------------------------------------------------------- 1 | How it Works 2 | ========== 3 | ![](login-classes.png) 4 | 5 | 1. View(Activity, Fragment, ...) calls presenter methods whenever there're user interaction 6 | 2. Presenter implementation calls the interactor(use case handler) to get results from business/domain layer 7 | 3. Interactor implementation returns the results or just returns the control to presenter implementation by calling listener methods 8 | 4. Presenter implementation calls view methods to update the UI by calling view interface. 9 | 10 | View, Presenter, Interactor and Listener interfaces are used to remove tight coupling. 11 | 12 | View becomes too humble to test. We can use fake(simulator) presenter to test view. 13 | 14 | Presenter and Interactor can be tested. 15 | -------------------------------------------------------------------------------- /app/proguard-rules.txt: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in C:/Desarrollo/Entorno Android/01 Entorno de Desarrollo/adt-bundle-windows/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the ProGuard 5 | # include property in project.properties. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} -------------------------------------------------------------------------------- /appkotlin/src/main/java/antonioleiva/com/appkotlin/login/LoginInteractor.kt: -------------------------------------------------------------------------------- 1 | package antonioleiva.com.appkotlin.login 2 | 3 | import antonioleiva.com.appkotlin.postDelayed 4 | 5 | class LoginInteractor { 6 | 7 | interface OnLoginFinishedListener { 8 | fun onUsernameError() 9 | fun onPasswordError() 10 | fun onSuccess() 11 | } 12 | 13 | fun login(username: String, password: String, listener: OnLoginFinishedListener) { 14 | // Mock login. I'm creating a handler to delay the answer a couple of seconds 15 | postDelayed(2000) { 16 | when { 17 | username.isEmpty() -> listener.onUsernameError() 18 | password.isEmpty() -> listener.onPasswordError() 19 | else -> listener.onSuccess() 20 | } 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /appkotlin/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /appkotlin/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 13 | 14 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Settings specified in this file will override any Gradle settings 5 | # configured through the IDE. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m 13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 14 | 15 | # When configured, Gradle will run in incubating parallel mode. 16 | # This option should only be used with decoupled projects. More details, visit 17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 18 | # org.gradle.parallel=true -------------------------------------------------------------------------------- /appkotlin/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 13 | 14 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /appkotlin/src/main/java/antonioleiva/com/appkotlin/login/LoginPresenter.kt: -------------------------------------------------------------------------------- 1 | package antonioleiva.com.appkotlin.login 2 | 3 | class LoginPresenter(var loginView: LoginView?, val loginInteractor: LoginInteractor) : 4 | LoginInteractor.OnLoginFinishedListener { 5 | 6 | fun validateCredentials(username: String, password: String) { 7 | loginView?.showProgress() 8 | loginInteractor.login(username, password, this) 9 | } 10 | 11 | fun onDestroy() { 12 | loginView = null 13 | } 14 | 15 | override fun onUsernameError() { 16 | loginView?.apply { 17 | setUsernameError() 18 | hideProgress() 19 | } 20 | } 21 | 22 | override fun onPasswordError() { 23 | loginView?.apply { 24 | setPasswordError() 25 | hideProgress() 26 | } 27 | } 28 | 29 | override fun onSuccess() { 30 | loginView?.navigateToHome() 31 | } 32 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 10 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 27 5 | defaultConfig { 6 | applicationId "com.antonioleiva.mvpexample.app" 7 | minSdkVersion 21 8 | targetSdkVersion 27 9 | versionCode 1 10 | versionName "1.0" 11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | compileOptions { 20 | sourceCompatibility JavaVersion.VERSION_1_8 21 | targetCompatibility JavaVersion.VERSION_1_8 22 | } 23 | } 24 | 25 | dependencies { 26 | implementation 'com.android.support:appcompat-v7:27.1.1' 27 | implementation 'com.android.support:recyclerview-v7:27.1.1' 28 | testImplementation 'junit:junit:4.12' 29 | testImplementation 'org.mockito:mockito-core:2.15.0' 30 | } 31 | -------------------------------------------------------------------------------- /app/src/main/java/com/antonioleiva/mvpexample/app/login/LoginInteractor.java: -------------------------------------------------------------------------------- 1 | package com.antonioleiva.mvpexample.app.login; 2 | 3 | import android.os.Handler; 4 | import android.text.TextUtils; 5 | 6 | public class LoginInteractor { 7 | 8 | interface OnLoginFinishedListener { 9 | void onUsernameError(); 10 | 11 | void onPasswordError(); 12 | 13 | void onSuccess(); 14 | } 15 | 16 | public void login(final String username, final String password, final OnLoginFinishedListener listener) { 17 | // Mock login. I'm creating a handler to delay the answer a couple of seconds 18 | new Handler().postDelayed(() -> { 19 | if (TextUtils.isEmpty(username)) { 20 | listener.onUsernameError(); 21 | return; 22 | } 23 | if (TextUtils.isEmpty(password)) { 24 | listener.onPasswordError(); 25 | return; 26 | } 27 | listener.onSuccess(); 28 | }, 2000); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /appkotlin/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'kotlin-android' 3 | apply plugin: 'kotlin-android-extensions' 4 | android { 5 | compileSdkVersion 27 6 | 7 | 8 | 9 | defaultConfig { 10 | applicationId "antonioleiva.com.appkotlin" 11 | minSdkVersion 21 12 | targetSdkVersion 27 13 | versionCode 1 14 | versionName "1.0" 15 | 16 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 17 | 18 | } 19 | 20 | buildTypes { 21 | release { 22 | minifyEnabled false 23 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 24 | } 25 | } 26 | 27 | } 28 | 29 | dependencies { 30 | implementation fileTree(dir: 'libs', include: ['*.jar']) 31 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 32 | implementation 'com.android.support:appcompat-v7:27.1.1' 33 | implementation 'com.android.support:recyclerview-v7:27.1.1' 34 | } 35 | -------------------------------------------------------------------------------- /app/src/main/java/com/antonioleiva/mvpexample/app/login/LoginView.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * Copyright (C) 2018 Antonio Leiva Gordillo. 4 | * * 5 | * * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * * you may not use this file except in compliance with the License. 7 | * * You may obtain a copy of the License at 8 | * * 9 | * * http://www.apache.org/licenses/LICENSE-2.0 10 | * * 11 | * * Unless required by applicable law or agreed to in writing, software 12 | * * distributed under the License is distributed on an "AS IS" BASIS, 13 | * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * * See the License for the specific language governing permissions and 15 | * * limitations under the License. 16 | * 17 | */ 18 | 19 | package com.antonioleiva.mvpexample.app.login; 20 | 21 | public interface LoginView { 22 | void showProgress(); 23 | 24 | void hideProgress(); 25 | 26 | void setUsernameError(); 27 | 28 | void setPasswordError(); 29 | 30 | void navigateToHome(); 31 | } 32 | -------------------------------------------------------------------------------- /app/src/main/java/com/antonioleiva/mvpexample/app/main/MainView.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * * Copyright (C) 2018 Antonio Leiva Gordillo. 4 | * * 5 | * * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * * you may not use this file except in compliance with the License. 7 | * * You may obtain a copy of the License at 8 | * * 9 | * * http://www.apache.org/licenses/LICENSE-2.0 10 | * * 11 | * * Unless required by applicable law or agreed to in writing, software 12 | * * distributed under the License is distributed on an "AS IS" BASIS, 13 | * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * * See the License for the specific language governing permissions and 15 | * * limitations under the License. 16 | * 17 | */ 18 | 19 | package com.antonioleiva.mvpexample.app.main; 20 | 21 | import java.util.List; 22 | 23 | public interface MainView { 24 | 25 | void showProgress(); 26 | 27 | void hideProgress(); 28 | 29 | void setItems(List items); 30 | 31 | void showMessage(String message); 32 | } -------------------------------------------------------------------------------- /appkotlin/src/main/java/antonioleiva/com/appkotlin/main/MainAdapter.kt: -------------------------------------------------------------------------------- 1 | package antonioleiva.com.appkotlin.main 2 | 3 | import android.support.v7.widget.RecyclerView 4 | import android.view.LayoutInflater 5 | import android.view.ViewGroup 6 | import android.widget.TextView 7 | import antonioleiva.com.appkotlin.R 8 | 9 | class MainAdapter(private val items: List, private val listener: (String) -> Unit) : 10 | RecyclerView.Adapter() { 11 | 12 | override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MainViewHolder { 13 | val v = LayoutInflater.from(parent.context) 14 | .inflate(R.layout.view_main_item, parent, false) as TextView 15 | 16 | return MainViewHolder(v) 17 | } 18 | 19 | override fun onBindViewHolder(holder: MainViewHolder, position: Int) { 20 | val item = items[position] 21 | holder.textView.text = item 22 | holder.textView.setOnClickListener { listener(item) } 23 | } 24 | 25 | override fun getItemCount(): Int = items.size 26 | 27 | class MainViewHolder(val textView: TextView) : RecyclerView.ViewHolder(textView) { 28 | 29 | } 30 | } -------------------------------------------------------------------------------- /appkotlin/src/main/java/antonioleiva/com/appkotlin/main/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package antonioleiva.com.appkotlin.main 2 | 3 | import android.os.Bundle 4 | import android.support.v7.app.AppCompatActivity 5 | import android.view.View 6 | import android.widget.Toast 7 | import antonioleiva.com.appkotlin.R 8 | import kotlinx.android.synthetic.main.activity_main.* 9 | 10 | class MainActivity : AppCompatActivity(), MainView { 11 | 12 | private val presenter = MainPresenter(this, FindItemsInteractor()) 13 | 14 | override fun onCreate(savedInstanceState: Bundle?) { 15 | super.onCreate(savedInstanceState) 16 | setContentView(R.layout.activity_main) 17 | } 18 | 19 | override fun onResume() { 20 | super.onResume() 21 | presenter.onResume() 22 | } 23 | 24 | override fun onDestroy() { 25 | super.onDestroy() 26 | presenter.onDestroy() 27 | } 28 | 29 | override fun showProgress() { 30 | progress.visibility = View.VISIBLE 31 | list.visibility = View.GONE 32 | } 33 | 34 | override fun hideProgress() { 35 | progress.visibility = View.GONE 36 | list.visibility = View.VISIBLE 37 | } 38 | 39 | override fun setItems(items: List) { 40 | list.adapter = MainAdapter(items, presenter::onItemClicked) 41 | } 42 | 43 | override fun showMessage(message: String) { 44 | Toast.makeText(this, message, Toast.LENGTH_LONG).show() 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_login.xml: -------------------------------------------------------------------------------- 1 | 8 | 9 | 18 | 19 | 28 | 29 |