├── .gitignore ├── LICENSE ├── README.md └── rxandroid ├── .gitignore ├── .idea ├── .name ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── encodings.xml ├── gradle.xml ├── misc.xml ├── modules.xml ├── scopes │ └── scope_settings.xml └── vcs.xml ├── api ├── .gitignore ├── build.gradle └── src │ └── main │ └── java │ └── com │ └── zsiegel │ └── rxandroid │ └── api │ ├── ApiModule.java │ └── UserApiService.java ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── zsiegel │ │ └── rxandroid │ │ └── test │ │ ├── ApplicationTest.java │ │ └── UserServiceTest.java │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── zsiegel │ │ └── rxandroid │ │ └── test │ │ ├── AppModule.java │ │ ├── Modules.java │ │ ├── SampleApp.java │ │ └── UserService.java │ └── res │ ├── drawable-hdpi │ └── ic_launcher.png │ ├── drawable-mdpi │ └── ic_launcher.png │ ├── drawable-xhdpi │ └── ic_launcher.png │ ├── drawable-xxhdpi │ └── ic_launcher.png │ └── values │ ├── strings.xml │ └── styles.xml ├── build.gradle ├── core ├── .gitignore ├── build.gradle └── src │ └── main │ └── java │ └── com │ └── zsiegel │ └── rxandroid │ ├── MockUserService.java │ ├── model │ └── User.java │ └── request │ └── DataRequest.java ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── libraries.gradle ├── persistence ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── zsiegel │ │ └── rxandroid │ │ └── persistence │ │ └── ApplicationTest.java │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── zsiegel │ │ └── rxandroid │ │ └── persistence │ │ ├── PersistenceModule.java │ │ └── UserPersistenceService.java │ └── res │ ├── drawable-hdpi │ └── ic_launcher.png │ ├── drawable-mdpi │ └── ic_launcher.png │ ├── drawable-xhdpi │ └── ic_launcher.png │ ├── drawable-xxhdpi │ └── ic_launcher.png │ └── values │ └── strings.xml └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | 15 | # Gradle files 16 | .gradle/ 17 | build/ 18 | 19 | # Local configuration file (sdk path, etc) 20 | local.properties 21 | 22 | # Proguard folder generated by Eclipse 23 | proguard/ 24 | 25 | # Log Files 26 | *.log 27 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Zac Siegel 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | rxandroid-architecture-sample 2 | ============================= 3 | 4 | A sample project to demonstrate an android app that can consume data from multiple sources using RxJava. 5 | 6 | The basic project has 4 modules 7 | 8 | - app (this is the android application) 9 | - persistence (this is an android module that typically would have a content provider or even local data caching) 10 | - api (this is a java module for communication with an API) 11 | - core (this is a java module containing core domain classes) 12 | 13 | Each module provides a specific set of functionality and exposes a Dagger module for access to their services. 14 | 15 | At the application level we combine these modules and services and create a single service layer for the application to communicate with. 16 | 17 | In this example the `UserService` that we use in our application is composed of both a `UserApiService` and a `UserPersistenceService`. The `UserService` is responsible for composing and serving the data based on the `DataRequest`. 18 | 19 | A `DataRequest` at the basic level allows the user to specify their source (local(persistence), remote(api), or refresh(persistence and api)) and an ID to get a specific resource. This can be later extended for more querying and filtering as needed. -------------------------------------------------------------------------------- /rxandroid/.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | *.iml 3 | *.ipr 4 | /local.properties 5 | /.idea/workspace.xml 6 | /.idea/libraries 7 | .DS_Store 8 | /build 9 | -------------------------------------------------------------------------------- /rxandroid/.idea/.name: -------------------------------------------------------------------------------- 1 | Sample -------------------------------------------------------------------------------- /rxandroid/.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /rxandroid/.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /rxandroid/.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /rxandroid/.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /rxandroid/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /rxandroid/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /rxandroid/.idea/scopes/scope_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | -------------------------------------------------------------------------------- /rxandroid/.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /rxandroid/api/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /rxandroid/api/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java' 2 | 3 | dependencies { 4 | compile project(':core') 5 | compile rootProject.ext.libraries.dagger 6 | compile rootProject.ext.libraries.daggerCompiler 7 | compile rootProject.ext.libraries.rxandroid 8 | 9 | testCompile rootProject.ext.testLibraries.junit 10 | } -------------------------------------------------------------------------------- /rxandroid/api/src/main/java/com/zsiegel/rxandroid/api/ApiModule.java: -------------------------------------------------------------------------------- 1 | package com.zsiegel.rxandroid.api; 2 | 3 | import javax.inject.Singleton; 4 | 5 | import dagger.Module; 6 | import dagger.Provides; 7 | 8 | @Module(complete = false, library = true) 9 | public class ApiModule { 10 | 11 | private final String baseUrl; 12 | 13 | public ApiModule(String baseUrl) { 14 | super(); 15 | this.baseUrl = baseUrl; 16 | } 17 | 18 | @Provides 19 | @Singleton 20 | UserApiService providesApiService() { 21 | return new UserApiService(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /rxandroid/api/src/main/java/com/zsiegel/rxandroid/api/UserApiService.java: -------------------------------------------------------------------------------- 1 | package com.zsiegel.rxandroid.api; 2 | 3 | import com.zsiegel.rxandroid.MockUserService; 4 | import com.zsiegel.rxandroid.model.User; 5 | 6 | import java.util.Arrays; 7 | import java.util.List; 8 | 9 | import rx.Observable; 10 | import rx.Subscriber; 11 | 12 | /** 13 | * A service to get users from a remote API 14 | * 15 | * @author zsiegel (zac@akta.com) 16 | */ 17 | public class UserApiService { 18 | 19 | public static final int MAX_API_USERS = 10; 20 | 21 | public UserApiService() { 22 | super(); 23 | } 24 | 25 | public Observable> get(final long id) { 26 | return Observable.create(new Observable.OnSubscribe>() { 27 | @Override 28 | public void call(Subscriber> subscriber) { 29 | 30 | if (id < 0) { 31 | //Return all the users ex. make a rest call to /users 32 | subscriber.onNext(MockUserService.getMockData(10)); 33 | } else { 34 | //Return a single user ex. make a REST call to /users/{id} 35 | try { 36 | subscriber.onNext(Arrays.asList(MockUserService.getMockData(MAX_API_USERS).get((int) id))); 37 | } catch (Exception e) { 38 | //Maybe we did not find the user 39 | subscriber.onError(new Exception("User not found")); 40 | return; 41 | } 42 | } 43 | subscriber.onCompleted(); 44 | } 45 | }); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /rxandroid/app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /rxandroid/app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 21 5 | buildToolsVersion rootProject.ext.buildToolsVersion 6 | 7 | defaultConfig { 8 | applicationId "com.zsiegel.rxandroid.sample" 9 | minSdkVersion 16 10 | targetSdkVersion 21 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | compileOptions { 15 | sourceCompatibility JavaVersion.VERSION_1_7 16 | targetCompatibility JavaVersion.VERSION_1_7 17 | } 18 | buildTypes { 19 | release { 20 | minifyEnabled false 21 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 22 | } 23 | } 24 | aaptOptions { 25 | noCompress 'txt' 26 | ignoreAssetsPattern "!.svn:!.git:!.ds_store:!*.scc:.*:_*:!CVS:!thumbs.db:!picasa.ini:!*~" 27 | } 28 | lintOptions { 29 | checkReleaseBuilds false 30 | abortOnError false 31 | } 32 | packagingOptions { 33 | exclude 'asm-license.txt' 34 | exclude 'LICENSE' 35 | exclude 'LICENSE.txt' 36 | exclude 'NOTICE' 37 | exclude 'META-INF/LICENSE.txt' 38 | exclude 'META-INF/NOTICE.txt' 39 | exclude 'META-INF/services/javax.annotation.processing.Processor' 40 | } 41 | } 42 | 43 | dependencies { 44 | compile project(':core') 45 | compile project(':api') 46 | compile project(':persistence') 47 | 48 | compile fileTree(dir: 'libs', include: ['*.jar']) 49 | compile rootProject.ext.libraries.appCompat 50 | compile rootProject.ext.libraries.androidSupport 51 | compile rootProject.ext.libraries.dagger 52 | compile rootProject.ext.libraries.daggerCompiler 53 | compile rootProject.ext.libraries.butterknife 54 | compile rootProject.ext.libraries.rxandroid 55 | } 56 | -------------------------------------------------------------------------------- /rxandroid/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 /usr/local/opt/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 | -------------------------------------------------------------------------------- /rxandroid/app/src/androidTest/java/com/zsiegel/rxandroid/test/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.zsiegel.rxandroid.test; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /rxandroid/app/src/androidTest/java/com/zsiegel/rxandroid/test/UserServiceTest.java: -------------------------------------------------------------------------------- 1 | package com.zsiegel.rxandroid.test; 2 | 3 | import android.test.AndroidTestCase; 4 | 5 | import com.zsiegel.rxandroid.api.UserApiService; 6 | import com.zsiegel.rxandroid.model.User; 7 | import com.zsiegel.rxandroid.persistence.UserPersistenceService; 8 | import com.zsiegel.rxandroid.request.DataRequest; 9 | 10 | import java.util.List; 11 | 12 | /** 13 | * @author zsiegel (zac@akta.com) 14 | */ 15 | public class UserServiceTest extends AndroidTestCase { 16 | 17 | UserApiService apiService; 18 | UserPersistenceService persistenceService; 19 | UserService userService; 20 | 21 | @Override 22 | protected void setUp() throws Exception { 23 | super.setUp(); 24 | apiService = new UserApiService(); 25 | persistenceService = new UserPersistenceService(); 26 | userService = new UserService(apiService, persistenceService); 27 | } 28 | 29 | public void testRefresh() { 30 | DataRequest refreshRequest = new DataRequest(DataRequest.Source.REFRESH, -1); 31 | List> users = userService.get(refreshRequest).toList().toBlocking().single(); 32 | assertEquals(users.size(), 2); 33 | assertEquals(users.get(0).size(), UserPersistenceService.MAX_LOCAL_USERS); 34 | assertEquals(users.get(1).size(), UserApiService.MAX_API_USERS); 35 | } 36 | 37 | public void testGetUsersRemote() { 38 | DataRequest remoteRequest = new DataRequest(DataRequest.Source.REMOTE, -1); 39 | List> users = userService.get(remoteRequest).toList().toBlocking().single(); 40 | assertEquals(users.size(), 1); 41 | assertEquals(users.get(0).size(), UserApiService.MAX_API_USERS); 42 | } 43 | 44 | public void testGetUsersLocal() { 45 | DataRequest localRequest = new DataRequest(DataRequest.Source.LOCAL, -1); 46 | List> users = userService.get(localRequest).toList().toBlocking().single(); 47 | assertEquals(users.size(), 1); 48 | assertEquals(users.get(0).size(), UserPersistenceService.MAX_LOCAL_USERS); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /rxandroid/app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /rxandroid/app/src/main/java/com/zsiegel/rxandroid/test/AppModule.java: -------------------------------------------------------------------------------- 1 | package com.zsiegel.rxandroid.test; 2 | 3 | import com.zsiegel.rxandroid.api.UserApiService; 4 | import com.zsiegel.rxandroid.persistence.UserPersistenceService; 5 | 6 | import javax.inject.Singleton; 7 | 8 | import dagger.Module; 9 | import dagger.Provides; 10 | 11 | /** 12 | * @author zsiegel (zac@akta.com) 13 | */ 14 | @Module(complete = false, library = true, injects = {}) 15 | public class AppModule { 16 | 17 | public AppModule() { 18 | super(); 19 | } 20 | 21 | @Provides 22 | @Singleton 23 | UserService providesUserService(UserApiService apiService, UserPersistenceService persistenceService) { 24 | return new UserService(apiService, persistenceService); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /rxandroid/app/src/main/java/com/zsiegel/rxandroid/test/Modules.java: -------------------------------------------------------------------------------- 1 | package com.zsiegel.rxandroid.test; 2 | 3 | import com.zsiegel.rxandroid.api.ApiModule; 4 | import com.zsiegel.rxandroid.persistence.PersistenceModule; 5 | 6 | import dagger.ObjectGraph; 7 | 8 | /** 9 | * @author zsiegel (zac@akta.com) 10 | */ 11 | public class Modules { 12 | 13 | private static Modules modules; 14 | 15 | private ObjectGraph objectGraph; 16 | 17 | private Modules() { 18 | } 19 | 20 | public static Modules instance() { 21 | if (modules == null) { 22 | modules = new Modules(); 23 | } 24 | return modules; 25 | } 26 | 27 | public static Object[] modulesForApp() { 28 | return new Object[]{ 29 | new AppModule(), 30 | new ApiModule("https://my.cool.endpoint"), 31 | new PersistenceModule(), 32 | }; 33 | } 34 | 35 | public void setObjectGraph(ObjectGraph objectGraph) { 36 | this.objectGraph = objectGraph; 37 | } 38 | 39 | public ObjectGraph getObjectGraph() { 40 | return objectGraph; 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /rxandroid/app/src/main/java/com/zsiegel/rxandroid/test/SampleApp.java: -------------------------------------------------------------------------------- 1 | package com.zsiegel.rxandroid.test; 2 | 3 | import android.app.Application; 4 | 5 | import dagger.ObjectGraph; 6 | 7 | /** 8 | * @author zsiegel (zac@akta.com) 9 | */ 10 | public class SampleApp extends Application { 11 | 12 | private static SampleApp appContext; 13 | 14 | public static SampleApp get() { 15 | return appContext; 16 | } 17 | 18 | @Override 19 | public void onCreate() { 20 | super.onCreate(); 21 | appContext = (SampleApp) getApplicationContext(); 22 | buildObjectGraph(); 23 | } 24 | 25 | private void buildObjectGraph() { 26 | ObjectGraph objectGraph = ObjectGraph.create(Modules.modulesForApp()); 27 | Modules.instance().setObjectGraph(objectGraph); 28 | } 29 | 30 | public void inject(Object obj) { 31 | Modules.instance().getObjectGraph().inject(obj); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /rxandroid/app/src/main/java/com/zsiegel/rxandroid/test/UserService.java: -------------------------------------------------------------------------------- 1 | package com.zsiegel.rxandroid.test; 2 | 3 | import com.zsiegel.rxandroid.api.UserApiService; 4 | import com.zsiegel.rxandroid.model.User; 5 | import com.zsiegel.rxandroid.persistence.UserPersistenceService; 6 | import com.zsiegel.rxandroid.request.DataRequest; 7 | 8 | import java.util.List; 9 | 10 | import rx.Observable; 11 | import rx.functions.Func1; 12 | 13 | /** 14 | * A service that routes a User request 15 | * 16 | * @author zsiegel (zac@akta.com) 17 | */ 18 | public class UserService { 19 | 20 | private final UserApiService apiService; 21 | private final UserPersistenceService persistenceService; 22 | 23 | public UserService(UserApiService apiService, UserPersistenceService persistenceService) { 24 | super(); 25 | this.apiService = apiService; 26 | this.persistenceService = persistenceService; 27 | } 28 | 29 | public Observable> get(DataRequest request) { 30 | 31 | //By default we go to the api for the data 32 | Observable> operation; 33 | 34 | switch (request.source) { 35 | case REMOTE: 36 | operation = apiService.get(request.id); 37 | break; 38 | case LOCAL: 39 | operation = persistenceService.get(request.id); 40 | break; 41 | case REFRESH: 42 | //return local data first, load from the api, save that local, then finally return latest 43 | operation = apiService.get(request.id).flatMap(new Func1, Observable>>() { 44 | @Override 45 | public Observable> call(List users) { 46 | return persistenceService.save(users); 47 | } 48 | }).startWith(persistenceService.get(request.id)); 49 | break; 50 | default: 51 | //default to the api request 52 | operation = apiService.get(request.id); 53 | break; 54 | } 55 | 56 | return operation; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /rxandroid/app/src/main/res/drawable-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zsiegel/rxandroid-architecture-sample/94247c3534c974dfa90a35e762977e0ecacb1f1e/rxandroid/app/src/main/res/drawable-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /rxandroid/app/src/main/res/drawable-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zsiegel/rxandroid-architecture-sample/94247c3534c974dfa90a35e762977e0ecacb1f1e/rxandroid/app/src/main/res/drawable-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /rxandroid/app/src/main/res/drawable-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zsiegel/rxandroid-architecture-sample/94247c3534c974dfa90a35e762977e0ecacb1f1e/rxandroid/app/src/main/res/drawable-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /rxandroid/app/src/main/res/drawable-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zsiegel/rxandroid-architecture-sample/94247c3534c974dfa90a35e762977e0ecacb1f1e/rxandroid/app/src/main/res/drawable-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /rxandroid/app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | Sample 3 | 4 | -------------------------------------------------------------------------------- /rxandroid/app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /rxandroid/build.gradle: -------------------------------------------------------------------------------- 1 | apply from: "./libraries.gradle" 2 | 3 | buildscript { 4 | repositories { 5 | jcenter() 6 | } 7 | dependencies { 8 | classpath 'com.android.tools.build:gradle:0.14.2' 9 | 10 | // NOTE: Do not place your application dependencies here; they belong 11 | // in the individual module build.gradle files 12 | } 13 | } 14 | 15 | allprojects { 16 | repositories { 17 | jcenter() 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /rxandroid/core/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /rxandroid/core/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java' 2 | 3 | dependencies { 4 | compile fileTree(dir: 'libs', include: ['*.jar']) 5 | } -------------------------------------------------------------------------------- /rxandroid/core/src/main/java/com/zsiegel/rxandroid/MockUserService.java: -------------------------------------------------------------------------------- 1 | package com.zsiegel.rxandroid; 2 | 3 | import com.zsiegel.rxandroid.model.User; 4 | 5 | import java.util.ArrayList; 6 | import java.util.Date; 7 | import java.util.List; 8 | 9 | public class MockUserService { 10 | 11 | public static List getMockData(int size) { 12 | List users = new ArrayList(); 13 | for (int idx = 0; idx < size; idx++) { 14 | User user = new User(); 15 | user.id = idx; 16 | user.lastUpdated = new Date(); 17 | user.status = (idx % 3 == 0) ? "ONLINE" : "OFFLINE"; 18 | users.add(user); 19 | } 20 | return users; 21 | } 22 | } -------------------------------------------------------------------------------- /rxandroid/core/src/main/java/com/zsiegel/rxandroid/model/User.java: -------------------------------------------------------------------------------- 1 | package com.zsiegel.rxandroid.model; 2 | 3 | import java.util.Date; 4 | 5 | /** 6 | * A basic User model 7 | * 8 | * @author zsiegel (zac@akta.com) 9 | */ 10 | public class User { 11 | 12 | public long id; 13 | public String username; 14 | public String status; 15 | public Date lastUpdated; 16 | } 17 | -------------------------------------------------------------------------------- /rxandroid/core/src/main/java/com/zsiegel/rxandroid/request/DataRequest.java: -------------------------------------------------------------------------------- 1 | package com.zsiegel.rxandroid.request; 2 | 3 | /** 4 | * @author zsiegel (zac@akta.com) 5 | */ 6 | public class DataRequest { 7 | 8 | public enum Source { 9 | REMOTE, //Fetch the data from the remote source 10 | LOCAL, //Fetch the data from the local source 11 | REFRESH //Fetch the local data first, then update the local data from the remote source 12 | } 13 | 14 | public long id = -1; 15 | public Source source; 16 | 17 | /** 18 | * A request for data 19 | * 20 | * @param source the source of the data 21 | * @param id the id of the data, if not specified all data will be returned 22 | */ 23 | public DataRequest(Source source, long id) { 24 | super(); 25 | this.source = source; 26 | this.id = id; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /rxandroid/gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m 13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 14 | 15 | # When configured, Gradle will run in incubating parallel mode. 16 | # This option should only be used with decoupled projects. More details, visit 17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 18 | # org.gradle.parallel=true -------------------------------------------------------------------------------- /rxandroid/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zsiegel/rxandroid-architecture-sample/94247c3534c974dfa90a35e762977e0ecacb1f1e/rxandroid/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /rxandroid/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Apr 10 15:27:10 PDT 2013 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.1-all.zip 7 | -------------------------------------------------------------------------------- /rxandroid/gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" >&- 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" >&- 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /rxandroid/gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /rxandroid/libraries.gradle: -------------------------------------------------------------------------------- 1 | ext { 2 | 3 | //Android 4 | buildToolsVersion = '21.1.1' 5 | daggerVersion = '1.2.2' 6 | butterknifeVersion = '6.0.0' 7 | androidSupportVersion = '21.0.+' 8 | rxandroidVersion = '0.22.0' 9 | appCompatVersion = '21.0.0' 10 | recyclerViewVersion = '+' 11 | 12 | //Java 13 | rxjavaVersion = '1.0.0-rc.12' 14 | 15 | //Testing 16 | junitVersion = '4.11' 17 | 18 | libraries = [ 19 | appCompat : "com.android.support:appcompat-v7:${appCompatVersion}", 20 | butterknife : "com.jakewharton:butterknife:${butterknifeVersion}", 21 | dagger : "com.squareup.dagger:dagger:${daggerVersion}", 22 | daggerCompiler: "com.squareup.dagger:dagger-compiler:${daggerVersion}", 23 | rxjava : "io.reactivex:rxjava:${rxjavaVersion}", 24 | rxandroid : "io.reactivex:rxandroid:${rxandroidVersion}", 25 | androidSupport: "com.android.support:support-v13:${androidSupportVersion}", 26 | ] 27 | 28 | testLibraries = [ 29 | junit: "junit:junit:${junitVersion}", 30 | ] 31 | 32 | } -------------------------------------------------------------------------------- /rxandroid/persistence/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /rxandroid/persistence/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion 21 5 | buildToolsVersion rootProject.ext.buildToolsVersion 6 | 7 | defaultConfig { 8 | applicationId "com.zsiegel.rxandroid.persistence" 9 | minSdkVersion 16 10 | targetSdkVersion 21 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile project(':core') 24 | compile rootProject.ext.libraries.dagger 25 | compile rootProject.ext.libraries.daggerCompiler 26 | compile rootProject.ext.libraries.rxandroid 27 | } 28 | -------------------------------------------------------------------------------- /rxandroid/persistence/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 /usr/local/opt/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 | -------------------------------------------------------------------------------- /rxandroid/persistence/src/androidTest/java/com/zsiegel/rxandroid/persistence/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.zsiegel.rxandroid.persistence; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /rxandroid/persistence/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /rxandroid/persistence/src/main/java/com/zsiegel/rxandroid/persistence/PersistenceModule.java: -------------------------------------------------------------------------------- 1 | package com.zsiegel.rxandroid.persistence; 2 | 3 | import dagger.Module; 4 | 5 | /** 6 | * @author zsiegel (zac@akta.com) 7 | */ 8 | @Module(complete = false, library = true) 9 | public class PersistenceModule { 10 | 11 | public PersistenceModule() { 12 | super(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /rxandroid/persistence/src/main/java/com/zsiegel/rxandroid/persistence/UserPersistenceService.java: -------------------------------------------------------------------------------- 1 | package com.zsiegel.rxandroid.persistence; 2 | 3 | import com.zsiegel.rxandroid.MockUserService; 4 | import com.zsiegel.rxandroid.model.User; 5 | 6 | import java.util.Arrays; 7 | import java.util.List; 8 | 9 | import rx.Observable; 10 | import rx.Subscriber; 11 | 12 | /** 13 | * A service to get users from a persistence source such as a ContentResolver 14 | * 15 | * @author zsiegel (zac@akta.com) 16 | */ 17 | public class UserPersistenceService { 18 | 19 | public static final int MAX_LOCAL_USERS = 5; 20 | 21 | public UserPersistenceService() { 22 | super(); 23 | } 24 | 25 | public Observable> get(final long id) { 26 | return Observable.create(new Observable.OnSubscribe>() { 27 | @Override 28 | public void call(Subscriber> subscriber) { 29 | 30 | if (id < 0) { 31 | //Return all the users in the database ex. select * from users; 32 | subscriber.onNext(MockUserService.getMockData(5)); 33 | } else { 34 | //Return a single user ex. select * from user with id = {id}; 35 | try { 36 | subscriber.onNext(Arrays.asList(MockUserService.getMockData(MAX_LOCAL_USERS).get((int) id))); 37 | } catch (Exception e) { 38 | //Maybe we did not find the user 39 | subscriber.onError(new Exception("User not found")); 40 | return; 41 | } 42 | } 43 | 44 | subscriber.onCompleted(); 45 | } 46 | }); 47 | } 48 | 49 | public Observable> save(final List users) { 50 | return Observable.create(new Observable.OnSubscribe>() { 51 | @Override 52 | public void call(Subscriber> subscriber) { 53 | 54 | //Return the users that were saved 55 | subscriber.onNext(users); 56 | subscriber.onCompleted(); 57 | } 58 | }); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /rxandroid/persistence/src/main/res/drawable-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zsiegel/rxandroid-architecture-sample/94247c3534c974dfa90a35e762977e0ecacb1f1e/rxandroid/persistence/src/main/res/drawable-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /rxandroid/persistence/src/main/res/drawable-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zsiegel/rxandroid-architecture-sample/94247c3534c974dfa90a35e762977e0ecacb1f1e/rxandroid/persistence/src/main/res/drawable-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /rxandroid/persistence/src/main/res/drawable-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zsiegel/rxandroid-architecture-sample/94247c3534c974dfa90a35e762977e0ecacb1f1e/rxandroid/persistence/src/main/res/drawable-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /rxandroid/persistence/src/main/res/drawable-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zsiegel/rxandroid-architecture-sample/94247c3534c974dfa90a35e762977e0ecacb1f1e/rxandroid/persistence/src/main/res/drawable-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /rxandroid/persistence/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | Persistence 3 | 4 | -------------------------------------------------------------------------------- /rxandroid/settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':api', ':persistence', ':core' 2 | --------------------------------------------------------------------------------