Skip to content

Commit 2a2d2d6

Browse files
Merge pull request #44 from mohammed-rampurawala/extended_version
TakeUntil Operator added.
2 parents 6bb6146 + 83e169f commit 2a2d2d6

File tree

7 files changed

+134
-60
lines changed

7 files changed

+134
-60
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@
131131

132132
<activity android:name=".ui.operators.TakeWhileExampleActivity"
133133
android:label="@string/take_while"/>
134+
135+
<activity android:name=".ui.operators.TakeUntilExampleActivity"
136+
android:label="@string/take_until"/>
134137
</application>
135138

136139
</manifest>

app/src/main/java/com/rxjava2/android/samples/ui/OperatorsActivity.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.rxjava2.android.samples.ui.operators.SkipExampleActivity;
3232
import com.rxjava2.android.samples.ui.operators.SwitchMapExampleActivity;
3333
import com.rxjava2.android.samples.ui.operators.TakeExampleActivity;
34+
import com.rxjava2.android.samples.ui.operators.TakeUntilExampleActivity;
3435
import com.rxjava2.android.samples.ui.operators.TakeWhileExampleActivity;
3536
import com.rxjava2.android.samples.ui.operators.ThrottleFirstExampleActivity;
3637
import com.rxjava2.android.samples.ui.operators.ThrottleLastExampleActivity;
@@ -175,4 +176,8 @@ public void startSwitchMapActivity(View view) {
175176
public void startTakeWhileActivity(View view) {
176177
startActivity(new Intent(OperatorsActivity.this, TakeWhileExampleActivity.class));
177178
}
179+
180+
public void startTakeUntilActivity(View view) {
181+
startActivity(new Intent(OperatorsActivity.this, TakeUntilExampleActivity.class));
182+
}
178183
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.rxjava2.android.samples.ui.operators;
2+
3+
import android.os.Bundle;
4+
import android.util.Log;
5+
import android.widget.Button;
6+
import android.widget.TextView;
7+
8+
import com.rxjava2.android.samples.R;
9+
import com.rxjava2.android.samples.utils.AppConstant;
10+
import com.rxjava2.android.samples.utils.ObserverAdapter;
11+
12+
import androidx.annotation.Nullable;
13+
import androidx.appcompat.app.AppCompatActivity;
14+
import io.reactivex.Observable;
15+
import io.reactivex.Observer;
16+
import io.reactivex.disposables.Disposable;
17+
18+
public abstract class TakeOperatorBaseActivity extends AppCompatActivity {
19+
private static final String TAG = TakeWhileExampleActivity.class.getSimpleName();
20+
21+
private Button btn;
22+
23+
protected TextView textView;
24+
25+
@Override
26+
protected void onCreate(@Nullable Bundle savedInstanceState) {
27+
super.onCreate(savedInstanceState);
28+
setContentView(R.layout.activity_example);
29+
btn = findViewById(R.id.btn);
30+
textView = findViewById(R.id.textView);
31+
32+
btn.setOnClickListener(view -> {
33+
doSomeWork();
34+
});
35+
}
36+
37+
/**
38+
* Need to be override based on the operation.
39+
*/
40+
abstract void doSomeWork();
41+
42+
protected Observer<? super String> getObserver() {
43+
return new ObserverAdapter<String>() {
44+
@Override
45+
public void onSubscribe(Disposable d) {
46+
Log.d(TAG, " onSubscribe : " + d.isDisposed());
47+
}
48+
49+
@Override
50+
public void onNext(String value) {
51+
textView.append(" onNext : value : " + value);
52+
textView.append(AppConstant.LINE_SEPARATOR);
53+
Log.d(TAG, " onNext value : " + value);
54+
}
55+
56+
@Override
57+
public void onComplete() {
58+
textView.append(" onComplete");
59+
textView.append(AppConstant.LINE_SEPARATOR);
60+
Log.d(TAG, " onComplete");
61+
}
62+
};
63+
}
64+
65+
66+
protected Observable<String> getStringObservable() {
67+
return Observable.just("Alpha", "Beta", "Cupcake", "Doughnut", "Eclair", "Froyo", "GingerBread",
68+
"Honeycomb", "Ice cream sandwich");
69+
}
70+
71+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.rxjava2.android.samples.ui.operators;
2+
3+
import android.util.Log;
4+
5+
import com.rxjava2.android.samples.utils.AppConstant;
6+
import com.rxjava2.android.samples.utils.ObserverAdapter;
7+
8+
import java.util.concurrent.TimeUnit;
9+
10+
import io.reactivex.Observable;
11+
import io.reactivex.android.schedulers.AndroidSchedulers;
12+
import io.reactivex.functions.BiFunction;
13+
14+
public class TakeUntilExampleActivity extends TakeOperatorBaseActivity {
15+
16+
private static final String TAG = TakeWhileExampleActivity.class.getSimpleName();
17+
18+
@Override
19+
protected void doSomeWork() {
20+
Observable<Long> timerObservable = Observable.timer(5, TimeUnit.SECONDS);
21+
timerObservable.subscribe(new ObserverAdapter<Long>() {
22+
@Override
23+
public void onComplete() {
24+
String print = " Timer completed";
25+
textView.append(print);
26+
textView.append(AppConstant.LINE_SEPARATOR);
27+
Log.d(TAG, print);
28+
}
29+
});
30+
31+
getStringObservable()
32+
//Delay item emission by one second
33+
.zipWith(Observable.interval(0, 1, TimeUnit.SECONDS), new BiFunction<String, Long, String>() {
34+
@Override
35+
public String apply(String s, Long aLong) throws Exception {
36+
return s;
37+
}
38+
})
39+
//Will receive the items from Strings observable until timerObservable doesn't start emitting data.
40+
.takeUntil(timerObservable)
41+
//We need to observe on MainThread because delay works on background thread to avoid UI blocking.
42+
.observeOn(AndroidSchedulers.mainThread())
43+
.subscribe(getObserver());
44+
}
45+
}
Lines changed: 2 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,18 @@
11
package com.rxjava2.android.samples.ui.operators;
22

3-
import android.os.Bundle;
4-
import android.util.Log;
5-
import android.widget.Button;
6-
import android.widget.TextView;
7-
8-
import com.rxjava2.android.samples.R;
9-
import com.rxjava2.android.samples.utils.AppConstant;
10-
import com.rxjava2.android.samples.utils.ObserverAdapter;
11-
123
import java.util.concurrent.TimeUnit;
134

14-
import androidx.annotation.Nullable;
15-
import androidx.appcompat.app.AppCompatActivity;
165
import io.reactivex.Observable;
17-
import io.reactivex.Observer;
186
import io.reactivex.android.schedulers.AndroidSchedulers;
19-
import io.reactivex.disposables.Disposable;
207
import io.reactivex.functions.BiFunction;
218
import io.reactivex.functions.Predicate;
229

23-
public class TakeWhileExampleActivity extends AppCompatActivity {
10+
public class TakeWhileExampleActivity extends TakeOperatorBaseActivity {
2411

2512
private static final String TAG = TakeWhileExampleActivity.class.getSimpleName();
2613

27-
private Button btn;
28-
29-
private TextView textView;
30-
3114
@Override
32-
protected void onCreate(@Nullable Bundle savedInstanceState) {
33-
super.onCreate(savedInstanceState);
34-
setContentView(R.layout.activity_example);
35-
btn = findViewById(R.id.btn);
36-
textView = findViewById(R.id.textView);
37-
38-
btn.setOnClickListener(view -> {
39-
doSomeWork();
40-
});
41-
}
42-
43-
private void doSomeWork() {
15+
protected void doSomeWork() {
4416
getStringObservable()
4517
//Delay item emission by one second
4618
.zipWith(Observable.interval(0, 1, TimeUnit.SECONDS), new BiFunction<String, Long, String>() {
@@ -60,34 +32,4 @@ public boolean test(String s) throws Exception {
6032
.observeOn(AndroidSchedulers.mainThread())
6133
.subscribe(getObserver());
6234
}
63-
64-
private Observer<? super String> getObserver() {
65-
return new ObserverAdapter<String>() {
66-
@Override
67-
public void onSubscribe(Disposable d) {
68-
Log.d(TAG, " onSubscribe : " + d.isDisposed());
69-
}
70-
71-
@Override
72-
public void onNext(String value) {
73-
textView.append(" onNext : value : " + value);
74-
textView.append(AppConstant.LINE_SEPARATOR);
75-
Log.d(TAG, " onNext value : " + value);
76-
}
77-
78-
@Override
79-
public void onComplete() {
80-
textView.append(" onComplete");
81-
textView.append(AppConstant.LINE_SEPARATOR);
82-
Log.d(TAG, " onComplete");
83-
}
84-
};
85-
}
86-
87-
88-
private Observable<String> getStringObservable() {
89-
return Observable.just("Alpha", "Beta", "Cupcake", "Doughnut", "Eclair", "Froyo", "GingerBread",
90-
"Honeycomb", "Ice cream sandwich");
91-
}
92-
9335
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,5 +239,12 @@
239239
android:text="@string/take_while"
240240
android:textColor="@android:color/black" />
241241

242+
<Button
243+
android:layout_width="match_parent"
244+
android:layout_height="wrap_content"
245+
android:onClick="startTakeUntilActivity"
246+
android:text="@string/take_until"
247+
android:textColor="@android:color/black" />
248+
242249
</LinearLayout>
243250
</ScrollView>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,5 @@
4444
<string name="result">Result</string>
4545
<string name="switch_map">Switch Map</string>
4646
<string name="take_while">Take While</string>
47+
<string name="take_until">Take Until</string>
4748
</resources>

0 commit comments

Comments
 (0)