├── .gitignore ├── .idea ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── markdown-navigator.xml ├── markdown-navigator │ └── profiles_settings.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── LICENSE ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── florent37 │ │ └── github │ │ └── com │ │ └── githubnewandroidarchitecture │ │ ├── MainActivity.java │ │ ├── MainApplication.java │ │ ├── MainFragment.java │ │ ├── ReposAdapter.java │ │ ├── dagger │ │ ├── AppComponent.java │ │ ├── AppModule.java │ │ └── RoomModule.java │ │ ├── local │ │ ├── UserDao.java │ │ ├── UserRepositoryLocal.java │ │ └── UserRoomDatabase.java │ │ ├── model │ │ ├── Repo.java │ │ ├── User.java │ │ ├── UserLocal.java │ │ ├── UserNetwork.java │ │ └── UserSearchResult.java │ │ ├── network │ │ ├── GithubAPI.java │ │ ├── RepoRepositoryNetwork.java │ │ └── UserRepositoryNetwork.java │ │ ├── repository │ │ ├── RepoRepository.java │ │ └── UserRepository.java │ │ └── viewmodel │ │ ├── ReposListViewModel.java │ │ └── UserViewModel.java │ └── res │ ├── layout │ ├── activity_main.xml │ ├── fragment_main.xml │ └── recycler_item.xml │ ├── mipmap-hdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-mdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xxhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xxxhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ └── values │ ├── colors.xml │ ├── strings.xml │ └── styles.xml ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── media ├── screen.png └── screen_small.png └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the ART/Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | out/ 15 | 16 | # Gradle files 17 | .gradle/ 18 | build/ 19 | 20 | # Local configuration file (sdk path, etc) 21 | local.properties 22 | 23 | # Proguard folder generated by Eclipse 24 | proguard/ 25 | 26 | # Log Files 27 | *.log 28 | 29 | # Android Studio Navigation editor temp files 30 | .navigation/ 31 | 32 | # Android Studio captures folder 33 | captures/ 34 | 35 | # Intellij 36 | *.iml 37 | .idea/workspace.xml 38 | .idea/tasks.xml 39 | .idea/gradle.xml 40 | .idea/dictionaries 41 | .idea/libraries 42 | 43 | # Keystore files 44 | *.jks 45 | 46 | # External native build folder generated in Android Studio 2.2 and later 47 | .externalNativeBuild 48 | 49 | # Google Services (e.g. APIs or Firebase) 50 | google-services.json 51 | 52 | # Freeline 53 | freeline.py 54 | freeline/ 55 | freeline_project_description.json 56 | -------------------------------------------------------------------------------- /.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/markdown-navigator.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 36 | 37 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | -------------------------------------------------------------------------------- /.idea/markdown-navigator/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 16 | 26 | 27 | 28 | 29 | 30 | 31 | 33 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # NewAndroidArchitecture-Github 2 | 3 | 4 | 5 | Android app on Google Play 6 | 7 | 8 | 9 | Sample project based on the new Android Component Architecture 10 | 11 | Lifecycle, LiveData, MVVM, ViewModels, DataBinding, Dagger, Retrofit 12 | 13 | https://developer.android.com/topic/libraries/architecture/guide.html 14 | 15 | ![Alt sample](https://raw.githubusercontent.com/florent37/NewAndroidArchitecture-Github/master/media/screen_small.png) 16 | 17 | # LiveData as Observables ! 18 | 19 | LiveDatas works like RxJava's Observables, 20 | they will notify the observer when the data is Available 21 | 22 | ```java 23 | @Override 24 | public LiveData getUser(String userName){ 25 | final MutableLiveData liveData = new MutableLiveData<>(); 26 | 27 | githubAPI.user(userName).enqueue(new Callback() { 28 | @Override 29 | public void onResponse(Call call, Response response) { 30 | if(response.isSuccessful()) { 31 | liveData.setValue(response.body()); 32 | } 33 | } 34 | 35 | @Override 36 | public void onFailure(Call call, Throwable t) { 37 | 38 | } 39 | }); 40 | 41 | return liveData; 42 | } 43 | ``` 44 | 45 | # LifeCycle Owner 46 | 47 | Use Support Fragment and AppCompatActivity to be attached to the application's state 48 | 49 | ```java 50 | public class MainFragment extends Fragment { 51 | ``` 52 | 53 | # DataBinding and ViewHolders 54 | 55 | MainFragment.java 56 | ```java 57 | reposAdapter = new ReposAdapter(); 58 | viewDataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); 59 | viewDataBinding.recyclerView.setAdapter(reposAdapter); 60 | 61 | AppComponent.from(getContext()).inject(this); 62 | //inject the viewmodel responding to User 63 | //inject the viewmodel responding to List 64 | 65 | //fetch the user from the datasource 66 | userViewModel.getUser("florent37") 67 | .observe(this, new Observer() { 68 | @Override 69 | public void onChanged(@Nullable User user) { 70 | viewDataBinding.setUser(user); 71 | } 72 | }); 73 | 74 | //fetch the repos from the datasource 75 | reposListViewModel.getRepos("florent37") 76 | .observe(this, new Observer>() { 77 | @Override 78 | public void onChanged(@Nullable List repos) { 79 | //when available, send it to the recyclerview 80 | reposAdapter.setRepos(repos); 81 | } 82 | }); 83 | ``` 84 | 85 | # Dagger and Repository 86 | 87 | ```java 88 | public class UserViewModel extends ViewModel { 89 | 90 | private final GithubRepository githubRepository; 91 | 92 | @Inject 93 | public UserViewModel(GithubRepository githubRepository) { 94 | this.githubRepository = githubRepository; 95 | } 96 | 97 | public LiveData getUser(String userName) { 98 | //userLiveData will be notified when the user is fetched 99 | return githubRepository.getUser(userName); 100 | } 101 | } 102 | ``` 103 | 104 | 105 | 106 | Android app on Google Play 107 | 108 | 109 | Fiches Plateau Moto : [https://www.fiches-plateau-moto.fr/](https://www.fiches-plateau-moto.fr/) 110 | 111 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 27 5 | defaultConfig { 6 | applicationId "florent37.github.com.architecturecomponants" 7 | minSdkVersion 15 8 | targetSdkVersion 27 9 | versionCode 1 10 | versionName "1.0" 11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | 20 | dataBinding { 21 | enabled = true; 22 | } 23 | 24 | compileOptions.incremental = false 25 | 26 | } 27 | 28 | ext { 29 | supportLibVersion = "27.0.2" 30 | archLifecycleVersion = "1.0.0" 31 | } 32 | 33 | dependencies { 34 | compile 'com.facebook.stetho:stetho:1.5.0' 35 | 36 | compile 'com.android.support:appcompat-v7:' + project.supportLibVersion 37 | compile 'com.android.support:support-v4:' + project.supportLibVersion 38 | compile 'com.android.support.constraint:constraint-layout:1.0.2' 39 | 40 | compile 'com.google.dagger:dagger:2.10' 41 | annotationProcessor 'com.google.dagger:dagger-compiler:2.10' 42 | 43 | compile 'com.squareup.retrofit2:retrofit:2.3.0' 44 | compile 'com.squareup.retrofit2:converter-gson:2.3.0' 45 | 46 | compile 'com.android.support:appcompat-v7:' + project.supportLibVersion; 47 | compile 'com.android.support:cardview-v7:' + project.supportLibVersion; 48 | compile 'com.android.support:recyclerview-v7:' + project.supportLibVersion; 49 | 50 | compile 'android.arch.lifecycle:extensions:' + project.archLifecycleVersion; 51 | annotationProcessor "android.arch.lifecycle:compiler:" + project.archLifecycleVersion; 52 | 53 | implementation "android.arch.persistence.room:runtime:" + project.archLifecycleVersion; 54 | annotationProcessor "android.arch.persistence.room:compiler:" + project.archLifecycleVersion; 55 | 56 | } 57 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/florentchampigny/Library/Android/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | 19 | # Uncomment this to preserve the line number information for 20 | # debugging stack traces. 21 | #-keepattributes SourceFile,LineNumberTable 22 | 23 | # If you keep the line number information, uncomment this to 24 | # hide the original source file name. 25 | #-renamesourcefileattribute SourceFile 26 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /app/src/main/java/florent37/github/com/githubnewandroidarchitecture/MainActivity.java: -------------------------------------------------------------------------------- 1 | package florent37.github.com.githubnewandroidarchitecture; 2 | 3 | import android.arch.lifecycle.LifecycleActivity; 4 | import android.os.Bundle; 5 | import android.support.v7.app.AppCompatActivity; 6 | 7 | public class MainActivity extends AppCompatActivity { 8 | 9 | @Override 10 | protected void onCreate(Bundle savedInstanceState) { 11 | super.onCreate(savedInstanceState); 12 | setContentView(R.layout.activity_main); 13 | 14 | getSupportFragmentManager() 15 | .beginTransaction() 16 | .replace(R.id.fragment_container, MainFragment.newInstance()) 17 | .commit(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /app/src/main/java/florent37/github/com/githubnewandroidarchitecture/MainApplication.java: -------------------------------------------------------------------------------- 1 | package florent37.github.com.githubnewandroidarchitecture; 2 | 3 | import android.app.Application; 4 | 5 | import com.facebook.stetho.Stetho; 6 | 7 | import javax.inject.Inject; 8 | 9 | import florent37.github.com.githubnewandroidarchitecture.dagger.AppComponent; 10 | import florent37.github.com.githubnewandroidarchitecture.dagger.AppModule; 11 | import florent37.github.com.githubnewandroidarchitecture.dagger.DaggerAppComponent; 12 | import florent37.github.com.githubnewandroidarchitecture.dagger.RoomModule; 13 | import florent37.github.com.githubnewandroidarchitecture.local.UserDao; 14 | import florent37.github.com.githubnewandroidarchitecture.model.UserLocal; 15 | 16 | /** 17 | * Created by florentchampigny on 18/05/2017. 18 | */ 19 | 20 | public class MainApplication extends Application { 21 | 22 | private AppComponent appComponent; 23 | 24 | @Inject 25 | UserDao userDao; 26 | 27 | @Override 28 | public void onCreate() { 29 | super.onCreate(); 30 | 31 | Stetho.initializeWithDefaults(this); 32 | 33 | this.appComponent = DaggerAppComponent.builder() 34 | .appModule(new AppModule(this)) 35 | .roomModule(new RoomModule(this)) 36 | .build(); 37 | 38 | appComponent.inject(this); 39 | 40 | new Thread(new Runnable() { 41 | @Override 42 | public void run() { 43 | //called first time created 44 | userDao.deleteAll(); 45 | userDao.insert(new UserLocal("kevin", "kévin", "")); 46 | userDao.insert(new UserLocal("florent37", "flo", "")); 47 | } 48 | }).start(); 49 | } 50 | 51 | public AppComponent getAppComponent() { 52 | return appComponent; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /app/src/main/java/florent37/github/com/githubnewandroidarchitecture/MainFragment.java: -------------------------------------------------------------------------------- 1 | package florent37.github.com.githubnewandroidarchitecture; 2 | 3 | import android.arch.lifecycle.Observer; 4 | import android.arch.lifecycle.ViewModelProviders; 5 | import android.databinding.DataBindingUtil; 6 | import android.os.Bundle; 7 | import android.support.annotation.Nullable; 8 | import android.support.v4.app.Fragment; 9 | import android.support.v7.widget.LinearLayoutManager; 10 | import android.view.LayoutInflater; 11 | import android.view.View; 12 | import android.view.ViewGroup; 13 | 14 | import java.util.List; 15 | 16 | import florent37.github.com.githubnewandroidarchitecture.databinding.FragmentMainBinding; 17 | import florent37.github.com.githubnewandroidarchitecture.model.Repo; 18 | import florent37.github.com.githubnewandroidarchitecture.model.UserSearchResult; 19 | import florent37.github.com.githubnewandroidarchitecture.viewmodel.ReposListViewModel; 20 | import florent37.github.com.githubnewandroidarchitecture.viewmodel.UserViewModel; 21 | 22 | public class MainFragment extends Fragment { 23 | 24 | UserViewModel userViewModel; 25 | ReposListViewModel reposListViewModel; 26 | 27 | private FragmentMainBinding viewDataBinding; 28 | private ReposAdapter reposAdapter; 29 | 30 | public static Fragment newInstance() { 31 | return new MainFragment(); 32 | } 33 | 34 | @Nullable 35 | @Override 36 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 37 | //get the databinding from the layout 38 | this.viewDataBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_main, container, false); 39 | return viewDataBinding.getRoot(); 40 | } 41 | 42 | @Override 43 | public void onActivityCreated(@Nullable Bundle savedInstanceState) { 44 | super.onActivityCreated(savedInstanceState); 45 | 46 | userViewModel = ViewModelProviders.of(this).get(UserViewModel.class); 47 | reposListViewModel = ViewModelProviders.of(this).get(ReposListViewModel.class); 48 | 49 | reposAdapter = new ReposAdapter(); 50 | viewDataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); 51 | viewDataBinding.recyclerView.setAdapter(reposAdapter); 52 | 53 | //Will change the UI when userLiveData will have 54 | userViewModel.getUserLiveData().observe(this, new Observer() { 55 | @Override 56 | public void onChanged(@Nullable UserSearchResult userSearchResult) { 57 | if (userSearchResult.success()) { 58 | viewDataBinding.setUser(userSearchResult.getUser()); 59 | } else { 60 | 61 | } 62 | } 63 | }); 64 | 65 | //fetch the repos from the datasource, and update whe UI 66 | reposListViewModel.getReposLiveData().observe(this, new Observer>() { 67 | @Override 68 | public void onChanged(@Nullable List repos) { 69 | //when available, send it to the recyclerview 70 | reposAdapter.setRepos(repos); 71 | } 72 | }); 73 | 74 | viewDataBinding.local.setOnClickListener(new View.OnClickListener() { 75 | @Override 76 | public void onClick(View view) { 77 | //fetch the user from the datasource 78 | userViewModel.searchLocally("florent37"); 79 | } 80 | }); 81 | viewDataBinding.network.setOnClickListener(new View.OnClickListener() { 82 | @Override 83 | public void onClick(View view) { 84 | //fetch the user from the datasource 85 | userViewModel.searchOnline("florent37"); 86 | } 87 | }); 88 | 89 | reposListViewModel.searchRepos("florent37"); 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /app/src/main/java/florent37/github/com/githubnewandroidarchitecture/ReposAdapter.java: -------------------------------------------------------------------------------- 1 | package florent37.github.com.githubnewandroidarchitecture; 2 | 3 | import android.databinding.DataBindingUtil; 4 | import android.support.v7.widget.RecyclerView; 5 | import android.view.LayoutInflater; 6 | import android.view.ViewGroup; 7 | 8 | import java.util.ArrayList; 9 | import java.util.List; 10 | 11 | import florent37.github.com.githubnewandroidarchitecture.databinding.RecyclerItemBinding; 12 | import florent37.github.com.githubnewandroidarchitecture.model.Repo; 13 | 14 | /** 15 | * Created by florentchampigny on 18/05/2017. 16 | */ 17 | 18 | public class ReposAdapter extends RecyclerView.Adapter { 19 | 20 | private final List repoList = new ArrayList<>(); 21 | 22 | @Override 23 | public RepoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 24 | final RecyclerItemBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.recycler_item, parent, false); 25 | return new RepoViewHolder(binding); 26 | } 27 | 28 | @Override 29 | public void onBindViewHolder(RepoViewHolder holder, int position) { 30 | holder.binding.setRepo(repoList.get(position)); 31 | } 32 | 33 | public void setRepos(List repos){ 34 | this.repoList.clear(); 35 | this.repoList.addAll(repos); 36 | notifyDataSetChanged(); 37 | } 38 | 39 | @Override 40 | public int getItemCount() { 41 | return repoList.size(); 42 | } 43 | 44 | public class RepoViewHolder extends RecyclerView.ViewHolder { 45 | 46 | final RecyclerItemBinding binding; 47 | 48 | public RepoViewHolder(RecyclerItemBinding binding) { 49 | super(binding.getRoot()); 50 | this.binding = binding; 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /app/src/main/java/florent37/github/com/githubnewandroidarchitecture/dagger/AppComponent.java: -------------------------------------------------------------------------------- 1 | package florent37.github.com.githubnewandroidarchitecture.dagger; 2 | 3 | import android.content.Context; 4 | import android.support.annotation.NonNull; 5 | 6 | import javax.inject.Singleton; 7 | 8 | import dagger.Component; 9 | import florent37.github.com.githubnewandroidarchitecture.MainApplication; 10 | import florent37.github.com.githubnewandroidarchitecture.MainFragment; 11 | import florent37.github.com.githubnewandroidarchitecture.viewmodel.ReposListViewModel; 12 | import florent37.github.com.githubnewandroidarchitecture.viewmodel.UserViewModel; 13 | 14 | /** 15 | * Created by florentchampigny on 18/05/2017. 16 | */ 17 | 18 | @Component(modules = AppModule.class) 19 | @Singleton 20 | public abstract class AppComponent { 21 | 22 | public static AppComponent from(@NonNull Context context){ 23 | return ((MainApplication) context.getApplicationContext()).getAppComponent(); 24 | } 25 | 26 | public abstract void inject(MainFragment mainFragment); 27 | 28 | public abstract void inject(ReposListViewModel reposListViewModel); 29 | 30 | public abstract void inject(UserViewModel userViewModel); 31 | 32 | public abstract void inject(MainApplication mainApplication); 33 | } 34 | -------------------------------------------------------------------------------- /app/src/main/java/florent37/github/com/githubnewandroidarchitecture/dagger/AppModule.java: -------------------------------------------------------------------------------- 1 | package florent37.github.com.githubnewandroidarchitecture.dagger; 2 | 3 | import android.app.Application; 4 | import android.arch.persistence.db.SupportSQLiteDatabase; 5 | import android.arch.persistence.room.Room; 6 | import android.arch.persistence.room.RoomDatabase; 7 | import android.content.Context; 8 | import android.support.annotation.NonNull; 9 | 10 | import javax.inject.Singleton; 11 | 12 | import dagger.Module; 13 | import dagger.Provides; 14 | import florent37.github.com.githubnewandroidarchitecture.local.UserRoomDatabase; 15 | import florent37.github.com.githubnewandroidarchitecture.model.UserLocal; 16 | import florent37.github.com.githubnewandroidarchitecture.network.GithubAPI; 17 | import florent37.github.com.githubnewandroidarchitecture.network.RepoRepositoryNetwork; 18 | import florent37.github.com.githubnewandroidarchitecture.repository.RepoRepository; 19 | import retrofit2.Retrofit; 20 | import retrofit2.converter.gson.GsonConverterFactory; 21 | 22 | /** 23 | * Created by florentchampigny on 18/05/2017. 24 | */ 25 | 26 | @Module(includes = RoomModule.class) 27 | public class AppModule { 28 | 29 | private final Application application; 30 | 31 | public AppModule(Application application) { 32 | this.application = application; 33 | } 34 | 35 | @Provides 36 | public Context providesAppContext() { 37 | return application; 38 | } 39 | 40 | @Provides 41 | @Singleton 42 | public GithubAPI providesGithubApi() { 43 | return new Retrofit.Builder() 44 | .baseUrl(GithubAPI.URL) 45 | .addConverterFactory(GsonConverterFactory.create()) 46 | .build() 47 | .create(GithubAPI.class); 48 | } 49 | 50 | @Provides 51 | @Singleton 52 | public RepoRepository providesGithubRepository(GithubAPI githubAPI) { 53 | return new RepoRepositoryNetwork(githubAPI); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /app/src/main/java/florent37/github/com/githubnewandroidarchitecture/dagger/RoomModule.java: -------------------------------------------------------------------------------- 1 | package florent37.github.com.githubnewandroidarchitecture.dagger; 2 | 3 | import android.app.Application; 4 | import android.arch.persistence.db.SupportSQLiteDatabase; 5 | import android.arch.persistence.room.Room; 6 | import android.arch.persistence.room.RoomDatabase; 7 | import android.content.Context; 8 | import android.os.Handler; 9 | import android.os.Looper; 10 | import android.support.annotation.NonNull; 11 | 12 | import javax.inject.Singleton; 13 | 14 | import dagger.Module; 15 | import dagger.Provides; 16 | import florent37.github.com.githubnewandroidarchitecture.local.UserDao; 17 | import florent37.github.com.githubnewandroidarchitecture.local.UserRoomDatabase; 18 | import florent37.github.com.githubnewandroidarchitecture.model.UserLocal; 19 | 20 | @Module 21 | public class RoomModule { 22 | 23 | private final UserRoomDatabase database; 24 | 25 | public RoomModule(Application application) { 26 | database = Room.databaseBuilder(application, UserRoomDatabase.class, "user_database") 27 | .build(); 28 | } 29 | 30 | @Provides 31 | @Singleton 32 | public UserRoomDatabase providesUserRoomDatabase(Context context) { 33 | return database; 34 | } 35 | 36 | @Singleton 37 | @Provides 38 | public UserDao providesProductDao(UserRoomDatabase userRoomDatabase) { 39 | return userRoomDatabase.userDao(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /app/src/main/java/florent37/github/com/githubnewandroidarchitecture/local/UserDao.java: -------------------------------------------------------------------------------- 1 | package florent37.github.com.githubnewandroidarchitecture.local; 2 | 3 | import android.arch.lifecycle.LiveData; 4 | import android.arch.persistence.room.Dao; 5 | import android.arch.persistence.room.Insert; 6 | import android.arch.persistence.room.OnConflictStrategy; 7 | import android.arch.persistence.room.Query; 8 | 9 | import florent37.github.com.githubnewandroidarchitecture.model.UserLocal; 10 | 11 | @Dao 12 | public interface UserDao { 13 | @Insert(onConflict = OnConflictStrategy.REPLACE) 14 | void insert(UserLocal user); 15 | 16 | @Query("DELETE FROM user_table") 17 | void deleteAll(); 18 | 19 | @Query("SELECT * from user_table WHERE login LIKE :login") 20 | LiveData getUser(String login); 21 | } 22 | -------------------------------------------------------------------------------- /app/src/main/java/florent37/github/com/githubnewandroidarchitecture/local/UserRepositoryLocal.java: -------------------------------------------------------------------------------- 1 | package florent37.github.com.githubnewandroidarchitecture.local; 2 | 3 | import android.arch.core.util.Function; 4 | import android.arch.lifecycle.LiveData; 5 | import android.arch.lifecycle.Transformations; 6 | 7 | import javax.inject.Inject; 8 | 9 | import florent37.github.com.githubnewandroidarchitecture.model.User; 10 | import florent37.github.com.githubnewandroidarchitecture.model.UserLocal; 11 | import florent37.github.com.githubnewandroidarchitecture.repository.UserRepository; 12 | 13 | public class UserRepositoryLocal implements UserRepository { 14 | 15 | private final UserDao userDao; 16 | 17 | @Inject 18 | public UserRepositoryLocal(UserDao userDao) { 19 | this.userDao = userDao; 20 | } 21 | 22 | @Override 23 | public LiveData searchUser(String userName){ 24 | return Transformations.map(userDao.getUser(userName), new Function() { 25 | @Override 26 | public User apply(UserLocal input) { 27 | if (input != null) { 28 | return input.toUser(); 29 | } else { 30 | return null; 31 | } 32 | } 33 | }); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /app/src/main/java/florent37/github/com/githubnewandroidarchitecture/local/UserRoomDatabase.java: -------------------------------------------------------------------------------- 1 | package florent37.github.com.githubnewandroidarchitecture.local; 2 | 3 | import android.arch.persistence.room.Database; 4 | import android.arch.persistence.room.RoomDatabase; 5 | import android.content.Context; 6 | 7 | import javax.inject.Singleton; 8 | 9 | import florent37.github.com.githubnewandroidarchitecture.model.UserLocal; 10 | 11 | @Singleton 12 | @Database(entities = {UserLocal.class}, version = 1) 13 | public abstract class UserRoomDatabase extends RoomDatabase { 14 | public abstract UserDao userDao(); 15 | } 16 | -------------------------------------------------------------------------------- /app/src/main/java/florent37/github/com/githubnewandroidarchitecture/model/Repo.java: -------------------------------------------------------------------------------- 1 | package florent37.github.com.githubnewandroidarchitecture.model; 2 | 3 | /** 4 | * Created by florentchampigny on 03/06/15. 5 | */ 6 | public class Repo implements Comparable { 7 | int id; 8 | String name; 9 | String full_name; 10 | String html_url; 11 | int forks_count; 12 | int stargazers_count; 13 | private int newStarsCount; 14 | private int newForksCount; 15 | 16 | //getters & setters 17 | 18 | public String getName() { 19 | return name; 20 | } 21 | 22 | public int getForks_count() { 23 | return forks_count; 24 | } 25 | 26 | public int getStargazers_count() { 27 | return stargazers_count; 28 | } 29 | 30 | public void setStargazers_count(int stargazers_count) { 31 | this.stargazers_count = stargazers_count; 32 | } 33 | 34 | public void setForks_count(int forks_count) { 35 | this.forks_count = forks_count; 36 | } 37 | 38 | public String getForks() { 39 | return String.valueOf(forks_count); 40 | } 41 | 42 | public String getStars() { 43 | return String.valueOf(stargazers_count); 44 | } 45 | 46 | public String getNewForks() { 47 | return String.valueOf(newForksCount); 48 | } 49 | 50 | public String getNewStars() { 51 | if (newStarsCount > 0) 52 | return "+" + String.valueOf(newStarsCount); 53 | else 54 | return String.valueOf(newStarsCount); 55 | } 56 | 57 | @Override 58 | public String toString() { 59 | return "Repo{" + 60 | "id=" + id + 61 | ", name='" + name + '\'' + 62 | ", forks_count=" + forks_count + 63 | ", stargazers_count=" + stargazers_count + 64 | '}'; 65 | } 66 | 67 | @Override 68 | public boolean equals(Object o) { 69 | if (this == o) return true; 70 | if (!(o instanceof Repo)) return false; 71 | 72 | Repo repo = (Repo) o; 73 | 74 | return id == repo.id; 75 | 76 | } 77 | 78 | @Override 79 | public int hashCode() { 80 | return id; 81 | } 82 | 83 | public void setNewStarsCount(int newStarsCount) { 84 | this.newStarsCount = newStarsCount; 85 | } 86 | 87 | public int getNewStarsCount() { 88 | return newStarsCount; 89 | } 90 | 91 | public void setNewForksCount(int newForksCount) { 92 | this.newForksCount = newForksCount; 93 | } 94 | 95 | public int getNewForksCount() { 96 | return newForksCount; 97 | } 98 | 99 | 100 | @Override 101 | public int compareTo(Repo another) { 102 | if (this.getNewStarsCount() > another.getNewStarsCount()) 103 | return -1; 104 | else if (this.getNewStarsCount() < another.getNewStarsCount()) 105 | return 1; 106 | else{ 107 | if(this.getStargazers_count() > another.getStargazers_count()) 108 | return -1; 109 | else if(this.getStargazers_count() < another.getStargazers_count()) 110 | return 1; 111 | } 112 | return 0; 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /app/src/main/java/florent37/github/com/githubnewandroidarchitecture/model/User.java: -------------------------------------------------------------------------------- 1 | package florent37.github.com.githubnewandroidarchitecture.model; 2 | 3 | public class User { 4 | 5 | private String avatarUrl; 6 | private String name; 7 | 8 | public String getAvatarUrl() { 9 | return avatarUrl; 10 | } 11 | 12 | public void setAvatarUrl(String avatarUrl) { 13 | this.avatarUrl = avatarUrl; 14 | } 15 | 16 | public String getName() { 17 | return name; 18 | } 19 | 20 | public void setName(String name) { 21 | this.name = name; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /app/src/main/java/florent37/github/com/githubnewandroidarchitecture/model/UserLocal.java: -------------------------------------------------------------------------------- 1 | package florent37.github.com.githubnewandroidarchitecture.model; 2 | 3 | import android.arch.persistence.room.ColumnInfo; 4 | import android.arch.persistence.room.Entity; 5 | import android.arch.persistence.room.PrimaryKey; 6 | 7 | @Entity(tableName = "user_table") 8 | public class UserLocal { 9 | 10 | @PrimaryKey(autoGenerate = true) 11 | private int id; 12 | 13 | @ColumnInfo(name = "avatar_url") 14 | private String avatarUrl; 15 | 16 | @ColumnInfo(name = "name") 17 | private String name; 18 | 19 | @ColumnInfo(name = "login") 20 | private String login; 21 | 22 | public UserLocal(String login, String name, String avatarUrl) { 23 | this.login = login; 24 | this.avatarUrl = avatarUrl; 25 | this.name = name; 26 | } 27 | 28 | public int getId() { 29 | return id; 30 | } 31 | 32 | public void setId(int id) { 33 | this.id = id; 34 | } 35 | 36 | public String getLogin() { 37 | return login; 38 | } 39 | 40 | public void setLogin(String login) { 41 | this.login = login; 42 | } 43 | 44 | public String getName() { 45 | return name; 46 | } 47 | 48 | public void setName(String name) { 49 | this.name = name; 50 | } 51 | 52 | public String getAvatarUrl() { 53 | return avatarUrl; 54 | } 55 | 56 | public void setAvatarUrl(String avatarUrl) { 57 | this.avatarUrl = avatarUrl; 58 | } 59 | 60 | public User toUser(){ 61 | final User user = new User(); 62 | user.setName(getName()); 63 | user.setAvatarUrl(getAvatarUrl()); 64 | return user; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /app/src/main/java/florent37/github/com/githubnewandroidarchitecture/model/UserNetwork.java: -------------------------------------------------------------------------------- 1 | package florent37.github.com.githubnewandroidarchitecture.model; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | 5 | public class UserNetwork { 6 | 7 | @SerializedName("avatar_url") 8 | String avatarUrl; 9 | @SerializedName("name") 10 | String name; 11 | 12 | public String getName() { 13 | return name; 14 | } 15 | 16 | public void setName(String name) { 17 | this.name = name; 18 | } 19 | 20 | public String getAvatarUrl() { 21 | return avatarUrl; 22 | } 23 | 24 | public void setAvatarUrl(String avatarUrl) { 25 | this.avatarUrl = avatarUrl; 26 | } 27 | 28 | public User toUser(){ 29 | final User user = new User(); 30 | user.setName(getName()); 31 | user.setAvatarUrl(getAvatarUrl()); 32 | return user; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /app/src/main/java/florent37/github/com/githubnewandroidarchitecture/model/UserSearchResult.java: -------------------------------------------------------------------------------- 1 | package florent37.github.com.githubnewandroidarchitecture.model; 2 | 3 | public class UserSearchResult { 4 | private final Exception exception; 5 | private final User user; 6 | 7 | public UserSearchResult(Exception exception) { 8 | this.exception = exception; 9 | this.user = null; 10 | } 11 | 12 | public UserSearchResult(User user){ 13 | this.user = user; 14 | this.exception = null; 15 | } 16 | 17 | public Exception getException() { 18 | return exception; 19 | } 20 | 21 | public User getUser() { 22 | return user; 23 | } 24 | 25 | public boolean success(){ 26 | return user != null; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /app/src/main/java/florent37/github/com/githubnewandroidarchitecture/network/GithubAPI.java: -------------------------------------------------------------------------------- 1 | package florent37.github.com.githubnewandroidarchitecture.network; 2 | 3 | import java.util.List; 4 | 5 | import florent37.github.com.githubnewandroidarchitecture.model.Repo; 6 | import florent37.github.com.githubnewandroidarchitecture.model.User; 7 | import florent37.github.com.githubnewandroidarchitecture.model.UserNetwork; 8 | import retrofit2.Call; 9 | import retrofit2.http.GET; 10 | import retrofit2.http.Path; 11 | 12 | /** 13 | * Created by florentchampigny on 30/07/15. 14 | */ 15 | public interface GithubAPI { 16 | 17 | String URL = "https://api.github.com/"; 18 | 19 | @GET("/users/{user}/repos") 20 | Call> listRepos(@Path("user") String user); 21 | 22 | @GET("/users/{user}") 23 | Call user(@Path("user") String user); 24 | 25 | } 26 | -------------------------------------------------------------------------------- /app/src/main/java/florent37/github/com/githubnewandroidarchitecture/network/RepoRepositoryNetwork.java: -------------------------------------------------------------------------------- 1 | package florent37.github.com.githubnewandroidarchitecture.network; 2 | 3 | import android.arch.lifecycle.LiveData; 4 | import android.arch.lifecycle.MutableLiveData; 5 | 6 | import java.util.List; 7 | 8 | import florent37.github.com.githubnewandroidarchitecture.network.GithubAPI; 9 | import florent37.github.com.githubnewandroidarchitecture.model.Repo; 10 | import florent37.github.com.githubnewandroidarchitecture.repository.RepoRepository; 11 | import retrofit2.Call; 12 | import retrofit2.Callback; 13 | import retrofit2.Response; 14 | 15 | /** 16 | * Created by florentchampigny on 18/05/2017. 17 | */ 18 | 19 | public class RepoRepositoryNetwork implements RepoRepository { 20 | 21 | private final GithubAPI githubAPI; 22 | 23 | public RepoRepositoryNetwork(GithubAPI githubAPI) { 24 | this.githubAPI = githubAPI; 25 | } 26 | 27 | @Override 28 | public LiveData> getRepos(String userName){ 29 | final MutableLiveData> liveData = new MutableLiveData<>(); 30 | 31 | githubAPI.listRepos(userName).enqueue(new Callback>() { 32 | @Override 33 | public void onResponse(Call> call, Response> response) { 34 | if(response.isSuccessful()){ 35 | liveData.setValue(response.body()); 36 | } 37 | } 38 | 39 | @Override 40 | public void onFailure(Call> call, Throwable t) { 41 | 42 | } 43 | }); 44 | 45 | return liveData; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /app/src/main/java/florent37/github/com/githubnewandroidarchitecture/network/UserRepositoryNetwork.java: -------------------------------------------------------------------------------- 1 | package florent37.github.com.githubnewandroidarchitecture.network; 2 | 3 | import android.arch.core.util.Function; 4 | import android.arch.lifecycle.LiveData; 5 | import android.arch.lifecycle.MutableLiveData; 6 | import android.arch.lifecycle.Transformations; 7 | 8 | import javax.inject.Inject; 9 | 10 | import florent37.github.com.githubnewandroidarchitecture.model.UserNetwork; 11 | import florent37.github.com.githubnewandroidarchitecture.network.GithubAPI; 12 | import florent37.github.com.githubnewandroidarchitecture.model.User; 13 | import florent37.github.com.githubnewandroidarchitecture.repository.UserRepository; 14 | import retrofit2.Call; 15 | import retrofit2.Callback; 16 | import retrofit2.Response; 17 | 18 | public class UserRepositoryNetwork implements UserRepository { 19 | 20 | private final GithubAPI githubAPI; 21 | 22 | @Inject 23 | public UserRepositoryNetwork(GithubAPI githubAPI) { 24 | this.githubAPI = githubAPI; 25 | } 26 | 27 | @Override 28 | public LiveData searchUser(String userName){ 29 | final MutableLiveData liveData = new MutableLiveData<>(); 30 | 31 | githubAPI.user(userName).enqueue(new Callback() { 32 | @Override 33 | public void onResponse(Call call, Response response) { 34 | if(response.isSuccessful()) { 35 | liveData.setValue(response.body()); 36 | } 37 | } 38 | 39 | @Override 40 | public void onFailure(Call call, Throwable t) { 41 | 42 | } 43 | }); 44 | 45 | return Transformations.map(liveData, new Function() { 46 | @Override 47 | public User apply(UserNetwork input) { 48 | return input.toUser(); 49 | } 50 | }); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /app/src/main/java/florent37/github/com/githubnewandroidarchitecture/repository/RepoRepository.java: -------------------------------------------------------------------------------- 1 | package florent37.github.com.githubnewandroidarchitecture.repository; 2 | 3 | import android.arch.lifecycle.LiveData; 4 | 5 | import java.util.List; 6 | 7 | import florent37.github.com.githubnewandroidarchitecture.model.Repo; 8 | 9 | /** 10 | * Created by florentchampigny on 18/05/2017. 11 | */ 12 | 13 | public interface RepoRepository { 14 | LiveData> getRepos(String userName); 15 | } 16 | -------------------------------------------------------------------------------- /app/src/main/java/florent37/github/com/githubnewandroidarchitecture/repository/UserRepository.java: -------------------------------------------------------------------------------- 1 | package florent37.github.com.githubnewandroidarchitecture.repository; 2 | 3 | import android.arch.lifecycle.LiveData; 4 | import android.arch.lifecycle.MutableLiveData; 5 | 6 | import florent37.github.com.githubnewandroidarchitecture.model.User; 7 | 8 | public interface UserRepository { 9 | LiveData searchUser(String name); 10 | } 11 | -------------------------------------------------------------------------------- /app/src/main/java/florent37/github/com/githubnewandroidarchitecture/viewmodel/ReposListViewModel.java: -------------------------------------------------------------------------------- 1 | package florent37.github.com.githubnewandroidarchitecture.viewmodel; 2 | 3 | import android.app.Application; 4 | import android.arch.lifecycle.AndroidViewModel; 5 | import android.arch.lifecycle.LiveData; 6 | import android.arch.lifecycle.MediatorLiveData; 7 | import android.arch.lifecycle.Observer; 8 | import android.support.annotation.NonNull; 9 | import android.support.annotation.Nullable; 10 | 11 | import java.util.List; 12 | 13 | import javax.inject.Inject; 14 | 15 | import florent37.github.com.githubnewandroidarchitecture.MainApplication; 16 | import florent37.github.com.githubnewandroidarchitecture.model.Repo; 17 | import florent37.github.com.githubnewandroidarchitecture.repository.RepoRepository; 18 | 19 | /** 20 | * Created by florentchampigny on 18/05/2017. 21 | */ 22 | 23 | public class ReposListViewModel extends AndroidViewModel { 24 | 25 | @Inject 26 | RepoRepository repoRepository; 27 | 28 | private MediatorLiveData> reposLiveData = new MediatorLiveData<>(); 29 | 30 | public ReposListViewModel(@NonNull Application application) { 31 | super(application); 32 | ((MainApplication) application).getAppComponent().inject(this); 33 | } 34 | 35 | public void searchRepos(@Nullable final String userName) { 36 | if (userName != null) { 37 | final LiveData> repoLiveData = repoRepository.getRepos(userName); 38 | reposLiveData.addSource(repoLiveData, new Observer>() { 39 | @Override 40 | public void onChanged(@Nullable List repos) { 41 | reposLiveData.setValue(repos); 42 | reposLiveData.removeSource(repoLiveData); 43 | } 44 | }); 45 | } 46 | } 47 | 48 | public LiveData> getReposLiveData() { 49 | return reposLiveData; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /app/src/main/java/florent37/github/com/githubnewandroidarchitecture/viewmodel/UserViewModel.java: -------------------------------------------------------------------------------- 1 | package florent37.github.com.githubnewandroidarchitecture.viewmodel; 2 | 3 | import android.app.Application; 4 | import android.arch.lifecycle.AndroidViewModel; 5 | import android.arch.lifecycle.LiveData; 6 | import android.arch.lifecycle.MediatorLiveData; 7 | import android.arch.lifecycle.MutableLiveData; 8 | import android.arch.lifecycle.Observer; 9 | import android.support.annotation.NonNull; 10 | import android.support.annotation.Nullable; 11 | 12 | import javax.inject.Inject; 13 | 14 | import florent37.github.com.githubnewandroidarchitecture.MainApplication; 15 | import florent37.github.com.githubnewandroidarchitecture.model.User; 16 | import florent37.github.com.githubnewandroidarchitecture.model.UserSearchResult; 17 | import florent37.github.com.githubnewandroidarchitecture.repository.RepoRepository; 18 | import florent37.github.com.githubnewandroidarchitecture.local.UserRepositoryLocal; 19 | import florent37.github.com.githubnewandroidarchitecture.network.UserRepositoryNetwork; 20 | 21 | /** 22 | * Created by florentchampigny on 18/05/2017. 23 | */ 24 | 25 | public class UserViewModel extends AndroidViewModel { 26 | 27 | @Inject 28 | RepoRepository repoRepository; 29 | 30 | @Inject 31 | UserRepositoryNetwork userRepositoryNetwork; 32 | 33 | @Inject 34 | UserRepositoryLocal userRepositoryLocal; 35 | 36 | public UserViewModel(@NonNull Application application) { 37 | super(application); 38 | ((MainApplication) application).getAppComponent().inject(this); 39 | } 40 | 41 | private final MediatorLiveData userLiveData = new MediatorLiveData<>(); 42 | 43 | public LiveData getUserLiveData() { 44 | return userLiveData; 45 | } 46 | 47 | public void searchOnline(@Nullable final String userName) { 48 | if (userName != null) { 49 | //userLiveData will be notified when the user is fetched 50 | final LiveData userLiveDataNetwork = userRepositoryNetwork.searchUser(userName); 51 | userLiveData.addSource(userLiveDataNetwork, new Observer() { 52 | @Override 53 | public void onChanged(@Nullable User user) { 54 | userLiveData.postValue(new UserSearchResult(user)); 55 | userLiveData.removeSource(userLiveDataNetwork); 56 | } 57 | }); 58 | } 59 | } 60 | 61 | public void searchLocally(@Nullable final String userName) { 62 | if (userName != null) { 63 | //userLiveData will be notified when the user is fetched 64 | final LiveData userLiveDataLocal = userRepositoryLocal.searchUser(userName); 65 | userLiveData.addSource(userLiveDataLocal, new Observer() { 66 | @Override 67 | public void onChanged(@Nullable User user) { 68 | userLiveData.postValue(new UserSearchResult(user)); 69 | userLiveData.removeSource(userLiveDataLocal); 70 | } 71 | }); 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | -------------------------------------------------------------------------------- /app/src/main/res/layout/fragment_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 11 | 12 | 13 | 17 | 18 | 23 | 24 |