├── .gitignore ├── .idea ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── gradle.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── br │ │ └── com │ │ └── monitoratec │ │ └── app │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── br │ │ │ └── com │ │ │ └── monitoratec │ │ │ └── app │ │ │ ├── MyApplication.java │ │ │ ├── dagger │ │ │ ├── DiComponent.java │ │ │ ├── UiComponent.java │ │ │ ├── module │ │ │ │ ├── ApplicationModule.java │ │ │ │ ├── PreferenceModule.java │ │ │ │ ├── infraestructure │ │ │ │ │ ├── ManagerModule.java │ │ │ │ │ ├── NetworkModule.java │ │ │ │ │ └── ServiceModule.java │ │ │ │ └── presentation │ │ │ │ │ ├── HelperModule.java │ │ │ │ │ └── PresenterModule.java │ │ │ └── scope │ │ │ │ └── PerActivity.java │ │ │ ├── domain │ │ │ ├── entity │ │ │ │ ├── AccessToken.java │ │ │ │ ├── Repo.java │ │ │ │ ├── Status.java │ │ │ │ └── User.java │ │ │ └── repository │ │ │ │ ├── GitHubOAuthRepository.java │ │ │ │ ├── GitHubRepository.java │ │ │ │ └── GitHubStatusRepository.java │ │ │ ├── infraestructure │ │ │ └── storage │ │ │ │ ├── manager │ │ │ │ ├── GitHubManager.java │ │ │ │ ├── GitHubOAuthManager.java │ │ │ │ └── GitHubStatusManager.java │ │ │ │ └── service │ │ │ │ ├── GitHubOAuthService.java │ │ │ │ ├── GitHubService.java │ │ │ │ └── GitHubStatusService.java │ │ │ └── presentation │ │ │ ├── base │ │ │ └── BaseActivity.java │ │ │ ├── helper │ │ │ └── AppHelper.java │ │ │ └── ui │ │ │ ├── auth │ │ │ ├── AuthActivity.java │ │ │ ├── AuthContract.java │ │ │ └── AuthPresenter.java │ │ │ └── repos │ │ │ ├── ReposActivity.java │ │ │ ├── ReposAdapter.java │ │ │ ├── ReposContract.java │ │ │ └── ReposPresenter.java │ └── res │ │ ├── drawable │ │ ├── ic_add.xml │ │ ├── ic_call.xml │ │ └── ic_github.xml │ │ ├── layout │ │ ├── activity_main.xml │ │ ├── activity_repos.xml │ │ └── content_repos.xml │ │ ├── menu │ │ └── main_menu.xml │ │ ├── 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 │ │ ├── values-pt-rBR │ │ └── strings.xml │ │ ├── values-v21 │ │ └── styles.xml │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── br │ └── com │ └── monitoratec │ └── app │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 46 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Tendências do Desenvolvimento Android 2 | 3 | Treinamento formulado com o objetivo de apresentar algumas das principais tendências relacionadas ao desenvolvimento Android, visando aplicações mais robustas, padronizadas e manuteníveis. Para isso, a seguinte ementa foi elaborada: 4 | 5 | 1. [Introdução](https://github.com/brain-machine/android-dev-trends/wiki/1.-Introdu%C3%A7%C3%A3o) 6 | 1. Retrofit 7 | + [Parte 1](https://github.com/brain-machine/android-dev-trends/wiki/2.-Retrofit-(Parte-1)) 8 | + [Parte 2](https://github.com/brain-machine/android-dev-trends/wiki/2.-Retrofit-(Parte-2)) 9 | 1. [RxJava](https://github.com/brain-machine/android-dev-trends/wiki/3.-RxJava) 10 | 1. [Dagger 2](https://github.com/brain-machine/android-dev-trends/wiki/4.-Dagger-2) 11 | 1. [Clean Architecture](https://github.com/brain-machine/android-dev-trends/wiki/5.-Clean-Architecture) 12 | 1. [Model-View-Presenter (MVP)](https://github.com/brain-machine/android-dev-trends/wiki/6.-Model-View-Presenter-(MVP)) 13 | 1. [Refactoring (Clean MVP)](https://github.com/brain-machine/android-dev-trends/wiki/7.-Refactoring-(Clean-MVP)) 14 | 15 | Tópicos [Extras](https://github.com/brain-machine/android-dev-trends/wiki/Extras) 16 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id "me.tatarka.retrolambda" version "3.4.0" 3 | } 4 | 5 | apply plugin: 'com.android.application' 6 | 7 | android { 8 | compileSdkVersion 25 9 | buildToolsVersion "25.0.2" 10 | defaultConfig { 11 | applicationId "br.com.monitoratec.app" 12 | minSdkVersion 15 13 | targetSdkVersion 25 14 | versionCode 1 15 | versionName "1.0" 16 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 17 | vectorDrawables.useSupportLibrary true 18 | } 19 | buildTypes { 20 | release { 21 | minifyEnabled false 22 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 23 | } 24 | } 25 | compileOptions { 26 | sourceCompatibility JavaVersion.VERSION_1_8 27 | targetCompatibility JavaVersion.VERSION_1_8 28 | } 29 | } 30 | 31 | ext { 32 | supportLibVersion = '25.1.0' 33 | retrofitLibVersion = '2.1.0' 34 | butterknifeLibVersion = '8.4.0' 35 | daggerLibVersion = '2.8' 36 | } 37 | 38 | dependencies { 39 | compile fileTree(dir: 'libs', include: ['*.jar']) 40 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 41 | exclude group: 'com.android.support', module: 'support-annotations' 42 | }) 43 | compile "com.android.support:appcompat-v7:${supportLibVersion}" 44 | compile "com.android.support:design:${supportLibVersion}" 45 | testCompile 'junit:junit:4.12' 46 | 47 | // Retrofit (by ReactiveX) 48 | compile "com.squareup.retrofit2:retrofit:${retrofitLibVersion}" 49 | compile "com.squareup.retrofit2:converter-gson:${retrofitLibVersion}" 50 | compile "com.squareup.retrofit2:adapter-rxjava:${retrofitLibVersion}" 51 | 52 | // RxJava (by ReactiveX) 53 | compile 'io.reactivex:rxandroid:1.2.1' 54 | compile 'io.reactivex:rxjava:1.1.6' 55 | 56 | // Butter Knife (by Jake Wharton) 57 | compile "com.jakewharton:butterknife:${butterknifeLibVersion}" 58 | annotationProcessor "com.jakewharton:butterknife-compiler:${butterknifeLibVersion}" 59 | 60 | // RxBinding (by Jake Wharton) 61 | compile 'com.jakewharton.rxbinding:rxbinding:1.0.0' 62 | 63 | // Dagger 2 (by Google) 64 | compile "com.google.dagger:dagger:${daggerLibVersion}" 65 | annotationProcessor "com.google.dagger:dagger-compiler:${daggerLibVersion}" 66 | compile 'com.android.support:recyclerview-v7:25.1.0' 67 | } 68 | -------------------------------------------------------------------------------- /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/falvojr/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/androidTest/java/br/com/monitoratec/app/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package br.com.monitoratec.app; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("br.com.monitoratec.app", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 30 | 31 | 32 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /app/src/main/java/br/com/monitoratec/app/MyApplication.java: -------------------------------------------------------------------------------- 1 | package br.com.monitoratec.app; 2 | 3 | import android.app.Application; 4 | 5 | import br.com.monitoratec.app.dagger.DaggerDiComponent; 6 | import br.com.monitoratec.app.dagger.DiComponent; 7 | import br.com.monitoratec.app.dagger.UiComponent; 8 | import br.com.monitoratec.app.dagger.module.ApplicationModule; 9 | 10 | /** 11 | * My custom {@link Application}. 12 | * 13 | * Created by falvojr on 1/12/17. 14 | */ 15 | public class MyApplication extends Application { 16 | 17 | private DiComponent mDiComponent; 18 | 19 | @Override 20 | public void onCreate() { 21 | super.onCreate(); 22 | 23 | mDiComponent = DaggerDiComponent.builder() 24 | .applicationModule(new ApplicationModule(this)) 25 | .build(); 26 | } 27 | 28 | public UiComponent getDaggerUiComponent() { 29 | return mDiComponent.uiComponent(); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /app/src/main/java/br/com/monitoratec/app/dagger/DiComponent.java: -------------------------------------------------------------------------------- 1 | package br.com.monitoratec.app.dagger; 2 | 3 | import javax.inject.Singleton; 4 | 5 | import br.com.monitoratec.app.dagger.module.ApplicationModule; 6 | import br.com.monitoratec.app.dagger.module.PreferenceModule; 7 | import br.com.monitoratec.app.dagger.module.infraestructure.ManagerModule; 8 | import br.com.monitoratec.app.dagger.module.infraestructure.NetworkModule; 9 | import br.com.monitoratec.app.dagger.module.infraestructure.ServiceModule; 10 | import br.com.monitoratec.app.dagger.module.presentation.HelperModule; 11 | import dagger.Component; 12 | 13 | /** 14 | * Main Dagger {@link Component}. 15 | * 16 | * Created by falvojr on 1/12/17. 17 | */ 18 | @Singleton 19 | @Component(modules = { 20 | ApplicationModule.class, 21 | HelperModule.class, 22 | PreferenceModule.class, 23 | NetworkModule.class, 24 | ServiceModule.class, 25 | ManagerModule.class 26 | }) 27 | public interface DiComponent { 28 | UiComponent uiComponent(); 29 | } 30 | -------------------------------------------------------------------------------- /app/src/main/java/br/com/monitoratec/app/dagger/UiComponent.java: -------------------------------------------------------------------------------- 1 | package br.com.monitoratec.app.dagger; 2 | 3 | import br.com.monitoratec.app.dagger.module.presentation.PresenterModule; 4 | import br.com.monitoratec.app.dagger.scope.PerActivity; 5 | import br.com.monitoratec.app.presentation.ui.auth.AuthActivity; 6 | import br.com.monitoratec.app.presentation.ui.repos.ReposActivity; 7 | import dagger.Subcomponent; 8 | 9 | /** 10 | * Dagger UI {@link Subcomponent} (per activity scope). 11 | * 12 | * Created by falvojr on 1/13/17. 13 | */ 14 | @PerActivity 15 | @Subcomponent(modules = {PresenterModule.class}) 16 | public interface UiComponent { 17 | void inject(AuthActivity activity); 18 | void inject(ReposActivity reposActivity); 19 | } 20 | -------------------------------------------------------------------------------- /app/src/main/java/br/com/monitoratec/app/dagger/module/ApplicationModule.java: -------------------------------------------------------------------------------- 1 | package br.com.monitoratec.app.dagger.module; 2 | 3 | import android.app.Application; 4 | import android.content.Context; 5 | 6 | import javax.inject.Singleton; 7 | 8 | import dagger.Module; 9 | import dagger.Provides; 10 | 11 | /** 12 | * Module for {@link Application} related instances. 13 | * 14 | * Created by falvojr on 1/12/17. 15 | */ 16 | @Module 17 | public class ApplicationModule { 18 | 19 | private Application mApplication; 20 | 21 | public ApplicationModule(Application app) { 22 | mApplication = app; 23 | } 24 | 25 | @Provides 26 | @Singleton 27 | Application providesApplication() { 28 | return mApplication; 29 | } 30 | 31 | @Provides 32 | @Singleton 33 | Context providesContext(Application app) { 34 | return app.getBaseContext(); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /app/src/main/java/br/com/monitoratec/app/dagger/module/PreferenceModule.java: -------------------------------------------------------------------------------- 1 | package br.com.monitoratec.app.dagger.module; 2 | 3 | import android.content.Context; 4 | import android.content.SharedPreferences; 5 | import android.preference.PreferenceManager; 6 | 7 | import javax.inject.Named; 8 | import javax.inject.Singleton; 9 | 10 | import br.com.monitoratec.app.R; 11 | import dagger.Module; 12 | import dagger.Provides; 13 | 14 | /** 15 | * Module for {@link SharedPreferences} instance(s). 16 | * 17 | * Created by falvojr on 1/12/17. 18 | */ 19 | @Module 20 | public class PreferenceModule { 21 | 22 | @Provides 23 | @Singleton 24 | SharedPreferences providesSharedPreferences(Context context) { 25 | final String fileName = context.getString(R.string.sp_file); 26 | return context.getSharedPreferences(fileName, Context.MODE_PRIVATE); 27 | } 28 | 29 | @Provides 30 | @Singleton 31 | @Named("non-secret") 32 | SharedPreferences providesSharedPreferencesDefault(Context context) { 33 | return PreferenceManager.getDefaultSharedPreferences(context); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /app/src/main/java/br/com/monitoratec/app/dagger/module/infraestructure/ManagerModule.java: -------------------------------------------------------------------------------- 1 | package br.com.monitoratec.app.dagger.module.infraestructure; 2 | 3 | import javax.inject.Singleton; 4 | 5 | import br.com.monitoratec.app.domain.repository.GitHubOAuthRepository; 6 | import br.com.monitoratec.app.domain.repository.GitHubRepository; 7 | import br.com.monitoratec.app.domain.repository.GitHubStatusRepository; 8 | import br.com.monitoratec.app.infraestructure.storage.manager.GitHubManager; 9 | import br.com.monitoratec.app.infraestructure.storage.manager.GitHubOAuthManager; 10 | import br.com.monitoratec.app.infraestructure.storage.manager.GitHubStatusManager; 11 | import br.com.monitoratec.app.infraestructure.storage.service.GitHubOAuthService; 12 | import br.com.monitoratec.app.infraestructure.storage.service.GitHubService; 13 | import br.com.monitoratec.app.infraestructure.storage.service.GitHubStatusService; 14 | import dagger.Module; 15 | import dagger.Provides; 16 | 17 | /** 18 | * Module for managers (Repository pattern implementations). 19 | * 20 | * Created by falvojr on 1/13/17. 21 | */ 22 | @Module 23 | public class ManagerModule { 24 | 25 | @Singleton 26 | @Provides 27 | GitHubRepository providesGitHubRepository( 28 | GitHubService gitHubService) { 29 | return new GitHubManager(gitHubService); 30 | } 31 | 32 | @Singleton 33 | @Provides 34 | GitHubStatusRepository providesGitHubStatusRepository( 35 | GitHubStatusService gitHubStatusService) { 36 | return new GitHubStatusManager(gitHubStatusService); 37 | } 38 | 39 | @Singleton 40 | @Provides 41 | GitHubOAuthRepository providesGitHubOAuthRepository( 42 | GitHubOAuthService gitHubOAuthService) { 43 | return new GitHubOAuthManager(gitHubOAuthService); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /app/src/main/java/br/com/monitoratec/app/dagger/module/infraestructure/NetworkModule.java: -------------------------------------------------------------------------------- 1 | package br.com.monitoratec.app.dagger.module.infraestructure; 2 | 3 | import com.google.gson.Gson; 4 | import com.google.gson.GsonBuilder; 5 | 6 | import javax.inject.Named; 7 | import javax.inject.Singleton; 8 | 9 | import br.com.monitoratec.app.infraestructure.storage.service.GitHubOAuthService; 10 | import br.com.monitoratec.app.infraestructure.storage.service.GitHubService; 11 | import br.com.monitoratec.app.infraestructure.storage.service.GitHubStatusService; 12 | import dagger.Module; 13 | import dagger.Provides; 14 | import retrofit2.Retrofit; 15 | import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory; 16 | import retrofit2.converter.gson.GsonConverterFactory; 17 | 18 | /** 19 | * Module for network instances (Retrofit configurations). 20 | * 21 | * Created by falvojr on 1/12/17. 22 | */ 23 | @Module 24 | public class NetworkModule { 25 | 26 | static final String RETROFIT_GITHUB = "GitHub"; 27 | static final String RETROFIT_GITHUB_STATUS = "GitHubStatus"; 28 | static final String RETROFIT_GITHUB_OAUTH = "GitHubOAuth"; 29 | 30 | @Provides 31 | @Singleton 32 | Gson providesGson() { 33 | return new GsonBuilder() 34 | .setDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'") 35 | .create(); 36 | } 37 | 38 | @Provides 39 | @Singleton 40 | GsonConverterFactory providesGsonConverterFactory(Gson gson) { 41 | return GsonConverterFactory.create(gson); 42 | } 43 | 44 | @Provides 45 | @Singleton 46 | RxJavaCallAdapterFactory providesRxJavaCallAdapterFactory() { 47 | return RxJavaCallAdapterFactory.create(); 48 | } 49 | 50 | @Provides 51 | @Singleton 52 | @Named(RETROFIT_GITHUB) 53 | Retrofit providesRetrofitGitHub(GsonConverterFactory gsonFactory, 54 | RxJavaCallAdapterFactory rxFactory) { 55 | return buildRetrofit(gsonFactory, rxFactory, GitHubService.BASE_URL); 56 | } 57 | 58 | @Provides 59 | @Singleton 60 | @Named(RETROFIT_GITHUB_STATUS) 61 | Retrofit providesRetrofitGitHubStatus(GsonConverterFactory gsonFactory, 62 | RxJavaCallAdapterFactory rxFactory) { 63 | return buildRetrofit(gsonFactory, rxFactory, GitHubStatusService.BASE_URL); 64 | } 65 | 66 | @Provides 67 | @Singleton 68 | @Named(RETROFIT_GITHUB_OAUTH) 69 | Retrofit providesRetrofitGitHubOAuth(GsonConverterFactory gsonFactory, 70 | RxJavaCallAdapterFactory rxFactory) { 71 | return buildRetrofit(gsonFactory, rxFactory, GitHubOAuthService.BASE_URL); 72 | } 73 | 74 | private Retrofit buildRetrofit(GsonConverterFactory converterFactory, 75 | RxJavaCallAdapterFactory callAdapterFactory, 76 | String baseUrl) { 77 | return new Retrofit.Builder() 78 | .baseUrl(baseUrl) 79 | .addConverterFactory(converterFactory) 80 | .addCallAdapterFactory(callAdapterFactory) 81 | .build(); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /app/src/main/java/br/com/monitoratec/app/dagger/module/infraestructure/ServiceModule.java: -------------------------------------------------------------------------------- 1 | package br.com.monitoratec.app.dagger.module.infraestructure; 2 | 3 | import javax.inject.Named; 4 | import javax.inject.Singleton; 5 | 6 | import br.com.monitoratec.app.infraestructure.storage.service.GitHubOAuthService; 7 | import br.com.monitoratec.app.infraestructure.storage.service.GitHubService; 8 | import br.com.monitoratec.app.infraestructure.storage.service.GitHubStatusService; 9 | import dagger.Module; 10 | import dagger.Provides; 11 | import retrofit2.Retrofit; 12 | 13 | import static br.com.monitoratec.app.dagger.module.infraestructure.NetworkModule.RETROFIT_GITHUB; 14 | import static br.com.monitoratec.app.dagger.module.infraestructure.NetworkModule.RETROFIT_GITHUB_OAUTH; 15 | import static br.com.monitoratec.app.dagger.module.infraestructure.NetworkModule.RETROFIT_GITHUB_STATUS; 16 | 17 | /** 18 | * Module for services (Retrofit interfaces). 19 | * 20 | * Created by falvojr on 1/12/17. 21 | */ 22 | @Module 23 | public class ServiceModule { 24 | 25 | @Singleton 26 | @Provides 27 | GitHubService providesGitHub(@Named(RETROFIT_GITHUB) Retrofit retrofit) { 28 | return retrofit.create(GitHubService.class); 29 | } 30 | 31 | @Singleton 32 | @Provides 33 | GitHubStatusService providesGitHubStatus(@Named(RETROFIT_GITHUB_STATUS) Retrofit retrofit) { 34 | return retrofit.create(GitHubStatusService.class); 35 | } 36 | 37 | @Singleton 38 | @Provides 39 | GitHubOAuthService providesGitHubOAuth(@Named(RETROFIT_GITHUB_OAUTH) Retrofit retrofit) { 40 | return retrofit.create(GitHubOAuthService.class); 41 | } 42 | 43 | } -------------------------------------------------------------------------------- /app/src/main/java/br/com/monitoratec/app/dagger/module/presentation/HelperModule.java: -------------------------------------------------------------------------------- 1 | package br.com.monitoratec.app.dagger.module.presentation; 2 | 3 | import android.content.Context; 4 | 5 | import br.com.monitoratec.app.presentation.helper.AppHelper; 6 | import dagger.Module; 7 | import dagger.Provides; 8 | import dagger.Reusable; 9 | 10 | /** 11 | * Module for app helpers. 12 | * 13 | * Created by falvojr on 1/13/17. 14 | */ 15 | @Module 16 | public class HelperModule { 17 | @Provides 18 | @Reusable 19 | AppHelper provideTextHelper(Context context) { 20 | return new AppHelper(context); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /app/src/main/java/br/com/monitoratec/app/dagger/module/presentation/PresenterModule.java: -------------------------------------------------------------------------------- 1 | package br.com.monitoratec.app.dagger.module.presentation; 2 | 3 | import br.com.monitoratec.app.dagger.scope.PerActivity; 4 | import br.com.monitoratec.app.domain.repository.GitHubOAuthRepository; 5 | import br.com.monitoratec.app.domain.repository.GitHubRepository; 6 | import br.com.monitoratec.app.domain.repository.GitHubStatusRepository; 7 | import br.com.monitoratec.app.presentation.ui.auth.AuthContract; 8 | import br.com.monitoratec.app.presentation.ui.auth.AuthPresenter; 9 | import br.com.monitoratec.app.presentation.ui.repos.ReposContract; 10 | import br.com.monitoratec.app.presentation.ui.repos.ReposPresenter; 11 | import dagger.Module; 12 | import dagger.Provides; 13 | 14 | /** 15 | * Module for presenters. 16 | * 17 | * Created by falvojr on 1/13/17. 18 | */ 19 | @Module 20 | public class PresenterModule { 21 | 22 | @PerActivity 23 | @Provides 24 | AuthContract.Presenter providesAuthPresenter( 25 | GitHubRepository gitHubRepository, 26 | GitHubStatusRepository gitHubStatusRepository, 27 | GitHubOAuthRepository gitHubOAuthRepository) { 28 | return new AuthPresenter(gitHubRepository, 29 | gitHubStatusRepository, 30 | gitHubOAuthRepository); 31 | } 32 | 33 | @PerActivity 34 | @Provides 35 | ReposContract.Presenter providesResposPresenter(GitHubRepository gitHubRepository) { 36 | return new ReposPresenter(gitHubRepository); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /app/src/main/java/br/com/monitoratec/app/dagger/scope/PerActivity.java: -------------------------------------------------------------------------------- 1 | package br.com.monitoratec.app.dagger.scope; 2 | 3 | import java.lang.annotation.Retention; 4 | import java.lang.annotation.RetentionPolicy; 5 | 6 | import javax.inject.Scope; 7 | 8 | /** 9 | * Custom {@link Scope} per activity. 10 | * 11 | * Created by falvojr on 1/13/17. 12 | */ 13 | @Scope 14 | @Retention(RetentionPolicy.RUNTIME) 15 | public @interface PerActivity { 16 | } 17 | -------------------------------------------------------------------------------- /app/src/main/java/br/com/monitoratec/app/domain/entity/AccessToken.java: -------------------------------------------------------------------------------- 1 | package br.com.monitoratec.app.domain.entity; 2 | 3 | /** 4 | * Access Token entity for GitHub API OAuth. 5 | * 6 | * @see Access Token 7 | * 8 | * Created by falvojr on 1/11/17. 9 | */ 10 | public class AccessToken { 11 | private String access_token; 12 | private String token_type; 13 | 14 | public String getAuthCredential() { 15 | final char firstChar = token_type.charAt(0); 16 | if (!Character.isUpperCase(firstChar)) { 17 | final String first = Character.toString(firstChar).toUpperCase(); 18 | token_type = first + token_type.substring(1); 19 | } 20 | return token_type + " " + access_token; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /app/src/main/java/br/com/monitoratec/app/domain/entity/Repo.java: -------------------------------------------------------------------------------- 1 | package br.com.monitoratec.app.domain.entity; 2 | 3 | /** 4 | * Repository entity. 5 | * 6 | * Created by falvojr on 1/14/17. 7 | */ 8 | public class Repo { 9 | public String name; 10 | } 11 | -------------------------------------------------------------------------------- /app/src/main/java/br/com/monitoratec/app/domain/entity/Status.java: -------------------------------------------------------------------------------- 1 | package br.com.monitoratec.app.domain.entity; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | 5 | import java.util.Date; 6 | 7 | import br.com.monitoratec.app.R; 8 | 9 | /** 10 | * Status entity for GitHub Status API. 11 | * 12 | * @see Last Message 13 | * 14 | * Created by falvojr on 1/9/17. 15 | */ 16 | public class Status { 17 | @SerializedName("status") 18 | public Type type; 19 | @SerializedName("body") 20 | public String message; 21 | @SerializedName("created_on") 22 | public Date createdOn; 23 | 24 | public enum Type { 25 | NONE(android.R.color.black, R.string.txt_loading), 26 | @SerializedName("good") 27 | GOOD(R.color.materialGreen500, R.string.txt_good), 28 | @SerializedName("minor") 29 | MINOR(R.color.materialOrange500, R.string.txt_minor), 30 | @SerializedName("major") 31 | MAJOR(R.color.materialRed500, R.string.txt_major); 32 | 33 | private int colorRes; 34 | private int messageRes; 35 | 36 | Type(int colorRes, int messageRes) { 37 | this.colorRes = colorRes; 38 | this.messageRes = messageRes; 39 | } 40 | 41 | public int getColorRes() { 42 | return colorRes; 43 | } 44 | 45 | public int getMessageRes() { 46 | return messageRes; 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /app/src/main/java/br/com/monitoratec/app/domain/entity/User.java: -------------------------------------------------------------------------------- 1 | package br.com.monitoratec.app.domain.entity; 2 | 3 | import android.os.Parcel; 4 | import android.os.Parcelable; 5 | 6 | import com.google.gson.annotations.SerializedName; 7 | 8 | /** 9 | * User entity for GitHub API. 10 | * 11 | * @see Get User 12 | * @see Android Parcelable Generator 13 | *

14 | * Created by falvojr on 1/11/17. 15 | */ 16 | public class User implements Parcelable { 17 | 18 | public String login; 19 | @SerializedName("avatar_url") 20 | public String avatarUrl; 21 | 22 | @Override 23 | public int describeContents() { 24 | return 0; 25 | } 26 | 27 | @Override 28 | public void writeToParcel(Parcel dest, int flags) { 29 | dest.writeString(this.login); 30 | dest.writeString(this.avatarUrl); 31 | } 32 | 33 | public User() { 34 | } 35 | 36 | protected User(Parcel in) { 37 | this.login = in.readString(); 38 | this.avatarUrl = in.readString(); 39 | } 40 | 41 | public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { 42 | @Override 43 | public User createFromParcel(Parcel source) { 44 | return new User(source); 45 | } 46 | 47 | @Override 48 | public User[] newArray(int size) { 49 | return new User[size]; 50 | } 51 | }; 52 | } 53 | -------------------------------------------------------------------------------- /app/src/main/java/br/com/monitoratec/app/domain/repository/GitHubOAuthRepository.java: -------------------------------------------------------------------------------- 1 | package br.com.monitoratec.app.domain.repository; 2 | 3 | import br.com.monitoratec.app.domain.entity.AccessToken; 4 | import rx.Observable; 5 | 6 | /** 7 | * Repository interface da API GitHub OAuth. 8 | *

9 | * Created by falvojr on 1/13/17. 10 | */ 11 | public interface GitHubOAuthRepository { 12 | 13 | Observable getAccessToken(String clientId, String clientSecret, String code); 14 | } 15 | -------------------------------------------------------------------------------- /app/src/main/java/br/com/monitoratec/app/domain/repository/GitHubRepository.java: -------------------------------------------------------------------------------- 1 | package br.com.monitoratec.app.domain.repository; 2 | 3 | import java.util.List; 4 | 5 | import br.com.monitoratec.app.domain.entity.Repo; 6 | import br.com.monitoratec.app.domain.entity.User; 7 | import rx.Observable; 8 | 9 | /** 10 | * Repository interface da API GitHub Status. 11 | *

12 | * Created by falvojr on 1/13/17. 13 | */ 14 | public interface GitHubRepository { 15 | Observable getUser(String credential); 16 | Observable> getRepos(String credential); 17 | } 18 | -------------------------------------------------------------------------------- /app/src/main/java/br/com/monitoratec/app/domain/repository/GitHubStatusRepository.java: -------------------------------------------------------------------------------- 1 | package br.com.monitoratec.app.domain.repository; 2 | 3 | import br.com.monitoratec.app.domain.entity.Status; 4 | import rx.Observable; 5 | 6 | /** 7 | * Repository interface da API GitHub Status. 8 | *

9 | * Created by falvojr on 1/13/17. 10 | */ 11 | public interface GitHubStatusRepository { 12 | 13 | Observable getLastStatus(); 14 | } 15 | -------------------------------------------------------------------------------- /app/src/main/java/br/com/monitoratec/app/infraestructure/storage/manager/GitHubManager.java: -------------------------------------------------------------------------------- 1 | package br.com.monitoratec.app.infraestructure.storage.manager; 2 | 3 | import java.util.List; 4 | 5 | import javax.inject.Inject; 6 | 7 | import br.com.monitoratec.app.domain.entity.Repo; 8 | import br.com.monitoratec.app.domain.entity.User; 9 | import br.com.monitoratec.app.domain.repository.GitHubRepository; 10 | import br.com.monitoratec.app.infraestructure.storage.service.GitHubService; 11 | import rx.Observable; 12 | import rx.android.schedulers.AndroidSchedulers; 13 | import rx.schedulers.Schedulers; 14 | 15 | /** 16 | * Manager for {@link GitHubRepository}. 17 | * 18 | * Created by falvojr on 1/13/17. 19 | */ 20 | public class GitHubManager implements GitHubRepository { 21 | 22 | private final GitHubService mGitHubService; 23 | 24 | @Inject 25 | public GitHubManager(GitHubService gitHubService) { 26 | mGitHubService = gitHubService; 27 | } 28 | 29 | @Override 30 | public Observable getUser(String authorization) { 31 | return mGitHubService.getUser(authorization) 32 | .subscribeOn(Schedulers.io()) 33 | .observeOn(AndroidSchedulers.mainThread()); 34 | } 35 | 36 | @Override 37 | public Observable> getRepos(String credential) { 38 | return mGitHubService.getRepos(credential) 39 | .subscribeOn(Schedulers.io()) 40 | .observeOn(AndroidSchedulers.mainThread()); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /app/src/main/java/br/com/monitoratec/app/infraestructure/storage/manager/GitHubOAuthManager.java: -------------------------------------------------------------------------------- 1 | package br.com.monitoratec.app.infraestructure.storage.manager; 2 | 3 | import javax.inject.Inject; 4 | 5 | import br.com.monitoratec.app.domain.entity.AccessToken; 6 | import br.com.monitoratec.app.domain.repository.GitHubOAuthRepository; 7 | import br.com.monitoratec.app.infraestructure.storage.service.GitHubOAuthService; 8 | import rx.Observable; 9 | import rx.android.schedulers.AndroidSchedulers; 10 | import rx.schedulers.Schedulers; 11 | 12 | /** 13 | * Manager for {@link GitHubOAuthRepository}. 14 | * 15 | * Created by falvojr on 1/13/17. 16 | */ 17 | public class GitHubOAuthManager implements GitHubOAuthRepository { 18 | 19 | private final GitHubOAuthService mGitHubOAuthService; 20 | 21 | @Inject 22 | public GitHubOAuthManager(GitHubOAuthService gitHubOAuthService) { 23 | mGitHubOAuthService = gitHubOAuthService; 24 | } 25 | 26 | @Override 27 | public Observable getAccessToken(String clientId, String clientSecret, String code) { 28 | return mGitHubOAuthService.getAccessToken(clientId, clientSecret, code) 29 | .subscribeOn(Schedulers.io()) 30 | .observeOn(AndroidSchedulers.mainThread()); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /app/src/main/java/br/com/monitoratec/app/infraestructure/storage/manager/GitHubStatusManager.java: -------------------------------------------------------------------------------- 1 | package br.com.monitoratec.app.infraestructure.storage.manager; 2 | 3 | import javax.inject.Inject; 4 | 5 | import br.com.monitoratec.app.domain.entity.Status; 6 | import br.com.monitoratec.app.domain.repository.GitHubStatusRepository; 7 | import br.com.monitoratec.app.infraestructure.storage.service.GitHubStatusService; 8 | import rx.Observable; 9 | import rx.android.schedulers.AndroidSchedulers; 10 | import rx.schedulers.Schedulers; 11 | 12 | /** 13 | * Manager for {@link GitHubStatusRepository}. 14 | * 15 | * Created by falvojr on 1/13/17. 16 | */ 17 | public class GitHubStatusManager implements GitHubStatusRepository { 18 | 19 | private final GitHubStatusService mGitHubStatusService; 20 | 21 | @Inject 22 | public GitHubStatusManager(GitHubStatusService gitHubStatusService) { 23 | mGitHubStatusService = gitHubStatusService; 24 | } 25 | 26 | @Override 27 | public Observable getLastStatus() { 28 | return mGitHubStatusService.getLastStatus() 29 | .subscribeOn(Schedulers.io()) 30 | .observeOn(AndroidSchedulers.mainThread()); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /app/src/main/java/br/com/monitoratec/app/infraestructure/storage/service/GitHubOAuthService.java: -------------------------------------------------------------------------------- 1 | package br.com.monitoratec.app.infraestructure.storage.service; 2 | 3 | import br.com.monitoratec.app.domain.entity.AccessToken; 4 | import retrofit2.http.Field; 5 | import retrofit2.http.FormUrlEncoded; 6 | import retrofit2.http.Headers; 7 | import retrofit2.http.POST; 8 | import rx.Observable; 9 | 10 | /** 11 | * Interface Retrofit da API GitHub OAuth. 12 | * 13 | * Created by falvojr on 1/11/17. 14 | */ 15 | public interface GitHubOAuthService { 16 | String BASE_URL = "https://github.com/login/oauth/"; 17 | 18 | @Headers({"Accept: application/json"}) 19 | @FormUrlEncoded 20 | @POST("access_token") 21 | Observable getAccessToken( 22 | @Field("client_id") String clientId, 23 | @Field("client_secret") String clientSecret, 24 | @Field("code") String code); 25 | } 26 | -------------------------------------------------------------------------------- /app/src/main/java/br/com/monitoratec/app/infraestructure/storage/service/GitHubService.java: -------------------------------------------------------------------------------- 1 | package br.com.monitoratec.app.infraestructure.storage.service; 2 | 3 | import java.util.List; 4 | 5 | import br.com.monitoratec.app.domain.entity.Repo; 6 | import br.com.monitoratec.app.domain.entity.User; 7 | import retrofit2.http.GET; 8 | import retrofit2.http.Header; 9 | import rx.Observable; 10 | 11 | /** 12 | * Interface Retrofit da API GitHub Status. 13 | * 14 | * Created by falvojr on 1/9/17. 15 | */ 16 | public interface GitHubService { 17 | 18 | String BASE_URL = "https://api.github.com/"; 19 | 20 | @GET("user") 21 | Observable getUser(@Header("Authorization") String credential); 22 | @GET("user/repos") 23 | Observable> getRepos(@Header("Authorization") String credential); 24 | } 25 | -------------------------------------------------------------------------------- /app/src/main/java/br/com/monitoratec/app/infraestructure/storage/service/GitHubStatusService.java: -------------------------------------------------------------------------------- 1 | package br.com.monitoratec.app.infraestructure.storage.service; 2 | 3 | import br.com.monitoratec.app.domain.entity.Status; 4 | import retrofit2.http.GET; 5 | import rx.Observable; 6 | 7 | /** 8 | * Interface Retrofit da API GitHub Status. 9 | *

10 | * Created by falvojr on 1/9/17. 11 | */ 12 | public interface GitHubStatusService { 13 | 14 | String BASE_URL = "https://status.github.com/api/"; 15 | 16 | @GET("last-message.json") 17 | Observable getLastStatus(); 18 | } 19 | -------------------------------------------------------------------------------- /app/src/main/java/br/com/monitoratec/app/presentation/base/BaseActivity.java: -------------------------------------------------------------------------------- 1 | package br.com.monitoratec.app.presentation.base; 2 | 3 | import android.support.v7.app.AppCompatActivity; 4 | 5 | import br.com.monitoratec.app.MyApplication; 6 | import br.com.monitoratec.app.dagger.UiComponent; 7 | 8 | /** 9 | * Base {@link AppCompatActivity}. 10 | * 11 | * Created by falvojr on 1/12/17. 12 | */ 13 | public abstract class BaseActivity extends AppCompatActivity { 14 | 15 | protected MyApplication getMyApplication() { 16 | return (MyApplication) getApplication(); 17 | } 18 | 19 | protected UiComponent getDaggerUiComponent() { 20 | return this.getMyApplication().getDaggerUiComponent(); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /app/src/main/java/br/com/monitoratec/app/presentation/helper/AppHelper.java: -------------------------------------------------------------------------------- 1 | package br.com.monitoratec.app.presentation.helper; 2 | 3 | import android.content.Context; 4 | import android.support.design.widget.TextInputLayout; 5 | import android.text.TextUtils; 6 | import android.widget.EditText; 7 | 8 | import javax.inject.Inject; 9 | 10 | import br.com.monitoratec.app.R; 11 | 12 | /** 13 | * Useful class for app. 14 | * 15 | * Created by falvojr on 1/11/17. 16 | */ 17 | public final class AppHelper { 18 | private final Context mContext; 19 | 20 | @Inject 21 | public AppHelper(Context context) { 22 | mContext = context; 23 | } 24 | 25 | public boolean validateRequiredFields(TextInputLayout... fields) { 26 | boolean isValid = true; 27 | for (TextInputLayout field : fields) { 28 | EditText editText = field.getEditText(); 29 | if (editText != null) { 30 | if (TextUtils.isEmpty(editText.getText())) { 31 | isValid = false; 32 | field.setErrorEnabled(true); 33 | field.setError(mContext.getString(R.string.msg_required_field)); 34 | } else { 35 | field.setErrorEnabled(false); 36 | field.setError(null); 37 | } 38 | } else { 39 | throw new RuntimeException("O TextInputLayout deve possuir um EditText."); 40 | } 41 | } 42 | return isValid; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /app/src/main/java/br/com/monitoratec/app/presentation/ui/auth/AuthActivity.java: -------------------------------------------------------------------------------- 1 | package br.com.monitoratec.app.presentation.ui.auth; 2 | 3 | import android.content.Intent; 4 | import android.net.Uri; 5 | import android.os.Bundle; 6 | import android.support.design.widget.Snackbar; 7 | import android.support.design.widget.TextInputLayout; 8 | import android.support.v4.content.ContextCompat; 9 | import android.support.v4.graphics.drawable.DrawableCompat; 10 | import android.view.Menu; 11 | import android.view.MenuInflater; 12 | import android.view.MenuItem; 13 | import android.widget.Button; 14 | import android.widget.EditText; 15 | import android.widget.ImageView; 16 | import android.widget.TextView; 17 | 18 | import com.jakewharton.rxbinding.view.RxView; 19 | import com.jakewharton.rxbinding.widget.RxTextView; 20 | 21 | import javax.inject.Inject; 22 | 23 | import br.com.monitoratec.app.R; 24 | import br.com.monitoratec.app.domain.entity.Status; 25 | import br.com.monitoratec.app.domain.entity.User; 26 | import br.com.monitoratec.app.infraestructure.storage.service.GitHubOAuthService; 27 | import br.com.monitoratec.app.presentation.base.BaseActivity; 28 | import br.com.monitoratec.app.presentation.helper.AppHelper; 29 | import br.com.monitoratec.app.presentation.ui.repos.ReposActivity; 30 | import butterknife.BindView; 31 | import butterknife.ButterKnife; 32 | import butterknife.OnClick; 33 | import okhttp3.Credentials; 34 | 35 | /** 36 | * GitHub authentication activity (view). 37 | * 38 | * Created by falvojr on 1/13/17. 39 | */ 40 | public class AuthActivity extends BaseActivity implements AuthContract.View { 41 | 42 | private static final String TAG = AuthActivity.class.getSimpleName(); 43 | 44 | @BindView(R.id.ivGitHub) ImageView mImgGitHub; 45 | @BindView(R.id.tvGitHub) TextView mTxtGitHub; 46 | @BindView(R.id.tilUsername) TextInputLayout mLayoutTxtUsername; 47 | @BindView(R.id.tilPassword) TextInputLayout mLayoutTxtPassword; 48 | @BindView(R.id.btOAuth) Button mBtnOAuth; 49 | 50 | @Inject AppHelper mAppHelper; 51 | @Inject AuthContract.Presenter mPresenter; 52 | 53 | @Override 54 | protected void onCreate(Bundle savedInstanceState) { 55 | super.onCreate(savedInstanceState); 56 | super.setContentView(R.layout.activity_main); 57 | 58 | ButterKnife.bind(this); 59 | super.getDaggerUiComponent().inject(this); 60 | mPresenter.setView(this); 61 | 62 | this.bindUsingRx(); 63 | } 64 | 65 | @Override 66 | protected void onResume() { 67 | super.onResume(); 68 | // Restore default GitHub fields status 69 | this.onGetStatusComplete(Status.Type.NONE); 70 | // Get last status from GitHub Status API 71 | mPresenter.getStatus(); 72 | // Process (if necessary) OAUth redirect callback 73 | this.processOAuthRedirectUri(); 74 | } 75 | 76 | @Override 77 | public boolean onCreateOptionsMenu(Menu menu) { 78 | MenuInflater inflater = getMenuInflater(); 79 | inflater.inflate(R.menu.main_menu, menu); 80 | return true; 81 | } 82 | 83 | @Override 84 | public boolean onOptionsItemSelected(MenuItem item) { 85 | switch (item.getItemId()) { 86 | case R.id.actionCall: 87 | final String normalNumber = "+55 16 99387-0941"; 88 | final Uri uri = Uri.fromParts("tel", normalNumber, null); 89 | final Intent intent = new Intent(Intent.ACTION_DIAL, uri); 90 | startActivity(intent); 91 | return true; 92 | default: 93 | return super.onOptionsItemSelected(item); 94 | } 95 | } 96 | 97 | @Override 98 | public void onGetStatusComplete(Status.Type statusType) { 99 | int color = ContextCompat.getColor(AuthActivity.this, statusType.getColorRes()); 100 | mTxtGitHub.setText(getString(statusType.getMessageRes())); 101 | mTxtGitHub.setTextColor(color); 102 | DrawableCompat.setTint(mImgGitHub.getDrawable(), color); 103 | } 104 | 105 | @Override 106 | public void onGetUserComplete(String credential, User user) { 107 | Intent intent = new Intent(this, ReposActivity.class); 108 | intent.putExtra(ReposActivity.EXTRA_CREDENTIAL, credential); 109 | intent.putExtra(ReposActivity.EXTRA_USER, user); 110 | startActivity(intent); 111 | } 112 | 113 | @Override 114 | public void showError(String message) { 115 | Snackbar.make(mImgGitHub, message, Snackbar.LENGTH_LONG).show(); 116 | } 117 | 118 | @OnClick(R.id.btBasicAuth) 119 | void onBasicAuthClick(Button view) { 120 | if (mAppHelper.validateRequiredFields(mLayoutTxtUsername, mLayoutTxtPassword)) { 121 | String username = mLayoutTxtUsername.getEditText().getText().toString(); 122 | String password = mLayoutTxtPassword.getEditText().getText().toString(); 123 | final String credential = Credentials.basic(username, password); 124 | mPresenter.getUser(credential); 125 | } 126 | } 127 | 128 | private void bindUsingRx() { 129 | final EditText editTextUsername = mLayoutTxtUsername.getEditText(); 130 | final EditText editTextPassword = mLayoutTxtPassword.getEditText(); 131 | if (editTextUsername != null && editTextPassword != null) { 132 | RxTextView.textChanges(editTextUsername) 133 | .skip(1) 134 | .subscribe(text -> { 135 | mAppHelper.validateRequiredFields(mLayoutTxtUsername); 136 | }); 137 | RxTextView.textChanges(editTextPassword) 138 | .skip(1) 139 | .subscribe(text -> { 140 | mAppHelper.validateRequiredFields(mLayoutTxtPassword); 141 | }); 142 | } 143 | RxView.clicks(mBtnOAuth).subscribe(aVoid -> { 144 | final String baseUrl = GitHubOAuthService.BASE_URL + "authorize"; 145 | final String clientId = getString(R.string.oauth_client_id); 146 | final String redirectUri = getOAuthRedirectUri(); 147 | final Uri uri = Uri.parse(baseUrl + "?client_id=" + clientId + "&redirect_uri=" + redirectUri); 148 | Intent intent = new Intent(Intent.ACTION_VIEW, uri); 149 | startActivity(intent); 150 | }); 151 | } 152 | 153 | private String getOAuthRedirectUri() { 154 | return getString(R.string.oauth_schema) + "://" + getString(R.string.oauth_host); 155 | } 156 | 157 | private void processOAuthRedirectUri() { 158 | // the intent filter defined in AndroidManifest will handle the return from ACTION_VIEW intent 159 | final Uri uri = getIntent().getData(); 160 | if (uri != null && uri.toString().startsWith(this.getOAuthRedirectUri())) { 161 | // use the parameter your API exposes for the code (mostly it's "code") 162 | String code = uri.getQueryParameter("code"); 163 | if (code != null) { 164 | // get access token and related User 165 | String clientId = getString(R.string.oauth_client_id); 166 | String clientSecret = getString(R.string.oauth_client_secret); 167 | mPresenter.getAccessTokenAndUser(clientId, clientSecret, code); 168 | } else if (uri.getQueryParameter("error") != null) { 169 | showError(uri.getQueryParameter("error")); 170 | } 171 | // Clear Intent Data preventing multiple calls 172 | getIntent().setData(null); 173 | } 174 | } 175 | } 176 | -------------------------------------------------------------------------------- /app/src/main/java/br/com/monitoratec/app/presentation/ui/auth/AuthContract.java: -------------------------------------------------------------------------------- 1 | package br.com.monitoratec.app.presentation.ui.auth; 2 | 3 | import br.com.monitoratec.app.domain.entity.Status; 4 | import br.com.monitoratec.app.domain.entity.User; 5 | 6 | /** 7 | * GitHub authentication MVP contract. 8 | * 9 | * Created by falvojr on 1/13/17. 10 | */ 11 | public interface AuthContract { 12 | 13 | interface View { 14 | void onGetStatusComplete(Status.Type statusType); 15 | 16 | void onGetUserComplete(String credential, User user); 17 | 18 | void showError(String message); 19 | } 20 | 21 | interface Presenter { 22 | void setView(AuthContract.View view); 23 | 24 | void getStatus(); 25 | 26 | void getUser(String authorization); 27 | 28 | void getAccessTokenAndUser(String clientId, String clientSecret, String code); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /app/src/main/java/br/com/monitoratec/app/presentation/ui/auth/AuthPresenter.java: -------------------------------------------------------------------------------- 1 | package br.com.monitoratec.app.presentation.ui.auth; 2 | 3 | import javax.inject.Inject; 4 | 5 | import br.com.monitoratec.app.domain.entity.Status; 6 | import br.com.monitoratec.app.domain.repository.GitHubOAuthRepository; 7 | import br.com.monitoratec.app.domain.repository.GitHubRepository; 8 | import br.com.monitoratec.app.domain.repository.GitHubStatusRepository; 9 | 10 | /** 11 | * GitHub authentication presenter. 12 | * 13 | * Created by falvojr on 1/13/17. 14 | */ 15 | public class AuthPresenter implements AuthContract.Presenter { 16 | 17 | private AuthContract.View mView; 18 | private GitHubRepository mGitHubRepository; 19 | private GitHubStatusRepository mGitHubStatusRepository; 20 | private GitHubOAuthRepository mGitHubOAuthRepository; 21 | 22 | @Inject 23 | public AuthPresenter(GitHubRepository gitHubRepository, 24 | GitHubStatusRepository gitHubStatusRepository, 25 | GitHubOAuthRepository gitHubOAuthRepository) { 26 | mGitHubRepository = gitHubRepository; 27 | mGitHubStatusRepository = gitHubStatusRepository; 28 | mGitHubOAuthRepository = gitHubOAuthRepository; 29 | } 30 | 31 | @Override 32 | public void setView(AuthContract.View view) { 33 | mView = view; 34 | } 35 | 36 | @Override 37 | public void getStatus() { 38 | mGitHubStatusRepository.getLastStatus() 39 | .subscribe(status -> { 40 | mView.onGetStatusComplete(status.type); 41 | }, error -> { 42 | mView.onGetStatusComplete(Status.Type.MAJOR); 43 | }); 44 | } 45 | 46 | @Override 47 | public void getUser(String authorization) { 48 | mGitHubRepository.getUser(authorization) 49 | .subscribe(user -> { 50 | mView.onGetUserComplete(authorization, user); 51 | }, error -> { 52 | mView.showError(error.getMessage()); 53 | }); 54 | } 55 | 56 | @Override 57 | public void getAccessTokenAndUser(String clientId, 58 | String clientSecret, 59 | String code) { 60 | mGitHubOAuthRepository.getAccessToken(clientId, clientSecret, code) 61 | .subscribe(entity -> { 62 | getUser(entity.getAuthCredential()); 63 | }, error -> { 64 | mView.showError(error.getMessage()); 65 | }); 66 | } 67 | } -------------------------------------------------------------------------------- /app/src/main/java/br/com/monitoratec/app/presentation/ui/repos/ReposActivity.java: -------------------------------------------------------------------------------- 1 | package br.com.monitoratec.app.presentation.ui.repos; 2 | 3 | import android.os.Bundle; 4 | import android.support.design.widget.FloatingActionButton; 5 | import android.support.design.widget.Snackbar; 6 | import android.support.v7.widget.LinearLayoutManager; 7 | import android.support.v7.widget.RecyclerView; 8 | import android.support.v7.widget.Toolbar; 9 | 10 | import java.util.List; 11 | 12 | import javax.inject.Inject; 13 | 14 | import br.com.monitoratec.app.R; 15 | import br.com.monitoratec.app.domain.entity.Repo; 16 | import br.com.monitoratec.app.domain.entity.User; 17 | import br.com.monitoratec.app.presentation.base.BaseActivity; 18 | import butterknife.BindView; 19 | import butterknife.ButterKnife; 20 | 21 | /** 22 | * List {@link Repo}'s activity (view). 23 | * 24 | * Created by falvojr on 1/14/17. 25 | */ 26 | public class ReposActivity extends BaseActivity implements ReposContract.View { 27 | 28 | public static final String EXTRA_CREDENTIAL = "Intent.Extra.Credential"; 29 | public static final String EXTRA_USER = "Intent.Extra.User"; 30 | 31 | @BindView(R.id.rvRepos) RecyclerView mRecyclerView; 32 | @BindView(R.id.toolbar) Toolbar mToolbar; 33 | @BindView(R.id.fab) FloatingActionButton mFab; 34 | 35 | @Inject ReposContract.Presenter mPresenter; 36 | 37 | @Override 38 | protected void onCreate(Bundle savedInstanceState) { 39 | super.onCreate(savedInstanceState); 40 | setContentView(R.layout.activity_repos); 41 | 42 | ButterKnife.bind(this); 43 | super.getDaggerUiComponent().inject(this); 44 | mPresenter.setView(this); 45 | 46 | setSupportActionBar(mToolbar); 47 | mFab.setOnClickListener(view -> this.startNewRepoActivity()); 48 | 49 | final String credential = getIntent().getStringExtra(EXTRA_CREDENTIAL); 50 | final User unusedUser = getIntent().getParcelableExtra(EXTRA_USER); 51 | if(credential != null && unusedUser != null) { 52 | mPresenter.loadRepos(credential); 53 | } else { 54 | this.showError(getString(R.string.msg_credential_not_found)); 55 | } 56 | } 57 | 58 | @Override 59 | public void setupRepos(List repos) { 60 | // use a linear layout manager 61 | RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this); 62 | mRecyclerView.setLayoutManager(layoutManager); 63 | // specify an adapter (see also next example) 64 | RecyclerView.Adapter adapter = new ReposAdapter(repos); 65 | mRecyclerView.setAdapter(adapter); 66 | } 67 | 68 | @Override 69 | public void startNewRepoActivity() { 70 | Snackbar.make(mFab, "TODO 'POST /user/repo'", Snackbar.LENGTH_LONG).show(); 71 | } 72 | 73 | @Override 74 | public void showError(String message) { 75 | Snackbar.make(mFab, message, Snackbar.LENGTH_LONG).show(); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /app/src/main/java/br/com/monitoratec/app/presentation/ui/repos/ReposAdapter.java: -------------------------------------------------------------------------------- 1 | package br.com.monitoratec.app.presentation.ui.repos; 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 java.util.List; 10 | 11 | import br.com.monitoratec.app.domain.entity.Repo; 12 | import butterknife.BindView; 13 | import butterknife.ButterKnife; 14 | 15 | /** 16 | * {@link Repo} {@link RecyclerView.Adapter} 17 | * 18 | * Created by falvojr on 1/14/17. 19 | */ 20 | public class ReposAdapter extends RecyclerView.Adapter { 21 | 22 | private final List mDataSet; 23 | 24 | ReposAdapter(List dataSet) { 25 | mDataSet = dataSet; 26 | } 27 | 28 | @Override 29 | public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 30 | final LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext()); 31 | final View view = layoutInflater.inflate(android.R.layout.simple_list_item_1, parent, false); 32 | return new ViewHolder(view); 33 | } 34 | 35 | @Override 36 | public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 37 | final Repo repo = mDataSet.get(position); 38 | ((ViewHolder) holder).mTextView.setText(repo.name); 39 | } 40 | 41 | @Override 42 | public int getItemCount() { 43 | return mDataSet.size(); 44 | } 45 | 46 | static class ViewHolder extends RecyclerView.ViewHolder { 47 | 48 | @BindView(android.R.id.text1) 49 | TextView mTextView; 50 | 51 | ViewHolder(View view) { 52 | super(view); 53 | ButterKnife.bind(this, view); 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /app/src/main/java/br/com/monitoratec/app/presentation/ui/repos/ReposContract.java: -------------------------------------------------------------------------------- 1 | package br.com.monitoratec.app.presentation.ui.repos; 2 | 3 | import java.util.List; 4 | 5 | import br.com.monitoratec.app.domain.entity.Repo; 6 | 7 | /** 8 | * GitHub list {@link Repo} MVP contract. 9 | * 10 | * Created by falvojr on 1/14/17. 11 | */ 12 | public interface ReposContract { 13 | interface View { 14 | void setupRepos(List repos); 15 | 16 | void startNewRepoActivity(); 17 | 18 | void showError(String message); 19 | } 20 | 21 | interface Presenter { 22 | void setView(ReposContract.View view); 23 | 24 | void loadRepos(String credential); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/java/br/com/monitoratec/app/presentation/ui/repos/ReposPresenter.java: -------------------------------------------------------------------------------- 1 | package br.com.monitoratec.app.presentation.ui.repos; 2 | 3 | import br.com.monitoratec.app.domain.entity.Repo; 4 | import br.com.monitoratec.app.domain.repository.GitHubRepository; 5 | 6 | /** 7 | * GitHub List {@link Repo}'s presenter. 8 | *

9 | * Created by falvojr on 1/14/17. 10 | */ 11 | public class ReposPresenter implements ReposContract.Presenter { 12 | 13 | private final GitHubRepository mGitHubRepository; 14 | private ReposContract.View mView; 15 | 16 | public ReposPresenter(GitHubRepository gitHubRepository) { 17 | mGitHubRepository = gitHubRepository; 18 | } 19 | 20 | @Override 21 | public void setView(ReposContract.View view) { 22 | mView = view; 23 | } 24 | 25 | @Override 26 | public void loadRepos(String credential) { 27 | mGitHubRepository.getRepos(credential) 28 | .subscribe(repos -> { 29 | mView.setupRepos(repos); 30 | }, error -> { 31 | mView.showError(error.getMessage()); 32 | }); 33 | 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_add.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_call.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_github.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 15 | 16 | 21 | 22 | 27 | 28 | 33 | 34 | 38 | 39 | 40 | 46 | 47 | 51 | 52 | 53 |