├── app ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── drawable │ │ │ │ ├── iron_bank.png │ │ │ │ ├── night_king.png │ │ │ │ ├── house_targaryen.png │ │ │ │ ├── house_stark_shield.png │ │ │ │ ├── house_lannister_shield.png │ │ │ │ ├── ic_thumb_up_black_24dp.xml │ │ │ │ └── ic_thumb_down_black_24dp.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 │ │ │ └── layout │ │ │ │ ├── debt_feed_item.xml │ │ │ │ ├── battle_feed_fragment.xml │ │ │ │ ├── dashboard_activity.xml │ │ │ │ ├── battle_feed_item.xml │ │ │ │ └── house_fragment.xml │ │ ├── java │ │ │ └── net │ │ │ │ └── tensory │ │ │ │ └── rxjavatalk │ │ │ │ ├── models │ │ │ │ ├── House.java │ │ │ │ ├── HouseBattleResult.java │ │ │ │ └── Battle.java │ │ │ │ ├── injection │ │ │ │ ├── AppComponent.java │ │ │ │ └── AppModule.java │ │ │ │ ├── views │ │ │ │ ├── activityfeed │ │ │ │ │ ├── ActivityPresenterFactory.java │ │ │ │ │ ├── ActivityPresenter.java │ │ │ │ │ ├── ActivityFragment.java │ │ │ │ │ └── ActivityAdapter.java │ │ │ │ ├── house │ │ │ │ │ ├── HousePresenterFactory.java │ │ │ │ │ ├── HousePresenter.java │ │ │ │ │ └── HouseFragment.java │ │ │ │ └── DashboardActivity.java │ │ │ │ ├── RxGotApplication.java │ │ │ │ ├── providers │ │ │ │ ├── BattleProvider.java │ │ │ │ ├── DebtProvider.java │ │ │ │ └── CreditRatingProvider.java │ │ │ │ └── repositories │ │ │ │ ├── DebtFeed.java │ │ │ │ ├── DragonManager.java │ │ │ │ └── BattleFrontFeed.java │ │ └── AndroidManifest.xml │ ├── test │ │ └── java │ │ │ └── net │ │ │ └── tensory │ │ │ └── rxjavatalk │ │ │ └── ExampleUnitTest.java │ └── androidTest │ │ └── java │ │ └── net │ │ └── tensory │ │ └── rxjavatalk │ │ └── ExampleInstrumentedTest.java ├── proguard-rules.pro └── build.gradle ├── settings.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── gradle.properties ├── README.md ├── gradlew.bat └── gradlew /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensory/RxJavaGoT/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /app/src/main/res/drawable/iron_bank.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensory/RxJavaGoT/HEAD/app/src/main/res/drawable/iron_bank.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/night_king.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensory/RxJavaGoT/HEAD/app/src/main/res/drawable/night_king.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/house_targaryen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensory/RxJavaGoT/HEAD/app/src/main/res/drawable/house_targaryen.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensory/RxJavaGoT/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensory/RxJavaGoT/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensory/RxJavaGoT/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensory/RxJavaGoT/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensory/RxJavaGoT/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/house_stark_shield.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensory/RxJavaGoT/HEAD/app/src/main/res/drawable/house_stark_shield.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensory/RxJavaGoT/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensory/RxJavaGoT/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/house_lannister_shield.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensory/RxJavaGoT/HEAD/app/src/main/res/drawable/house_lannister_shield.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensory/RxJavaGoT/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensory/RxJavaGoT/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensory/RxJavaGoT/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /.idea/ 4 | /local.properties 5 | /.idea/workspace.xml 6 | /.idea/libraries 7 | .DS_Store 8 | /build 9 | /captures 10 | .externalNativeBuild 11 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Apr 04 21:42:55 PDT 2018 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-4.4-all.zip 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | Iron Bank Dashboard 3 | %s vs. %s 4 | %d 5 | 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/main/java/net/tensory/rxjavatalk/models/House.java: -------------------------------------------------------------------------------- 1 | package net.tensory.rxjavatalk.models; 2 | 3 | public enum House { 4 | STARK("Stark"), 5 | TARGARYEN("Targaryen"), 6 | LANNISTER("Lannister"), 7 | NIGHT_KING("Night King"); 8 | 9 | private String houseName; 10 | 11 | House(String houseName) { 12 | this.houseName = houseName; 13 | } 14 | 15 | public String getHouseName() { 16 | return houseName; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /app/src/test/java/net/tensory/rxjavatalk/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package net.tensory.rxjavatalk; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() throws Exception { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /app/src/main/res/layout/debt_feed_item.xml: -------------------------------------------------------------------------------- 1 | 6 | 7 | 14 | 15 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_thumb_up_black_24dp.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_thumb_down_black_24dp.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/java/net/tensory/rxjavatalk/injection/AppComponent.java: -------------------------------------------------------------------------------- 1 | package net.tensory.rxjavatalk.injection; 2 | 3 | import net.tensory.rxjavatalk.providers.BattleProvider; 4 | import net.tensory.rxjavatalk.providers.CreditRatingProvider; 5 | import net.tensory.rxjavatalk.providers.DebtProvider; 6 | import net.tensory.rxjavatalk.views.house.HouseFragment; 7 | 8 | import javax.inject.Singleton; 9 | 10 | import dagger.Component; 11 | 12 | @Singleton 13 | @Component(modules = {AppModule.class}) 14 | public interface AppComponent { 15 | 16 | void inject(HouseFragment fragment); 17 | 18 | BattleProvider providesBattles(); 19 | 20 | DebtProvider providesDebts(); 21 | 22 | CreditRatingProvider providesCreditRatings(); 23 | } 24 | -------------------------------------------------------------------------------- /app/src/main/java/net/tensory/rxjavatalk/views/activityfeed/ActivityPresenterFactory.java: -------------------------------------------------------------------------------- 1 | package net.tensory.rxjavatalk.views.activityfeed; 2 | 3 | import android.arch.lifecycle.ViewModelProvider; 4 | 5 | import net.tensory.rxjavatalk.injection.AppComponent; 6 | 7 | public class ActivityPresenterFactory implements ViewModelProvider.Factory { 8 | 9 | private final AppComponent appComponent; 10 | 11 | ActivityPresenterFactory(AppComponent appComponent) { 12 | this.appComponent = appComponent; 13 | } 14 | 15 | @Override 16 | public ActivityPresenter create(Class modelClass) { 17 | return new ActivityPresenter( 18 | appComponent.providesBattles(), appComponent.providesDebts()); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | org.gradle.jvmargs=-Xmx1536m 13 | 14 | # When configured, Gradle will run in incubating parallel mode. 15 | # This option should only be used with decoupled projects. More details, visit 16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 17 | # org.gradle.parallel=true 18 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 5 | 6 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /app/src/main/java/net/tensory/rxjavatalk/views/activityfeed/ActivityPresenter.java: -------------------------------------------------------------------------------- 1 | package net.tensory.rxjavatalk.views.activityfeed; 2 | 3 | import android.arch.lifecycle.ViewModel; 4 | 5 | import net.tensory.rxjavatalk.providers.BattleProvider; 6 | import net.tensory.rxjavatalk.providers.DebtProvider; 7 | 8 | import io.reactivex.Observable; 9 | 10 | class ActivityPresenter extends ViewModel { 11 | 12 | private final BattleProvider battleProvider; 13 | private final DebtProvider debtProvider; 14 | 15 | ActivityPresenter(BattleProvider battleProvider, DebtProvider debtProvider) { 16 | this.battleProvider = battleProvider; 17 | this.debtProvider = debtProvider; 18 | } 19 | 20 | Observable getEventFeed() { 21 | return Observable.merge(battleProvider.observeBattles(), debtProvider.observeDebt()); 22 | } 23 | 24 | } -------------------------------------------------------------------------------- /app/src/main/java/net/tensory/rxjavatalk/RxGotApplication.java: -------------------------------------------------------------------------------- 1 | package net.tensory.rxjavatalk; 2 | 3 | import android.app.Application; 4 | 5 | import net.tensory.rxjavatalk.injection.AppComponent; 6 | import net.tensory.rxjavatalk.injection.AppModule; 7 | import net.tensory.rxjavatalk.injection.DaggerAppComponent; 8 | 9 | public class RxGotApplication extends Application { 10 | 11 | private AppComponent appComponent; 12 | 13 | @Override 14 | public void onCreate() { 15 | super.onCreate(); 16 | appComponent = initDagger(this); 17 | } 18 | 19 | private AppComponent initDagger(RxGotApplication application) { 20 | return DaggerAppComponent.builder() 21 | .appModule(new AppModule(application)) 22 | .build(); 23 | } 24 | 25 | public AppComponent getAppComponent() { 26 | return appComponent; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /app/src/main/res/layout/battle_feed_fragment.xml: -------------------------------------------------------------------------------- 1 | 6 | 7 | 15 | 16 | 21 | -------------------------------------------------------------------------------- /app/src/androidTest/java/net/tensory/rxjavatalk/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package net.tensory.rxjavatalk; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("net.tensory.rxjavatalk", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/java/net/tensory/rxjavatalk/views/house/HousePresenterFactory.java: -------------------------------------------------------------------------------- 1 | package net.tensory.rxjavatalk.views.house; 2 | 3 | import android.arch.lifecycle.ViewModelProvider; 4 | 5 | import net.tensory.rxjavatalk.injection.AppComponent; 6 | import net.tensory.rxjavatalk.models.House; 7 | 8 | public class HousePresenterFactory implements ViewModelProvider.Factory { 9 | 10 | private final House house; 11 | private final AppComponent appComponent; 12 | 13 | HousePresenterFactory(House house, AppComponent appComponent) { 14 | this.house = house; 15 | this.appComponent = appComponent; 16 | } 17 | 18 | @Override 19 | public HousePresenter create(Class modelClass) { 20 | return new HousePresenter( 21 | house, 22 | appComponent.providesBattles(), 23 | appComponent.providesDebts(), 24 | appComponent.providesCreditRatings() 25 | ); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RxJavaGoT 2 | 3 | This our example app of how we built a live dashboard for the Iron Bank of Braavos. It contains examples of how to observe multiple streams and display them in varying ways through the use of RxJava functions. 4 | 5 | Some of the functions used are: 6 | - map 7 | - filter 8 | - dispose 9 | - merge 10 | - combineLatest 11 | - distinctUntilChanged 12 | 13 | The code is also built using Clean Architecture with Repositories, Providers, and View layers. Dependency injection is managed using Dagger. 14 | 15 | Our slides are limited, as we primarily live coded the talk. However, here they are if you're interested! 16 | https://docs.google.com/presentation/d/1PaPi1Tm7z5hoFKiX-W-AF1ICKMSYLUwtXgB72rrGeLs/edit?usp=sharing 17 | 18 | Other helpful links are: 19 | - http://rxmarbles.com/ 20 | - https://blog.mindorks.com/ 21 | - https://caster.io/ 22 | - https://github.com/aderington/RxExamples (Hopefully more to come, but this is an example of RetryWhen) 23 | -------------------------------------------------------------------------------- /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/ari/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/java/net/tensory/rxjavatalk/providers/BattleProvider.java: -------------------------------------------------------------------------------- 1 | package net.tensory.rxjavatalk.providers; 2 | 3 | import net.tensory.rxjavatalk.models.Battle; 4 | import net.tensory.rxjavatalk.repositories.BattleFrontFeed; 5 | import net.tensory.rxjavatalk.repositories.DragonManager; 6 | 7 | import io.reactivex.Observable; 8 | 9 | public class BattleProvider { 10 | 11 | private final BattleFrontFeed northernFrontFeed; 12 | private final BattleFrontFeed southernFrontFeed; 13 | 14 | public BattleProvider(DragonManager dragonManager) { 15 | northernFrontFeed = new BattleFrontFeed(BattleFrontFeed.Front.NORTHERN, dragonManager); 16 | southernFrontFeed = new BattleFrontFeed(BattleFrontFeed.Front.SOUTHERN, dragonManager); 17 | } 18 | 19 | /** 20 | * Observable emitting a stream of Battle events. 21 | * 22 | * @return hot Observable 23 | */ 24 | public Observable observeBattles() { 25 | return Observable.merge(northernFrontFeed.observeBattles(), southernFrontFeed.observeBattles()); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /app/src/main/java/net/tensory/rxjavatalk/models/HouseBattleResult.java: -------------------------------------------------------------------------------- 1 | package net.tensory.rxjavatalk.models; 2 | 3 | public class HouseBattleResult { 4 | 5 | private House house; 6 | 7 | private int currentArmySize; 8 | private int currentDragonCount; 9 | 10 | public HouseBattleResult(House house, int currentArmySize, int currentDragonCount) { 11 | this.house = house; 12 | this.currentArmySize = currentArmySize; 13 | this.currentDragonCount = currentDragonCount; 14 | } 15 | 16 | public House getHouse() { 17 | return house; 18 | } 19 | 20 | public int getCurrentArmySize() { 21 | return currentArmySize; 22 | } 23 | 24 | public int getCurrentDragonCount() { 25 | return currentDragonCount; 26 | } 27 | 28 | @Override 29 | public String toString() { 30 | return "HouseBattleResult{" + 31 | "house=" + house + 32 | ", currentArmySize=" + currentArmySize + 33 | ", currentDragonCount=" + currentDragonCount + 34 | '}'; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /app/src/main/java/net/tensory/rxjavatalk/models/Battle.java: -------------------------------------------------------------------------------- 1 | package net.tensory.rxjavatalk.models; 2 | 3 | import android.util.Pair; 4 | 5 | import net.tensory.rxjavatalk.repositories.BattleFrontFeed; 6 | 7 | public class Battle { 8 | 9 | private final BattleFrontFeed.Front front; 10 | private final HouseBattleResult winner; 11 | private final HouseBattleResult loser; 12 | 13 | public Battle(BattleFrontFeed.Front front, 14 | Pair battleResult) { 15 | this.front = front; 16 | this.winner = battleResult.first; 17 | this.loser = battleResult.second; 18 | } 19 | 20 | public BattleFrontFeed.Front getFront() { 21 | return front; 22 | } 23 | 24 | public HouseBattleResult getWinner() { 25 | return winner; 26 | } 27 | 28 | public HouseBattleResult getLoser() { 29 | return loser; 30 | } 31 | 32 | @Override 33 | public String toString() { 34 | return "Battle{" + 35 | "front=" + front + 36 | ", winner=" + winner + 37 | ", loser=" + loser + 38 | '}'; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /app/src/main/java/net/tensory/rxjavatalk/providers/DebtProvider.java: -------------------------------------------------------------------------------- 1 | package net.tensory.rxjavatalk.providers; 2 | 3 | import android.util.Pair; 4 | 5 | import net.tensory.rxjavatalk.models.House; 6 | import net.tensory.rxjavatalk.repositories.DebtFeed; 7 | 8 | import javax.inject.Inject; 9 | 10 | import io.reactivex.Observable; 11 | 12 | public class DebtProvider { 13 | 14 | private DebtFeed debtFeed; 15 | 16 | @Inject 17 | public DebtProvider(DebtFeed debtFeed) { 18 | this.debtFeed = debtFeed; 19 | } 20 | 21 | public Observable observeDebt(House house) { 22 | switch (house) { 23 | case LANNISTER: 24 | return debtFeed.observeLannisterDebt(); 25 | case STARK: 26 | return debtFeed.observeStarkDebt(); 27 | case TARGARYEN: 28 | return debtFeed.observeTargaryenDebt(); 29 | } 30 | return Observable.just(0.0); 31 | } 32 | 33 | public Observable> observeDebt() { 34 | return Observable.merge( 35 | observeDebt(House.LANNISTER) 36 | .map(debtValue -> new Pair<>(House.LANNISTER, debtValue)), 37 | observeDebt(House.STARK) 38 | .map(debtValue -> new Pair<>(House.STARK, debtValue)), 39 | observeDebt(House.TARGARYEN) 40 | .map(debtValue -> new Pair<>(House.TARGARYEN, debtValue))); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /app/src/main/java/net/tensory/rxjavatalk/injection/AppModule.java: -------------------------------------------------------------------------------- 1 | package net.tensory.rxjavatalk.injection; 2 | 3 | import android.app.Application; 4 | import android.content.Context; 5 | 6 | import net.tensory.rxjavatalk.providers.BattleProvider; 7 | import net.tensory.rxjavatalk.providers.CreditRatingProvider; 8 | import net.tensory.rxjavatalk.providers.DebtProvider; 9 | import net.tensory.rxjavatalk.repositories.DebtFeed; 10 | import net.tensory.rxjavatalk.repositories.DragonManager; 11 | 12 | import javax.inject.Singleton; 13 | 14 | import dagger.Module; 15 | import dagger.Provides; 16 | 17 | @Module 18 | public class AppModule { 19 | 20 | private Application application; 21 | 22 | public AppModule(Application application) { 23 | this.application = application; 24 | } 25 | 26 | @Provides 27 | @Singleton 28 | public Context provideContext() { 29 | return application; 30 | } 31 | 32 | 33 | @Provides 34 | @Singleton 35 | public DragonManager provideDragonManager() { 36 | return new DragonManager(); 37 | } 38 | 39 | @Provides 40 | @Singleton 41 | public DebtFeed provideDebtFeed() { 42 | return new DebtFeed(); 43 | } 44 | 45 | @Provides 46 | @Singleton 47 | public BattleProvider provideBattles(DragonManager dragonManager) { 48 | return new BattleProvider(dragonManager); 49 | } 50 | 51 | @Provides 52 | @Singleton 53 | public CreditRatingProvider provideCreditRatings(BattleProvider battleProvider, DebtProvider debtProvider) { 54 | return new CreditRatingProvider(battleProvider, debtProvider); 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /app/src/main/java/net/tensory/rxjavatalk/views/DashboardActivity.java: -------------------------------------------------------------------------------- 1 | package net.tensory.rxjavatalk.views; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.NonNull; 5 | import android.support.v4.app.Fragment; 6 | import android.support.v4.app.FragmentTransaction; 7 | import android.support.v7.app.AppCompatActivity; 8 | 9 | import net.tensory.rxjavatalk.R; 10 | import net.tensory.rxjavatalk.models.House; 11 | import net.tensory.rxjavatalk.views.activityfeed.ActivityFragment; 12 | import net.tensory.rxjavatalk.views.house.HouseFragment; 13 | 14 | public class DashboardActivity extends AppCompatActivity { 15 | 16 | @Override 17 | protected void onCreate(Bundle savedInstanceState) { 18 | super.onCreate(savedInstanceState); 19 | setContentView(R.layout.dashboard_activity); 20 | 21 | final FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); 22 | 23 | fragmentTransaction.replace(R.id.house_stark, createHouseFragment(House.STARK)); 24 | fragmentTransaction.replace(R.id.house_targaryen, createHouseFragment(House.TARGARYEN)); 25 | fragmentTransaction.replace(R.id.house_lannister, createHouseFragment(House.LANNISTER)); 26 | fragmentTransaction.replace(R.id.house_night_king, createHouseFragment(House.NIGHT_KING)); 27 | 28 | fragmentTransaction.replace(R.id.battle_feed_fragment, new ActivityFragment()); 29 | 30 | fragmentTransaction.commit(); 31 | } 32 | 33 | @NonNull 34 | private Fragment createHouseFragment(House house) { 35 | final Fragment fragment = new HouseFragment(); 36 | final Bundle args = new Bundle(); 37 | args.putSerializable(HouseFragment.ARG_HOUSE, house); 38 | fragment.setArguments(args); 39 | return fragment; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 27 5 | 6 | defaultConfig { 7 | applicationId "net.tensory.rxjavatalk" 8 | minSdkVersion 24 9 | targetSdkVersion 27 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | 21 | compileOptions { 22 | sourceCompatibility JavaVersion.VERSION_1_8 23 | targetCompatibility JavaVersion.VERSION_1_8 24 | } 25 | } 26 | 27 | dependencies { 28 | // RxJava 29 | implementation 'io.reactivex.rxjava2:rxjava:2.1.9' 30 | implementation 'io.reactivex.rxjava2:rxandroid:2.0.2' 31 | 32 | implementation 'com.android.support.constraint:constraint-layout:1.0.2' 33 | 34 | final def SUPPORT_LIBRARY_VERSION = '26.1.0' 35 | implementation 'com.android.support:appcompat-v7:' + SUPPORT_LIBRARY_VERSION 36 | implementation 'com.android.support:support-v4:' + SUPPORT_LIBRARY_VERSION 37 | implementation 'com.android.support:recyclerview-v7:' + SUPPORT_LIBRARY_VERSION 38 | 39 | final def ARCH_VERSION = "1.0.0-alpha9-1" 40 | implementation "android.arch.lifecycle:extensions:" + ARCH_VERSION 41 | implementation "android.arch.persistence.room:runtime:" + ARCH_VERSION 42 | implementation "android.arch.persistence.room:rxjava2:" + ARCH_VERSION 43 | 44 | implementation fileTree(dir: 'libs', include: ['*.jar']) 45 | 46 | final DAGGER_VERSION = "2.11" 47 | compile 'com.google.dagger:dagger:' + DAGGER_VERSION 48 | annotationProcessor 'com.google.dagger:dagger-compiler:' + DAGGER_VERSION 49 | 50 | androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', { 51 | exclude group: 'com.android.support', module: 'support-annotations' 52 | }) 53 | testImplementation 'junit:junit:4.12' 54 | } 55 | -------------------------------------------------------------------------------- /app/src/main/java/net/tensory/rxjavatalk/repositories/DebtFeed.java: -------------------------------------------------------------------------------- 1 | package net.tensory.rxjavatalk.repositories; 2 | 3 | import java.util.concurrent.ThreadLocalRandom; 4 | import java.util.concurrent.TimeUnit; 5 | 6 | import io.reactivex.BackpressureStrategy; 7 | import io.reactivex.Observable; 8 | import io.reactivex.subjects.BehaviorSubject; 9 | 10 | public class DebtFeed { 11 | 12 | private static final double ADJUSTMENT = 10000; 13 | private static final double INITIAL_DEBT_SEED = 100000; 14 | 15 | private BehaviorSubject lannisterDebt = BehaviorSubject.createDefault(ThreadLocalRandom.current().nextDouble(INITIAL_DEBT_SEED)); 16 | private BehaviorSubject starkDebt = BehaviorSubject.createDefault(ThreadLocalRandom.current().nextDouble(INITIAL_DEBT_SEED / 2)); 17 | private BehaviorSubject targaryenDebt = BehaviorSubject.createDefault(ThreadLocalRandom.current().nextDouble(INITIAL_DEBT_SEED / 3)); 18 | 19 | public DebtFeed() { 20 | setupSubjects(lannisterDebt, 21); 21 | setupSubjects(starkDebt, 29); 22 | setupSubjects(targaryenDebt, 37); 23 | 24 | // White Walkers don't know what money is, so there's no need to count their debts... 25 | } 26 | 27 | private void setupSubjects(BehaviorSubject subject, int intervalSeconds) { 28 | getTimedEmittedValue(intervalSeconds) 29 | .map(t -> subject.getValue() + t) 30 | .subscribe(subject::onNext); 31 | } 32 | 33 | private Observable getTimedEmittedValue(long intervalSeconds) { 34 | return Observable.interval(intervalSeconds, TimeUnit.SECONDS) 35 | .map(timeInterval -> ThreadLocalRandom.current().nextDouble(ADJUSTMENT)); 36 | } 37 | 38 | public Observable observeLannisterDebt() { 39 | return lannisterDebt.toFlowable(BackpressureStrategy.LATEST).toObservable(); 40 | } 41 | 42 | public Observable observeStarkDebt() { 43 | return starkDebt.toFlowable(BackpressureStrategy.LATEST).toObservable(); 44 | } 45 | 46 | public Observable observeTargaryenDebt() { 47 | return targaryenDebt.toFlowable(BackpressureStrategy.LATEST).toObservable(); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /app/src/main/java/net/tensory/rxjavatalk/providers/CreditRatingProvider.java: -------------------------------------------------------------------------------- 1 | package net.tensory.rxjavatalk.providers; 2 | 3 | import net.tensory.rxjavatalk.models.Battle; 4 | import net.tensory.rxjavatalk.models.House; 5 | import net.tensory.rxjavatalk.models.HouseBattleResult; 6 | 7 | import io.reactivex.Observable; 8 | 9 | public class CreditRatingProvider { 10 | 11 | private static final Double MAX_DEBT_GOLD = 100000.0; 12 | 13 | private BattleProvider battleProvider; 14 | private DebtProvider debtProvider; 15 | 16 | public CreditRatingProvider(BattleProvider battleProvider, DebtProvider debtProvider) { 17 | setupSubscription(battleProvider, debtProvider); 18 | } 19 | 20 | private void setupSubscription(BattleProvider battleProvider, DebtProvider debtProvider) { 21 | this.battleProvider = battleProvider; 22 | this.debtProvider = debtProvider; 23 | } 24 | 25 | public Observable observeCreditRating(House house) { 26 | return Observable.combineLatest( 27 | debtProvider.observeDebt(house), 28 | observeAssetRating(house, battleProvider), 29 | this::calculateCreditRating); 30 | } 31 | 32 | private Observable observeAssetRating(House house, BattleProvider battleProvider) { 33 | return battleProvider.observeBattles() 34 | .map(battle -> getHouseBattleResult(house, battle)) 35 | .map(this::calculateAssetRating); 36 | } 37 | 38 | private HouseBattleResult getHouseBattleResult(House house, Battle battle) { 39 | boolean isWinningHouse = house.equals(battle.getWinner().getHouse()); 40 | return isWinningHouse ? battle.getWinner() : battle.getLoser(); 41 | } 42 | 43 | private Double calculateAssetRating(HouseBattleResult houseAssets) { 44 | return houseAssets.getCurrentArmySize() * 0.35 + houseAssets.getCurrentDragonCount() * 10000; 45 | } 46 | 47 | private Double calculateCreditRating(double houseDebt, double assetRating) { 48 | double debtRatio = Math.abs((MAX_DEBT_GOLD - houseDebt) / MAX_DEBT_GOLD); 49 | 50 | if (houseDebt == 0.0) { 51 | debtRatio = 0.5; 52 | } 53 | 54 | final double liabilitiesRating = (debtRatio * assetRating) / MAX_DEBT_GOLD; 55 | 56 | return liabilitiesRating * 10; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /app/src/main/java/net/tensory/rxjavatalk/repositories/DragonManager.java: -------------------------------------------------------------------------------- 1 | package net.tensory.rxjavatalk.repositories; 2 | 3 | import net.tensory.rxjavatalk.models.House; 4 | 5 | import java.util.HashMap; 6 | import java.util.Map; 7 | import java.util.concurrent.ThreadLocalRandom; 8 | 9 | public class DragonManager { 10 | 11 | private final Object lock = new Object(); 12 | 13 | private final Map dragonCounts = new HashMap<>(); 14 | 15 | public DragonManager() { 16 | for (House house : House.values()) { 17 | dragonCounts.put(house, 0); 18 | } 19 | 20 | // Initialize the dragon count to 3 for Targaryen's 21 | dragonCounts.put(House.TARGARYEN, 3); 22 | } 23 | 24 | void considerAllegianceChange(House winningHouse, House losingHouse) { 25 | synchronized (lock) { 26 | final Integer currentWinningValue = dragonCounts.get(winningHouse); 27 | 28 | final Integer currentLosingValue = dragonCounts.get(losingHouse); 29 | final boolean losingHouseHasDragon = currentLosingValue > 0; 30 | 31 | if (losingHouseHasDragon) { 32 | if (losingHouse == House.TARGARYEN) { 33 | final double rollOfTheDice = ThreadLocalRandom.current().nextDouble(0.0, 1.0); 34 | 35 | // Targaryen's only lose a dragon in extreme odds 36 | if (rollOfTheDice > 0.8) { 37 | switch (winningHouse) { 38 | case STARK: 39 | case NIGHT_KING: 40 | dragonCounts.put(House.TARGARYEN, currentLosingValue - 1); 41 | 42 | // Only Starks and Night King can gain a dragon, because they're awesome like that 43 | dragonCounts.put(winningHouse, currentWinningValue + 1); 44 | break; 45 | case LANNISTER: 46 | // Lannister's killed a dragon, NOOOOOOOOO!!!!!! 47 | dragonCounts.put(House.TARGARYEN, currentLosingValue - 1); 48 | break; 49 | } 50 | } 51 | } else { 52 | dragonCounts.put(losingHouse, currentLosingValue - 1); 53 | dragonCounts.put(winningHouse, currentWinningValue + 1); 54 | } 55 | } 56 | } 57 | } 58 | 59 | int getDragonCount(House house) { 60 | return dragonCounts.get(house); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /app/src/main/res/layout/dashboard_activity.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 17 | 18 | 28 | 29 | 39 | 40 | 50 | 51 | 60 | 61 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /app/src/main/java/net/tensory/rxjavatalk/views/activityfeed/ActivityFragment.java: -------------------------------------------------------------------------------- 1 | package net.tensory.rxjavatalk.views.activityfeed; 2 | 3 | 4 | import android.arch.lifecycle.ViewModelProviders; 5 | import android.os.Bundle; 6 | import android.support.annotation.Nullable; 7 | import android.support.v4.app.Fragment; 8 | import android.support.v7.widget.LinearLayoutManager; 9 | import android.support.v7.widget.RecyclerView; 10 | import android.view.LayoutInflater; 11 | import android.view.View; 12 | import android.view.ViewGroup; 13 | 14 | import net.tensory.rxjavatalk.R; 15 | import net.tensory.rxjavatalk.RxGotApplication; 16 | import net.tensory.rxjavatalk.injection.AppComponent; 17 | 18 | import io.reactivex.android.schedulers.AndroidSchedulers; 19 | import io.reactivex.disposables.Disposable; 20 | 21 | public class ActivityFragment extends Fragment { 22 | 23 | private ActivityAdapter adapter; 24 | private ActivityPresenter presenter; 25 | private Disposable disposable; 26 | private RecyclerView recyclerView; 27 | 28 | @Override 29 | public void onCreate(@Nullable Bundle savedInstanceState) { 30 | super.onCreate(savedInstanceState); 31 | final AppComponent appComponent = ((RxGotApplication) getActivity().getApplication()).getAppComponent(); 32 | 33 | final ActivityPresenterFactory presenterFactory = 34 | new ActivityPresenterFactory(appComponent); 35 | presenter = ViewModelProviders.of(this, presenterFactory).get(ActivityPresenter.class); 36 | } 37 | 38 | @Override 39 | public View onCreateView(LayoutInflater inflater, ViewGroup container, 40 | Bundle savedInstanceState) { 41 | final View view = inflater.inflate(R.layout.battle_feed_fragment, container, false); 42 | recyclerView = view.findViewById(R.id.event_feed_recycler_view); 43 | 44 | // use this setting to improve performance if you know that changes 45 | // in content do not change the layout size of the RecyclerView 46 | recyclerView.setHasFixedSize(true); 47 | 48 | recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); 49 | 50 | adapter = new ActivityAdapter(); 51 | recyclerView.setAdapter(adapter); 52 | 53 | return view; 54 | } 55 | 56 | @Override 57 | public void onStart() { 58 | super.onStart(); 59 | 60 | disposable = presenter.getEventFeed() 61 | .observeOn(AndroidSchedulers.mainThread()) 62 | .subscribe(battle -> { 63 | adapter.update(battle); 64 | recyclerView.scrollToPosition(0); 65 | }); 66 | } 67 | 68 | @Override 69 | public void onStop() { 70 | disposable.dispose(); 71 | 72 | super.onStop(); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /app/src/main/java/net/tensory/rxjavatalk/views/house/HousePresenter.java: -------------------------------------------------------------------------------- 1 | package net.tensory.rxjavatalk.views.house; 2 | 3 | import android.arch.lifecycle.ViewModel; 4 | import android.content.Context; 5 | import android.graphics.drawable.Drawable; 6 | 7 | import net.tensory.rxjavatalk.R; 8 | import net.tensory.rxjavatalk.models.Battle; 9 | import net.tensory.rxjavatalk.models.House; 10 | import net.tensory.rxjavatalk.models.HouseBattleResult; 11 | import net.tensory.rxjavatalk.providers.BattleProvider; 12 | import net.tensory.rxjavatalk.providers.CreditRatingProvider; 13 | import net.tensory.rxjavatalk.providers.DebtProvider; 14 | 15 | import io.reactivex.Observable; 16 | 17 | class HousePresenter extends ViewModel { 18 | 19 | private final House house; 20 | private final BattleProvider battleProvider; 21 | private final DebtProvider debtProvider; 22 | private final CreditRatingProvider creditRatingProvider; 23 | 24 | HousePresenter(House house, 25 | BattleProvider battleProvider, 26 | DebtProvider debtProvider, 27 | CreditRatingProvider creditRatingProvider) { 28 | this.house = house; 29 | this.battleProvider = battleProvider; 30 | this.debtProvider = debtProvider; 31 | this.creditRatingProvider = creditRatingProvider; 32 | } 33 | 34 | Observable observeSoldiers() { 35 | return observeBattleResult() 36 | .map(HouseBattleResult::getCurrentArmySize) 37 | .distinctUntilChanged(); 38 | } 39 | 40 | Observable observeDragons() { 41 | return observeBattleResult() 42 | .map(HouseBattleResult::getCurrentDragonCount) 43 | .distinctUntilChanged(); 44 | } 45 | 46 | private Observable observeBattleResult() { 47 | return battleProvider.observeBattles() 48 | .filter(this::wereWeInTheBattle) 49 | .map(this::getHouseBattleResult); 50 | } 51 | 52 | private boolean wereWeInTheBattle(Battle battle) { 53 | return battle.getWinner().getHouse() == house || battle.getLoser().getHouse() == house; 54 | } 55 | 56 | private HouseBattleResult getHouseBattleResult(Battle battle) { 57 | boolean isWinningHouse = house.equals(battle.getWinner().getHouse()); 58 | return isWinningHouse ? battle.getWinner() : battle.getLoser(); 59 | } 60 | 61 | Observable observeRating() { 62 | return creditRatingProvider.observeCreditRating(house); 63 | } 64 | 65 | Observable observeDebt() { 66 | return debtProvider.observeDebt(house); 67 | } 68 | 69 | Drawable getShield(final Context context) { 70 | switch (house) { 71 | case STARK: 72 | return context.getResources().getDrawable(R.drawable.house_stark_shield, null); 73 | case TARGARYEN: 74 | return context.getResources().getDrawable(R.drawable.house_targaryen, null); 75 | case LANNISTER: 76 | return context.getResources().getDrawable(R.drawable.house_lannister_shield, null); 77 | case NIGHT_KING: 78 | return context.getResources().getDrawable(R.drawable.night_king, null); 79 | } 80 | 81 | return null; 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /app/src/main/res/layout/battle_feed_item.xml: -------------------------------------------------------------------------------- 1 | 8 | 9 | 19 | 20 | 29 | 30 | 39 | 40 | 50 | 51 | 60 | 61 | 70 | 71 | 80 | 81 | -------------------------------------------------------------------------------- /app/src/main/java/net/tensory/rxjavatalk/repositories/BattleFrontFeed.java: -------------------------------------------------------------------------------- 1 | package net.tensory.rxjavatalk.repositories; 2 | 3 | import android.support.annotation.NonNull; 4 | import android.util.Pair; 5 | 6 | import net.tensory.rxjavatalk.models.Battle; 7 | import net.tensory.rxjavatalk.models.House; 8 | import net.tensory.rxjavatalk.models.HouseBattleResult; 9 | 10 | import java.util.concurrent.ThreadLocalRandom; 11 | import java.util.concurrent.TimeUnit; 12 | 13 | import io.reactivex.Observable; 14 | import io.reactivex.subjects.BehaviorSubject; 15 | import io.reactivex.subjects.Subject; 16 | 17 | public class BattleFrontFeed { 18 | private static final int EMIT_RATE_SECONDS = 16; 19 | 20 | public enum Front { 21 | NORTHERN, 22 | SOUTHERN 23 | } 24 | 25 | private final DragonManager dragonManager; 26 | 27 | private Subject battleSubject = BehaviorSubject.create().toSerialized(); 28 | 29 | public BattleFrontFeed(Front front, DragonManager dragonManager) { 30 | this.dragonManager = dragonManager; 31 | 32 | if (front == Front.SOUTHERN) { 33 | Observable.just(new Battle(front, generateBattleResults())) 34 | .delay(EMIT_RATE_SECONDS / 2, TimeUnit.SECONDS) 35 | .subscribe(battleSubject::onNext); 36 | } else { 37 | battleSubject.onNext(new Battle(front, generateBattleResults())); 38 | } 39 | 40 | Observable observable = Observable.interval(EMIT_RATE_SECONDS, TimeUnit.SECONDS) 41 | .map(timeInterval -> new Battle(front, generateBattleResults())); 42 | 43 | if (front == Front.SOUTHERN) { 44 | observable = observable.delay(EMIT_RATE_SECONDS / 2, TimeUnit.SECONDS); 45 | } 46 | 47 | observable.subscribe(battleSubject::onNext); 48 | } 49 | 50 | /** 51 | * Observable emitting a stream of Battle events. 52 | * 53 | * @return hot Observable 54 | */ 55 | public Observable observeBattles() { 56 | return battleSubject.cast(Battle.class); 57 | } 58 | 59 | private Pair generateBattleResults() { 60 | final Pair combatants = getCombatants(); 61 | 62 | House winningHouse = House.values()[combatants.first]; 63 | House losingHouse = House.values()[combatants.second]; 64 | 65 | dragonManager.considerAllegianceChange(winningHouse, losingHouse); 66 | 67 | return new Pair<>( 68 | generateHouseBattleResult(winningHouse), 69 | generateHouseBattleResult(losingHouse)); 70 | } 71 | 72 | @NonNull 73 | private HouseBattleResult generateHouseBattleResult(House house) { 74 | return new HouseBattleResult( 75 | house, 76 | generateArmySize(house), 77 | dragonManager.getDragonCount(house)); 78 | } 79 | 80 | private int generateArmySize(House house) { 81 | int origin = 2000; 82 | if (house == House.NIGHT_KING) { 83 | origin = 10000; 84 | } 85 | return ThreadLocalRandom.current().nextInt(origin, 200000); 86 | } 87 | 88 | private Pair getCombatants() { 89 | final int first = ThreadLocalRandom.current().nextInt(0, House.values().length); 90 | int second; 91 | 92 | do { 93 | second = ThreadLocalRandom.current().nextInt(0, House.values().length); 94 | } while (second == first); 95 | 96 | return new Pair<>(first, second); 97 | } 98 | 99 | } 100 | -------------------------------------------------------------------------------- /app/src/main/java/net/tensory/rxjavatalk/views/activityfeed/ActivityAdapter.java: -------------------------------------------------------------------------------- 1 | package net.tensory.rxjavatalk.views.activityfeed; 2 | 3 | import android.support.v7.widget.RecyclerView; 4 | import android.util.Pair; 5 | import android.view.LayoutInflater; 6 | import android.view.View; 7 | import android.view.ViewGroup; 8 | import android.widget.TextView; 9 | 10 | import junit.framework.Assert; 11 | 12 | import net.tensory.rxjavatalk.R; 13 | import net.tensory.rxjavatalk.models.Battle; 14 | import net.tensory.rxjavatalk.models.House; 15 | import net.tensory.rxjavatalk.models.HouseBattleResult; 16 | 17 | import java.text.NumberFormat; 18 | import java.util.ArrayList; 19 | import java.util.List; 20 | import java.util.Locale; 21 | 22 | class ActivityAdapter extends RecyclerView.Adapter { 23 | 24 | private final List items = new ArrayList<>(); 25 | 26 | void update(Object item) { 27 | Assert.assertTrue(item instanceof Battle || item instanceof Pair); 28 | 29 | items.add(0, item); 30 | notifyItemInserted(0); 31 | } 32 | 33 | @Override 34 | public int getItemViewType(int position) { 35 | final Object item = items.get(position); 36 | return item instanceof Battle ? 0 : 1; 37 | } 38 | 39 | @Override 40 | public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 41 | if (viewType == 0) { 42 | final View view = LayoutInflater.from(parent.getContext()) 43 | .inflate(R.layout.battle_feed_item, parent, false); 44 | return new BattleViewHolder(view); 45 | } else { 46 | final View view = LayoutInflater.from(parent.getContext()) 47 | .inflate(R.layout.debt_feed_item, parent, false); 48 | return new DebtViewHolder(view); 49 | } 50 | } 51 | 52 | @Override 53 | public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 54 | if (getItemViewType(position) == 0) { 55 | ((BattleViewHolder) holder).bind((Battle) items.get(position)); 56 | } else { 57 | ((DebtViewHolder) holder).bind((Pair) items.get(position)); 58 | } 59 | } 60 | 61 | @Override 62 | public int getItemCount() { 63 | return items.size(); 64 | } 65 | 66 | private static class BattleViewHolder extends RecyclerView.ViewHolder { 67 | 68 | private TextView frontView; 69 | private TextView winnerView; 70 | private TextView winnerDescriptionView; 71 | private TextView loserView; 72 | private TextView loserDescriptionView; 73 | 74 | private BattleViewHolder(View view) { 75 | super(view); 76 | frontView = view.findViewById(R.id.battle_front); 77 | winnerView = view.findViewById(R.id.battle_winner); 78 | winnerDescriptionView = view.findViewById(R.id.battle_winner_description); 79 | loserView = view.findViewById(R.id.battle_loser); 80 | loserDescriptionView = view.findViewById(R.id.battle_loser_description); 81 | } 82 | 83 | private void bind(Battle battle) { 84 | frontView.setText(battle.getFront().name()); 85 | winnerView.setText(battle.getWinner().getHouse().getHouseName()); 86 | winnerDescriptionView.setText(getResultsDescription(battle.getWinner())); 87 | loserView.setText(battle.getLoser().getHouse().getHouseName()); 88 | loserDescriptionView.setText(getResultsDescription(battle.getLoser())); 89 | } 90 | 91 | private String getResultsDescription(HouseBattleResult result) { 92 | return "Army: " + result.getCurrentArmySize() 93 | + " Dragons: " + result.getCurrentDragonCount(); 94 | } 95 | } 96 | 97 | private static class DebtViewHolder extends RecyclerView.ViewHolder { 98 | 99 | private NumberFormat formatter = NumberFormat.getCurrencyInstance(Locale.US); 100 | 101 | private TextView debtView; 102 | 103 | private DebtViewHolder(View view) { 104 | super(view); 105 | debtView = view.findViewById(R.id.debt_event); 106 | formatter.setMaximumFractionDigits(0); 107 | formatter.setGroupingUsed(true); 108 | } 109 | 110 | private void bind(Pair debtEvent) { 111 | debtView.setText(getResultsDescription(debtEvent)); 112 | } 113 | 114 | private String getResultsDescription(Pair result) { 115 | return result.first.getHouseName() 116 | + " current debt " + formatter.format(result.second); 117 | } 118 | } 119 | } 120 | 121 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /app/src/main/res/layout/house_fragment.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 15 | 16 | 28 | 29 | 39 | 40 | 49 | 50 | 61 | 62 | 70 | 71 | 81 | 82 | 90 | 91 | 101 | 102 | 110 | 111 | 121 | 122 | 123 | 124 | 130 | 131 | 137 | 138 | 144 | 145 | 146 | -------------------------------------------------------------------------------- /app/src/main/java/net/tensory/rxjavatalk/views/house/HouseFragment.java: -------------------------------------------------------------------------------- 1 | package net.tensory.rxjavatalk.views.house; 2 | 3 | import android.animation.ArgbEvaluator; 4 | import android.animation.ObjectAnimator; 5 | import android.arch.lifecycle.ViewModelProviders; 6 | import android.graphics.Color; 7 | import android.os.Bundle; 8 | import android.support.annotation.Nullable; 9 | import android.support.v4.app.Fragment; 10 | import android.view.LayoutInflater; 11 | import android.view.View; 12 | import android.view.ViewGroup; 13 | import android.view.animation.AccelerateInterpolator; 14 | import android.widget.ImageView; 15 | import android.widget.TextView; 16 | 17 | import junit.framework.Assert; 18 | 19 | import net.tensory.rxjavatalk.R; 20 | import net.tensory.rxjavatalk.RxGotApplication; 21 | import net.tensory.rxjavatalk.injection.AppComponent; 22 | import net.tensory.rxjavatalk.models.House; 23 | 24 | import java.text.NumberFormat; 25 | import java.util.Locale; 26 | 27 | import io.reactivex.android.schedulers.AndroidSchedulers; 28 | import io.reactivex.disposables.CompositeDisposable; 29 | 30 | public class HouseFragment extends Fragment { 31 | 32 | public static final String ARG_HOUSE = "ARG_HOUSE"; 33 | private static final long ANIMATION_DURATION = 8000L; 34 | 35 | private HousePresenter presenter; 36 | 37 | private CompositeDisposable compositeDisposable; 38 | 39 | private ImageView shieldView; 40 | private TextView nameView; 41 | private TextView ratingView; 42 | private TextView debtView; 43 | private TextView soldiersView; 44 | private TextView dragonsView; 45 | 46 | private House house; 47 | 48 | private NumberFormat doubleFormat = NumberFormat.getNumberInstance(); 49 | private NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(Locale.US); 50 | 51 | @Override 52 | public void onCreate(@Nullable Bundle savedInstanceState) { 53 | super.onCreate(savedInstanceState); 54 | final AppComponent appComponent = ((RxGotApplication) getActivity().getApplication()).getAppComponent(); 55 | appComponent.inject(this); 56 | 57 | Assert.assertNotNull(getArguments()); 58 | 59 | house = (House) getArguments().getSerializable(ARG_HOUSE); 60 | Assert.assertNotNull(house); 61 | 62 | final HousePresenterFactory presenterFactory = 63 | new HousePresenterFactory(house, appComponent); 64 | presenter = ViewModelProviders.of(this, presenterFactory).get(HousePresenter.class); 65 | 66 | doubleFormat.setMaximumFractionDigits(2); 67 | doubleFormat.setMinimumFractionDigits(2); 68 | 69 | currencyFormat.setMaximumFractionDigits(0); 70 | } 71 | 72 | @Override 73 | public View onCreateView(LayoutInflater inflater, ViewGroup container, 74 | Bundle savedInstanceState) { 75 | final View view = inflater.inflate(R.layout.house_fragment, container, false); 76 | 77 | shieldView = view.findViewById(R.id.house_shield); 78 | nameView = view.findViewById(R.id.house_name); 79 | ratingView = view.findViewById(R.id.house_rating); 80 | debtView = view.findViewById(R.id.house_debt); 81 | soldiersView = view.findViewById(R.id.house_soldiers); 82 | dragonsView = view.findViewById(R.id.house_dragons); 83 | 84 | return view; 85 | } 86 | 87 | @Override 88 | public void onStart() { 89 | super.onStart(); 90 | 91 | shieldView.setImageDrawable(presenter.getShield(getContext())); 92 | 93 | nameView.setText(house.getHouseName()); 94 | 95 | compositeDisposable = new CompositeDisposable(); 96 | 97 | compositeDisposable.add(presenter.observeRating() 98 | .map(rating -> doubleFormat.format(rating)) 99 | .observeOn(AndroidSchedulers.mainThread()) 100 | .subscribe(displayText -> ratingView.setText(displayText))); 101 | 102 | compositeDisposable.add(presenter.observeDebt() 103 | .map(debt -> { 104 | if (debt > 0) { 105 | return currencyFormat.format(debt); 106 | } else { 107 | return getString(R.string.en_dash); 108 | } 109 | }) 110 | .observeOn(AndroidSchedulers.mainThread()) 111 | .subscribe(displayText -> { 112 | debtView.setText(displayText); 113 | animateTextChange(debtView); 114 | })); 115 | 116 | compositeDisposable.add(presenter.observeSoldiers() 117 | .map(String::valueOf) 118 | .observeOn(AndroidSchedulers.mainThread()) 119 | .subscribe(displayText -> { 120 | soldiersView.setText(displayText); 121 | animateTextChange(soldiersView); 122 | })); 123 | 124 | compositeDisposable.add(presenter.observeDragons() 125 | .map(String::valueOf) 126 | .observeOn(AndroidSchedulers.mainThread()) 127 | .subscribe(displayText -> { 128 | dragonsView.setText(displayText); 129 | animateTextChange(dragonsView); 130 | })); 131 | } 132 | 133 | private void animateTextChange(TextView textView) { 134 | final int defaultTextColor = getResources().getColor(android.R.color.primary_text_light, null); 135 | 136 | final ObjectAnimator animator = ObjectAnimator.ofInt(textView, "textColor", Color.RED, defaultTextColor); 137 | animator.setDuration(ANIMATION_DURATION); 138 | animator.setEvaluator(new ArgbEvaluator()); 139 | animator.setInterpolator(new AccelerateInterpolator()); 140 | animator.start(); 141 | } 142 | 143 | @Override 144 | public void onStop() { 145 | compositeDisposable.dispose(); 146 | 147 | super.onStop(); 148 | } 149 | } 150 | --------------------------------------------------------------------------------