Skip to content

Commit f3d8a23

Browse files
author
Kaushik Gopal
committed
feat: add example for executing task, then with delay
1 parent 543e933 commit f3d8a23

4 files changed

Lines changed: 66 additions & 13 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ Cases demonstrated here:
125125
2. run a task constantly every 1s (there's a delay of 1s before the first task fires off)
126126
3. run a task constantly every 1s (same as above but there's no delay before the first task fires off)
127127
4. run a task constantly every 3s, but after running it 5 times, terminate automatically
128+
5. run a task A, pause for sometime, then execute Task B, then terminate
128129

129130
### Exponential backoff
130131

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

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import rx.Observable;
2525
import rx.Observer;
2626
import rx.Subscription;
27+
import rx.functions.Action1;
2728
import timber.log.Timber;
2829

2930
import static android.os.Looper.getMainLooper;
@@ -66,7 +67,7 @@ public void onDestroyView() {
6667
// -----------------------------------------------------------------------------------
6768

6869
@OnClick(R.id.btn_demo_timing_1)
69-
public void Btn1_RunSingleTaskAfter2s() {
70+
public void btn1_RunSingleTaskAfter2s() {
7071
_log(String.format("A1 [%s] --- BTN click", _getCurrentTimestamp()));
7172

7273
Observable.timer(2, TimeUnit.SECONDS)//
@@ -90,7 +91,7 @@ public void onNext(Long number) {
9091
}
9192

9293
@OnClick(R.id.btn_demo_timing_2)
93-
public void Btn2_RunTask_IntervalOf1s() {
94+
public void btn2_RunTask_IntervalOf1s() {
9495
if (_subscription1 != null && !_subscription1.isUnsubscribed()) {
9596
_subscription1.unsubscribe();
9697
_log(String.format("B2 [%s] XXX BTN KILLED", _getCurrentTimestamp()));
@@ -120,7 +121,7 @@ public void onNext(Long number) {
120121
}
121122

122123
@OnClick(R.id.btn_demo_timing_3)
123-
public void Btn3_RunTask_IntervalOf1s_StartImmediately() {
124+
public void btn3_RunTask_IntervalOf1s_StartImmediately() {
124125
if (_subscription2 != null && !_subscription2.isUnsubscribed()) {
125126
_subscription2.unsubscribe();
126127
_log(String.format("C3 [%s] XXX BTN KILLED", _getCurrentTimestamp()));
@@ -150,7 +151,7 @@ public void onNext(Long number) {
150151
}
151152

152153
@OnClick(R.id.btn_demo_timing_4)
153-
public void Btn4_RunTask5Times_IntervalOf3s() {
154+
public void btn4_RunTask5Times_IntervalOf3s() {
154155
_log(String.format("D4 [%s] --- BTN click", _getCurrentTimestamp()));
155156

156157
Observable//
@@ -173,6 +174,42 @@ public void onNext(Long number) {
173174
});
174175
}
175176

177+
@OnClick(R.id.btn_demo_timing_5)
178+
public void btn5_RunTask5Times_IntervalOf3s() {
179+
_log(String.format("D5 [%s] --- BTN click", _getCurrentTimestamp()));
180+
181+
Observable.just("Do task A right away")
182+
.doOnNext(new Action1<String>() {
183+
@Override
184+
public void call(String input) {
185+
_log(String.format("D5 %s [%s]", input, _getCurrentTimestamp()));
186+
}
187+
})
188+
.delay(1, TimeUnit.SECONDS)
189+
.doOnNext(new Action1<String>() {
190+
@Override
191+
public void call(String oldInput) {
192+
_log(String.format("D5 %s [%s]", "Doing Task B after a delay", _getCurrentTimestamp()));
193+
}
194+
})
195+
.subscribe(new Observer<String>() {
196+
@Override
197+
public void onCompleted() {
198+
_log(String.format("D5 [%s] XXX COMPLETE", _getCurrentTimestamp()));
199+
}
200+
201+
@Override
202+
public void onError(Throwable e) {
203+
Timber.e(e, "something went wrong in TimingDemoFragment example");
204+
}
205+
206+
@Override
207+
public void onNext(String number) {
208+
_log(String.format("D5 [%s] NEXT", _getCurrentTimestamp()));
209+
}
210+
});
211+
}
212+
176213
// -----------------------------------------------------------------------------------
177214
// Method that help wiring up the example (irrelevant to RxJava)
178215

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

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
android:gravity="left"
2626
android:text="@string/msg_demo_timing"
2727
android:layout_span="2"
28-
2928
/>
3029
</TableRow>
3130

@@ -72,6 +71,29 @@
7271
android:text="BTN 4"
7372
/>
7473
</TableRow>
74+
75+
<TableRow
76+
android:layout_height="wrap_content"
77+
android:layout_width="match_parent"
78+
>
79+
80+
<Button
81+
android:id="@+id/btn_demo_timing_5"
82+
android:layout_weight="1"
83+
android:layout_height="wrap_content"
84+
android:layout_width="wrap_content"
85+
android:text="BTN 5"
86+
/>
87+
88+
<Button
89+
android:id="@+id/btn_clr"
90+
android:src="@android:drawable/ic_menu_close_clear_cancel"
91+
android:layout_weight="1"
92+
android:layout_height="wrap_content"
93+
android:layout_width="wrap_content"
94+
android:text="CLEAR LOG"
95+
/>
96+
</TableRow>
7597
</TableLayout>
7698

7799

@@ -87,13 +109,6 @@
87109
android:layout_height="match_parent"
88110
android:layout_width="0dp"
89111
/>
90-
91-
<ImageButton
92-
android:id="@+id/btn_clr"
93-
android:src="@android:drawable/ic_menu_close_clear_cancel"
94-
android:layout_height="wrap_content"
95-
android:layout_width="wrap_content"
96-
/>
97112
</LinearLayout>
98113

99114

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@
3232
<string name="msg_demo_exponential_backoff">These two examples demonstrate retrying and executing with a delay using an exponential backoff strategy.</string>
3333
<string name="msg_demo_rxbus_1">Tap on the below button and RxBus will listen to the events</string>
3434
<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>
35-
<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>
35+
<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>
3636
<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>
3737
</resources>

0 commit comments

Comments
 (0)