Skip to content

Commit 91cbb7c

Browse files
author
Kaushik Gopal
committed
feat: run task 5 times with interval then complete + refactor: cleanup presentation
1 parent 42570be commit 91cbb7c

File tree

3 files changed

+51
-12
lines changed

3 files changed

+51
-12
lines changed

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

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ public View onCreateView(LayoutInflater inflater,
4949
// -----------------------------------------------------------------------------------
5050

5151
@OnClick(R.id.btn_demo_timing_1)
52-
public void onRunOnceWithDelay() {
53-
_log(String.format("C1 [%s] --- BTN click", _getCurrentTimestamp()));
52+
public void Btn1_RunSingleTaskAfter2s() {
53+
_log(String.format("B1 [%s] --- BTN click", _getCurrentTimestamp()));
5454

55-
Observable.just(1).delay(1, TimeUnit.SECONDS).subscribe(new Observer<Integer>() {
55+
Observable.just(1).delay(2, TimeUnit.SECONDS).subscribe(new Observer<Integer>() {
5656
@Override
5757
public void onCompleted() {
58-
_log(String.format("C1 [%s] XX COMPLETE", _getCurrentTimestamp()));
58+
_log(String.format("B1 [%s] XX COMPLETE", _getCurrentTimestamp()));
5959
}
6060

6161
@Override
@@ -65,7 +65,29 @@ public void onError(Throwable e) {
6565

6666
@Override
6767
public void onNext(Integer integer) {
68-
_log(String.format("C1 [%s] NEXT", _getCurrentTimestamp()));
68+
_log(String.format("B1 [%s] NEXT", _getCurrentTimestamp()));
69+
}
70+
});
71+
}
72+
73+
@OnClick(R.id.btn_demo_timing_4)
74+
public void Btn4_RunTask5Times_IntervalOf3s() {
75+
_log(String.format("C2 [%s] --- BTN click", _getCurrentTimestamp()));
76+
77+
Observable.interval(3, TimeUnit.SECONDS).take(5).subscribe(new Observer<Long>() {
78+
@Override
79+
public void onCompleted() {
80+
_log(String.format("C2 [%s] XX COMPLETE", _getCurrentTimestamp()));
81+
}
82+
83+
@Override
84+
public void onError(Throwable e) {
85+
Timber.e(e, "something went wrong in TimingDemoFragment example");
86+
}
87+
88+
@Override
89+
public void onNext(Long number) {
90+
_log(String.format("C2 [%s] NEXT", _getCurrentTimestamp()));
6991
}
7092
});
7193
}

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

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,23 @@
1212
android:layout_width="match_parent"
1313
>
1414

15+
<TableRow
16+
android:layout_height="wrap_content"
17+
android:layout_width="match_parent"
18+
>
19+
20+
<TextView
21+
android:layout_height="wrap_content"
22+
android:layout_width="match_parent"
23+
android:gravity="left"
24+
android:layout_weight="1"
25+
android:layout_span="2"
26+
android:padding="@dimen/text_micro"
27+
android:text="@string/msg_demo_timing"
28+
29+
/>
30+
</TableRow>
31+
1532
<TableRow
1633
android:layout_height="wrap_content"
1734
android:layout_width="match_parent"
@@ -22,15 +39,15 @@
2239
android:layout_weight="1"
2340
android:layout_height="wrap_content"
2441
android:layout_width="wrap_content"
25-
android:text="C1: run once after a delay"
42+
android:text="BTN 1"
2643
/>
2744

2845
<Button
29-
android:id="@+id/btn_demo_timing_3"
46+
android:id="@+id/btn_demo_timing_2"
3047
android:layout_weight="1"
3148
android:layout_height="wrap_content"
3249
android:layout_width="wrap_content"
33-
android:text="C3: run at intervals (start with delay)"
50+
android:text="BTN 2"
3451
/>
3552
</TableRow>
3653

@@ -40,19 +57,19 @@
4057
>
4158

4259
<Button
43-
android:id="@+id/btn_demo_timing_2"
60+
android:id="@+id/btn_demo_timing_3"
4461
android:layout_weight="1"
4562
android:layout_height="wrap_content"
4663
android:layout_width="wrap_content"
47-
android:text="C2: run 5 times at specific intervals"
64+
android:text="BTN 3"
4865
/>
4966

5067
<Button
5168
android:id="@+id/btn_demo_timing_4"
5269
android:layout_weight="1"
5370
android:layout_height="wrap_content"
5471
android:layout_width="wrap_content"
55-
android:text="C4: intervals (but start immediately)"
72+
android:text="BTN 4"
5673
/>
5774
</TableRow>
5875
</TableLayout>

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">Demo of how you can use timing/interval/delay and play with the timings of when your events are emitted</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>
3131
</resources>

0 commit comments

Comments
 (0)