|
| 1 | +package com.morihacky.android.rxjava; |
| 2 | + |
| 3 | +import android.os.Bundle; |
| 4 | +import android.support.annotation.Nullable; |
| 5 | +import android.util.Pair; |
| 6 | +import android.view.LayoutInflater; |
| 7 | +import android.view.View; |
| 8 | +import android.view.ViewGroup; |
| 9 | +import android.widget.ArrayAdapter; |
| 10 | +import android.widget.ListView; |
| 11 | +import butterknife.ButterKnife; |
| 12 | +import butterknife.InjectView; |
| 13 | +import butterknife.OnClick; |
| 14 | +import com.morihacky.android.rxjava.retrofit.Contributor; |
| 15 | +import com.morihacky.android.rxjava.retrofit.GithubApi; |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.HashMap; |
| 18 | +import java.util.List; |
| 19 | +import retrofit.RequestInterceptor; |
| 20 | +import retrofit.RestAdapter; |
| 21 | +import rx.Observable; |
| 22 | +import rx.Subscriber; |
| 23 | +import rx.Subscription; |
| 24 | +import rx.android.schedulers.AndroidSchedulers; |
| 25 | +import rx.functions.Func1; |
| 26 | +import timber.log.Timber; |
| 27 | + |
| 28 | +import static com.google.common.base.Strings.isNullOrEmpty; |
| 29 | +import static java.lang.String.format; |
| 30 | + |
| 31 | +public class PseudoCacheMergeFragment |
| 32 | + extends BaseFragment { |
| 33 | + |
| 34 | + @InjectView(R.id.log_list) ListView _resultList; |
| 35 | + |
| 36 | + private Subscription _subscription = null; |
| 37 | + private HashMap<String, Long> _contributionMap = null; |
| 38 | + private HashMap<Contributor, Long> _resultAgeMap = new HashMap<>(); |
| 39 | + private ArrayAdapter<String> _adapter; |
| 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_concat, container, false); |
| 46 | + ButterKnife.inject(this, layout); |
| 47 | + _initializeCache(); |
| 48 | + return layout; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public void onPause() { |
| 53 | + super.onPause(); |
| 54 | + if (_subscription != null) { |
| 55 | + _subscription.unsubscribe(); |
| 56 | + } |
| 57 | + } |
| 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.merge(_getCachedData(), _getFreshData()) |
| 70 | + .observeOn(AndroidSchedulers.mainThread()) |
| 71 | + .subscribe(new Subscriber<Pair<Contributor, Long>>() { |
| 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(Pair<Contributor, Long> contributorAgePair) { |
| 84 | + Contributor contributor = contributorAgePair.first; |
| 85 | + |
| 86 | + if (_resultAgeMap.containsKey(contributor) && |
| 87 | + _resultAgeMap.get(contributor) > contributorAgePair.second) { |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + _contributionMap.put(contributor.login, contributor.contributions); |
| 92 | + _resultAgeMap.put(contributor, contributorAgePair.second); |
| 93 | + |
| 94 | + _adapter.clear(); |
| 95 | + _adapter.addAll(getListStringFromMap()); |
| 96 | + } |
| 97 | + }); |
| 98 | + } |
| 99 | + |
| 100 | + private List<String> getListStringFromMap() { |
| 101 | + List<String> list = new ArrayList<>(); |
| 102 | + |
| 103 | + for (String username : _contributionMap.keySet()) { |
| 104 | + String rowLog = String.format("%s [%d]", username, _contributionMap.get(username)); |
| 105 | + list.add(rowLog); |
| 106 | + } |
| 107 | + |
| 108 | + return list; |
| 109 | + } |
| 110 | + |
| 111 | + private Observable<Pair<Contributor, Long>> _getCachedData() { |
| 112 | + |
| 113 | + List<Pair<Contributor, Long>> list = new ArrayList<>(); |
| 114 | + |
| 115 | + Pair<Contributor, Long> dataWithAgePair; |
| 116 | + |
| 117 | + for (String username : _contributionMap.keySet()) { |
| 118 | + Contributor c = new Contributor(); |
| 119 | + c.login = username; |
| 120 | + c.contributions = _contributionMap.get(username); |
| 121 | + |
| 122 | + dataWithAgePair = new Pair<>(c, System.currentTimeMillis()); |
| 123 | + list.add(dataWithAgePair); |
| 124 | + } |
| 125 | + |
| 126 | + return Observable.from(list); |
| 127 | + } |
| 128 | + |
| 129 | + private Observable<Pair<Contributor, Long>> _getFreshData() { |
| 130 | + return _createGithubApi().contributors("square", "retrofit") |
| 131 | + .flatMap(new Func1<List<Contributor>, Observable<Contributor>>() { |
| 132 | + @Override |
| 133 | + public Observable<Contributor> call(List<Contributor> contributors) { |
| 134 | + return Observable.from(contributors); |
| 135 | + } |
| 136 | + }) |
| 137 | + .map(new Func1<Contributor, Pair<Contributor, Long>>() { |
| 138 | + @Override |
| 139 | + public Pair<Contributor, Long> call(Contributor contributor) { |
| 140 | + return new Pair<>(contributor, System.currentTimeMillis()); |
| 141 | + } |
| 142 | + }); |
| 143 | + } |
| 144 | + |
| 145 | + private GithubApi _createGithubApi() { |
| 146 | + |
| 147 | + RestAdapter.Builder builder = new RestAdapter.Builder().setEndpoint( |
| 148 | + "https://api.github.com/"); |
| 149 | + //.setLogLevel(RestAdapter.LogLevel.FULL); |
| 150 | + |
| 151 | + final String githubToken = getResources().getString(R.string.github_oauth_token); |
| 152 | + if (!isNullOrEmpty(githubToken)) { |
| 153 | + builder.setRequestInterceptor(new RequestInterceptor() { |
| 154 | + @Override |
| 155 | + public void intercept(RequestFacade request) { |
| 156 | + request.addHeader("Authorization", format("token %s", githubToken)); |
| 157 | + } |
| 158 | + }); |
| 159 | + } |
| 160 | + |
| 161 | + return builder.build().create(GithubApi.class); |
| 162 | + } |
| 163 | + |
| 164 | + private void _initializeCache() { |
| 165 | + _contributionMap = new HashMap<>(); |
| 166 | + _contributionMap.put("JakeWharton", 0l); |
| 167 | + _contributionMap.put("pforhan", 0l); |
| 168 | + _contributionMap.put("edenman", 0l); |
| 169 | + _contributionMap.put("swankjesse", 0l); |
| 170 | + _contributionMap.put("bruceLee", 0l); |
| 171 | + } |
| 172 | +} |
0 commit comments