Skip to content

Commit e83b02e

Browse files
author
Kaushik Gopal
committed
fix: update rotation persist changes
1 parent 86c8340 commit e83b02e

5 files changed

Lines changed: 127 additions & 3 deletions

File tree

app/build.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ dependencies {
3636
compile "org.jetbrains.kotlin:kotlin-stdlib:${kotlinVersion}"
3737
compile "com.nhaarman:mockito-kotlin:${mockitoKotlinVersion}"
3838

39+
compile "android.arch.lifecycle:runtime:${archComponentsVersion}"
40+
compile "android.arch.lifecycle:extensions:${archComponentsVersion}"
41+
kapt "android.arch.lifecycle:compiler:${archComponentsVersion}"
42+
3943
// ----------------------------------
4044
// Rx dependencies
4145

@@ -48,6 +52,7 @@ dependencies {
4852
compile 'com.jakewharton.rx:replaying-share-kotlin:2.0.0'
4953
compile "com.github.akarnokd:rxjava2-extensions:0.16.0"
5054
compile 'com.jakewharton.rxrelay2:rxrelay:2.0.0'
55+
5156
compile 'com.jakewharton.rxbinding2:rxbinding:2.0.0'
5257
compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'
5358

app/src/main/java/com/morihacky/android/rxjava/fragments/MainFragment.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,9 @@ void demoExponentialBackoff() {
9696

9797
@OnClick(R.id.btn_demo_rotation_persist)
9898
void demoRotationPersist() {
99-
clickedOn(new RotationPersist2Fragment());
100-
//clickedOn(new RotationPersist1Fragment());
99+
clickedOn(new RotationPersist3Fragment());
100+
// clickedOn(new RotationPersist2Fragment());
101+
// clickedOn(new RotationPersist1Fragment());
101102
}
102103

103104
@OnClick(R.id.btn_demo_pagination)
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package com.morihacky.android.rxjava.fragments
2+
3+
import android.arch.lifecycle.ViewModel
4+
import android.arch.lifecycle.ViewModelProviders
5+
import android.os.Bundle
6+
import android.os.Handler
7+
import android.os.Looper.getMainLooper
8+
import android.view.LayoutInflater
9+
import android.view.View
10+
import android.view.ViewGroup
11+
import android.widget.ListView
12+
import butterknife.BindView
13+
import butterknife.ButterKnife
14+
import butterknife.OnClick
15+
import com.morihacky.android.rxjava.MyApp
16+
import com.morihacky.android.rxjava.R
17+
import com.morihacky.android.rxjava.ext.plus
18+
import com.morihacky.android.rxjava.wiring.LogAdapter
19+
import io.reactivex.Flowable
20+
import io.reactivex.disposables.CompositeDisposable
21+
import io.reactivex.disposables.Disposable
22+
import timber.log.Timber
23+
import java.util.concurrent.TimeUnit
24+
25+
class RotationPersist3Fragment : BaseFragment() {
26+
27+
@BindView(R.id.list_threading_log)
28+
lateinit var logList: ListView
29+
lateinit var adapter: LogAdapter
30+
lateinit var sharedViewModel: SharedViewModel
31+
32+
private var logs: MutableList<String> = ArrayList()
33+
private var disposables = CompositeDisposable()
34+
35+
// -----------------------------------------------------------------------------------
36+
37+
override fun onCreate(savedInstanceState: Bundle?) {
38+
super.onCreate(savedInstanceState)
39+
sharedViewModel = ViewModelProviders.of(activity).get(SharedViewModel::class.java)
40+
}
41+
42+
override fun onCreateView(
43+
inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
44+
val layout = inflater!!.inflate(R.layout.fragment_rotation_persist, container, false)
45+
ButterKnife.bind(this, layout)
46+
return layout
47+
}
48+
49+
@OnClick(R.id.btn_rotate_persist)
50+
fun startOperationFromWorkerFrag() {
51+
logs = ArrayList<String>()
52+
adapter.clear()
53+
54+
disposables +=
55+
sharedViewModel
56+
.sourceStream()
57+
.subscribe({ l ->
58+
_log("Received element $l")
59+
})
60+
}
61+
62+
// -----------------------------------------------------------------------------------
63+
// Boilerplate
64+
// -----------------------------------------------------------------------------------
65+
66+
override fun onActivityCreated(savedInstanceState: Bundle?) {
67+
super.onActivityCreated(savedInstanceState)
68+
_setupLogger()
69+
}
70+
71+
override fun onPause() {
72+
super.onPause()
73+
disposables.clear()
74+
}
75+
76+
private fun _setupLogger() {
77+
logs = ArrayList<String>()
78+
adapter = LogAdapter(activity, ArrayList<String>())
79+
logList.adapter = adapter
80+
}
81+
82+
private fun _log(logMsg: String) {
83+
logs.add(0, logMsg)
84+
85+
// You can only do below stuff on main thread.
86+
Handler(getMainLooper())
87+
.post {
88+
adapter.clear()
89+
adapter.addAll(logs)
90+
}
91+
}
92+
}
93+
94+
class SharedViewModel : ViewModel() {
95+
var disposable: Disposable? = null
96+
97+
var sharedObservable: Flowable<Long> =
98+
Flowable.interval(1, TimeUnit.SECONDS)
99+
.take(20)
100+
.doOnNext { l -> Timber.tag("KG").d("onNext $l") }
101+
// .replayingShare()
102+
.replay(1)
103+
.autoConnect(1) { t -> disposable = t }
104+
105+
fun sourceStream(): Flowable<Long> {
106+
return sharedObservable
107+
}
108+
109+
override fun onCleared() {
110+
super.onCleared()
111+
Timber.tag("KG").d("Clearing ViewModel")
112+
disposable?.dispose()
113+
MyApp.getRefWatcher().watch(this)
114+
}
115+
}
116+

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
<string name="msg_demo_rxbus_1">Tap on the below button and RxBus will listen to the events</string>
4141
<string name="msg_demo_form_comb_latest">Monitor the state of multiple observables with the combineLatest operator. Only after all the 3 inputs contain valid entries will the submit button light up</string>
4242
<string name="msg_demo_timing">BTN 1: run single task once (after 2s complete)\nBTN 2: run task every 1s (start delay of 1s) toggle \nBTN 3: run task every 1s (start immediately) toggle \nBTN 4: run task 5 times every 3s (then complete) \nBTN 5: run task A, pause for sometime, then proceed with Task B</string>
43-
<string name="msg_demo_rotation_persist">This is an example of starting an Observable and using the result across rotations. There are many ways to do this, we use a retained fragment in this example</string>
43+
<string name="msg_demo_rotation_persist">This is an example of starting an Observable and using the result across rotations. There are many ways to do this, we use ViewModels from architecture components</string>
4444
<string name="msg_demo_network_detector">This is a demo of how to use Subjects to detect Network connectivity\nToggle your Wifi/Network on or off and notice the logs</string>
4545
<string name="msg_demo_using">This is a demo of the somewhat unknown operator "using".\n\nYou typically use it for managing setup/teardown of resources. Classic cases are DB connections (like Realm), sockets, locks etc.\n\nTap the button and look at the logcat. Particularly notice how the Realm instance is self-contained. That is, it is auto-disposed right after use.</string>
4646
<string name="msg_demo_multicast_publishRefCount">RefCount starts the upstream right away and gets disposed off, when all subscribers stop. Hit S1, Hit S2, Hit S1, Hit S2. Hit S1/S2 now and notice the stream starts all over.</string>

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ buildscript {
1717
allprojects {
1818
repositories {
1919
jcenter()
20+
maven { url 'https://maven.google.com' }
2021
maven {
2122
url "https://jitpack.io"
2223
}
@@ -28,6 +29,7 @@ allprojects {
2829

2930
kotlinVersion = "1.1.3-2"
3031

32+
archComponentsVersion = "1.0.0-alpha3"
3133
butterKnifeVersion = '8.5.1'
3234
mockitoKotlinVersion = "1.4.0"
3335
okhttpVersion = "3.0.1"

0 commit comments

Comments
 (0)