Skip to content

Commit 55f7617

Browse files
author
Kaushik Gopal
committed
feat: add button 2 non-stop interval
1 parent 91cbb7c commit 55f7617

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

app/src/main/java/com/morihacky/android/rxjava/TimingDemoFragment.java

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.concurrent.TimeUnit;
1919
import rx.Observable;
2020
import rx.Observer;
21+
import rx.Subscription;
2122
import timber.log.Timber;
2223

2324
import static android.os.Looper.getMainLooper;
@@ -31,6 +32,9 @@ public class TimingDemoFragment
3132
private LogAdapter _adapter;
3233
private List<String> _logs;
3334

35+
private Subscription _subscription1 = null;
36+
private Subscription _subscription2 = null;
37+
3438
@Override
3539
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
3640
super.onActivityCreated(savedInstanceState);
@@ -50,12 +54,12 @@ public View onCreateView(LayoutInflater inflater,
5054

5155
@OnClick(R.id.btn_demo_timing_1)
5256
public void Btn1_RunSingleTaskAfter2s() {
53-
_log(String.format("B1 [%s] --- BTN click", _getCurrentTimestamp()));
57+
_log(String.format("A1 [%s] --- BTN click", _getCurrentTimestamp()));
5458

5559
Observable.just(1).delay(2, TimeUnit.SECONDS).subscribe(new Observer<Integer>() {
5660
@Override
5761
public void onCompleted() {
58-
_log(String.format("B1 [%s] XX COMPLETE", _getCurrentTimestamp()));
62+
_log(String.format("A1 [%s] XXX COMPLETE", _getCurrentTimestamp()));
5963
}
6064

6165
@Override
@@ -65,19 +69,47 @@ public void onError(Throwable e) {
6569

6670
@Override
6771
public void onNext(Integer integer) {
68-
_log(String.format("B1 [%s] NEXT", _getCurrentTimestamp()));
72+
_log(String.format("A1 [%s] NEXT", _getCurrentTimestamp()));
73+
}
74+
});
75+
}
76+
77+
@OnClick(R.id.btn_demo_timing_2)
78+
public void Btn2_RunTask_IntervalOf1s() {
79+
if (_subscription1 != null && !_subscription1.isUnsubscribed()) {
80+
_subscription1.unsubscribe();
81+
_log(String.format("B2 [%s] XXX BTN KILLED", _getCurrentTimestamp()));
82+
return;
83+
}
84+
85+
_log(String.format("B2 [%s] --- BTN click", _getCurrentTimestamp()));
86+
87+
_subscription1 = Observable.interval(1, TimeUnit.SECONDS).subscribe(new Observer<Long>() {
88+
@Override
89+
public void onCompleted() {
90+
_log(String.format("B2 [%s] XXXX COMPLETE", _getCurrentTimestamp()));
91+
}
92+
93+
@Override
94+
public void onError(Throwable e) {
95+
Timber.e(e, "something went wrong in TimingDemoFragment example");
96+
}
97+
98+
@Override
99+
public void onNext(Long number) {
100+
_log(String.format("B2 [%s] NEXT", _getCurrentTimestamp()));
69101
}
70102
});
71103
}
72104

73105
@OnClick(R.id.btn_demo_timing_4)
74106
public void Btn4_RunTask5Times_IntervalOf3s() {
75-
_log(String.format("C2 [%s] --- BTN click", _getCurrentTimestamp()));
107+
_log(String.format("D4 [%s] --- BTN click", _getCurrentTimestamp()));
76108

77109
Observable.interval(3, TimeUnit.SECONDS).take(5).subscribe(new Observer<Long>() {
78110
@Override
79111
public void onCompleted() {
80-
_log(String.format("C2 [%s] XX COMPLETE", _getCurrentTimestamp()));
112+
_log(String.format("D4 [%s] XXX COMPLETE", _getCurrentTimestamp()));
81113
}
82114

83115
@Override
@@ -87,7 +119,7 @@ public void onError(Throwable e) {
87119

88120
@Override
89121
public void onNext(Long number) {
90-
_log(String.format("C2 [%s] NEXT", _getCurrentTimestamp()));
122+
_log(String.format("D4 [%s] NEXT", _getCurrentTimestamp()));
91123
}
92124
});
93125
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@
2727
<string name="msg_demo_polling">This is demo of polling or making a call repeatedly with RxJava. \n\nSimple polling: Notice in the logs how a network call (simulated) is repeatedly made in the background.</string>
2828
<string name="msg_demo_rxbus_1">Tap on the below button and RxBus will listen to the events</string>
2929
<string name="msg_demo_form_comb_latest">Monitor the state of multiple observables with the combineLatest operator. The submit button uses combineLatest to monitor validity of each of the 3 inputs. Only after the 3 inputs contain valid inputs will the submit button be enabled</string>
30-
<string name="msg_demo_timing">BTN 1: run single task once (after 2s complete)\nBTN 2: run task every 1s (starts with delay of 1s)\nBTN 3: run task every 1s (start immediately)\nBTN 4: run task 5 times every 3s (then complete)</string>
30+
<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)</string>
3131
</resources>

0 commit comments

Comments
 (0)