1
+ package com.mindorks.rxjava3.android.examples.ui.operators
2
+
3
+ import android.os.Bundle
4
+ import android.util.Log
5
+ import android.widget.Button
6
+ import android.widget.TextView
7
+ import androidx.appcompat.app.AppCompatActivity
8
+ import com.mindorks.rxjava3.android.examples.R
9
+ import com.mindorks.rxjava3.android.examples.utils.AppConstant
10
+ import io.reactivex.rxjava3.core.Observable
11
+ import io.reactivex.rxjava3.core.Observer
12
+ import io.reactivex.rxjava3.disposables.Disposable
13
+
14
+ class ConcatExampleActivity : AppCompatActivity () {
15
+
16
+ companion object {
17
+ private const val TAG = " ConcatExampleActivity"
18
+ }
19
+
20
+ private lateinit var btn: Button
21
+ private lateinit var textView: TextView
22
+
23
+ override fun onCreate (savedInstanceState : Bundle ? ) {
24
+ super .onCreate(savedInstanceState)
25
+ setContentView(R .layout.activity_example)
26
+ btn = findViewById(R .id.btn)
27
+ textView = findViewById(R .id.textView)
28
+ btn.setOnClickListener {
29
+ doSomeWork()
30
+ }
31
+ }
32
+
33
+ /*
34
+ * Using concat operator to combine Observable : concat maintain
35
+ * the order of Observable.
36
+ * It will emit all the 7 values in order
37
+ * here - first "A1", "A2", "A3", "A4" and then "B1", "B2", "B3"
38
+ * first all from the first Observable and then
39
+ * all from the second Observable all in order
40
+ */
41
+ private fun doSomeWork () {
42
+ val observableA = Observable .fromArray(" A1" , " A2" , " A3" , " A4" )
43
+ val observableB = Observable .fromArray(" B1" , " B2" , " B3" , " B4" )
44
+
45
+ Observable .concat(observableA, observableB)
46
+ .subscribe(getObserver())
47
+ }
48
+
49
+ private fun getObserver (): Observer <String > {
50
+ return object : Observer <String > {
51
+
52
+ override fun onSubscribe (d : Disposable ) {
53
+ Log .d(TAG , " onSubscribe : " + d.isDisposed)
54
+ }
55
+
56
+ override fun onNext (value : String ) {
57
+ textView.append(" onNext : value : $value " )
58
+ textView.append(AppConstant .LINE_SEPARATOR )
59
+ Log .d(TAG , " onNext : value : $value " )
60
+ }
61
+
62
+ override fun onError (e : Throwable ) {
63
+ textView.append(" onError : " + e.message)
64
+ textView.append(AppConstant .LINE_SEPARATOR )
65
+ Log .d(TAG , " onError : " + e.message)
66
+ }
67
+
68
+ override fun onComplete () {
69
+ textView.append(" onComplete" )
70
+ textView.append(AppConstant .LINE_SEPARATOR )
71
+ Log .d(TAG , " onComplete" )
72
+ }
73
+ }
74
+ }
75
+
76
+ }
0 commit comments