Skip to content

Commit 61e69ff

Browse files
author
Kaushik Gopal
committed
feat: flush out pseudo cache example using .concat
1 parent 5a3ae78 commit 61e69ff

File tree

5 files changed

+152
-1
lines changed

5 files changed

+152
-1
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,13 @@ public void formValidation() {
103103
.replace(R.id.activity_main, new FormValidationCombineLatestFragment(), this.toString())
104104
.commit();
105105
}
106+
107+
@OnClick(R.id.btn_demo_pseudo_cache)
108+
public void pseudoCacheDemo() {
109+
getActivity().getSupportFragmentManager()
110+
.beginTransaction()
111+
.addToBackStack(this.toString())
112+
.replace(R.id.activity_main, new PseudoCacheConcatFragment(), this.toString())
113+
.commit();
114+
}
106115
}

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

Lines changed: 119 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,45 @@
66
import android.view.LayoutInflater;
77
import android.view.View;
88
import android.view.ViewGroup;
9+
import android.widget.ArrayAdapter;
10+
import android.widget.ListView;
911
import butterknife.ButterKnife;
12+
import butterknife.InjectView;
13+
import butterknife.OnClick;
1014
import com.morihacky.android.rxjava.app.R;
15+
import com.morihacky.android.rxjava.retrofit.Contributor;
16+
import com.morihacky.android.rxjava.retrofit.GithubApi;
17+
import java.util.ArrayList;
18+
import java.util.HashMap;
19+
import java.util.List;
20+
import retrofit.RequestInterceptor;
21+
import retrofit.RestAdapter;
22+
import rx.Observable;
23+
import rx.Subscriber;
1124
import rx.Subscription;
25+
import rx.android.schedulers.AndroidSchedulers;
26+
import rx.functions.Func1;
27+
import timber.log.Timber;
28+
29+
import static com.google.common.base.Strings.isNullOrEmpty;
30+
import static java.lang.String.format;
1231

1332
public class PseudoCacheConcatFragment
1433
extends Fragment {
1534

35+
@InjectView(R.id.log_list) ListView _resultList;
36+
1637
private Subscription _subscription = null;
38+
private HashMap<String, Long> _contributionMap = null;
39+
private ArrayAdapter<String> _adapter;
1740

1841
@Override
1942
public View onCreateView(LayoutInflater inflater,
2043
@Nullable ViewGroup container,
2144
@Nullable Bundle savedInstanceState) {
22-
View layout = inflater.inflate(R.layout.fragment_form_validation_comb_latest, container, false);
45+
View layout = inflater.inflate(R.layout.fragment_pseudo_cache_concat, container, false);
2346
ButterKnife.inject(this, layout);
47+
_initializeCache();
2448
return layout;
2549
}
2650

@@ -31,4 +55,98 @@ public void onPause() {
3155
_subscription.unsubscribe();
3256
}
3357
}
58+
59+
@OnClick(R.id.btn_start_pseudo_cache)
60+
public void onDemoPseudoCacheClicked() {
61+
_adapter = new ArrayAdapter<>(getActivity(),
62+
R.layout.item_log,
63+
R.id.item_log,
64+
new ArrayList<String>());
65+
66+
_resultList.setAdapter(_adapter);
67+
_initializeCache();
68+
69+
Observable.concat(_getCachedData(), _getFreshData())
70+
.observeOn(AndroidSchedulers.mainThread())
71+
.subscribe(new Subscriber<Contributor>() {
72+
@Override
73+
public void onCompleted() {
74+
Timber.d("done loading all data");
75+
}
76+
77+
@Override
78+
public void onError(Throwable e) {
79+
Timber.e(e, "arr something went wrong");
80+
}
81+
82+
@Override
83+
public void onNext(Contributor contributor) {
84+
_contributionMap.put(contributor.login, contributor.contributions);
85+
_adapter.clear();
86+
_adapter.addAll(getListStringFromMap());
87+
}
88+
});
89+
}
90+
91+
private List<String> getListStringFromMap() {
92+
List<String> list = new ArrayList<>();
93+
94+
for (String username : _contributionMap.keySet()) {
95+
String rowLog = String.format("%s [%d]", username, _contributionMap.get(username));
96+
list.add(rowLog);
97+
}
98+
99+
return list;
100+
}
101+
102+
private Observable<Contributor> _getCachedData() {
103+
104+
List<Contributor> list = new ArrayList<>();
105+
106+
for (String username : _contributionMap.keySet()) {
107+
Contributor c = new Contributor();
108+
c.login = username;
109+
c.contributions = _contributionMap.get(username);
110+
list.add(c);
111+
}
112+
113+
return Observable.from(list);
114+
}
115+
116+
private Observable<Contributor> _getFreshData() {
117+
return _createGithubApi().contributors("square", "retrofit")
118+
.flatMap(new Func1<List<Contributor>, Observable<Contributor>>() {
119+
@Override
120+
public Observable<Contributor> call(List<Contributor> contributors) {
121+
return Observable.from(contributors);
122+
}
123+
});
124+
}
125+
126+
private GithubApi _createGithubApi() {
127+
128+
RestAdapter.Builder builder = new RestAdapter.Builder().setEndpoint("https://api.github.com/");
129+
//.setLogLevel(RestAdapter.LogLevel.FULL);
130+
131+
final String githubToken = getResources().getString(R.string.github_oauth_token);
132+
if (!isNullOrEmpty(githubToken)) {
133+
builder.setRequestInterceptor(new RequestInterceptor() {
134+
@Override
135+
public void intercept(RequestFacade request) {
136+
request.addHeader("Authorization", format("token %s", githubToken));
137+
}
138+
});
139+
}
140+
141+
return builder.build().create(GithubApi.class);
142+
}
143+
144+
private void _initializeCache() {
145+
_contributionMap = new HashMap<>();
146+
_contributionMap.put("JakeWharton", 0l);
147+
_contributionMap.put("pforhan", 0l);
148+
_contributionMap.put("edenman", 0l);
149+
_contributionMap.put("swankjesse", 0l);
150+
_contributionMap.put("bruceLee", 0l);
151+
}
34152
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,11 @@
6262
android:layout_height="wrap_content"
6363
android:layout_width="match_parent"
6464
android:text="@string/btn_demo_form_validation_combinel"/>
65+
66+
<Button
67+
android:id="@+id/btn_demo_pseudo_cache"
68+
android:layout_height="wrap_content"
69+
android:layout_width="match_parent"
70+
android:text="@string/btn_demo_pseudo_cache"/>
6571
</LinearLayout>
6672
</ScrollView>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,24 @@
11
<LinearLayout
2+
android:baselineAligned="false"
23
android:orientation="vertical"
34
android:layout_height="match_parent"
45
android:layout_width="match_parent"
56
xmlns:android="http://schemas.android.com/apk/res/android"
67
>
8+
9+
<Button
10+
android:id="@+id/btn_start_pseudo_cache"
11+
android:layout_gravity="center"
12+
android:layout_height="wrap_content"
13+
android:layout_width="wrap_content"
14+
android:text="Start disk > network call"
15+
android:layout_margin="30dp"
16+
/>
17+
18+
<ListView
19+
android:id="@+id/log_list"
20+
android:layout_height="wrap_content"
21+
android:layout_width="match_parent"
22+
/>
23+
724
</LinearLayout>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<string name="btn_demo_polling">Polling with RxJava</string>
1616
<string name="btn_demo_rxbus">Event Bus with RxJava</string>
1717
<string name="btn_demo_form_validation_combinel">Form Validation with CombineLatest</string>
18+
<string name="btn_demo_pseudo_cache">Pseudo cache using concat</string>
1819

1920
<string name="msg_demo_concurrency_schedulers">This is a demo of how long running operations can be offloaded to a background thread. After the operation is done, we resume back on the main thread. All using RxJava! \n\n To really see this shine. Hit the button multiple times and see how the button click which is a ui operation is never blocked because the long operation only runs in the background</string>
2021
<string name="msg_demo_buffer">This is a demo of how events can be accumulated using the "buffer" operation. Tap the button below repetitively and you will notice in the logs that button taps are collected over a span of 2s and printed below.</string>

0 commit comments

Comments
 (0)