|
| 1 | +package com.morihacky.android.rxjava.fragments; |
| 2 | + |
| 3 | +import android.os.Bundle; |
| 4 | +import android.os.Handler; |
| 5 | +import android.os.Looper; |
| 6 | +import android.support.annotation.Nullable; |
| 7 | +import android.view.LayoutInflater; |
| 8 | +import android.view.View; |
| 9 | +import android.view.ViewGroup; |
| 10 | +import android.widget.ArrayAdapter; |
| 11 | +import android.widget.ListView; |
| 12 | +import android.widget.TextView; |
| 13 | +import butterknife.Bind; |
| 14 | +import butterknife.ButterKnife; |
| 15 | +import butterknife.OnClick; |
| 16 | +import com.morihacky.android.rxjava.R; |
| 17 | +import com.morihacky.android.rxjava.retrofit.Contributor; |
| 18 | +import com.morihacky.android.rxjava.retrofit.GithubApi; |
| 19 | +import com.morihacky.android.rxjava.retrofit.GithubService; |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.concurrent.TimeUnit; |
| 25 | +import rx.Observable; |
| 26 | +import rx.Subscriber; |
| 27 | +import rx.android.schedulers.AndroidSchedulers; |
| 28 | +import rx.schedulers.Schedulers; |
| 29 | +import timber.log.Timber; |
| 30 | + |
| 31 | +public class PseudoCacheFragment |
| 32 | + extends BaseFragment { |
| 33 | + |
| 34 | + @Bind(R.id.info_pseudoCache_demo) TextView infoText; |
| 35 | + @Bind(R.id.info_pseudoCache_listSubscription) ListView listSubscriptionInfo; |
| 36 | + @Bind(R.id.info_pseudoCache_listDtl) ListView listDetail; |
| 37 | + |
| 38 | + private ArrayAdapter<String> adapterDetail, adapterSubscriptionInfo; |
| 39 | + private HashMap<String, Long> contributionMap = null; |
| 40 | + |
| 41 | + @Override |
| 42 | + public View onCreateView(LayoutInflater inflater, |
| 43 | + @Nullable ViewGroup container, |
| 44 | + @Nullable Bundle savedInstanceState) { |
| 45 | + View layout = inflater.inflate(R.layout.fragment_pseudo_cache, container, false); |
| 46 | + ButterKnife.bind(this, layout); |
| 47 | + return layout; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public void onDestroyView() { |
| 52 | + super.onDestroyView(); |
| 53 | + ButterKnife.unbind(this); |
| 54 | + } |
| 55 | + |
| 56 | + @OnClick(R.id.btn_pseudoCache_concat) |
| 57 | + public void onConcatBtnClicked() { |
| 58 | + infoText.setText(R.string.msg_pseudoCache_demoInfo_concat); |
| 59 | + wireupDemo(); |
| 60 | + |
| 61 | + Observable.concat(getSlowCachedDiskData(), getFreshNetworkData()) |
| 62 | + .subscribeOn(Schedulers.io()) // we want to add a list item at time of subscription |
| 63 | + .observeOn(AndroidSchedulers.mainThread()) |
| 64 | + .subscribe(new Subscriber<Contributor>() { |
| 65 | + @Override |
| 66 | + public void onCompleted() { |
| 67 | + Timber.d("done loading all data"); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void onError(Throwable e) { |
| 72 | + Timber.e(e, "arr something went wrong"); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public void onNext(Contributor contributor) { |
| 77 | + contributionMap.put(contributor.login, contributor.contributions); |
| 78 | + adapterDetail.clear(); |
| 79 | + adapterDetail.addAll(mapAsList(contributionMap)); |
| 80 | + } |
| 81 | + }); |
| 82 | + } |
| 83 | + |
| 84 | + @OnClick(R.id.btn_pseudoCache_concatEager) |
| 85 | + public void onConcatEagerBtnClicked() { |
| 86 | + infoText.setText(R.string.msg_pseudoCache_demoInfo_concatEager); |
| 87 | + wireupDemo(); |
| 88 | + |
| 89 | + Observable.concatEager(getSlowCachedDiskData(), getFreshNetworkData()) |
| 90 | + .subscribeOn(Schedulers.io()) // we want to add a list item at time of subscription |
| 91 | + .observeOn(AndroidSchedulers.mainThread()) |
| 92 | + .subscribe(new Subscriber<Contributor>() { |
| 93 | + @Override |
| 94 | + public void onCompleted() { |
| 95 | + Timber.d("done loading all data"); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public void onError(Throwable e) { |
| 100 | + Timber.e(e, "arr something went wrong"); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public void onNext(Contributor contributor) { |
| 105 | + contributionMap.put(contributor.login, contributor.contributions); |
| 106 | + adapterDetail.clear(); |
| 107 | + adapterDetail.addAll(mapAsList(contributionMap)); |
| 108 | + } |
| 109 | + }); |
| 110 | + |
| 111 | + } |
| 112 | + |
| 113 | + @OnClick(R.id.btn_pseudoCache_merge) |
| 114 | + public void onMergeBtnClicked() { |
| 115 | + infoText.setText(R.string.msg_pseudoCache_demoInfo_merge); |
| 116 | + wireupDemo(); |
| 117 | + |
| 118 | + Observable.merge(getCachedDiskData(), getFreshNetworkData()) |
| 119 | + .subscribeOn(Schedulers.io()) // we want to add a list item at time of subscription |
| 120 | + .observeOn(AndroidSchedulers.mainThread()) |
| 121 | + .subscribe(new Subscriber<Contributor>() { |
| 122 | + @Override |
| 123 | + public void onCompleted() { |
| 124 | + Timber.d("done loading all data"); |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public void onError(Throwable e) { |
| 129 | + Timber.e(e, "arr something went wrong"); |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public void onNext(Contributor contributor) { |
| 134 | + contributionMap.put(contributor.login, contributor.contributions); |
| 135 | + adapterDetail.clear(); |
| 136 | + adapterDetail.addAll(mapAsList(contributionMap)); |
| 137 | + } |
| 138 | + }); |
| 139 | + } |
| 140 | + |
| 141 | + @OnClick(R.id.btn_pseudoCache_mergeSlowDisk) |
| 142 | + public void onMergeSlowBtnClicked() { |
| 143 | + infoText.setText(R.string.msg_pseudoCache_demoInfo_mergeSlowDisk); |
| 144 | + wireupDemo(); |
| 145 | + |
| 146 | + Observable.merge(getSlowCachedDiskData(), getFreshNetworkData()) |
| 147 | + .subscribeOn(Schedulers.io()) // we want to add a list item at time of subscription |
| 148 | + .observeOn(AndroidSchedulers.mainThread()) |
| 149 | + .subscribe(new Subscriber<Contributor>() { |
| 150 | + @Override |
| 151 | + public void onCompleted() { |
| 152 | + Timber.d("done loading all data"); |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public void onError(Throwable e) { |
| 157 | + Timber.e(e, "arr something went wrong"); |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + public void onNext(Contributor contributor) { |
| 162 | + contributionMap.put(contributor.login, contributor.contributions); |
| 163 | + adapterDetail.clear(); |
| 164 | + adapterDetail.addAll(mapAsList(contributionMap)); |
| 165 | + } |
| 166 | + }); |
| 167 | + } |
| 168 | + |
| 169 | + @OnClick(R.id.btn_pseudoCache_mergeOptimized) |
| 170 | + public void onMergeOptimizedBtnClicked() { |
| 171 | + infoText.setText(R.string.msg_pseudoCache_demoInfo_mergeOptimized); |
| 172 | + wireupDemo(); |
| 173 | + |
| 174 | + getFreshNetworkData()// |
| 175 | + .publish(network ->// |
| 176 | + Observable.merge(network,// |
| 177 | + getCachedDiskData().takeUntil(network))) |
| 178 | + .subscribeOn(Schedulers.io()) // we want to add a list item at time of subscription |
| 179 | + .observeOn(AndroidSchedulers.mainThread()) |
| 180 | + .subscribe(new Subscriber<Contributor>() { |
| 181 | + @Override |
| 182 | + public void onCompleted() { |
| 183 | + Timber.d("done loading all data"); |
| 184 | + } |
| 185 | + |
| 186 | + @Override |
| 187 | + public void onError(Throwable e) { |
| 188 | + Timber.e(e, "arr something went wrong"); |
| 189 | + } |
| 190 | + |
| 191 | + @Override |
| 192 | + public void onNext(Contributor contributor) { |
| 193 | + contributionMap.put(contributor.login, contributor.contributions); |
| 194 | + adapterDetail.clear(); |
| 195 | + adapterDetail.addAll(mapAsList(contributionMap)); |
| 196 | + } |
| 197 | + }); |
| 198 | + } |
| 199 | + |
| 200 | + @OnClick(R.id.btn_pseudoCache_mergeOptimizedSlowDisk) |
| 201 | + public void onMergeOptimizedWithSlowDiskBtnClicked() { |
| 202 | + infoText.setText(R.string.msg_pseudoCache_demoInfo_mergeOptimizedSlowDisk); |
| 203 | + wireupDemo(); |
| 204 | + |
| 205 | + getFreshNetworkData()// |
| 206 | + .publish(network ->// |
| 207 | + Observable.merge(network,// |
| 208 | + getSlowCachedDiskData().takeUntil(network))) |
| 209 | + .subscribeOn(Schedulers.io()) // we want to add a list item at time of subscription |
| 210 | + .observeOn(AndroidSchedulers.mainThread()) |
| 211 | + .subscribe(new Subscriber<Contributor>() { |
| 212 | + @Override |
| 213 | + public void onCompleted() { |
| 214 | + Timber.d("done loading all data"); |
| 215 | + } |
| 216 | + |
| 217 | + @Override |
| 218 | + public void onError(Throwable e) { |
| 219 | + Timber.e(e, "arr something went wrong"); |
| 220 | + } |
| 221 | + |
| 222 | + @Override |
| 223 | + public void onNext(Contributor contributor) { |
| 224 | + contributionMap.put(contributor.login, contributor.contributions); |
| 225 | + adapterDetail.clear(); |
| 226 | + adapterDetail.addAll(mapAsList(contributionMap)); |
| 227 | + } |
| 228 | + }); |
| 229 | + } |
| 230 | + |
| 231 | + // ----------------------------------------------------------------------------------- |
| 232 | + // WIRING for example |
| 233 | + |
| 234 | + private void wireupDemo() { |
| 235 | + contributionMap = new HashMap<>(); |
| 236 | + |
| 237 | + adapterDetail = new ArrayAdapter<>(getActivity(), R.layout.item_log_white, R.id.item_log, new ArrayList<>()); |
| 238 | + listDetail.setAdapter(adapterDetail); |
| 239 | + |
| 240 | + adapterSubscriptionInfo = new ArrayAdapter<>(getActivity(), |
| 241 | + R.layout.item_log_white, |
| 242 | + R.id.item_log, |
| 243 | + new ArrayList<>()); |
| 244 | + listSubscriptionInfo.setAdapter(adapterSubscriptionInfo); |
| 245 | + } |
| 246 | + |
| 247 | + private Observable<Contributor> getSlowCachedDiskData() { |
| 248 | + return Observable.timer(1, TimeUnit.SECONDS).flatMap(dummy -> getCachedDiskData()); |
| 249 | + } |
| 250 | + |
| 251 | + private Observable<Contributor> getCachedDiskData() { |
| 252 | + List<Contributor> list = new ArrayList<>(); |
| 253 | + Map<String, Long> map = dummyDiskData(); |
| 254 | + |
| 255 | + for (String username : map.keySet()) { |
| 256 | + Contributor c = new Contributor(); |
| 257 | + c.login = username; |
| 258 | + c.contributions = map.get(username); |
| 259 | + list.add(c); |
| 260 | + } |
| 261 | + |
| 262 | + return Observable.from(list)// |
| 263 | + .doOnSubscribe(() -> new Handler(Looper.getMainLooper())// |
| 264 | + .post(() -> adapterSubscriptionInfo.add("(disk) cache subscribed")))// |
| 265 | + .doOnCompleted(() -> new Handler(Looper.getMainLooper())// |
| 266 | + .post(() -> adapterSubscriptionInfo.add("(disk) cache completed"))); |
| 267 | + } |
| 268 | + |
| 269 | + private Observable<Contributor> getFreshNetworkData() { |
| 270 | + String githubToken = getResources().getString(R.string.github_oauth_token); |
| 271 | + GithubApi githubService = GithubService.createGithubService(githubToken); |
| 272 | + |
| 273 | + return githubService.contributors("square", "retrofit") |
| 274 | + .flatMap(Observable::from) |
| 275 | + .doOnSubscribe(() -> new Handler(Looper.getMainLooper())// |
| 276 | + .post(() -> adapterSubscriptionInfo.add("(network) subscribed")))// |
| 277 | + .doOnCompleted(() -> new Handler(Looper.getMainLooper())// |
| 278 | + .post(() -> adapterSubscriptionInfo.add("(network) completed"))); |
| 279 | + } |
| 280 | + |
| 281 | + private List<String> mapAsList(HashMap<String, Long> map) { |
| 282 | + List<String> list = new ArrayList<>(); |
| 283 | + |
| 284 | + for (String username : map.keySet()) { |
| 285 | + String rowLog = String.format("%s [%d]", username, contributionMap.get(username)); |
| 286 | + list.add(rowLog); |
| 287 | + } |
| 288 | + |
| 289 | + return list; |
| 290 | + } |
| 291 | + |
| 292 | + private Map<String, Long> dummyDiskData() { |
| 293 | + Map<String, Long> map = new HashMap<>(); |
| 294 | + map.put("JakeWharton", 0L); |
| 295 | + map.put("pforhan", 0L); |
| 296 | + map.put("edenman", 0L); |
| 297 | + map.put("swankjesse", 0L); |
| 298 | + map.put("bruceLee", 0L); |
| 299 | + return map; |
| 300 | + } |
| 301 | +} |
0 commit comments