├── app
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── drawable-mdpi
│ │ │ │ ├── octocat.png
│ │ │ │ ├── profile.jpg
│ │ │ │ ├── placeholder.png
│ │ │ │ └── ic_search_white_36dp.png
│ │ │ ├── drawable-xxhdpi
│ │ │ │ ├── octocat.png
│ │ │ │ ├── placeholder.png
│ │ │ │ └── ic_search_white_36dp.png
│ │ │ ├── mipmap-hdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── drawable-hdpi
│ │ │ │ └── ic_search_white_36dp.png
│ │ │ ├── drawable-xhdpi
│ │ │ │ └── ic_search_white_36dp.png
│ │ │ ├── drawable-xxxhdpi
│ │ │ │ └── ic_search_white_36dp.png
│ │ │ ├── values-v21
│ │ │ │ └── styles.xml
│ │ │ ├── values-w820dp
│ │ │ │ └── dimens.xml
│ │ │ ├── layout
│ │ │ │ ├── activity_search_user.xml
│ │ │ │ ├── activity_user_details.xml
│ │ │ │ ├── view_progressbar.xml
│ │ │ │ ├── fragment_user_details.xml
│ │ │ │ ├── item_user.xml
│ │ │ │ ├── fragment_search_user.xml
│ │ │ │ └── item_repo.xml
│ │ │ ├── menu
│ │ │ │ └── menu_main.xml
│ │ │ ├── values
│ │ │ │ ├── dimens.xml
│ │ │ │ ├── strings.xml
│ │ │ │ ├── colors.xml
│ │ │ │ └── styles.xml
│ │ │ └── layout-large
│ │ │ │ └── activity_search_user.xml
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── hugo
│ │ │ │ └── mvpsampleapplication
│ │ │ │ ├── utils
│ │ │ │ ├── ThreadExecutor.java
│ │ │ │ ├── PostExecutionThread.java
│ │ │ │ ├── dependencyinjection
│ │ │ │ │ ├── PerActivity.java
│ │ │ │ │ ├── components
│ │ │ │ │ │ ├── UserComponent.java
│ │ │ │ │ │ └── ApplicationComponent.java
│ │ │ │ │ └── modules
│ │ │ │ │ │ ├── UserModule.java
│ │ │ │ │ │ └── ApplicationModule.java
│ │ │ │ ├── UiThread.java
│ │ │ │ └── JobExecutor.java
│ │ │ │ ├── features
│ │ │ │ ├── Presenter.java
│ │ │ │ ├── DefaultSubscriber.java
│ │ │ │ ├── userdetails
│ │ │ │ │ ├── UserDetailsView.java
│ │ │ │ │ ├── LoadUserDetailsUseCase.java
│ │ │ │ │ ├── UserDetailsActivity.java
│ │ │ │ │ ├── UserDetailsPresenter.java
│ │ │ │ │ ├── RepositoriesAdapter.java
│ │ │ │ │ └── UserDetailsFragment.java
│ │ │ │ ├── searchuser
│ │ │ │ │ ├── SearchUserView.java
│ │ │ │ │ ├── SearchUserUseCase.java
│ │ │ │ │ ├── SearchUserActivity.java
│ │ │ │ │ ├── SearchUserPresenter.java
│ │ │ │ │ ├── UserListAdapter.java
│ │ │ │ │ └── SearchUserFragment.java
│ │ │ │ ├── UseCase.java
│ │ │ │ └── BaseActivity.java
│ │ │ │ ├── model
│ │ │ │ ├── network
│ │ │ │ │ ├── SearchResponse.java
│ │ │ │ │ └── GitHubService.java
│ │ │ │ └── entities
│ │ │ │ │ ├── User.java
│ │ │ │ │ └── Repository.java
│ │ │ │ └── app
│ │ │ │ └── MVPApplication.java
│ │ └── AndroidManifest.xml
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── hugo
│ │ │ └── mvpsampleapplication
│ │ │ ├── utils
│ │ │ ├── JobExecutorTest.java
│ │ │ └── UiThreadTest.java
│ │ │ ├── features
│ │ │ ├── searchuser
│ │ │ │ ├── UserListAdapterTest.java
│ │ │ │ ├── SearchUserUseCaseTest.java
│ │ │ │ └── SearchUserPresenterTest.java
│ │ │ ├── UseCaseTest.java
│ │ │ └── userdetails
│ │ │ │ ├── LoadUserDetailsUseCaseTest.java
│ │ │ │ └── UserDetailsPresenterTest.java
│ │ │ └── app
│ │ │ └── MVPApplicationTest.java
│ ├── androidTest
│ │ └── java
│ │ │ └── com
│ │ │ └── hugo
│ │ │ └── mvpsampleapplication
│ │ │ ├── OrientationChangeAction.java
│ │ │ └── features
│ │ │ ├── userdetails
│ │ │ └── UserDetailsActivityTest.java
│ │ │ └── searchuser
│ │ │ └── SearchUserActivityTest.java
│ └── sharedTest
│ │ └── java
│ │ └── MockFactory.java
├── proguard-rules.pro
└── build.gradle
├── .idea
├── .name
├── copyright
│ └── profiles_settings.xml
├── vcs.xml
├── inspectionProfiles
│ ├── profiles_settings.xml
│ └── Project_Default.xml
├── modules.xml
├── runConfigurations.xml
├── gradle.xml
├── compiler.xml
└── misc.xml
├── settings.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── gradle.properties
├── gradlew.bat
└── gradlew
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/.idea/.name:
--------------------------------------------------------------------------------
1 | MVPSampleApplication
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------
/.idea/copyright/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hugokallstrom/MVPSampleApplication/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/workspace.xml
5 | /.idea/libraries
6 | .DS_Store
7 | /build
8 | /captures
9 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-mdpi/octocat.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hugokallstrom/MVPSampleApplication/HEAD/app/src/main/res/drawable-mdpi/octocat.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-mdpi/profile.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hugokallstrom/MVPSampleApplication/HEAD/app/src/main/res/drawable-mdpi/profile.jpg
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/octocat.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hugokallstrom/MVPSampleApplication/HEAD/app/src/main/res/drawable-xxhdpi/octocat.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hugokallstrom/MVPSampleApplication/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hugokallstrom/MVPSampleApplication/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-mdpi/placeholder.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hugokallstrom/MVPSampleApplication/HEAD/app/src/main/res/drawable-mdpi/placeholder.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hugokallstrom/MVPSampleApplication/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hugokallstrom/MVPSampleApplication/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/placeholder.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hugokallstrom/MVPSampleApplication/HEAD/app/src/main/res/drawable-xxhdpi/placeholder.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hugokallstrom/MVPSampleApplication/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-hdpi/ic_search_white_36dp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hugokallstrom/MVPSampleApplication/HEAD/app/src/main/res/drawable-hdpi/ic_search_white_36dp.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-mdpi/ic_search_white_36dp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hugokallstrom/MVPSampleApplication/HEAD/app/src/main/res/drawable-mdpi/ic_search_white_36dp.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xhdpi/ic_search_white_36dp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hugokallstrom/MVPSampleApplication/HEAD/app/src/main/res/drawable-xhdpi/ic_search_white_36dp.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/ic_search_white_36dp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hugokallstrom/MVPSampleApplication/HEAD/app/src/main/res/drawable-xxhdpi/ic_search_white_36dp.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxxhdpi/ic_search_white_36dp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hugokallstrom/MVPSampleApplication/HEAD/app/src/main/res/drawable-xxxhdpi/ic_search_white_36dp.png
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/java/com/hugo/mvpsampleapplication/utils/ThreadExecutor.java:
--------------------------------------------------------------------------------
1 | package com.hugo.mvpsampleapplication.utils;
2 |
3 | import rx.Scheduler;
4 |
5 | public interface ThreadExecutor {
6 | Scheduler getScheduler();
7 | }
8 |
--------------------------------------------------------------------------------
/app/src/main/java/com/hugo/mvpsampleapplication/utils/PostExecutionThread.java:
--------------------------------------------------------------------------------
1 | package com.hugo.mvpsampleapplication.utils;
2 |
3 | import rx.Scheduler;
4 |
5 | public interface PostExecutionThread {
6 | Scheduler getScheduler();
7 | }
8 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Wed Oct 21 11:34:03 PDT 2015
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-2.8-all.zip
7 |
--------------------------------------------------------------------------------
/app/src/main/java/com/hugo/mvpsampleapplication/features/Presenter.java:
--------------------------------------------------------------------------------
1 | package com.hugo.mvpsampleapplication.features;
2 |
3 | public interface Presenter {
4 |
5 | void attachView(V view);
6 |
7 | void detachView();
8 |
9 | void destroy(boolean unsubscribe);
10 | }
11 |
--------------------------------------------------------------------------------
/.idea/inspectionProfiles/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/java/com/hugo/mvpsampleapplication/utils/dependencyinjection/PerActivity.java:
--------------------------------------------------------------------------------
1 | package com.hugo.mvpsampleapplication.utils.dependencyinjection;
2 |
3 | import java.lang.annotation.Retention;
4 | import javax.inject.Scope;
5 |
6 | import static java.lang.annotation.RetentionPolicy.RUNTIME;
7 |
8 | @Scope
9 | @Retention(RUNTIME)
10 | public @interface PerActivity {}
--------------------------------------------------------------------------------
/app/src/main/res/values-v21/styles.xml:
--------------------------------------------------------------------------------
1 | >
2 |
3 |
9 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.idea/inspectionProfiles/Project_Default.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_search_user.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_user_details.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/view_progressbar.xml:
--------------------------------------------------------------------------------
1 |
7 |
8 |
12 |
13 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/menu_main.xml:
--------------------------------------------------------------------------------
1 |
11 |
--------------------------------------------------------------------------------
/app/src/main/java/com/hugo/mvpsampleapplication/utils/UiThread.java:
--------------------------------------------------------------------------------
1 | package com.hugo.mvpsampleapplication.utils;
2 |
3 | import javax.inject.Inject;
4 | import rx.Scheduler;
5 | import rx.android.schedulers.AndroidSchedulers;
6 |
7 | /**
8 | * Created by hugo on 2/13/16.
9 | */
10 | public class UiThread implements PostExecutionThread {
11 |
12 | @Inject
13 | public UiThread() {
14 |
15 | }
16 |
17 | @Override
18 | public Scheduler getScheduler() {
19 | return AndroidSchedulers.mainThread();
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/app/src/main/java/com/hugo/mvpsampleapplication/features/DefaultSubscriber.java:
--------------------------------------------------------------------------------
1 | package com.hugo.mvpsampleapplication.features;
2 |
3 | /**
4 | * Created by hugo on 1/29/16.
5 | */
6 | public class DefaultSubscriber extends rx.Subscriber {
7 | @Override public void onCompleted() {
8 | // no-op by default.
9 | }
10 |
11 | @Override public void onError(Throwable e) {
12 | // no-op by default.
13 | }
14 |
15 | @Override public void onNext(T t) {
16 | // no-op by default.
17 | }
18 | }
--------------------------------------------------------------------------------
/app/src/test/java/com/hugo/mvpsampleapplication/utils/JobExecutorTest.java:
--------------------------------------------------------------------------------
1 | package com.hugo.mvpsampleapplication.utils;
2 |
3 | import org.junit.Test;
4 | import rx.schedulers.Schedulers;
5 |
6 | import static org.junit.Assert.assertEquals;
7 |
8 | /**
9 | * Created by hugo on 2/25/16.
10 | */
11 | public class JobExecutorTest {
12 |
13 | @Test
14 | public void getSchedulerShouldReturnIoScheduler() {
15 | JobExecutor jobExecutor = new JobExecutor();
16 | assertEquals(Schedulers.io(), jobExecutor.getScheduler());
17 | }
18 | }
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 12dp
4 | 12dp
5 | 6dp
6 | 6dp
7 |
8 | 16dp
9 | 16dp
10 |
11 |
--------------------------------------------------------------------------------
/app/src/test/java/com/hugo/mvpsampleapplication/utils/UiThreadTest.java:
--------------------------------------------------------------------------------
1 | package com.hugo.mvpsampleapplication.utils;
2 |
3 | import org.junit.Test;
4 | import rx.android.schedulers.AndroidSchedulers;
5 |
6 | import static org.junit.Assert.assertEquals;
7 |
8 | /**
9 | * Created by hugo on 2/25/16.
10 | */
11 | public class UiThreadTest {
12 |
13 | @Test
14 | public void getSchedulerShouldReturnAndroidSchedulersMainThread() {
15 | UiThread uiThread = new UiThread();
16 | assertEquals(AndroidSchedulers.mainThread(), uiThread.getScheduler());
17 | }
18 | }
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Github Browser
3 | Settings
4 |
5 |
6 | Hello blank fragment
7 | Repos
8 | Hireable
9 | Not Hireable
10 | Github Username
11 | UserDetailsActivity
12 |
13 |
--------------------------------------------------------------------------------
/app/src/main/java/com/hugo/mvpsampleapplication/features/userdetails/UserDetailsView.java:
--------------------------------------------------------------------------------
1 | package com.hugo.mvpsampleapplication.features.userdetails;
2 |
3 | import android.content.Context;
4 |
5 | import com.hugo.mvpsampleapplication.model.entities.Repository;
6 |
7 | import java.util.List;
8 |
9 | /**
10 | * Created by hugo on 1/26/16.
11 | */
12 | public interface UserDetailsView {
13 |
14 | void showMessage(String message);
15 |
16 | void showProgressIndicator();
17 |
18 | void hideProgressIndicator();
19 |
20 | void showRepositories(List repositories);
21 |
22 | Context getContext();
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/app/src/main/java/com/hugo/mvpsampleapplication/features/searchuser/SearchUserView.java:
--------------------------------------------------------------------------------
1 | package com.hugo.mvpsampleapplication.features.searchuser;
2 |
3 | import android.content.Context;
4 |
5 | import com.hugo.mvpsampleapplication.model.entities.User;
6 |
7 | import java.util.List;
8 |
9 | /**
10 | * Created by hugo on 1/22/16.
11 | */
12 | public interface SearchUserView {
13 |
14 | void showMessage(String message);
15 |
16 | void showProgressIndicator();
17 |
18 | void hideProgressIndicator();
19 |
20 | void showUsers(List users);
21 |
22 | void startUserDetailsActivity(String username);
23 |
24 | Context getContext();
25 | }
26 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/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 /home/hugo/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 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_user_details.xml:
--------------------------------------------------------------------------------
1 |
6 |
7 |
14 |
15 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/app/src/main/java/com/hugo/mvpsampleapplication/utils/dependencyinjection/components/UserComponent.java:
--------------------------------------------------------------------------------
1 | package com.hugo.mvpsampleapplication.utils.dependencyinjection.components;
2 |
3 | import com.hugo.mvpsampleapplication.features.searchuser.SearchUserFragment;
4 | import com.hugo.mvpsampleapplication.features.userdetails.UserDetailsFragment;
5 | import com.hugo.mvpsampleapplication.utils.dependencyinjection.PerActivity;
6 | import com.hugo.mvpsampleapplication.utils.dependencyinjection.modules.UserModule;
7 | import dagger.Component;
8 |
9 | @PerActivity
10 | @Component(dependencies = ApplicationComponent.class, modules = UserModule.class)
11 | public interface UserComponent {
12 | void inject(SearchUserFragment searchUserFragment);
13 | void inject(UserDetailsFragment userDetailsFragment);
14 | }
15 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 | #000000
7 | #ffffff
8 | #75ffffff
9 | #e1e1e1
10 | #3F51B5
11 | #303F9F
12 | #C5CAE9
13 | #03A9F4
14 | #212121
15 | #727272
16 | #FFFFFF
17 | #cbcbcb
18 |
19 |
--------------------------------------------------------------------------------
/app/src/main/java/com/hugo/mvpsampleapplication/model/network/SearchResponse.java:
--------------------------------------------------------------------------------
1 | package com.hugo.mvpsampleapplication.model.network;
2 |
3 | import com.google.gson.annotations.SerializedName;
4 | import com.hugo.mvpsampleapplication.model.entities.User;
5 |
6 | import java.util.ArrayList;
7 | import java.util.List;
8 |
9 | /**
10 | * Created by hugo on 1/26/16.
11 | */
12 | public class SearchResponse {
13 | @SerializedName("total_count")
14 | private int totalCount;
15 | @SerializedName("incomplete_results")
16 | private boolean incompleteResults;
17 | @SerializedName("items")
18 | private List users = new ArrayList();
19 |
20 | public SearchResponse() {}
21 |
22 | public List getUsers() {
23 | return users;
24 | }
25 |
26 | public void setUsers(ArrayList users) {
27 | this.users = users;
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/app/src/main/java/com/hugo/mvpsampleapplication/utils/dependencyinjection/components/ApplicationComponent.java:
--------------------------------------------------------------------------------
1 | package com.hugo.mvpsampleapplication.utils.dependencyinjection.components;
2 |
3 | import com.hugo.mvpsampleapplication.features.BaseActivity;
4 | import com.hugo.mvpsampleapplication.model.network.GitHubService;
5 | import com.hugo.mvpsampleapplication.utils.PostExecutionThread;
6 | import com.hugo.mvpsampleapplication.utils.ThreadExecutor;
7 | import com.hugo.mvpsampleapplication.utils.dependencyinjection.modules.ApplicationModule;
8 | import dagger.Component;
9 | import javax.inject.Singleton;
10 |
11 | @Singleton
12 | @Component(modules = ApplicationModule.class)
13 | public interface ApplicationComponent {
14 | void inject(BaseActivity baseActivity);
15 |
16 | GitHubService gitHubService();
17 | ThreadExecutor threadExecutor();
18 | PostExecutionThread postExecutionThread();
19 |
20 | }
21 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
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
--------------------------------------------------------------------------------
/app/src/main/java/com/hugo/mvpsampleapplication/features/searchuser/SearchUserUseCase.java:
--------------------------------------------------------------------------------
1 | package com.hugo.mvpsampleapplication.features.searchuser;
2 |
3 | import com.hugo.mvpsampleapplication.features.UseCase;
4 | import com.hugo.mvpsampleapplication.model.network.GitHubService;
5 |
6 | import com.hugo.mvpsampleapplication.utils.PostExecutionThread;
7 | import com.hugo.mvpsampleapplication.utils.ThreadExecutor;
8 | import javax.inject.Inject;
9 | import rx.Observable;
10 |
11 | public class SearchUserUseCase extends UseCase {
12 |
13 | @Inject
14 | public SearchUserUseCase(GitHubService gitHubService, ThreadExecutor threadExecutor,
15 | PostExecutionThread postExecutionThread) {
16 | super(gitHubService, threadExecutor, postExecutionThread);
17 | }
18 |
19 | @Override
20 | public Observable buildUseCase(String username) throws NullPointerException {
21 | if (username == null) {
22 | throw new NullPointerException("Query must not be null");
23 | }
24 | return getGitHubService().searchUser(username);
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/java/com/hugo/mvpsampleapplication/features/userdetails/LoadUserDetailsUseCase.java:
--------------------------------------------------------------------------------
1 | package com.hugo.mvpsampleapplication.features.userdetails;
2 |
3 | import com.hugo.mvpsampleapplication.features.UseCase;
4 | import com.hugo.mvpsampleapplication.model.network.GitHubService;
5 |
6 | import com.hugo.mvpsampleapplication.utils.PostExecutionThread;
7 | import com.hugo.mvpsampleapplication.utils.ThreadExecutor;
8 | import javax.inject.Inject;
9 | import rx.Observable;
10 |
11 | public class LoadUserDetailsUseCase extends UseCase {
12 |
13 |
14 | @Inject
15 | public LoadUserDetailsUseCase(GitHubService gitHubService, ThreadExecutor threadExecutor,
16 | PostExecutionThread postExecutionThread) {
17 | super(gitHubService, threadExecutor, postExecutionThread);
18 | }
19 |
20 | @Override public Observable buildUseCase(String username) throws NullPointerException {
21 | if (username == null) {
22 | throw new NullPointerException("Username must not be null");
23 | }
24 | return getGitHubService().getRepositoriesFromUser(username);
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/test/java/com/hugo/mvpsampleapplication/features/searchuser/UserListAdapterTest.java:
--------------------------------------------------------------------------------
1 | package com.hugo.mvpsampleapplication.features.searchuser;
2 |
3 | import com.hugo.mvpsampleapplication.MockFactory;
4 | import com.hugo.mvpsampleapplication.model.entities.User;
5 | import java.util.ArrayList;
6 | import java.util.List;
7 | import org.junit.Before;
8 | import org.junit.Test;
9 | import org.junit.runner.RunWith;
10 | import org.mockito.runners.MockitoJUnitRunner;
11 |
12 | import static org.junit.Assert.*;
13 |
14 | @RunWith(MockitoJUnitRunner.class)
15 | public class UserListAdapterTest {
16 |
17 | private List users = new ArrayList<>();
18 | private UserListAdapter userListAdapter;
19 |
20 | @Before
21 | public void setUp() {
22 | User user = MockFactory.buildMockUser();
23 | users.add(user);
24 | userListAdapter = new UserListAdapter();
25 | }
26 |
27 | @Test
28 | public void getItemCountShouldReturnNumberOfUsersInList() {
29 | userListAdapter.setUsers(users);
30 | int itemCount = userListAdapter.getItemCount();
31 | assertEquals(users.size(), itemCount);
32 | }
33 |
34 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/hugo/mvpsampleapplication/utils/JobExecutor.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2015 Fernando Cejas Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.hugo.mvpsampleapplication.utils;
17 |
18 | import javax.inject.Inject;
19 | import rx.Scheduler;
20 | import rx.schedulers.Schedulers;
21 |
22 | public class JobExecutor implements ThreadExecutor {
23 |
24 | @Inject
25 | public JobExecutor() {
26 |
27 | }
28 |
29 | @Override
30 | public Scheduler getScheduler() {
31 | return Schedulers.io();
32 | }
33 |
34 | }
--------------------------------------------------------------------------------
/app/src/main/res/layout-large/activity_search_user.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
17 |
18 |
24 |
--------------------------------------------------------------------------------
/app/src/main/java/com/hugo/mvpsampleapplication/features/userdetails/UserDetailsActivity.java:
--------------------------------------------------------------------------------
1 | package com.hugo.mvpsampleapplication.features.userdetails;
2 |
3 | import android.content.Context;
4 | import android.content.Intent;
5 | import android.os.Bundle;
6 |
7 | import com.hugo.mvpsampleapplication.R;
8 | import com.hugo.mvpsampleapplication.features.BaseActivity;
9 |
10 | public class UserDetailsActivity extends BaseActivity {
11 |
12 | private static final String EXTRA_USERNAME = "USERNAME";
13 |
14 | public static Intent newIntent(Context context, String username) {
15 | Intent intent = new Intent(context, UserDetailsActivity.class);
16 | intent.putExtra(EXTRA_USERNAME, username);
17 | return intent;
18 | }
19 |
20 | @Override protected void onCreate(Bundle savedInstanceState) {
21 | super.onCreate(savedInstanceState);
22 | setContentView(R.layout.activity_user_details);
23 | String username = getIntent().getStringExtra(EXTRA_USERNAME);
24 | if (getFragmentManager().findFragmentById(R.id.content_activity_user_details) == null) {
25 | addFragment(R.id.content_activity_user_details, UserDetailsFragment.newInstance(username));
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/app/src/test/java/com/hugo/mvpsampleapplication/app/MVPApplicationTest.java:
--------------------------------------------------------------------------------
1 | package com.hugo.mvpsampleapplication.app;
2 |
3 | import com.hugo.mvpsampleapplication.utils.dependencyinjection.components.ApplicationComponent;
4 | import org.junit.Before;
5 | import org.junit.Test;
6 | import org.mockito.Mock;
7 | import org.mockito.MockitoAnnotations;
8 |
9 | import static org.junit.Assert.*;
10 |
11 | /**
12 | * Created by hugo on 2/25/16.
13 | */
14 | public class MVPApplicationTest {
15 |
16 | @Mock
17 | private ApplicationComponent applicationComponent;
18 | private MVPApplication mvpApplication;
19 |
20 | @Before
21 | public void setUp() {
22 | MockitoAnnotations.initMocks(this);
23 | mvpApplication = new MVPApplication();
24 | }
25 |
26 | @Test
27 | public void testSetAndGetApplicationComponent() throws Exception {
28 | ApplicationComponent nullApplicationComponent = mvpApplication.getApplicationComponent();
29 | assertNull(nullApplicationComponent);
30 |
31 | mvpApplication.setApplicationComponent(applicationComponent);
32 | applicationComponent = mvpApplication.getApplicationComponent();
33 | assertNotNull(applicationComponent);
34 | }
35 |
36 |
37 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/hugo/mvpsampleapplication/model/entities/User.java:
--------------------------------------------------------------------------------
1 | package com.hugo.mvpsampleapplication.model.entities;
2 |
3 | import com.google.gson.annotations.SerializedName;
4 |
5 | /**
6 | * Created by hugo on 1/22/16.
7 | */
8 | public class User {
9 |
10 | private long id;
11 | private String login;
12 | @SerializedName("avatar_url")
13 | private String avatarUrl;
14 | @SerializedName("repos_url")
15 | private String reposUrl;
16 |
17 | public User() {}
18 |
19 | public long getId() {
20 | return id;
21 | }
22 |
23 | public void setId(int id) {
24 | this.id = id;
25 | }
26 |
27 | public String getLogin() {
28 | return login;
29 | }
30 |
31 | public void setLogin(String login) {
32 | this.login = login;
33 | }
34 |
35 | public String getAvatarUrl() {
36 | return avatarUrl;
37 | }
38 |
39 | public void setAvatarUrl(String avatarUrl) {
40 | this.avatarUrl = avatarUrl;
41 | }
42 |
43 | public String getReposUrl() {
44 | return reposUrl;
45 | }
46 |
47 | public void setReposUrl(String reposUrl) {
48 | this.reposUrl = reposUrl;
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/app/src/main/java/com/hugo/mvpsampleapplication/app/MVPApplication.java:
--------------------------------------------------------------------------------
1 | package com.hugo.mvpsampleapplication.app;
2 |
3 | import android.app.Application;
4 | import android.content.Context;
5 |
6 | import com.hugo.mvpsampleapplication.model.network.GitHubService;
7 |
8 | import com.hugo.mvpsampleapplication.utils.dependencyinjection.components.ApplicationComponent;
9 | import com.hugo.mvpsampleapplication.utils.dependencyinjection.components.DaggerApplicationComponent;
10 | import com.hugo.mvpsampleapplication.utils.dependencyinjection.modules.ApplicationModule;
11 | import rx.Scheduler;
12 | import rx.schedulers.Schedulers;
13 |
14 | public class MVPApplication extends Application {
15 |
16 | private ApplicationComponent applicationComponent;
17 |
18 | @Override
19 | public void onCreate() {
20 | super.onCreate();
21 | if(applicationComponent == null) {
22 | applicationComponent =
23 | DaggerApplicationComponent.builder().applicationModule(new ApplicationModule()).build();
24 | }
25 | }
26 |
27 | public void setApplicationComponent(ApplicationComponent applicationComponent) {
28 | this.applicationComponent = applicationComponent;
29 | }
30 |
31 | public ApplicationComponent getApplicationComponent() {
32 | return applicationComponent;
33 | }
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
14 |
15 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
30 |
31 |
32 |
33 |
34 |
--------------------------------------------------------------------------------
/app/src/main/java/com/hugo/mvpsampleapplication/model/network/GitHubService.java:
--------------------------------------------------------------------------------
1 | package com.hugo.mvpsampleapplication.model.network;
2 |
3 | import com.hugo.mvpsampleapplication.model.entities.Repository;
4 |
5 | import java.util.List;
6 |
7 | import retrofit.GsonConverterFactory;
8 | import retrofit.Retrofit;
9 | import retrofit.RxJavaCallAdapterFactory;
10 | import retrofit.http.GET;
11 | import retrofit.http.Path;
12 | import retrofit.http.Query;
13 | import rx.Observable;
14 |
15 | public interface GitHubService {
16 |
17 | String endpoint = "https://api.github.com/";
18 | String getRepoUrl = "users/{username}/repos";
19 | String searchUserUrl = "search/users";
20 |
21 | @GET(getRepoUrl)
22 | Observable> getRepositoriesFromUser(@Path("username") String username);
23 |
24 | @GET(searchUserUrl)
25 | Observable searchUser(@Query("q") String username);
26 |
27 | class Factory {
28 | public static GitHubService create() {
29 | Retrofit retrofit = new Retrofit.Builder()
30 | .baseUrl(endpoint)
31 | .addConverterFactory(GsonConverterFactory.create())
32 | .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
33 | .build();
34 | return retrofit.create(GitHubService.class);
35 | }
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/app/src/main/java/com/hugo/mvpsampleapplication/features/searchuser/SearchUserActivity.java:
--------------------------------------------------------------------------------
1 | package com.hugo.mvpsampleapplication.features.searchuser;
2 |
3 | import android.os.Bundle;
4 |
5 | import android.util.Log;
6 | import com.hugo.mvpsampleapplication.R;
7 | import com.hugo.mvpsampleapplication.features.BaseActivity;
8 | import com.hugo.mvpsampleapplication.features.userdetails.UserDetailsActivity;
9 | import com.hugo.mvpsampleapplication.features.userdetails.UserDetailsFragment;
10 |
11 | public class SearchUserActivity extends BaseActivity
12 | implements SearchUserFragment.ActivityListener {
13 |
14 | @Override
15 | protected void onCreate(Bundle savedInstanceState) {
16 | super.onCreate(savedInstanceState);
17 | setContentView(R.layout.activity_search_user);
18 | if (savedInstanceState == null) {
19 | addFragment(R.id.content_activity_search_user, SearchUserFragment.newInstance());
20 | }
21 | }
22 |
23 | @Override
24 | public void startUserDetails(String username) {
25 | UserDetailsFragment userDetailsFragment =
26 | (UserDetailsFragment) getSupportFragmentManager().findFragmentById(
27 | R.id.userDetailsFragment);
28 | if (userDetailsFragment == null) {
29 | Log.d("activity", "starting user details " + username);
30 | startActivity(UserDetailsActivity.newIntent(this, username));
31 | } else {
32 | userDetailsFragment.loadRepositories(username);
33 | }
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/app/src/main/java/com/hugo/mvpsampleapplication/utils/dependencyinjection/modules/UserModule.java:
--------------------------------------------------------------------------------
1 | package com.hugo.mvpsampleapplication.utils.dependencyinjection.modules;
2 |
3 |
4 | import com.hugo.mvpsampleapplication.features.UseCase;
5 | import com.hugo.mvpsampleapplication.features.searchuser.SearchUserUseCase;
6 | import com.hugo.mvpsampleapplication.features.userdetails.LoadUserDetailsUseCase;
7 | import com.hugo.mvpsampleapplication.model.network.GitHubService;
8 | import com.hugo.mvpsampleapplication.utils.PostExecutionThread;
9 | import com.hugo.mvpsampleapplication.utils.ThreadExecutor;
10 | import com.hugo.mvpsampleapplication.utils.dependencyinjection.PerActivity;
11 | import dagger.Module;
12 | import dagger.Provides;
13 | import javax.inject.Named;
14 |
15 | @Module
16 | public class UserModule {
17 |
18 | public UserModule() {
19 | }
20 |
21 | @Provides
22 | @PerActivity
23 | @Named("searchUser")
24 | public UseCase provideSearchUserUseCase(GitHubService gitHubService, ThreadExecutor threadExecutor,
25 | PostExecutionThread postExecutionThread) {
26 | return new SearchUserUseCase(gitHubService, threadExecutor, postExecutionThread);
27 | }
28 |
29 | @Provides
30 | @PerActivity
31 | @Named("userDetails")
32 | public UseCase provideUserDetailsUseCase(GitHubService gitHubService, ThreadExecutor threadExecutor,
33 | PostExecutionThread postExecutionThread) {
34 | return new LoadUserDetailsUseCase(gitHubService, threadExecutor, postExecutionThread);
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/app/src/main/java/com/hugo/mvpsampleapplication/utils/dependencyinjection/modules/ApplicationModule.java:
--------------------------------------------------------------------------------
1 | package com.hugo.mvpsampleapplication.utils.dependencyinjection.modules;
2 |
3 | import com.hugo.mvpsampleapplication.model.network.GitHubService;
4 | import com.hugo.mvpsampleapplication.utils.JobExecutor;
5 | import com.hugo.mvpsampleapplication.utils.PostExecutionThread;
6 | import com.hugo.mvpsampleapplication.utils.ThreadExecutor;
7 | import com.hugo.mvpsampleapplication.utils.UiThread;
8 | import dagger.Module;
9 | import dagger.Provides;
10 | import javax.inject.Singleton;
11 | import retrofit.GsonConverterFactory;
12 | import retrofit.Retrofit;
13 | import retrofit.RxJavaCallAdapterFactory;
14 |
15 | @Module
16 | public class ApplicationModule {
17 |
18 | public ApplicationModule() {
19 |
20 | }
21 |
22 | @Provides
23 | @Singleton
24 | public GitHubService provideGitHubService() {
25 | Retrofit retrofit = new Retrofit.Builder().baseUrl("https://api.github.com/")
26 | .addConverterFactory(GsonConverterFactory.create())
27 | .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
28 | .build();
29 | return retrofit.create(GitHubService.class);
30 | }
31 |
32 | @Provides
33 | @Singleton
34 | public ThreadExecutor provideThreadExecutor(JobExecutor jobExecutor) {
35 | return jobExecutor;
36 | }
37 |
38 | @Provides
39 | @Singleton
40 | public PostExecutionThread providePostExecutionThread(UiThread uiThread) {
41 | return uiThread;
42 | }
43 |
44 | }
45 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
15 |
16 |
23 |
24 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
--------------------------------------------------------------------------------
/app/src/main/java/com/hugo/mvpsampleapplication/features/UseCase.java:
--------------------------------------------------------------------------------
1 | package com.hugo.mvpsampleapplication.features;
2 |
3 | import com.hugo.mvpsampleapplication.model.network.GitHubService;
4 | import com.hugo.mvpsampleapplication.utils.PostExecutionThread;
5 | import com.hugo.mvpsampleapplication.utils.ThreadExecutor;
6 | import rx.Observable;
7 | import rx.Subscriber;
8 | import rx.Subscription;
9 | import rx.subscriptions.Subscriptions;
10 |
11 | public abstract class UseCase {
12 |
13 | private final GitHubService gitHubService;
14 | private final ThreadExecutor threadExecutor;
15 | private final PostExecutionThread postExecutionThread;
16 | private Subscription subscription = Subscriptions.empty();
17 |
18 | public UseCase(GitHubService gitHubService, ThreadExecutor threadExecutor,
19 | PostExecutionThread postExecutionThread) {
20 | this.gitHubService = gitHubService;
21 | this.threadExecutor = threadExecutor;
22 | this.postExecutionThread = postExecutionThread;
23 | }
24 |
25 | public GitHubService getGitHubService() {
26 | return gitHubService;
27 | }
28 |
29 | public Subscription getSubscription() {
30 | return subscription;
31 | }
32 |
33 | public abstract Observable buildUseCase(String query);
34 |
35 | @SuppressWarnings("unchecked") public void execute(Subscriber useCaseSubscriber, String query) {
36 | subscription = buildUseCase(query)
37 | .observeOn(postExecutionThread.getScheduler())
38 | .subscribeOn(threadExecutor.getScheduler())
39 | .subscribe(useCaseSubscriber);
40 | }
41 |
42 | public void unsubscribe() {
43 | if (subscription != null && !subscription.isUnsubscribed()) {
44 | subscription.unsubscribe();
45 | }
46 | }
47 | }
48 |
49 |
--------------------------------------------------------------------------------
/app/src/main/java/com/hugo/mvpsampleapplication/model/entities/Repository.java:
--------------------------------------------------------------------------------
1 | package com.hugo.mvpsampleapplication.model.entities;
2 |
3 | import com.google.gson.annotations.SerializedName;
4 |
5 | /**
6 | * Created by hugo on 1/22/16.
7 | */
8 | public class Repository {
9 |
10 | private int id;
11 | private String name;
12 | private String description;
13 | private String language;
14 | private int forks;
15 | private int watchers;
16 | @SerializedName("stargazers_count")
17 | private int stars;
18 |
19 | public Repository() {}
20 |
21 | public int getId() {
22 | return id;
23 | }
24 |
25 | public void setId(int id) {
26 | this.id = id;
27 | }
28 |
29 | public String getName() {
30 | return name;
31 | }
32 |
33 | public void setName(String name) {
34 | this.name = name;
35 | }
36 |
37 | public String getDescription() {
38 | return description;
39 | }
40 |
41 | public void setDescription(String description) {
42 | this.description = description;
43 | }
44 |
45 | public String getLanguage() {
46 | return language;
47 | }
48 |
49 | public void setLanguage(String language) {
50 | this.language = language;
51 | }
52 |
53 | public int getForks() {
54 | return forks;
55 | }
56 |
57 | public void setForks(int forks) {
58 | this.forks = forks;
59 | }
60 |
61 | public int getWatchers() {
62 | return watchers;
63 | }
64 |
65 | public void setWatchers(int watchers) {
66 | this.watchers = watchers;
67 | }
68 |
69 | public int getStars() {
70 | return stars;
71 | }
72 |
73 | public void setStars(int stars) {
74 | this.stars = stars;
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/app/src/main/java/com/hugo/mvpsampleapplication/features/BaseActivity.java:
--------------------------------------------------------------------------------
1 | package com.hugo.mvpsampleapplication.features;
2 |
3 | import android.os.Bundle;
4 | import android.support.v4.app.FragmentTransaction;
5 | import android.support.v7.app.AppCompatActivity;
6 | import android.support.v4.app.Fragment;
7 | import com.hugo.mvpsampleapplication.app.MVPApplication;
8 | import com.hugo.mvpsampleapplication.utils.dependencyinjection.components.ApplicationComponent;
9 | import com.hugo.mvpsampleapplication.utils.dependencyinjection.components.DaggerUserComponent;
10 | import com.hugo.mvpsampleapplication.utils.dependencyinjection.components.UserComponent;
11 | import com.hugo.mvpsampleapplication.utils.dependencyinjection.modules.UserModule;
12 |
13 | /**
14 | * Created by hugo on 1/22/16.
15 | */
16 | public abstract class BaseActivity extends AppCompatActivity {
17 |
18 | private UserComponent userComponent;
19 |
20 | @Override
21 | protected void onCreate(Bundle savedInstanceState) {
22 | super.onCreate(savedInstanceState);
23 | getApplicationComponent().inject(this);
24 | initializeInject();
25 | }
26 |
27 | protected void addFragment(int containerId, Fragment fragment) {
28 | FragmentTransaction fragmentTransaction = this.getSupportFragmentManager().beginTransaction();
29 | fragmentTransaction.add(containerId, fragment);
30 | fragmentTransaction.commit();
31 | }
32 |
33 | public ApplicationComponent getApplicationComponent() {
34 | return ((MVPApplication) getApplication()).getApplicationComponent();
35 | }
36 |
37 | private void initializeInject() {
38 | userComponent = DaggerUserComponent.builder()
39 | .applicationComponent(getApplicationComponent())
40 | .userModule(new UserModule())
41 | .build();
42 | }
43 |
44 | public UserComponent getUserComponent() {
45 | return userComponent;
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/item_user.xml:
--------------------------------------------------------------------------------
1 |
2 |
13 |
14 |
20 |
21 |
31 |
32 |
44 |
45 |
46 |
47 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/app/src/test/java/com/hugo/mvpsampleapplication/features/UseCaseTest.java:
--------------------------------------------------------------------------------
1 | package com.hugo.mvpsampleapplication.features;
2 |
3 | import com.hugo.mvpsampleapplication.MockFactory;
4 | import com.hugo.mvpsampleapplication.model.network.GitHubService;
5 | import com.hugo.mvpsampleapplication.model.network.SearchResponse;
6 | import com.hugo.mvpsampleapplication.utils.PostExecutionThread;
7 | import com.hugo.mvpsampleapplication.utils.ThreadExecutor;
8 | import org.junit.Before;
9 | import org.junit.Test;
10 | import org.mockito.Mock;
11 | import org.mockito.MockitoAnnotations;
12 | import rx.Observable;
13 | import rx.Subscription;
14 | import rx.observers.TestSubscriber;
15 | import rx.schedulers.Schedulers;
16 |
17 | import static org.junit.Assert.*;
18 | import static org.mockito.Mockito.when;
19 |
20 | /**
21 | * Created by hugo on 2/25/16.
22 | */
23 | public class UseCaseTest {
24 | @Mock
25 | private GitHubService gitHubService;
26 | @Mock
27 | private PostExecutionThread mockPostExecutionThread;
28 | @Mock
29 | private ThreadExecutor mockThreadExecutor;
30 |
31 | private UseCase useCase;
32 |
33 | @Before
34 | public void setUp() {
35 | MockitoAnnotations.initMocks(this);
36 | when(mockPostExecutionThread.getScheduler()).thenReturn(Schedulers.immediate());
37 | when(mockThreadExecutor.getScheduler()).thenReturn(Schedulers.immediate());
38 | useCase = new UseCase(gitHubService, mockThreadExecutor, mockPostExecutionThread) {
39 | @Override public Observable buildUseCase(String query) {
40 | return Observable.just(MockFactory.buildMockSearchResponse());
41 | }
42 | };
43 | }
44 |
45 | @Test
46 | public void buildUseCaseShouldReturnObservable() throws Exception {
47 | Observable observable = useCase.buildUseCase(MockFactory.TEST_USERNAME);
48 | assertNotNull(observable);
49 | }
50 | @Test
51 | public void getGitHubServiceShouldNotReturnNull() throws Exception {
52 | GitHubService gitHubService = useCase.getGitHubService();
53 | assertNotNull(gitHubService);
54 | }
55 |
56 | @Test
57 | public void unsubcribeShouldUnsubscribeSubscription() throws Exception {
58 | TestSubscriber testSubscriber = new TestSubscriber<>();
59 | useCase.execute(testSubscriber, MockFactory.TEST_USERNAME);
60 | useCase.unsubscribe();
61 | Subscription subscription = useCase.getSubscription();
62 | assertEquals(true, subscription.isUnsubscribed());
63 | }
64 |
65 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/hugo/mvpsampleapplication/features/userdetails/UserDetailsPresenter.java:
--------------------------------------------------------------------------------
1 | package com.hugo.mvpsampleapplication.features.userdetails;
2 |
3 | import com.hugo.mvpsampleapplication.features.Presenter;
4 | import com.hugo.mvpsampleapplication.features.UseCase;
5 | import com.hugo.mvpsampleapplication.features.DefaultSubscriber;
6 | import com.hugo.mvpsampleapplication.model.entities.Repository;
7 |
8 | import com.hugo.mvpsampleapplication.utils.dependencyinjection.PerActivity;
9 | import java.util.List;
10 | import javax.inject.Inject;
11 | import javax.inject.Named;
12 |
13 | /**
14 | * UserDetailsPresenter controlling UserDetailsFragment. LoadUserDetailsUseCase is used in order
15 | * to get data from the model. After data is fetched this presenter updates the UserDetailsFragment
16 | * with the data.
17 | */
18 | @PerActivity
19 | public class UserDetailsPresenter implements Presenter {
20 |
21 | private UserDetailsView userDetailsView;
22 | private UseCase loadUserDetailsUseCase;
23 |
24 | @Inject
25 | public UserDetailsPresenter(@Named("userDetails") UseCase loadUserDetailsUseCase) {
26 | this.loadUserDetailsUseCase = loadUserDetailsUseCase;
27 | }
28 |
29 | @Override
30 | public void attachView(UserDetailsView userDetailsView) {
31 | this.userDetailsView = userDetailsView;
32 | }
33 |
34 | public void loadRepositories(String username) {
35 | userDetailsView.showProgressIndicator();
36 | loadUserDetailsUseCase.execute(new LoadRepositoriesSubscriber(), username);
37 | }
38 |
39 | private final class LoadRepositoriesSubscriber extends DefaultSubscriber> {
40 |
41 | @Override
42 | public void onCompleted() {
43 | userDetailsView.hideProgressIndicator();
44 | }
45 | @Override
46 | public void onError(Throwable e) {
47 | userDetailsView.hideProgressIndicator();
48 | userDetailsView.showMessage("Error loading repositories");
49 | e.printStackTrace();
50 | }
51 |
52 | @Override
53 | public void onNext(List repositories) {
54 | if (repositories.isEmpty()) {
55 | userDetailsView.showMessage("No public repositories");
56 | }
57 | userDetailsView.showRepositories(repositories);
58 | }
59 |
60 | }
61 |
62 | @Override
63 | public void detachView() {
64 | this.userDetailsView = null;
65 | }
66 |
67 | @Override
68 | public void destroy(boolean unsubscribe) {
69 | userDetailsView = null;
70 | if(unsubscribe) {
71 | loadUserDetailsUseCase.unsubscribe();
72 | }
73 | }
74 |
75 | }
76 |
--------------------------------------------------------------------------------
/app/src/main/java/com/hugo/mvpsampleapplication/features/userdetails/RepositoriesAdapter.java:
--------------------------------------------------------------------------------
1 | package com.hugo.mvpsampleapplication.features.userdetails;
2 |
3 | import android.support.v7.widget.RecyclerView;
4 | import android.view.LayoutInflater;
5 | import android.view.View;
6 | import android.view.ViewGroup;
7 | import android.widget.TextView;
8 |
9 | import com.hugo.mvpsampleapplication.R;
10 | import com.hugo.mvpsampleapplication.model.entities.Repository;
11 |
12 | import java.util.Collections;
13 | import java.util.List;
14 |
15 | import butterknife.Bind;
16 | import butterknife.ButterKnife;
17 |
18 | public class RepositoriesAdapter extends RecyclerView.Adapter {
19 |
20 | private List reposetories;
21 |
22 | public RepositoriesAdapter() {
23 | this.reposetories = Collections.emptyList();
24 | }
25 |
26 | public void setReposetories(List users) {
27 | this.reposetories = users;
28 | }
29 |
30 | @Override
31 | public RepoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
32 | final View itemView = LayoutInflater.from(parent.getContext())
33 | .inflate(R.layout.item_repo, parent, false);
34 | return new RepoViewHolder(itemView);
35 | }
36 |
37 | @Override
38 | public void onBindViewHolder(RepoViewHolder holder, int position) {
39 | Repository repository = reposetories.get(position);
40 | holder.repoTitle.setText(repository.getName());
41 | holder.repoDescription.setText(repository.getDescription());
42 | holder.forks.setText(Integer.toString(repository.getForks()));
43 | holder.watchers.setText(Integer.toString(repository.getWatchers()));
44 | holder.stars.setText(Integer.toString(repository.getStars()));
45 | }
46 |
47 | @Override
48 | public int getItemCount() {
49 | return reposetories.size();
50 | }
51 |
52 | public class RepoViewHolder extends RecyclerView.ViewHolder {
53 |
54 | @Bind(R.id.layout_content)
55 | View contentLayout;
56 | @Bind(R.id.text_repo_title)
57 | TextView repoTitle;
58 | @Bind(R.id.text_repo_description)
59 | TextView repoDescription;
60 | @Bind(R.id.text_forks)
61 | TextView forks;
62 | @Bind(R.id.text_watchers)
63 | TextView watchers;
64 | @Bind(R.id.text_stars)
65 | TextView stars;
66 |
67 | public RepoViewHolder(View itemView) {
68 | super(itemView);
69 | ButterKnife.bind(this, itemView);
70 | }
71 | }
72 |
73 | }
74 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_search_user.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
18 |
19 |
30 |
31 |
41 |
42 |
54 |
55 |
56 |
57 |
66 |
67 |
70 |
71 |
72 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/hugo/mvpsampleapplication/OrientationChangeAction.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2015 - Nathan Barraille
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | *
24 | */
25 | package com.hugo.mvpsampleapplication;
26 |
27 | import android.app.Activity;
28 | import android.content.pm.ActivityInfo;
29 | import android.support.test.espresso.UiController;
30 | import android.support.test.espresso.ViewAction;
31 | import android.view.View;
32 | import org.hamcrest.Matcher;
33 |
34 | import static android.support.test.espresso.matcher.ViewMatchers.isRoot;
35 |
36 | /**
37 | * An Espresso ViewAction that changes the orientation of the screen
38 | */
39 | public class OrientationChangeAction implements ViewAction {
40 | private final int orientation;
41 |
42 | private OrientationChangeAction(int orientation) {
43 | this.orientation = orientation;
44 | }
45 |
46 | @Override
47 | public Matcher getConstraints() {
48 | return isRoot();
49 | }
50 |
51 | @Override
52 | public String getDescription() {
53 | return "change orientation to " + orientation;
54 | }
55 |
56 | @Override
57 | public void perform(UiController uiController, View view) {
58 | uiController.loopMainThreadUntilIdle();
59 | final Activity activity = (Activity) view.getContext();
60 | activity.setRequestedOrientation(orientation);
61 | }
62 |
63 | public static ViewAction orientationLandscape() {
64 | return new OrientationChangeAction(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
65 | }
66 |
67 | public static ViewAction orientationPortrait() {
68 | return new OrientationChangeAction(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
69 | }
70 | }
--------------------------------------------------------------------------------
/app/src/test/java/com/hugo/mvpsampleapplication/features/userdetails/LoadUserDetailsUseCaseTest.java:
--------------------------------------------------------------------------------
1 | package com.hugo.mvpsampleapplication.features.userdetails;
2 |
3 | import com.hugo.mvpsampleapplication.MockFactory;
4 | import com.hugo.mvpsampleapplication.model.entities.Repository;
5 | import com.hugo.mvpsampleapplication.model.network.GitHubService;
6 | import com.hugo.mvpsampleapplication.utils.PostExecutionThread;
7 | import com.hugo.mvpsampleapplication.utils.ThreadExecutor;
8 | import java.util.List;
9 | import org.junit.Before;
10 | import org.junit.Test;
11 | import org.mockito.Mock;
12 | import org.mockito.MockitoAnnotations;
13 | import rx.Observable;
14 | import rx.observers.TestSubscriber;
15 | import rx.schedulers.Schedulers;
16 |
17 | import static org.junit.Assert.*;
18 | import static org.mockito.Matchers.any;
19 | import static org.mockito.Mockito.verify;
20 | import static org.mockito.Mockito.when;
21 |
22 | /**
23 | * Created by hugo on 2/25/16.
24 | */
25 | public class LoadUserDetailsUseCaseTest {
26 |
27 | @Mock private GitHubService gitHubService;
28 | @Mock private PostExecutionThread mockPostExecutionThread;
29 | @Mock private ThreadExecutor mockThreadExecutor;
30 |
31 | private LoadUserDetailsUseCase loadUserDetailsUseCase;
32 |
33 | @Before
34 | public void setUp() {
35 | MockitoAnnotations.initMocks(this);
36 | when(mockPostExecutionThread.getScheduler()).thenReturn(Schedulers.immediate());
37 | when(mockThreadExecutor.getScheduler()).thenReturn(Schedulers.immediate());
38 | List mockRepositories = MockFactory.buildMockUserDetailsResponse();
39 | when(gitHubService.getRepositoriesFromUser(any(String.class))).thenReturn(
40 | Observable.just(mockRepositories));
41 | loadUserDetailsUseCase =
42 | new LoadUserDetailsUseCase(gitHubService, mockThreadExecutor, mockPostExecutionThread);
43 | }
44 |
45 | @Test
46 | public void buildUseCaseShouldGetReposFromUser() throws Exception {
47 | loadUserDetailsUseCase.buildUseCase(MockFactory.TEST_USERNAME);
48 | verify(gitHubService).getRepositoriesFromUser(MockFactory.TEST_USERNAME);
49 | }
50 |
51 | @Test(expected = NullPointerException.class)
52 | public void buildUseCaseShouldThrowNullPointerExceptionIfUsernameNotSet() throws Exception {
53 | loadUserDetailsUseCase.buildUseCase(null);
54 | }
55 |
56 | @Test
57 | public void executeShouldReturnRepositoryList() {
58 | TestSubscriber testSubscriber = new TestSubscriber<>();
59 | loadUserDetailsUseCase.execute(testSubscriber, MockFactory.TEST_USERNAME);
60 | testSubscriber.assertCompleted();
61 | testSubscriber.assertNoErrors();
62 | List searchResponseList = testSubscriber.getOnNextEvents();
63 | assertEquals(1, searchResponseList.size());
64 | }
65 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/hugo/mvpsampleapplication/features/searchuser/SearchUserPresenter.java:
--------------------------------------------------------------------------------
1 | package com.hugo.mvpsampleapplication.features.searchuser;
2 |
3 | import com.hugo.mvpsampleapplication.features.UseCase;
4 | import com.hugo.mvpsampleapplication.model.entities.User;
5 | import com.hugo.mvpsampleapplication.features.DefaultSubscriber;
6 | import com.hugo.mvpsampleapplication.model.network.SearchResponse;
7 | import com.hugo.mvpsampleapplication.features.Presenter;
8 |
9 | import com.hugo.mvpsampleapplication.utils.dependencyinjection.PerActivity;
10 | import java.util.List;
11 | import javax.inject.Inject;
12 | import javax.inject.Named;
13 |
14 | /**
15 | * SearchUserPresenter controlling SearchUserFragment. SearchUserUseCase is used in order
16 | * to get data from the model. After data is fetched this presenter updates the SearchUserFragment
17 | * with the data.
18 | */
19 | @PerActivity
20 | public class SearchUserPresenter implements Presenter {
21 |
22 | private SearchUserView searchUserView;
23 | private UseCase searchUserUseCase;
24 |
25 | @Inject
26 | public SearchUserPresenter(@Named("searchUser") UseCase searchUserUseCase) {
27 | this.searchUserUseCase = searchUserUseCase;
28 | }
29 |
30 | @Override
31 | public void attachView(SearchUserView searchUserView) {
32 | this.searchUserView = searchUserView;
33 | }
34 |
35 | public void loadUsers(String username) {
36 | if(username == null || username.isEmpty()) {
37 | searchUserView.showMessage("Enter a username");
38 | } else {
39 | searchUserView.showProgressIndicator();
40 | searchUserUseCase.execute(new SearchUserSubscriber(), username);
41 | }
42 | }
43 |
44 | public void onUserClick(String username) {
45 | searchUserView.startUserDetailsActivity(username);
46 | }
47 |
48 | private final class SearchUserSubscriber extends DefaultSubscriber {
49 |
50 | @Override
51 | public void onCompleted() {
52 | searchUserView.hideProgressIndicator();
53 | }
54 |
55 | @Override
56 | public void onError(Throwable e) {
57 | searchUserView.hideProgressIndicator();
58 | searchUserView.showMessage("Error loading users");
59 | }
60 |
61 | @Override
62 | public void onNext(SearchResponse searchResponse) {
63 | List users = searchResponse.getUsers();
64 | if (users.isEmpty()) {
65 | searchUserView.showMessage("No users found");
66 | }
67 | searchUserView.showUsers(users);
68 | }
69 |
70 | }
71 |
72 | @Override
73 | public void detachView() {
74 | this.searchUserView = null;
75 | }
76 |
77 | @Override
78 | public void destroy(boolean unsubscribe) {
79 | searchUserView = null;
80 | if(unsubscribe) {
81 | searchUserUseCase.unsubscribe();
82 | }
83 | }
84 |
85 | }
86 |
--------------------------------------------------------------------------------
/app/src/main/java/com/hugo/mvpsampleapplication/features/searchuser/UserListAdapter.java:
--------------------------------------------------------------------------------
1 | package com.hugo.mvpsampleapplication.features.searchuser;
2 |
3 | import android.content.Context;
4 | import android.support.v7.widget.RecyclerView;
5 | import android.view.LayoutInflater;
6 | import android.view.View;
7 | import android.view.ViewGroup;
8 | import android.widget.TextView;
9 |
10 | import com.hugo.mvpsampleapplication.R;
11 | import com.hugo.mvpsampleapplication.model.entities.User;
12 | import com.squareup.picasso.Picasso;
13 |
14 | import java.util.Collections;
15 | import java.util.List;
16 |
17 | import de.hdodenhof.circleimageview.CircleImageView;
18 |
19 | public class UserListAdapter extends RecyclerView.Adapter {
20 |
21 | private List users;
22 | private OnItemClickListener onItemClickListener;
23 |
24 | public interface OnItemClickListener {
25 | void onItemClick(String username);
26 | }
27 |
28 | public UserListAdapter() {
29 | this.users = Collections.emptyList();
30 | }
31 |
32 | public void setUsers(List users) {
33 | this.users = users;
34 | }
35 |
36 | public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
37 | this.onItemClickListener = onItemClickListener;
38 | }
39 |
40 | @Override public UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
41 | final View itemView =
42 | LayoutInflater.from(parent.getContext()).inflate(R.layout.item_user, parent, false);
43 | final UserViewHolder viewHolder = new UserViewHolder(itemView);
44 | View content = viewHolder.contentLayout;
45 | content.setOnClickListener(new View.OnClickListener() {
46 | @Override public void onClick(View v) {
47 | if (onItemClickListener != null) {
48 | onItemClickListener.onItemClick(viewHolder.getUsername());
49 | }
50 | }
51 | });
52 | return viewHolder;
53 | }
54 |
55 | @Override public void onBindViewHolder(UserViewHolder holder, int position) {
56 | User user = users.get(position);
57 | Context context = holder.usernameTextView.getContext();
58 | holder.usernameTextView.setText(user.getLogin());
59 | Picasso.with(context).load(user.getAvatarUrl()).fit().into(holder.avatar);
60 | }
61 |
62 | @Override public int getItemCount() {
63 | return users.size();
64 | }
65 |
66 | public class UserViewHolder extends RecyclerView.ViewHolder {
67 | View contentLayout;
68 | TextView usernameTextView;
69 | CircleImageView avatar;
70 |
71 | public UserViewHolder(View itemView) {
72 | super(itemView);
73 | contentLayout = itemView.findViewById(R.id.layout_content);
74 | usernameTextView = (TextView) itemView.findViewById(R.id.item_username);
75 | avatar = (CircleImageView) itemView.findViewById(R.id.avatar);
76 | }
77 |
78 | public String getUsername() {
79 | return usernameTextView.getText().toString();
80 | }
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/app/src/sharedTest/java/MockFactory.java:
--------------------------------------------------------------------------------
1 | package com.hugo.mvpsampleapplication;
2 |
3 | import com.hugo.mvpsampleapplication.model.entities.Repository;
4 | import com.hugo.mvpsampleapplication.model.entities.User;
5 | import com.hugo.mvpsampleapplication.model.network.SearchResponse;
6 | import java.util.ArrayList;
7 | import java.util.List;
8 |
9 | /**
10 | * Created by hugo on 2/15/16.
11 | */
12 | public class MockFactory {
13 |
14 | public static final String TEST_USERNAME = "Tester";
15 | public static final String TEST_REPOSITORY = "TestRepo";
16 | public static final String TEST_USERNAME_NO_RESULTS = "TesterNoResults";
17 | public static final String TEST_USERNAME_ERROR = "TesterError";
18 | private static final String TEST_REPO_URL = "http://repo.url";
19 | private static final String TEST_AVATAR_URL = "http://avatar.url";
20 | private static final String TEST_LANGUAGE = "Java";
21 | private static final String TEST_DESCRIPTION = "Test Description";
22 |
23 | public static SearchResponse buildMockSearchResponse() {
24 | User testUser = new User();
25 | testUser.setLogin(TEST_USERNAME);
26 | ArrayList users = new ArrayList<>();
27 | users.add(testUser);
28 | SearchResponse searchResponse = new SearchResponse();
29 | searchResponse.setUsers(users);
30 | return searchResponse;
31 | }
32 |
33 | public static List buildMockUserDetailsResponse() {
34 | Repository repository = new Repository();
35 | repository.setId(1);
36 | repository.setStars(2);
37 | repository.setForks(3);
38 | repository.setWatchers(4);
39 | repository.setDescription("Test Repository");
40 | repository.setName(TEST_REPOSITORY);
41 | repository.setLanguage("Java");
42 | ArrayList repositories = new ArrayList<>();
43 | repositories.add(repository);
44 | return repositories;
45 | }
46 |
47 | public static User buildMockUser() {
48 | User user = new User();
49 | user.setId(1);
50 | user.setLogin(TEST_USERNAME);
51 | user.setReposUrl(TEST_REPO_URL);
52 | user.setAvatarUrl(TEST_AVATAR_URL);
53 | return user;
54 | }
55 |
56 | public static Repository buildMockRepository() {
57 | Repository repository = new Repository();
58 | repository.setId(1);
59 | repository.setLanguage(TEST_LANGUAGE);
60 | repository.setName(TEST_USERNAME);
61 | repository.setDescription(TEST_DESCRIPTION);
62 | repository.setForks(1);
63 | repository.setStars(2);
64 | repository.setWatchers(3);
65 | return repository;
66 | }
67 |
68 | public static SearchResponse buildEmptyMockSearchResponse() {
69 | ArrayList users = new ArrayList<>(0);
70 | SearchResponse searchResponse = new SearchResponse();
71 | searchResponse.setUsers(users);
72 | return searchResponse;
73 | }
74 |
75 | public static List buildEmptyRepositoryList() {
76 | ArrayList repositories = new ArrayList<>();
77 | return repositories;
78 | }
79 | }
80 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 | apply plugin: 'com.neenbedankt.android-apt'
3 |
4 | android {
5 | compileSdkVersion 23
6 | buildToolsVersion "23.0.2"
7 |
8 | defaultConfig {
9 | applicationId "com.hugo.mvpsampleapplication"
10 | minSdkVersion 15
11 | targetSdkVersion 23
12 | versionCode 1
13 | versionName "1.0"
14 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
15 | }
16 |
17 | buildTypes {
18 | release {
19 | minifyEnabled false
20 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
21 | }
22 | }
23 |
24 | sourceSets {
25 | String sharedTestDir = 'src/sharedTest/java'
26 | test {
27 | java.srcDir sharedTestDir
28 | }
29 | androidTest {
30 | java.srcDir sharedTestDir
31 | }
32 | }
33 | testOptions {
34 | unitTests.returnDefaultValues = true
35 | }
36 | }
37 |
38 | dependencies {
39 | compile fileTree(dir: 'libs', include: ['*.jar'])
40 | compile 'com.android.support:appcompat-v7:23.1.1'
41 | compile 'com.android.support:design:23.1.1'
42 | compile 'com.android.support:cardview-v7:23.1.1'
43 | compile 'com.android.support:recyclerview-v7:23.1.1'
44 | compile 'com.android.support:support-v4:23.1.1'
45 |
46 | compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
47 | compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
48 | compile 'com.squareup.retrofit:adapter-rxjava:2.0.0-beta2'
49 |
50 | compile 'io.reactivex:rxandroid:1.0.1'
51 | compile 'com.jakewharton:butterknife:7.0.1'
52 | compile 'de.hdodenhof:circleimageview:2.0.0'
53 | compile 'com.squareup.picasso:picasso:2.5.2'
54 |
55 | //dagger
56 | compile 'com.google.dagger:dagger:2.0.2'
57 | apt "com.google.dagger:dagger-compiler:2.0.1"
58 | compile 'javax.annotation:jsr250-api:1.0'
59 |
60 | //dagger test
61 | androidTestCompile 'com.google.dagger:dagger:2.0.2'
62 | testApt "com.google.dagger:dagger-compiler:2.0.1"
63 | androidTestCompile 'javax.annotation:jsr250-api:1.0'
64 |
65 | testCompile 'org.mockito:mockito-core:1.10.19'
66 | testCompile 'junit:junit:4.12'
67 |
68 | testCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.3.1'
69 | androidTestCompile 'com.github.fabioCollini:DaggerMock:0.5'
70 | androidTestCompile 'junit:junit:4.12'
71 | androidTestCompile 'org.mockito:mockito-core:1.10.19'
72 | androidTestCompile 'com.google.dexmaker:dexmaker:1.2'
73 | androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.2'
74 | androidTestCompile 'com.android.support:support-annotations:23.1.1'
75 | androidTestCompile 'com.android.support.test:runner:0.4.1'
76 | androidTestCompile 'com.android.support.test:rules:0.4.1'
77 | androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
78 | androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
79 | androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.0') {
80 | exclude group: 'com.android.support', module: 'appcompat'
81 | exclude group: 'com.android.support', module: 'support-v4'
82 | exclude module: 'recyclerview-v7'
83 | }
84 | }
85 |
--------------------------------------------------------------------------------
/app/src/test/java/com/hugo/mvpsampleapplication/features/searchuser/SearchUserUseCaseTest.java:
--------------------------------------------------------------------------------
1 | package com.hugo.mvpsampleapplication.features.searchuser;
2 |
3 | import com.hugo.mvpsampleapplication.MockFactory;
4 | import com.hugo.mvpsampleapplication.model.entities.User;
5 | import com.hugo.mvpsampleapplication.model.network.GitHubService;
6 | import com.hugo.mvpsampleapplication.model.network.SearchResponse;
7 | import com.hugo.mvpsampleapplication.utils.PostExecutionThread;
8 | import com.hugo.mvpsampleapplication.utils.ThreadExecutor;
9 | import java.util.List;
10 | import junit.framework.Assert;
11 | import org.junit.Before;
12 | import org.junit.Test;
13 | import org.junit.runner.RunWith;
14 | import org.mockito.Mock;
15 | import org.mockito.MockitoAnnotations;
16 | import org.mockito.runners.MockitoJUnitRunner;
17 | import rx.Observable;
18 | import rx.observers.TestSubscriber;
19 | import rx.schedulers.Schedulers;
20 |
21 | import static org.mockito.Matchers.any;
22 | import static org.mockito.Mockito.verify;
23 | import static org.mockito.Mockito.when;
24 |
25 | /**
26 | * Created by hugo on 2/25/16.
27 | */
28 | @RunWith(MockitoJUnitRunner.class)
29 | public class SearchUserUseCaseTest {
30 | @Mock
31 | private GitHubService gitHubService;
32 | @Mock
33 | private PostExecutionThread mockPostExecutionThread;
34 | @Mock
35 | private ThreadExecutor mockThreadExecutor;
36 |
37 | private SearchUserUseCase searchUserUseCase;
38 |
39 | @Before
40 | public void setUp() {
41 | MockitoAnnotations.initMocks(this);
42 | when(mockPostExecutionThread.getScheduler()).thenReturn(Schedulers.immediate());
43 | when(mockThreadExecutor.getScheduler()).thenReturn(Schedulers.immediate());
44 | SearchResponse searchResponse = MockFactory.buildMockSearchResponse();
45 | when(gitHubService.searchUser(any(String.class))).thenReturn(Observable.just(searchResponse));
46 | searchUserUseCase = new SearchUserUseCase(gitHubService, mockThreadExecutor, mockPostExecutionThread);
47 | }
48 |
49 | @Test
50 | public void buildUseCaseShouldCallSearchUser() throws Exception {
51 | searchUserUseCase.buildUseCase(MockFactory.TEST_USERNAME);
52 | verify(gitHubService).searchUser(MockFactory.TEST_USERNAME);
53 | }
54 |
55 | @Test(expected = NullPointerException.class)
56 | public void buildUseCaseShouldThrowNullPointerExceptionIfQueryNotSet() throws Exception {
57 | searchUserUseCase.buildUseCase(null);
58 | }
59 |
60 | @Test
61 | public void executeShouldReturnOneSearchResponse() {
62 | TestSubscriber testSubscriber = new TestSubscriber<>();
63 | searchUserUseCase.execute(testSubscriber, MockFactory.TEST_USERNAME);
64 | testSubscriber.assertCompleted();
65 | testSubscriber.assertNoErrors();
66 | List searchResponseList = testSubscriber.getOnNextEvents();
67 | Assert.assertEquals(1, searchResponseList.size());
68 | }
69 |
70 | @Test
71 | public void executeShouldReturnSearchResponseWithUser() {
72 | TestSubscriber testSubscriber = new TestSubscriber<>();
73 | searchUserUseCase.execute(testSubscriber, MockFactory.TEST_USERNAME);
74 | testSubscriber.assertCompleted();
75 | testSubscriber.assertNoErrors();
76 | List searchResponseList = testSubscriber.getOnNextEvents();
77 | List users = searchResponseList.get(0).getUsers();
78 | Assert.assertEquals(1, users.size());
79 | }
80 | }
--------------------------------------------------------------------------------
/app/src/main/res/layout/item_repo.xml:
--------------------------------------------------------------------------------
1 |
2 |
14 |
15 |
21 |
22 |
33 |
34 |
45 |
46 |
50 |
51 |
55 |
56 |
64 |
65 |
73 |
74 |
82 |
83 |
84 |
85 |
86 |
87 |
--------------------------------------------------------------------------------
/app/src/main/java/com/hugo/mvpsampleapplication/features/userdetails/UserDetailsFragment.java:
--------------------------------------------------------------------------------
1 | package com.hugo.mvpsampleapplication.features.userdetails;
2 |
3 | import android.content.Context;
4 | import android.os.Bundle;
5 | import android.support.design.widget.Snackbar;
6 | import android.support.v4.app.Fragment;
7 | import android.support.v7.widget.LinearLayoutManager;
8 | import android.support.v7.widget.RecyclerView;
9 | import android.util.Log;
10 | import android.view.LayoutInflater;
11 | import android.view.View;
12 | import android.view.ViewGroup;
13 | import android.widget.RelativeLayout;
14 | import android.widget.Toast;
15 |
16 | import com.hugo.mvpsampleapplication.R;
17 | import com.hugo.mvpsampleapplication.features.BaseActivity;
18 | import com.hugo.mvpsampleapplication.model.entities.Repository;
19 |
20 | import java.util.List;
21 |
22 | import butterknife.Bind;
23 | import butterknife.ButterKnife;
24 | import javax.inject.Inject;
25 |
26 | /**
27 | * UserDetailsFragment is a passive view where all user interactions is passed to
28 | * UserDetailsPresenter. UserDetailsFragment implements UserDetailsView in order to receive calls
29 | * from UserDetailsPresenter.
30 | */
31 | public class UserDetailsFragment extends Fragment implements UserDetailsView {
32 |
33 | @Bind(R.id.progress_indicator) RelativeLayout progressIndicator;
34 | @Bind(R.id.repositories_list) RecyclerView repositoriesList;
35 |
36 | @Inject UserDetailsPresenter userDetailsPresenter;
37 | private RepositoriesAdapter userDetailsAdapter;
38 | private LinearLayoutManager layoutManager;
39 |
40 | public UserDetailsFragment() {
41 | }
42 |
43 | public static UserDetailsFragment newInstance(String username) {
44 | UserDetailsFragment userDetailsFragment = new UserDetailsFragment();
45 | Bundle bundle = new Bundle();
46 | bundle.putString("username", username);
47 | userDetailsFragment.setArguments(bundle);
48 | return userDetailsFragment;
49 | }
50 |
51 | @Override
52 | public void onCreate(Bundle savedInstanceState) {
53 | super.onCreate(savedInstanceState);
54 | setRetainInstance(true);
55 | ((BaseActivity) getActivity()).getUserComponent().inject(this);
56 | userDetailsAdapter = new RepositoriesAdapter();
57 | }
58 |
59 | @Override
60 | public void onAttach(Context context) {
61 | super.onAttach(context);
62 | layoutManager = new LinearLayoutManager(context);
63 | }
64 |
65 | @Override
66 | public View onCreateView(LayoutInflater inflater, ViewGroup container,
67 | Bundle savedInstanceState) {
68 | View fragmentView = inflater.inflate(R.layout.fragment_user_details, container, false);
69 | ButterKnife.bind(this, fragmentView);
70 | userDetailsPresenter.attachView(this);
71 | repositoriesList.setAdapter(userDetailsAdapter);
72 | repositoriesList.setLayoutManager(layoutManager);
73 | if (getArguments() != null && savedInstanceState == null) {
74 | String username = getArguments().getString("username");
75 | loadRepositories(username);
76 | }
77 | return fragmentView;
78 | }
79 |
80 | public void loadRepositories(String username) {
81 | userDetailsPresenter.loadRepositories(username);
82 | }
83 |
84 | @Override
85 | public void showMessage(String message) {
86 | View rootView = getActivity().getWindow().getDecorView().findViewById(android.R.id.content);
87 | Snackbar snackbar = Snackbar.make(rootView, message, Snackbar.LENGTH_SHORT);
88 | snackbar.show();
89 | }
90 |
91 | @Override
92 | public void showProgressIndicator() {
93 | repositoriesList.setVisibility(View.INVISIBLE);
94 | progressIndicator.setVisibility(View.VISIBLE);
95 | }
96 |
97 | @Override
98 | public void hideProgressIndicator() {
99 | progressIndicator.setVisibility(View.INVISIBLE);
100 | repositoriesList.setVisibility(View.VISIBLE);
101 | }
102 |
103 | @Override
104 | public void showRepositories(List repositories) {
105 | userDetailsAdapter.setReposetories(repositories);
106 | userDetailsAdapter.notifyDataSetChanged();
107 | }
108 |
109 | @Override
110 | public void onDetach() {
111 | super.onDetach();
112 | layoutManager = null;
113 | }
114 |
115 | @Override
116 | public void onDestroyView() {
117 | super.onDestroyView();
118 | ButterKnife.unbind(this);
119 | userDetailsPresenter.destroy(false);
120 | }
121 |
122 | @Override
123 | public void onDestroy() {
124 | super.onDestroy();
125 | userDetailsPresenter.destroy(true);
126 | }
127 | }
128 |
--------------------------------------------------------------------------------
/app/src/test/java/com/hugo/mvpsampleapplication/features/userdetails/UserDetailsPresenterTest.java:
--------------------------------------------------------------------------------
1 | package com.hugo.mvpsampleapplication.features.userdetails;
2 |
3 | import com.hugo.mvpsampleapplication.MockFactory;
4 | import com.hugo.mvpsampleapplication.model.entities.Repository;
5 | import com.hugo.mvpsampleapplication.model.network.GitHubService;
6 | import com.hugo.mvpsampleapplication.utils.PostExecutionThread;
7 | import com.hugo.mvpsampleapplication.utils.ThreadExecutor;
8 | import java.util.List;
9 | import org.junit.After;
10 | import org.junit.Before;
11 | import org.junit.Test;
12 | import org.junit.runner.RunWith;
13 | import org.mockito.Mock;
14 | import org.mockito.MockitoAnnotations;
15 | import org.mockito.runners.MockitoJUnitRunner;
16 | import retrofit.HttpException;
17 | import retrofit.Response;
18 | import rx.Observable;
19 | import rx.schedulers.Schedulers;
20 |
21 | import static org.mockito.Matchers.anyList;
22 | import static org.mockito.Mockito.verify;
23 | import static org.mockito.Mockito.verifyZeroInteractions;
24 | import static org.mockito.Mockito.when;
25 |
26 | /**
27 | * Created by hugo on 2/25/16.
28 | */
29 | @RunWith(MockitoJUnitRunner.class)
30 | public class UserDetailsPresenterTest {
31 |
32 | @Mock private GitHubService gitHubService;
33 | @Mock private ThreadExecutor threadExecutor;
34 | @Mock private PostExecutionThread postExecutionThread;
35 | @Mock private LoadUserDetailsUseCase mockLoadUserDetailsUseCase;
36 |
37 | @Mock private UserDetailsView userDetailsView;
38 | private UserDetailsPresenter userDetailsPresenter;
39 | private HttpException mockHttpException;
40 |
41 | @Before
42 | public void setUp() {
43 | MockitoAnnotations.initMocks(this);
44 | setUpMocks();
45 | LoadUserDetailsUseCase loadUserDetailsUseCase =
46 | new LoadUserDetailsUseCase(gitHubService, threadExecutor, postExecutionThread);
47 | userDetailsPresenter = new UserDetailsPresenter(loadUserDetailsUseCase);
48 | userDetailsPresenter.attachView(userDetailsView);
49 | }
50 |
51 | private void setUpMocks() {
52 | when(gitHubService.getRepositoriesFromUser(MockFactory.TEST_USERNAME)).thenReturn(
53 | Observable.just(MockFactory.buildMockUserDetailsResponse()));
54 | mockHttpException = new HttpException(Response.error(404, null));
55 | when(gitHubService.getRepositoriesFromUser(MockFactory.TEST_USERNAME_ERROR)).thenReturn(
56 | Observable.>error(mockHttpException));
57 | when(gitHubService.getRepositoriesFromUser(MockFactory.TEST_USERNAME_NO_RESULTS)).thenReturn(
58 | Observable.just(MockFactory.buildEmptyRepositoryList()));
59 | when(threadExecutor.getScheduler()).thenReturn(Schedulers.immediate());
60 | when(postExecutionThread.getScheduler()).thenReturn(Schedulers.immediate());
61 | }
62 |
63 | @Test
64 | public void loadRepositoriesShouldShowProgressIndicator() {
65 | userDetailsPresenter.loadRepositories(MockFactory.TEST_USERNAME);
66 | verify(userDetailsView).showProgressIndicator();
67 | }
68 |
69 | @Test
70 | public void loadRepositoriesShouldShowMessageIfEmptyResponse() {
71 | userDetailsPresenter.loadRepositories(MockFactory.TEST_USERNAME_NO_RESULTS);
72 | verify(userDetailsView).showMessage("The user does not have any public repositories");
73 | }
74 |
75 | @Test
76 | public void loadRepositoriesShouldShowMessageIfError() {
77 | userDetailsPresenter.loadRepositories(MockFactory.TEST_USERNAME_ERROR);
78 | verify(userDetailsView).showMessage("Error loading repositories");
79 | }
80 |
81 | @Test
82 | public void loadRepositoriesShouldHideProgressIndicatorOnComplete() {
83 | userDetailsPresenter.loadRepositories(MockFactory.TEST_USERNAME);
84 | verify(userDetailsView).hideProgressIndicator();
85 | }
86 |
87 | @Test
88 | public void loadRepositoriesShouldCallShowRepositoriesWhenReceivedResults() {
89 | userDetailsPresenter.loadRepositories(MockFactory.TEST_USERNAME);
90 | verify(userDetailsView).showRepositories(anyList());
91 | }
92 |
93 | @Test
94 | public void destroyShouldUnsubscribeIfTrue() {
95 | userDetailsPresenter = new UserDetailsPresenter(mockLoadUserDetailsUseCase);
96 | userDetailsPresenter.destroy(true);
97 | verify(mockLoadUserDetailsUseCase).unsubscribe();
98 | }
99 |
100 | @Test
101 | public void destroyShouldNotUnsubScribeIfFalse() {
102 | userDetailsPresenter = new UserDetailsPresenter(mockLoadUserDetailsUseCase);
103 | userDetailsPresenter.destroy(false);
104 | verifyZeroInteractions(mockLoadUserDetailsUseCase);
105 | }
106 |
107 | @After
108 | public void cleanUp() {
109 | userDetailsPresenter.detachView();
110 | }
111 |
112 | }
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/hugo/mvpsampleapplication/features/userdetails/UserDetailsActivityTest.java:
--------------------------------------------------------------------------------
1 | package com.hugo.mvpsampleapplication.features.userdetails;
2 |
3 | import android.content.Intent;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.rule.ActivityTestRule;
6 | import android.support.test.runner.AndroidJUnit4;
7 | import android.test.suitebuilder.annotation.LargeTest;
8 | import com.hugo.mvpsampleapplication.MockFactory;
9 | import com.hugo.mvpsampleapplication.app.MVPApplication;
10 | import com.hugo.mvpsampleapplication.model.network.GitHubService;
11 | import com.hugo.mvpsampleapplication.utils.dependencyinjection.components.ApplicationComponent;
12 | import com.hugo.mvpsampleapplication.utils.dependencyinjection.modules.ApplicationModule;
13 | import it.cosenonjaviste.daggermock.DaggerMockRule;
14 | import org.junit.Before;
15 | import org.junit.Rule;
16 | import org.junit.Test;
17 | import org.junit.runner.RunWith;
18 | import org.mockito.Mock;
19 | import rx.Observable;
20 |
21 | import static android.support.test.espresso.Espresso.onView;
22 | import static android.support.test.espresso.assertion.ViewAssertions.matches;
23 | import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
24 | import static android.support.test.espresso.matcher.ViewMatchers.isRoot;
25 | import static android.support.test.espresso.matcher.ViewMatchers.withId;
26 | import static android.support.test.espresso.matcher.ViewMatchers.withText;
27 | import static com.hugo.mvpsampleapplication.OrientationChangeAction.orientationLandscape;
28 | import static org.mockito.Mockito.when;
29 |
30 | @RunWith(AndroidJUnit4.class)
31 | @LargeTest
32 | public class UserDetailsActivityTest {
33 |
34 | @Rule public DaggerMockRule daggerMockRule =
35 | new DaggerMockRule<>(ApplicationComponent.class, new ApplicationModule()).set(
36 | new DaggerMockRule.ComponentSetter() {
37 | @Override
38 | public void setComponent(ApplicationComponent applicationComponent) {
39 | getApp().setApplicationComponent(applicationComponent);
40 | }
41 | });
42 |
43 | @Rule public final ActivityTestRule activityTestRule =
44 | new ActivityTestRule<>(UserDetailsActivity.class, false, false);
45 |
46 | @Mock GitHubService mockGitHubService;
47 |
48 | private MVPApplication getApp() {
49 | return (MVPApplication) InstrumentationRegistry.getInstrumentation()
50 | .getTargetContext()
51 | .getApplicationContext();
52 | }
53 |
54 | @Before
55 | public void setUp() {
56 | setUpMockGitHubService();
57 | }
58 |
59 | @SuppressWarnings("unchecked")
60 | private void setUpMockGitHubService() {
61 | when(mockGitHubService.getRepositoriesFromUser(MockFactory.TEST_USERNAME)).thenReturn(
62 | Observable.just(MockFactory.buildMockUserDetailsResponse()));
63 | Observable error = Observable.error(new Throwable("Error"));
64 | when(mockGitHubService.getRepositoriesFromUser(MockFactory.TEST_USERNAME_ERROR)).thenReturn(
65 | error);
66 | when(
67 | mockGitHubService.getRepositoriesFromUser(MockFactory.TEST_USERNAME_NO_RESULTS)).thenReturn(
68 | Observable.just(MockFactory.buildEmptyRepositoryList()));
69 | }
70 |
71 | @Test
72 | public void shouldDisplayRepositories() throws Exception {
73 | activityTestRule.launchActivity(buildIntent(MockFactory.TEST_USERNAME));
74 | onView(withText(MockFactory.TEST_REPOSITORY)).check(matches(isDisplayed()));
75 | }
76 |
77 | @Test
78 | public void shouldDisplayRepositoriesAfterConfigurationChange() throws Exception {
79 | activityTestRule.launchActivity(buildIntent(MockFactory.TEST_USERNAME));
80 | onView(isRoot()).perform(orientationLandscape());
81 | onView(withText(MockFactory.TEST_REPOSITORY)).check(matches(isDisplayed()));
82 | }
83 |
84 | @Test
85 | public void shouldDisplayNetworkErrorInSnackBar() throws Exception {
86 | activityTestRule.launchActivity(buildIntent(MockFactory.TEST_USERNAME_ERROR));
87 | onView(withId(android.support.design.R.id.snackbar_text)).check(matches(withText("Error loading repositories")));
88 | }
89 |
90 | @Test
91 | public void shouldDisplayEmptyRepositoriesErrorInSnackBar() throws Exception {
92 | activityTestRule.launchActivity(buildIntent(MockFactory.TEST_USERNAME_NO_RESULTS));
93 | onView(withId(android.support.design.R.id.snackbar_text)).check(matches(withText("No public repositories")));
94 | }
95 |
96 | private Intent buildIntent(String username) {
97 | Intent intent = new Intent();
98 | intent.putExtra("USERNAME", username);
99 | return intent;
100 | }
101 | }
--------------------------------------------------------------------------------
/app/src/test/java/com/hugo/mvpsampleapplication/features/searchuser/SearchUserPresenterTest.java:
--------------------------------------------------------------------------------
1 | package com.hugo.mvpsampleapplication.features.searchuser;
2 |
3 | import com.hugo.mvpsampleapplication.MockFactory;
4 | import com.hugo.mvpsampleapplication.model.network.GitHubService;
5 | import com.hugo.mvpsampleapplication.model.network.SearchResponse;
6 | import com.hugo.mvpsampleapplication.utils.PostExecutionThread;
7 | import com.hugo.mvpsampleapplication.utils.ThreadExecutor;
8 | import org.junit.After;
9 | import org.junit.Before;
10 | import org.junit.Test;
11 | import org.junit.runner.RunWith;
12 | import org.mockito.Mock;
13 | import org.mockito.MockitoAnnotations;
14 | import org.mockito.runners.MockitoJUnitRunner;
15 | import retrofit.HttpException;
16 | import retrofit.Response;
17 | import rx.Observable;
18 | import rx.schedulers.Schedulers;
19 |
20 | import static org.mockito.Matchers.any;
21 | import static org.mockito.Matchers.anyList;
22 | import static org.mockito.Mockito.times;
23 | import static org.mockito.Mockito.verify;
24 | import static org.mockito.Mockito.verifyZeroInteractions;
25 | import static org.mockito.Mockito.when;
26 |
27 | @RunWith(MockitoJUnitRunner.class)
28 | public class SearchUserPresenterTest {
29 |
30 | @Mock private GitHubService gitHubService;
31 | @Mock private ThreadExecutor threadExecutor;
32 | @Mock private PostExecutionThread postExecutionThread;
33 | @Mock private SearchUserUseCase mockSearchUserUseCase;
34 |
35 | @Mock private SearchUserView searchUserView;
36 | private SearchUserPresenter searchUserPresenter;
37 | private HttpException mockHttpException;
38 |
39 | @Before
40 | public void setUp() {
41 | MockitoAnnotations.initMocks(this);
42 | setUpMocks();
43 | SearchUserUseCase searchUserUseCase =
44 | new SearchUserUseCase(gitHubService, threadExecutor, postExecutionThread);
45 | searchUserPresenter = new SearchUserPresenter(searchUserUseCase);
46 | searchUserPresenter.attachView(searchUserView);
47 | }
48 |
49 | private void setUpMocks() {
50 | when(gitHubService.searchUser(MockFactory.TEST_USERNAME)).thenReturn(
51 | Observable.just(MockFactory.buildMockSearchResponse()));
52 | mockHttpException = new HttpException(Response.error(404, null));
53 | when(gitHubService.searchUser(MockFactory.TEST_USERNAME_ERROR)).thenReturn(
54 | Observable.error(mockHttpException));
55 | when(gitHubService.searchUser(MockFactory.TEST_USERNAME_NO_RESULTS)).thenReturn(
56 | Observable.just(MockFactory.buildEmptyMockSearchResponse()));
57 | when(threadExecutor.getScheduler()).thenReturn(Schedulers.immediate());
58 | when(postExecutionThread.getScheduler()).thenReturn(Schedulers.immediate());
59 | }
60 |
61 | @Test
62 | public void loadUsersShouldShowProgressIndicator() {
63 | searchUserPresenter.loadUsers(MockFactory.TEST_USERNAME);
64 | verify(searchUserView, times(1)).showProgressIndicator();
65 | }
66 |
67 | @Test
68 | public void loadUsersShouldShowErrorMessageIfNoInput() {
69 | searchUserPresenter.loadUsers(null);
70 | verify(searchUserView).showMessage("Enter a username");
71 | }
72 |
73 | @Test
74 | public void loadUsersShouldShowErrorMessageIfEmptyInput() {
75 | searchUserPresenter.loadUsers("");
76 | verify(searchUserView).showMessage("Enter a username");
77 | }
78 |
79 | @Test
80 | public void loadUsersShouldDisplayMessageIfNoResults() {
81 | searchUserPresenter.loadUsers(MockFactory.TEST_USERNAME_NO_RESULTS);
82 | verify(searchUserView).showMessage("No results");
83 | }
84 |
85 | @Test
86 | public void loadUsersShouldAddUsersWhenReceivedResult() {
87 | searchUserPresenter.loadUsers(MockFactory.TEST_USERNAME);
88 | SearchResponse searchResponse = MockFactory.buildMockSearchResponse();
89 | verify(searchUserView).showUsers(anyList());
90 | }
91 |
92 | @Test
93 | public void loadUsersShouldDisplayMessageOnError() {
94 | searchUserPresenter.loadUsers(MockFactory.TEST_USERNAME_ERROR);
95 | verify(searchUserView).showMessage("Error loading repositories");
96 | }
97 |
98 | @Test
99 | public void loadUsersShouldHideProgressIndicatorAfterCompleted() {
100 | searchUserPresenter.loadUsers(MockFactory.TEST_USERNAME);
101 | verify(searchUserView).hideProgressIndicator();
102 | }
103 |
104 | @Test
105 | public void onUserClickShouldStartDetailsActivity() {
106 | searchUserPresenter.onUserClick(MockFactory.TEST_USERNAME);
107 | verify(searchUserView).startUserDetailsActivity(MockFactory.TEST_USERNAME);
108 | }
109 |
110 | @Test
111 | public void destroyShouldUnsubscribeIfTrue() {
112 | searchUserPresenter = new SearchUserPresenter(mockSearchUserUseCase);
113 | searchUserPresenter.destroy(true);
114 | verify(mockSearchUserUseCase).unsubscribe();
115 | }
116 |
117 | @Test
118 | public void destroyShouldNotUnsubScribeIfFalse() {
119 | searchUserPresenter = new SearchUserPresenter(mockSearchUserUseCase);
120 | searchUserPresenter.destroy(false);
121 | verifyZeroInteractions(mockSearchUserUseCase);
122 | }
123 |
124 | @After
125 | public void cleanUp() {
126 | searchUserPresenter.detachView();
127 | }
128 | }
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/hugo/mvpsampleapplication/features/searchuser/SearchUserActivityTest.java:
--------------------------------------------------------------------------------
1 | package com.hugo.mvpsampleapplication.features.searchuser;
2 |
3 | import android.support.test.InstrumentationRegistry;
4 | import android.support.test.espresso.contrib.RecyclerViewActions;
5 | import android.support.test.rule.ActivityTestRule;
6 | import android.support.test.runner.AndroidJUnit4;
7 | import android.test.suitebuilder.annotation.LargeTest;
8 | import com.hugo.mvpsampleapplication.MockFactory;
9 | import com.hugo.mvpsampleapplication.R;
10 | import com.hugo.mvpsampleapplication.app.MVPApplication;
11 | import com.hugo.mvpsampleapplication.model.network.GitHubService;
12 | import com.hugo.mvpsampleapplication.model.network.SearchResponse;
13 | import com.hugo.mvpsampleapplication.utils.dependencyinjection.components.ApplicationComponent;
14 | import com.hugo.mvpsampleapplication.utils.dependencyinjection.modules.ApplicationModule;
15 | import it.cosenonjaviste.daggermock.DaggerMockRule;
16 | import org.junit.Before;
17 | import org.junit.Rule;
18 | import org.junit.Test;
19 | import org.junit.runner.RunWith;
20 | import org.mockito.Mock;
21 | import retrofit.HttpException;
22 | import retrofit.Response;
23 | import rx.Observable;
24 |
25 | import static android.support.test.espresso.Espresso.onView;
26 | import static android.support.test.espresso.action.ViewActions.click;
27 | import static android.support.test.espresso.action.ViewActions.typeText;
28 | import static android.support.test.espresso.assertion.ViewAssertions.matches;
29 | import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
30 | import static android.support.test.espresso.matcher.ViewMatchers.isRoot;
31 | import static android.support.test.espresso.matcher.ViewMatchers.withId;
32 | import static android.support.test.espresso.matcher.ViewMatchers.withText;
33 | import static com.hugo.mvpsampleapplication.OrientationChangeAction.orientationLandscape;
34 | import static org.hamcrest.Matchers.not;
35 | import static org.hamcrest.core.AllOf.allOf;
36 | import static org.mockito.Mockito.when;
37 |
38 | @RunWith(AndroidJUnit4.class)
39 | @LargeTest
40 | public class SearchUserActivityTest {
41 |
42 | @Rule public DaggerMockRule daggerMockRule =
43 | new DaggerMockRule<>(ApplicationComponent.class, new ApplicationModule()).set(
44 | new DaggerMockRule.ComponentSetter() {
45 | @Override
46 | public void setComponent(ApplicationComponent applicationComponent) {
47 | getApp().setApplicationComponent(applicationComponent);
48 | }
49 | });
50 |
51 | @Rule public final ActivityTestRule activityTestRule =
52 | new ActivityTestRule<>(SearchUserActivity.class, false, false);
53 |
54 | @Mock GitHubService mockGitHubService;
55 |
56 | private MVPApplication getApp() {
57 | return (MVPApplication) InstrumentationRegistry.getInstrumentation()
58 | .getTargetContext()
59 | .getApplicationContext();
60 | }
61 |
62 | @Before
63 | public void setUp() {
64 | setUpMockGitHubService();
65 | activityTestRule.launchActivity(null);
66 | }
67 |
68 | @SuppressWarnings("unchecked")
69 | private void setUpMockGitHubService() {
70 | when(mockGitHubService.searchUser(MockFactory.TEST_USERNAME)).thenReturn(
71 | Observable.just(MockFactory.buildMockSearchResponse()));
72 | when(mockGitHubService.getRepositoriesFromUser(MockFactory.TEST_USERNAME)).thenReturn(
73 | Observable.just(MockFactory.buildMockUserDetailsResponse()));
74 | when(mockGitHubService.searchUser(MockFactory.TEST_USERNAME_NO_RESULTS)).thenReturn(
75 | Observable.just(MockFactory.buildEmptyMockSearchResponse()));
76 |
77 | HttpException mockHttpException = new HttpException(Response.error(404, null));
78 | when(mockGitHubService.searchUser(MockFactory.TEST_USERNAME_ERROR)).thenReturn(
79 | Observable.error(mockHttpException));
80 | }
81 |
82 | @Test
83 | public void clickSearchWithNoQueryInputShouldReturnErrorMessage() throws Exception {
84 | onView(withId(R.id.button_search)).perform(click());
85 | onView(allOf(withId(android.support.design.R.id.snackbar_text),
86 | withText("Enter a username"))).check(matches(isDisplayed()));
87 | }
88 |
89 | @Test
90 | public void networkErrorShouldDisplayInSnackBar() throws Exception {
91 | performSearch(MockFactory.TEST_USERNAME_ERROR);
92 | onView(withId(android.support.design.R.id.snackbar_text)).check(
93 | matches(withText("Error loading users")));
94 | }
95 |
96 | @Test
97 | public void noUserErrorShouldDisplayInSnackBar() throws Exception {
98 | performSearch(MockFactory.TEST_USERNAME_NO_RESULTS);
99 | onView(withId(android.support.design.R.id.snackbar_text)).check(
100 | matches(withText("No users found")));
101 | }
102 |
103 | @Test
104 | public void searchResultsShouldBeHiddenIfNoSearch() throws Exception {
105 | onView(withId(R.id.search_user_list)).check(matches(not(isDisplayed())));
106 | }
107 |
108 | @Test
109 | public void clickSearchShouldDisplayResults() throws Exception {
110 | performSearch(MockFactory.TEST_USERNAME);
111 | onView(withId(R.id.search_user_list)).check(matches(isDisplayed()));
112 | }
113 |
114 | @Test
115 | public void clickOnSearchResultItemShouldDisplayUserDetails() throws Exception {
116 | performSearch(MockFactory.TEST_USERNAME);
117 | onView(withId(R.id.search_user_list)).perform(
118 | RecyclerViewActions.actionOnItemAtPosition(0, click()));
119 | onView(withText(MockFactory.TEST_REPOSITORY)).check(matches(isDisplayed()));
120 | }
121 |
122 | @Test
123 | public void searchQueryTextShouldPersistAfterOrientationChange() throws Exception {
124 | onView(withId(R.id.edit_text_username)).perform(typeText("test"));
125 | onView(isRoot()).perform(orientationLandscape());
126 | onView(withId(R.id.edit_text_username)).check(matches(withText("test")));
127 | }
128 |
129 | @Test
130 | public void searchResultsShouldPersistAfterOrientationChange() throws Exception {
131 | performSearch(MockFactory.TEST_USERNAME);
132 | onView(isRoot()).perform(orientationLandscape());
133 | onView(withId(R.id.search_user_list)).check(matches(isDisplayed()));
134 | }
135 |
136 | private void performSearch(String query) {
137 | onView(withId(R.id.edit_text_username)).perform(typeText(query));
138 | onView(withId(R.id.button_search)).perform(click());
139 | }
140 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/hugo/mvpsampleapplication/features/searchuser/SearchUserFragment.java:
--------------------------------------------------------------------------------
1 | package com.hugo.mvpsampleapplication.features.searchuser;
2 |
3 | import android.content.Context;
4 | import android.os.Bundle;
5 | import android.support.design.widget.Snackbar;
6 | import android.support.v4.app.Fragment;
7 | import android.support.v7.widget.LinearLayoutManager;
8 | import android.support.v7.widget.RecyclerView;
9 | import android.view.KeyEvent;
10 | import android.view.LayoutInflater;
11 | import android.view.View;
12 | import android.view.ViewGroup;
13 | import android.view.inputmethod.EditorInfo;
14 | import android.view.inputmethod.InputMethodManager;
15 | import android.widget.EditText;
16 | import android.widget.ImageButton;
17 | import android.widget.RelativeLayout;
18 | import android.widget.TextView;
19 | import butterknife.Bind;
20 | import butterknife.ButterKnife;
21 | import com.hugo.mvpsampleapplication.R;
22 | import com.hugo.mvpsampleapplication.features.BaseActivity;
23 | import com.hugo.mvpsampleapplication.model.entities.User;
24 | import java.util.List;
25 | import javax.inject.Inject;
26 |
27 | public class SearchUserFragment extends Fragment implements SearchUserView {
28 |
29 | private static final String SEARCH_USER_LIST_VISIBILITY = "searchUserListVisibility";
30 | private static final String PROGRESS_VISIBILITY = "progressVisibility";
31 |
32 | @Bind(R.id.search_user_list) RecyclerView userList;
33 | @Bind(R.id.progress_indicator) RelativeLayout progressIndicator;
34 | @Bind(R.id.button_search) ImageButton searchButton;
35 | @Bind(R.id.edit_text_username) EditText editTextUsername;
36 |
37 | @Inject SearchUserPresenter searchUserPresenter;
38 | private UserListAdapter userListAdapter;
39 | private ActivityListener activityListener;
40 | private LinearLayoutManager layoutManager;
41 |
42 | public interface ActivityListener {
43 | void startUserDetails(String username);
44 | }
45 |
46 | public SearchUserFragment() {
47 |
48 | }
49 |
50 | public static SearchUserFragment newInstance() {
51 | return new SearchUserFragment();
52 | }
53 |
54 | @Override
55 | public void onCreate(Bundle savedInstanceState) {
56 | super.onCreate(savedInstanceState);
57 | setRetainInstance(true);
58 | ((BaseActivity) getActivity()).getUserComponent().inject(this);
59 | setUpUserListAdapter();
60 | }
61 |
62 | private void setUpUserListAdapter() {
63 | userListAdapter = new UserListAdapter();
64 | userListAdapter.setOnItemClickListener(new UserListAdapter.OnItemClickListener() {
65 | @Override
66 | public void onItemClick(String username) {
67 | if (searchUserPresenter != null && !username.isEmpty()) {
68 | searchUserPresenter.onUserClick(username);
69 | }
70 | }
71 | });
72 | }
73 |
74 | @Override
75 | public void onAttach(Context context) {
76 | super.onAttach(context);
77 | if (context instanceof ActivityListener) {
78 | this.activityListener = (ActivityListener) context;
79 | }
80 | layoutManager = new LinearLayoutManager(context);
81 | }
82 |
83 | @Override
84 | public View onCreateView(LayoutInflater inflater, ViewGroup container,
85 | Bundle savedInstanceState) {
86 | View fragmentView = inflater.inflate(R.layout.fragment_search_user, container, false);
87 | ButterKnife.bind(this, fragmentView);
88 | if (savedInstanceState != null) {
89 | restoreState(savedInstanceState);
90 | }
91 | setSearchButtonOnClickListener();
92 | setEditTextActionListener();
93 | searchUserPresenter.attachView(this);
94 | userList.setLayoutManager(layoutManager);
95 | userList.setAdapter(userListAdapter);
96 | return fragmentView;
97 | }
98 |
99 | public void setSearchButtonOnClickListener() {
100 | searchButton.setOnClickListener(new View.OnClickListener() {
101 | @Override
102 | public void onClick(View v) {
103 | searchUsers();
104 | }
105 | });
106 | }
107 |
108 | private void setEditTextActionListener() {
109 | editTextUsername.setOnEditorActionListener(new TextView.OnEditorActionListener() {
110 | @Override
111 | public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
112 | if (actionId == EditorInfo.IME_ACTION_SEARCH) {
113 | searchUsers();
114 | return true;
115 | }
116 | return false;
117 | }
118 | });
119 | }
120 |
121 | public void searchUsers() {
122 | String username = editTextUsername.getText().toString();
123 | hideKeyBoard(editTextUsername);
124 | searchUserPresenter.loadUsers(username);
125 | }
126 |
127 | private void hideKeyBoard(View view) {
128 | view.clearFocus();
129 | InputMethodManager mgr =
130 | (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
131 | mgr.hideSoftInputFromWindow(view.getWindowToken(), 0);
132 | }
133 |
134 | @Override
135 | public void showMessage(String message) {
136 | View rootView = getActivity().getWindow().getDecorView().findViewById(android.R.id.content);
137 | Snackbar snackbar = Snackbar.make(rootView, message, Snackbar.LENGTH_SHORT);
138 | snackbar.show();
139 | }
140 |
141 | @Override
142 | public void showProgressIndicator() {
143 | userList.setVisibility(View.INVISIBLE);
144 | progressIndicator.setVisibility(View.VISIBLE);
145 | }
146 |
147 | @Override
148 | public void hideProgressIndicator() {
149 | progressIndicator.setVisibility(View.INVISIBLE);
150 | userList.setVisibility(View.VISIBLE);
151 | }
152 |
153 | @Override
154 | public void showUsers(List users) {
155 | userListAdapter.setUsers(users);
156 | userListAdapter.notifyDataSetChanged();
157 | }
158 |
159 | @Override
160 | public void startUserDetailsActivity(String username) {
161 | activityListener.startUserDetails(username);
162 | }
163 |
164 | @SuppressWarnings("ResourceType")
165 | private void restoreState(Bundle savedInstanceState) {
166 | progressIndicator.setVisibility(savedInstanceState.getInt(PROGRESS_VISIBILITY));
167 | userList.setVisibility(savedInstanceState.getInt(SEARCH_USER_LIST_VISIBILITY));
168 | }
169 |
170 | @Override
171 | public void onSaveInstanceState(Bundle state) {
172 | super.onSaveInstanceState(state);
173 | state.putInt(PROGRESS_VISIBILITY, progressIndicator.getVisibility());
174 | state.putInt(SEARCH_USER_LIST_VISIBILITY, userList.getVisibility());
175 | }
176 |
177 | @Override
178 | public void onDetach() {
179 | super.onDetach();
180 | activityListener = null;
181 | layoutManager = null;
182 | ButterKnife.unbind(this);
183 | searchUserPresenter.destroy(false);
184 | }
185 |
186 | @Override
187 | public void onDestroyView() {
188 | super.onDestroyView();
189 | }
190 |
191 | @Override
192 | public void onDestroy() {
193 | super.onDestroy();
194 | searchUserPresenter.destroy(true);
195 | }
196 | }
197 |
--------------------------------------------------------------------------------