Skip to content

Commit 0ef587b

Browse files
author
AnkitDroidGit
committed
Added Retry operator
1 parent d952025 commit 0ef587b

File tree

6 files changed

+101
-3
lines changed

6 files changed

+101
-3
lines changed

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ Adding more operator examples
103103

104104

105105

106-
107106
# RxBinding
108107

109108
Rx is powerful because we can compose transformations. What that means is that we can have reusable, safe and more functional code that simply plugs into your code.
@@ -122,7 +121,7 @@ Consider the following rules that we want for our email addresses
122121
* [RxLoginScreenActivity](https://github.com/AnkitDroidGit/RxKotlinOperators-Android/blob/master/RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/RxBinding/RxLoginScreenActivity.kt) - Login Screen using RxBinding
123122

124123

125-
# Pagination And Lazyloading using RxKotlin
124+
# Pagination And Lazy loading using RxKotlin
126125

127126

128127
## Highlights :
@@ -131,7 +130,6 @@ Consider the following rules that we want for our email addresses
131130

132131

133132

134-
135133
### Contact - Let's connect to learn together
136134
- [Twitter](https://twitter.com/KumarAnkitRKE)
137135
- [Github](https://github.com/AnkitDroidGit)

RxOperators/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
<activity android:name="com.freeankit.rxkotlinoperators.ui.RxOperators.combiningOperators.CombineLatestOperatorActivity" />
5555
<activity android:name=".ui.RxPagination.PaginationActivity" />
5656
<activity android:name=".ui.RxBinding.RxLoginScreenActivity" />
57+
<activity android:name=".ui.RxOperators.errorHandlingOperators.RetryOperatorActivity" />
5758
</application>
5859

5960
</manifest>

RxOperators/src/main/java/com/freeankit/rxkotlinoperators/ui/OperatorsActivity.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import com.freeankit.rxkotlinoperators.ui.RxOperators.connectableOperators.Repla
1313
import com.freeankit.rxkotlinoperators.ui.RxOperators.connectableOperators.ReplaySubjectOperatorActivity
1414
import com.freeankit.rxkotlinoperators.ui.RxOperators.creatingOperators.DeferOperatorActivity
1515
import com.freeankit.rxkotlinoperators.ui.RxOperators.creatingOperators.IntervalOperatorActivity
16+
import com.freeankit.rxkotlinoperators.ui.RxOperators.errorHandlingOperators.RetryOperatorActivity
1617
import com.freeankit.rxkotlinoperators.ui.RxOperators.filteringOperators.*
1718
import com.freeankit.rxkotlinoperators.ui.RxOperators.mathematicalOperators.ConcatOperatorActivity
1819
import com.freeankit.rxkotlinoperators.ui.RxOperators.mathematicalOperators.ReduceOperatorActivity
@@ -147,4 +148,8 @@ class OperatorsActivity : AppCompatActivity() {
147148
fun startDelayActivity(view: View) {
148149
startActivity(Intent(this@OperatorsActivity, DelayOperatorActivity::class.java))
149150
}
151+
152+
fun startRetryActivity(view: View) {
153+
startActivity(Intent(this@OperatorsActivity, RetryOperatorActivity::class.java))
154+
}
150155
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.freeankit.rxkotlinoperators.ui.RxOperators.errorHandlingOperators
2+
3+
import android.os.Bundle
4+
import android.support.v7.app.AppCompatActivity
5+
import android.util.Log
6+
import com.freeankit.rxkotlinoperators.R
7+
import com.freeankit.rxkotlinoperators.utils.Constant
8+
import io.reactivex.Observable
9+
import io.reactivex.Observer
10+
import io.reactivex.android.schedulers.AndroidSchedulers
11+
import io.reactivex.disposables.Disposable
12+
import io.reactivex.schedulers.Schedulers
13+
import kotlinx.android.synthetic.main.activity_example_operator.*
14+
import java.util.concurrent.TimeUnit
15+
16+
/**
17+
* @author Ankit Kumar ([email protected]) on 22/04/2018 (MM/DD/YYYY)
18+
*/
19+
class RetryOperatorActivity : AppCompatActivity() {
20+
override fun onCreate(savedInstanceState: Bundle?) {
21+
super.onCreate(savedInstanceState)
22+
setContentView(R.layout.activity_example_operator)
23+
24+
btn.setOnClickListener({ executeRetryOperator() })
25+
}
26+
27+
28+
// if a source Observable emits an error, resubscribe to it in the hopes that it will complete without error
29+
30+
private fun executeRetryOperator() {
31+
getObservable()
32+
.retry()
33+
// .retryWhen(o -> o.delay(100, TimeUnit.MILLISECONDS))
34+
.debounce(500, TimeUnit.MILLISECONDS)
35+
// Run on a background thread
36+
.subscribeOn(Schedulers.io())
37+
// Be notified on the main thread
38+
.observeOn(AndroidSchedulers.mainThread())
39+
.subscribe(getObserver())
40+
}
41+
42+
private fun getObservable(): Observable<Int> {
43+
return Observable.create { emitter ->
44+
// send events with simulated time wait
45+
emitter.onNext(1) // skip
46+
Thread.sleep(400)
47+
emitter.onNext(2) // deliver
48+
Thread.sleep(505)
49+
emitter.onNext(3) // skip
50+
Thread.sleep(100)
51+
emitter.onNext(4) // deliver kj h ihioh
52+
Thread.sleep(605) ///////////////
53+
emitter.onNext(5) // deliver
54+
Thread.sleep(510)
55+
emitter.onComplete()
56+
}
57+
}
58+
59+
60+
private fun getObserver(): Observer<Int> {
61+
return object : Observer<Int> {
62+
override fun onSubscribe(d: Disposable) {
63+
Log.d(Constant().TAG, " onSubscribe : " + d.isDisposed)
64+
}
65+
66+
override fun onNext(value: Int) {
67+
textView.append(" onNext : value : $value")
68+
textView.append(Constant().LINE_SEPARATOR)
69+
Log.d(Constant().TAG, " onNext value : $value")
70+
}
71+
72+
override fun onError(e: Throwable) {
73+
textView.append(" onError : " + e.message)
74+
textView.append(Constant().LINE_SEPARATOR)
75+
Log.d(Constant().TAG, " onError : " + e.message)
76+
}
77+
78+
override fun onComplete() {
79+
textView.append(" onComplete")
80+
textView.append(Constant().LINE_SEPARATOR)
81+
Log.d(Constant().TAG, " onComplete")
82+
}
83+
}
84+
}
85+
86+
}

RxOperators/src/main/res/layout/activity_operators.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,5 +223,12 @@
223223
android:onClick="startDelayActivity"
224224
android:text="@string/delay"
225225
android:textColor="@android:color/black" />
226+
227+
<Button
228+
android:layout_width="match_parent"
229+
android:layout_height="wrap_content"
230+
android:onClick="startRetryActivity"
231+
android:text="@string/retry"
232+
android:textColor="@android:color/black" />
226233
</LinearLayout>
227234
</ScrollView>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@
3333
<string name="delay">Delay</string>
3434
<string name="rxpagination">RxPagination</string>
3535
<string name="rxbinding">RxBinding</string>
36+
<string name="retry">Retry</string>
3637
</resources>

0 commit comments

Comments
 (0)