├── .gitignore ├── .idea ├── .name ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── gradle.xml ├── libraries │ ├── appcompat_v7_22_0_0.xml │ ├── butterknife_7_0_1.xml │ ├── gson_2_3_1.xml │ ├── retrofit_1_9_0.xml │ ├── support_annotations_22_0_0.xml │ └── support_v4_22_0_0.xml ├── misc.xml ├── modules.xml └── vcs.xml ├── LICENSE ├── README.md ├── android-mvp.iml ├── app ├── .gitignore ├── app.iml ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── io │ │ └── jpotts18 │ │ └── android_mvp │ │ └── ApplicationTest.java │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── io │ │ └── jpotts18 │ │ └── android_mvp │ │ └── domain │ │ ├── GithubService.java │ │ ├── login │ │ ├── ILoginPresenter.java │ │ ├── ILoginView.java │ │ ├── LoginPresenter.java │ │ ├── async │ │ │ ├── AsyncLoginInteractor.java │ │ │ ├── IAsyncLoginInteractor.java │ │ │ └── OnLoginFinishedListener.java │ │ └── sync │ │ │ ├── ISynchronousLoginInteractor.java │ │ │ └── SynchronousLoginInteractor.java │ │ ├── models │ │ └── Repo.java │ │ ├── repos │ │ ├── IRepoListPresenter.java │ │ ├── IRepoListView.java │ │ ├── OnRepoInteractorFinishedListener.java │ │ ├── RepoAdapter.java │ │ ├── RepoListInteractor.java │ │ └── RepoListPresenter.java │ │ └── ui │ │ ├── LoginActivity.java │ │ ├── activities │ │ ├── LoginActivity.java │ │ ├── RepoListActivity.java │ │ └── RepoListFragmentActivity.java │ │ └── fragments │ │ └── RepoListFragment.java │ └── res │ ├── layout │ ├── activity_login.xml │ ├── activity_repo_list.xml │ ├── activity_repo_list_fragment.xml │ └── fragment_repo_list.xml │ ├── menu │ └── menu_login.xml │ ├── mipmap-hdpi │ └── ic_launcher.png │ ├── mipmap-mdpi │ └── ic_launcher.png │ ├── mipmap-xhdpi │ └── ic_launcher.png │ ├── mipmap-xxhdpi │ └── ic_launcher.png │ ├── values-w820dp │ └── dimens.xml │ └── values │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── mvp.png └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | .idea/workspace.xml 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 | build/ 18 | /*/build/ 19 | 20 | # Local configuration file (sdk path, etc) 21 | local.properties 22 | 23 | # Proguard folder generated by Eclipse 24 | proguard/ 25 | 26 | # Log Files 27 | *.log 28 | -------------------------------------------------------------------------------- /.idea/.name: -------------------------------------------------------------------------------- 1 | android-mvp -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 22 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /.idea/libraries/appcompat_v7_22_0_0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/libraries/butterknife_7_0_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /.idea/libraries/gson_2_3_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /.idea/libraries/retrofit_1_9_0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /.idea/libraries/support_annotations_22_0_0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /.idea/libraries/support_v4_22_0_0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | Android Lint 39 | 40 | 41 | 42 | 43 | Abstraction issues 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 65 | 66 | 67 | 68 | 69 | 1.7 70 | 71 | 76 | 77 | 78 | 79 | 80 | 81 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Jeff Potter 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Android MVP 2 | 3 | This repository demonstrates the [Model View Presenter](http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93presenter) architecture and was inspired by [Antonio Leiva's Android MVP](https://github.com/antoniolg/androidmvp). You can find an example with updated libraries in the [master-2](https://github.com/jpotts18/android-mvp/tree/master-2) branch from [RajuSe](https://github.com/RajuSE). 4 | 5 | ## Videos 6 | 7 | [Android MVP Playlist - Youtube](https://www.youtube.com/playlist?list=PLfbTKxZYb1mhQQaajZw0OntPcioSPdfKM) 8 | 9 | ## Getting Started 10 | 11 | 1. Clone the Repo - ``git clone git@github.com:jpotts18/android-mvp.git`` 12 | 1. Look at all of the branches - ``git branch -a`` 13 | 2. **Read about the branches** 14 | 1. Don't forget to checkout the `master-2` it has updated libraries like RxJava, Butterknife, etc. 15 | 16 | This repository contains a **chain of branches** that shows the logical progression of the Android MVP. 17 | 18 | * ``git checkout 1-login-view`` - demonstates a simple View which is a LoginActivity 19 | * ``git checkout 2-synchronous-login-mvp`` - demonstrates Login MVP implementation with a synchronous Model (or interactor). 20 | * ``git checkout 3-async-login-mvp`` - demonstrates Login MVP with an asynchronous Model which does not change any code in the LoginActivity 21 | * ``git checkout 4-list-activity-view`` - demonstates a more complex View (RepoListActivity) and an Asynchronous Networked Model which shows a users Github repositories sorted by stars. 22 | * ``git checkout 5-list-fragment`` - substitutes the RepoListActivity for a Fragment without changing any code in the Model. 23 | * The ``master`` branch contains the completed project. 24 | 25 | ## What is MVP? 26 | 27 | [Model-View-Presenter](http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93presenter) is a user interface architectural pattern engineered to facilitate automated unit testing and improve the separation of concerns in presentation logic: 28 | 29 | * The **Model** is an interface defining the data to be displayed or otherwise acted upon in the user interface. 30 | * The **View** is a passive interface that displays data (the Model) and routes user commands (events) to the presenter to act upon that data. 31 | * The **Presenter** acts upon the model and the view. It retrieves data from repositories (the model), and formats it for display in the view. 32 | 33 | 34 | ## Benefits 35 | 36 | * **Loose Coupling** - The Presenter is an intermediary between the View code and the Model. This allows the View and the Model to evolve independently of each other. 37 | * **[Separation of Concerns](http://en.wikipedia.org/wiki/Separation_of_concerns)** - Individual sections can be reused, as well as developed and updated independently. 38 | * **More Testable** – By isolating each major component (UI, Presenter, and Model) it is easier to write unit tests. This is especially true when using the MVP pattern which only interacts with the view using an interface. 39 | * **Code Reuse** – By using a separation of concerns/responsible design approach you will increase code reuse. 40 | * **Flexibility** - By isolating most of your code into the Presenter and Model components your code base is more flexible to change in the View. 41 | 42 | ## Key differences between MVC and MVP 43 | 44 | MVP Pattern 45 | * View is more loosely coupled to the model. The presenter is responsible for binding the model to the view. 46 | * Easier to unit test because interaction with the view is through an interface 47 | * Usually view to presenter map one to one. Complex views may have multi presenters. 48 | 49 | MVC Pattern 50 | * Controller are based on behaviors and can be shared across views 51 | * Can be responsible for determining which view to display (Front Controller Pattern) 52 | 53 | #### References 54 | [MVC or MVP Pattern – Whats the difference?](http://www.infragistics.com/community/blogs/todd_snyder/archive/2007/10/17/mvc-or-mvp-pattern-whats-the-difference.aspx) 55 | 56 | [Model-View-Presenter - Wikipedia](http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93presenter) 57 | 58 | [Separation of Concerns - Wikipedia](http://en.wikipedia.org/wiki/Separation_of_concerns) 59 | -------------------------------------------------------------------------------- /android-mvp.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/app.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 8 | 9 | 10 | 11 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 22 5 | buildToolsVersion "21.1.2" 6 | 7 | defaultConfig { 8 | applicationId "io.jpotts18.android_mvp" 9 | minSdkVersion 16 10 | targetSdkVersion 22 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | 21 | lintOptions { 22 | disable 'InvalidPackage' 23 | } 24 | } 25 | 26 | dependencies { 27 | compile fileTree(dir: 'libs', include: ['*.jar']) 28 | compile 'com.android.support:appcompat-v7:22.0.0' 29 | compile 'com.jakewharton:butterknife:7.0.1' 30 | compile 'com.squareup.retrofit:retrofit:1.9.0' 31 | } 32 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/jpotts18/Library/Android/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 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 | #} 18 | 19 | 20 | -keep class butterknife.** { *; } 21 | -dontwarn butterknife.internal.** 22 | -keep class **$$ViewInjector { *; } 23 | 24 | -keepclasseswithmembernames class * { 25 | @butterknife.* ; 26 | } 27 | 28 | -keepclasseswithmembernames class * { 29 | @butterknife.* ; 30 | } -------------------------------------------------------------------------------- /app/src/androidTest/java/io/jpotts18/android_mvp/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package io.jpotts18.android_mvp; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 12 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 24 | 25 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /app/src/main/java/io/jpotts18/android_mvp/domain/GithubService.java: -------------------------------------------------------------------------------- 1 | package io.jpotts18.android_mvp.domain; 2 | 3 | import java.util.List; 4 | 5 | import io.jpotts18.android_mvp.domain.models.Repo; 6 | import retrofit.Callback; 7 | import retrofit.http.GET; 8 | import retrofit.http.Path; 9 | 10 | /** 11 | * Created by jpotts18 on 5/12/15. 12 | */ 13 | 14 | public interface GithubService { 15 | @GET("/users/{user}/repos") 16 | void listRepos(@Path("user") String user, Callback> callback); 17 | } -------------------------------------------------------------------------------- /app/src/main/java/io/jpotts18/android_mvp/domain/login/ILoginPresenter.java: -------------------------------------------------------------------------------- 1 | package io.jpotts18.android_mvp.domain.login; 2 | 3 | /** 4 | * Created by jpotts18 on 5/11/15. 5 | */ 6 | public interface ILoginPresenter { 7 | void attemptLogin(String username, String password); 8 | } 9 | -------------------------------------------------------------------------------- /app/src/main/java/io/jpotts18/android_mvp/domain/login/ILoginView.java: -------------------------------------------------------------------------------- 1 | package io.jpotts18.android_mvp.domain.login; 2 | 3 | public interface ILoginView { 4 | void navigateToListActivity(); 5 | void loginFailed(); 6 | } 7 | -------------------------------------------------------------------------------- /app/src/main/java/io/jpotts18/android_mvp/domain/login/LoginPresenter.java: -------------------------------------------------------------------------------- 1 | package io.jpotts18.android_mvp.domain.login; 2 | 3 | import io.jpotts18.android_mvp.domain.login.async.AsyncLoginInteractor; 4 | import io.jpotts18.android_mvp.domain.login.async.OnLoginFinishedListener; 5 | 6 | /** 7 | * Created by jpotts18 on 5/11/15. 8 | */ 9 | public class LoginPresenter implements ILoginPresenter, OnLoginFinishedListener { 10 | 11 | /****************************************************************************************** 12 | - LoginPresenter has a reference to both the View and the Interactor 13 | - LoginPresenter retrieves data from the model, and notifies the view to display it. 14 | - OnLoginFinishedListener adds the methods that are necessary for asynchronous callbacks which leaves the rest of the interface intact 15 | ******************************************************************************************* 16 | */ 17 | 18 | // Referencing any class that implements the ILoginView interface provides greater flexibility 19 | private ILoginView view; 20 | private AsyncLoginInteractor interactor; 21 | 22 | public LoginPresenter(ILoginView loginView) { 23 | this.view = loginView; 24 | this.interactor = new AsyncLoginInteractor(); 25 | } 26 | 27 | public void attemptLogin(String username, String password){ 28 | interactor.validateCredentialsAsync(this, username, password); 29 | } 30 | 31 | @Override 32 | public void onError() { 33 | view.loginFailed(); 34 | } 35 | 36 | @Override 37 | public void onSuccess() { 38 | view.navigateToListActivity(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /app/src/main/java/io/jpotts18/android_mvp/domain/login/async/AsyncLoginInteractor.java: -------------------------------------------------------------------------------- 1 | package io.jpotts18.android_mvp.domain.login.async; 2 | 3 | import android.os.Handler; 4 | 5 | /** 6 | * Created by jpotts18 on 5/11/15. 7 | */ 8 | public class AsyncLoginInteractor implements IAsyncLoginInteractor { 9 | 10 | /****************************************************************************************** 11 | * An Interactor helps models cross application boundaries such as networks or serialization 12 | * This LoginInteractor knows nothing about a UI or the LoginPresenter 13 | * Because this is an asynchronous call it will call back on the OnLoginFinishedListener when complete 14 | ******************************************************************************************* 15 | */ 16 | 17 | public void validateCredentialsAsync(final OnLoginFinishedListener listener, final String username, final String password){ 18 | // Mock login. I'm creating a handler to delay the answer a couple of seconds 19 | new Handler().postDelayed(new Runnable() { 20 | @Override public void run() { 21 | if ((username.length() > 3) && (password.length() > 3)) { 22 | listener.onSuccess(); 23 | } else { 24 | listener.onError(); 25 | } 26 | } 27 | }, 2000); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /app/src/main/java/io/jpotts18/android_mvp/domain/login/async/IAsyncLoginInteractor.java: -------------------------------------------------------------------------------- 1 | package io.jpotts18.android_mvp.domain.login.async; 2 | 3 | /** 4 | * Created by jpotts18 on 5/11/15. 5 | */ 6 | public interface IAsyncLoginInteractor { 7 | void validateCredentialsAsync(OnLoginFinishedListener listener, String username, String password); 8 | } 9 | -------------------------------------------------------------------------------- /app/src/main/java/io/jpotts18/android_mvp/domain/login/async/OnLoginFinishedListener.java: -------------------------------------------------------------------------------- 1 | package io.jpotts18.android_mvp.domain.login.async; 2 | 3 | /** 4 | * Created by jpotts18 on 5/11/15. 5 | */ 6 | public interface OnLoginFinishedListener { 7 | void onError(); 8 | void onSuccess(); 9 | } 10 | -------------------------------------------------------------------------------- /app/src/main/java/io/jpotts18/android_mvp/domain/login/sync/ISynchronousLoginInteractor.java: -------------------------------------------------------------------------------- 1 | package io.jpotts18.android_mvp.domain.login.sync; 2 | 3 | /** 4 | * Created by jpotts18 on 5/11/15. 5 | */ 6 | public interface ISynchronousLoginInteractor { 7 | boolean validatedCredentials(String username, String password); 8 | } 9 | -------------------------------------------------------------------------------- /app/src/main/java/io/jpotts18/android_mvp/domain/login/sync/SynchronousLoginInteractor.java: -------------------------------------------------------------------------------- 1 | package io.jpotts18.android_mvp.domain.login.sync; 2 | 3 | import io.jpotts18.android_mvp.domain.login.sync.ISynchronousLoginInteractor; 4 | 5 | /** 6 | * Created by jpotts18 on 5/11/15. 7 | */ 8 | public class SynchronousLoginInteractor implements ISynchronousLoginInteractor { 9 | 10 | /****************************************************************************************** 11 | * An Interactor helps models cross application boundaries such as networks or serialization 12 | * This LoginInteractor knows nothing about a UI or the LoginPresenter 13 | ******************************************************************************************* 14 | */ 15 | 16 | public SynchronousLoginInteractor() { } 17 | 18 | public boolean validatedCredentials(String username, String password) { 19 | return username.contains("gmail"); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /app/src/main/java/io/jpotts18/android_mvp/domain/models/Repo.java: -------------------------------------------------------------------------------- 1 | package io.jpotts18.android_mvp.domain.models; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | 5 | /** 6 | * Created by jpotts18 on 5/12/15. 7 | */ 8 | public class Repo { 9 | public int id; 10 | public String name; 11 | public boolean fork; 12 | @SerializedName("stargazers_count") 13 | public int stars; 14 | } 15 | -------------------------------------------------------------------------------- /app/src/main/java/io/jpotts18/android_mvp/domain/repos/IRepoListPresenter.java: -------------------------------------------------------------------------------- 1 | package io.jpotts18.android_mvp.domain.repos; 2 | 3 | /** 4 | * Created by jpotts18 on 5/11/15. 5 | */ 6 | public interface IRepoListPresenter { 7 | public void loadCommits(String username); 8 | } 9 | -------------------------------------------------------------------------------- /app/src/main/java/io/jpotts18/android_mvp/domain/repos/IRepoListView.java: -------------------------------------------------------------------------------- 1 | package io.jpotts18.android_mvp.domain.repos; 2 | 3 | import java.util.List; 4 | 5 | import io.jpotts18.android_mvp.domain.models.Repo; 6 | import retrofit.RetrofitError; 7 | import retrofit.client.Response; 8 | 9 | /** 10 | * Created by jpotts18 on 5/11/15. 11 | */ 12 | public interface IRepoListView { 13 | void onReposLoadedSuccess(List list, Response response); 14 | void onReposLoadedFailure(RetrofitError error); 15 | } 16 | -------------------------------------------------------------------------------- /app/src/main/java/io/jpotts18/android_mvp/domain/repos/OnRepoInteractorFinishedListener.java: -------------------------------------------------------------------------------- 1 | package io.jpotts18.android_mvp.domain.repos; 2 | 3 | import java.util.List; 4 | 5 | import io.jpotts18.android_mvp.domain.models.Repo; 6 | import retrofit.RetrofitError; 7 | import retrofit.client.Response; 8 | 9 | /** 10 | * Created by jpotts18 on 5/12/15. 11 | */ 12 | public interface OnRepoInteractorFinishedListener { 13 | void onNetworkSuccess(List list, Response response); 14 | void onNetworkFailure(RetrofitError error); 15 | } 16 | -------------------------------------------------------------------------------- /app/src/main/java/io/jpotts18/android_mvp/domain/repos/RepoAdapter.java: -------------------------------------------------------------------------------- 1 | package io.jpotts18.android_mvp.domain.repos; 2 | 3 | import android.app.Activity; 4 | import android.content.Context; 5 | import android.view.View; 6 | import android.view.ViewGroup; 7 | import android.widget.BaseAdapter; 8 | import android.widget.TextView; 9 | 10 | import java.util.List; 11 | 12 | import butterknife.Bind; 13 | import butterknife.ButterKnife; 14 | import io.jpotts18.android_mvp.domain.models.Repo; 15 | 16 | /** 17 | * Created by jpotts18 on 5/12/15. 18 | */ 19 | public class RepoAdapter extends BaseAdapter { 20 | 21 | private List list; 22 | private Context context; 23 | 24 | public RepoAdapter(Context context, List list) { 25 | this.list = list; 26 | this.context = context; 27 | } 28 | 29 | @Override 30 | public int getCount() { 31 | return list.size(); 32 | } 33 | 34 | @Override 35 | public Object getItem(int i) { 36 | return list.get(i); 37 | } 38 | 39 | @Override 40 | public long getItemId(int i) { 41 | return list.get(i).id; 42 | } 43 | 44 | @Override 45 | public View getView(int position, View view, ViewGroup parent) { 46 | ViewHolder holder; 47 | if (view != null) { 48 | holder = (ViewHolder) view.getTag(); 49 | } else { 50 | view = ((Activity) context).getLayoutInflater().inflate(android.R.layout.simple_list_item_1, parent, false); 51 | holder = new ViewHolder(view); 52 | view.setTag(holder); 53 | } 54 | Repo repo = (Repo) getItem(position); 55 | holder.text1.setText(String.format("%s - %d", repo.name, repo.stars)); 56 | return view; 57 | } 58 | 59 | public static class ViewHolder { 60 | @Bind(android.R.id.text1) 61 | TextView text1; 62 | 63 | public ViewHolder(View view) { 64 | ButterKnife.bind(this, view); 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /app/src/main/java/io/jpotts18/android_mvp/domain/repos/RepoListInteractor.java: -------------------------------------------------------------------------------- 1 | package io.jpotts18.android_mvp.domain.repos; 2 | 3 | import java.util.Collections; 4 | import java.util.Comparator; 5 | import java.util.List; 6 | 7 | import io.jpotts18.android_mvp.domain.GithubService; 8 | import io.jpotts18.android_mvp.domain.models.Repo; 9 | import retrofit.Callback; 10 | import retrofit.RestAdapter; 11 | import retrofit.RetrofitError; 12 | import retrofit.client.Response; 13 | 14 | /** 15 | * Created by jpotts18 on 5/11/15. 16 | */ 17 | public class RepoListInteractor implements Callback> { 18 | 19 | private OnRepoInteractorFinishedListener listener; 20 | 21 | public RepoListInteractor(OnRepoInteractorFinishedListener listener) { 22 | this.listener = listener; 23 | } 24 | 25 | private RestAdapter initRestAdapter(){ 26 | RestAdapter restAdapter = new RestAdapter.Builder() 27 | .setLogLevel(RestAdapter.LogLevel.FULL) 28 | .setEndpoint("https://api.github.com") 29 | .build(); 30 | return restAdapter; 31 | } 32 | 33 | public void loadRecentCommits(String username) { 34 | RestAdapter adapter = initRestAdapter(); 35 | adapter.create(GithubService.class).listRepos(username, this); 36 | } 37 | 38 | @Override 39 | public void success(List list, Response response) { 40 | Collections.sort(list, new Comparator() { 41 | @Override 42 | public int compare(Repo left, Repo right) { 43 | return (left.stars > right.stars) ? -1 : 1; 44 | } 45 | }); 46 | 47 | listener.onNetworkSuccess(list, response); 48 | } 49 | 50 | @Override 51 | public void failure(RetrofitError error) { 52 | listener.onNetworkFailure(error); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /app/src/main/java/io/jpotts18/android_mvp/domain/repos/RepoListPresenter.java: -------------------------------------------------------------------------------- 1 | package io.jpotts18.android_mvp.domain.repos; 2 | 3 | import java.util.List; 4 | 5 | import io.jpotts18.android_mvp.domain.models.Repo; 6 | import retrofit.RetrofitError; 7 | import retrofit.client.Response; 8 | 9 | /** 10 | * Created by jpotts18 on 5/11/15. 11 | */ 12 | public class RepoListPresenter implements IRepoListPresenter, OnRepoInteractorFinishedListener { 13 | 14 | private IRepoListView view; 15 | private RepoListInteractor interactor; 16 | 17 | public RepoListPresenter(IRepoListView view) { 18 | this.view = view; 19 | this.interactor = new RepoListInteractor(this); // pass in the InteractorListener 20 | } 21 | 22 | @Override 23 | public void loadCommits(String username) { 24 | interactor.loadRecentCommits(username); 25 | } 26 | 27 | @Override 28 | public void onNetworkSuccess(List list, Response response) { 29 | view.onReposLoadedSuccess(list, response); 30 | } 31 | 32 | @Override 33 | public void onNetworkFailure(RetrofitError error) { 34 | view.onReposLoadedFailure(error); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /app/src/main/java/io/jpotts18/android_mvp/domain/ui/LoginActivity.java: -------------------------------------------------------------------------------- 1 | package io.jpotts18.android_mvp.domain.ui; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.ActionBarActivity; 5 | import android.widget.EditText; 6 | 7 | import butterknife.Bind; 8 | import butterknife.ButterKnife; 9 | import io.jpotts18.android_mvp.R; 10 | 11 | 12 | public class LoginActivity extends ActionBarActivity { 13 | 14 | @Bind(R.id.login_github_username) 15 | EditText loginEditText; 16 | 17 | @Bind(R.id.login_fake_password) 18 | EditText passwordEditText; 19 | 20 | @Override 21 | protected void onCreate(Bundle savedInstanceState) { 22 | super.onCreate(savedInstanceState); 23 | setContentView(R.layout.activity_login); 24 | ButterKnife.bind(this); 25 | } 26 | 27 | } 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /app/src/main/java/io/jpotts18/android_mvp/domain/ui/activities/LoginActivity.java: -------------------------------------------------------------------------------- 1 | package io.jpotts18.android_mvp.domain.ui.activities; 2 | 3 | import android.app.ProgressDialog; 4 | import android.content.Intent; 5 | import android.support.v7.app.ActionBarActivity; 6 | import android.os.Bundle; 7 | import android.view.View; 8 | import android.widget.EditText; 9 | import android.widget.Toast; 10 | 11 | import butterknife.Bind; 12 | import butterknife.ButterKnife; 13 | import butterknife.OnClick; 14 | import io.jpotts18.android_mvp.BuildConfig; 15 | import io.jpotts18.android_mvp.R; 16 | import io.jpotts18.android_mvp.domain.login.ILoginView; 17 | import io.jpotts18.android_mvp.domain.login.LoginPresenter; 18 | 19 | 20 | public class LoginActivity extends ActionBarActivity implements ILoginView { 21 | 22 | /****************************************************************************************** 23 | - LoginActivity ONLY knows how to display views and sending events and data to the presenter 24 | - LoginActivity doesn't know anything about the model (SynchronousLoginInteractor) 25 | - The only changes to the LoginActivity to allow for asynchronous behavior was to add a ProgressDialog 26 | ******************************************************************************************** 27 | */ 28 | 29 | @Bind(R.id.login_github_username) 30 | EditText githubUsernameEditText; 31 | 32 | @Bind(R.id.login_fake_password) 33 | EditText fakePasswordEditText; 34 | 35 | LoginPresenter presenter; 36 | ProgressDialog progressDialog; 37 | 38 | @Override 39 | protected void onCreate(Bundle savedInstanceState) { 40 | super.onCreate(savedInstanceState); 41 | setContentView(R.layout.activity_login); 42 | ButterKnife.bind(this); 43 | 44 | if (BuildConfig.DEBUG) { 45 | githubUsernameEditText.setText("jakewharton"); 46 | fakePasswordEditText.setText("$uper$ecret"); 47 | } 48 | 49 | presenter = new LoginPresenter(this); 50 | } 51 | 52 | @OnClick(R.id.login_submit_button) 53 | public void loginTapped(View view){ 54 | progressDialog = ProgressDialog.show(this, "Authenticating...", null); 55 | String email = githubUsernameEditText.getText().toString(); 56 | String password = fakePasswordEditText.getText().toString(); 57 | // Pass user event straight to presenter 58 | presenter.attemptLogin(email, password); 59 | } 60 | 61 | @Override 62 | public void navigateToListActivity() { 63 | progressDialog.dismiss(); 64 | Toast.makeText(this, "Login Success!",Toast.LENGTH_SHORT).show(); 65 | // TODO: This seems to have to do with persisting data. Where should we move this? 66 | Intent i = new Intent(this, RepoListFragmentActivity.class); 67 | i.putExtra("username", githubUsernameEditText.getText().toString()); 68 | startActivity(i); 69 | } 70 | 71 | @Override 72 | public void loginFailed() { 73 | progressDialog.dismiss(); 74 | Toast.makeText(this, "Login Invalid: Must be 3 letters or longer", Toast.LENGTH_SHORT).show(); 75 | } 76 | } 77 | 78 | 79 | 80 | -------------------------------------------------------------------------------- /app/src/main/java/io/jpotts18/android_mvp/domain/ui/activities/RepoListActivity.java: -------------------------------------------------------------------------------- 1 | package io.jpotts18.android_mvp.domain.ui.activities; 2 | 3 | import android.content.Intent; 4 | import android.os.Bundle; 5 | import android.support.v7.app.ActionBarActivity; 6 | import android.view.View; 7 | import android.widget.AdapterView; 8 | import android.widget.ListView; 9 | import android.widget.Toast; 10 | 11 | import java.util.List; 12 | 13 | import butterknife.Bind; 14 | import butterknife.ButterKnife; 15 | import io.jpotts18.android_mvp.R; 16 | import io.jpotts18.android_mvp.domain.models.Repo; 17 | import io.jpotts18.android_mvp.domain.repos.IRepoListView; 18 | import io.jpotts18.android_mvp.domain.repos.RepoAdapter; 19 | import io.jpotts18.android_mvp.domain.repos.RepoListPresenter; 20 | import retrofit.RetrofitError; 21 | import retrofit.client.Response; 22 | 23 | public class RepoListActivity extends ActionBarActivity implements IRepoListView, AdapterView.OnItemClickListener { 24 | 25 | /** 26 | * Questions: 27 | * 28 | * TODO: What should we do with Adapters? They are a structural pattern to present our views but they combine Views and Data 29 | * TODO: What about Input Validations? Should the Views or the Models know how to validate themselves? 30 | */ 31 | 32 | @Bind(R.id.repo_list_view) 33 | ListView listView; 34 | 35 | private RepoListPresenter presenter; 36 | 37 | @Override 38 | protected void onCreate(Bundle savedInstanceState) { 39 | super.onCreate(savedInstanceState); 40 | setContentView(R.layout.activity_repo_list); 41 | ButterKnife.bind(this); 42 | presenter = new RepoListPresenter(this); 43 | listView.setOnItemClickListener(this); 44 | } 45 | 46 | @Override 47 | protected void onResume() { 48 | super.onResume(); 49 | // TODO: Should the view get this data? 50 | Intent intent = getIntent(); 51 | String username = intent.getExtras().getString("username"); 52 | presenter.loadCommits(username); 53 | } 54 | 55 | @Override 56 | public void onReposLoadedSuccess(List list, Response response) { 57 | listView.setAdapter(new RepoAdapter(this, list)); 58 | } 59 | 60 | @Override 61 | public void onReposLoadedFailure(RetrofitError error) { 62 | Toast.makeText(this, error.getLocalizedMessage(), Toast.LENGTH_SHORT).show(); 63 | } 64 | 65 | @Override 66 | public void onItemClick(AdapterView adapterView, View view, int i, long l) { 67 | // TODO: Is there a better way to do this? 68 | Repo repo = (Repo) adapterView.getAdapter().getItem(i); 69 | Toast.makeText(this, String.format("Forked = %b", repo.fork), Toast.LENGTH_SHORT).show(); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /app/src/main/java/io/jpotts18/android_mvp/domain/ui/activities/RepoListFragmentActivity.java: -------------------------------------------------------------------------------- 1 | package io.jpotts18.android_mvp.domain.ui.activities; 2 | 3 | import android.support.v7.app.ActionBarActivity; 4 | import android.os.Bundle; 5 | import android.view.Menu; 6 | import android.view.MenuItem; 7 | 8 | import io.jpotts18.android_mvp.R; 9 | 10 | public class RepoListFragmentActivity extends ActionBarActivity { 11 | 12 | @Override 13 | protected void onCreate(Bundle savedInstanceState) { 14 | super.onCreate(savedInstanceState); 15 | setContentView(R.layout.activity_repo_list_fragment); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /app/src/main/java/io/jpotts18/android_mvp/domain/ui/fragments/RepoListFragment.java: -------------------------------------------------------------------------------- 1 | package io.jpotts18.android_mvp.domain.ui.fragments; 2 | 3 | import android.os.Bundle; 4 | import android.support.v4.app.Fragment; 5 | import android.view.LayoutInflater; 6 | import android.view.View; 7 | import android.view.ViewGroup; 8 | import android.widget.ListView; 9 | import android.widget.Toast; 10 | 11 | import java.util.List; 12 | 13 | import butterknife.Bind; 14 | import butterknife.ButterKnife; 15 | import io.jpotts18.android_mvp.R; 16 | import io.jpotts18.android_mvp.domain.models.Repo; 17 | import io.jpotts18.android_mvp.domain.repos.IRepoListView; 18 | import io.jpotts18.android_mvp.domain.repos.RepoAdapter; 19 | import io.jpotts18.android_mvp.domain.repos.RepoListPresenter; 20 | import retrofit.RetrofitError; 21 | import retrofit.client.Response; 22 | 23 | public class RepoListFragment extends Fragment implements IRepoListView { 24 | 25 | public RepoListFragment() {} 26 | 27 | @Bind(R.id.fragment_repo_list_view) 28 | ListView listView; 29 | 30 | private RepoListPresenter presenter; 31 | 32 | @Override 33 | public void onCreate(Bundle savedInstanceState) { 34 | super.onCreate(savedInstanceState); 35 | presenter = new RepoListPresenter(this); 36 | } 37 | 38 | @Override 39 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 40 | View view = inflater.inflate(R.layout.fragment_repo_list, container, false); 41 | ButterKnife.bind(this, view); 42 | return view; 43 | } 44 | 45 | @Override 46 | public void onResume() { 47 | super.onResume(); 48 | presenter.loadCommits("JakeWharton"); 49 | } 50 | 51 | @Override 52 | public void onReposLoadedSuccess(List list, Response response) { 53 | listView.setAdapter(new RepoAdapter(getActivity(), list)); 54 | } 55 | 56 | @Override 57 | public void onReposLoadedFailure(RetrofitError error) { 58 | Toast.makeText(getActivity(), error.getLocalizedMessage(), Toast.LENGTH_SHORT).show(); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_login.xml: -------------------------------------------------------------------------------- 1 | 9 | 10 | 15 | 16 | 22 | 23 |