Search in sources :

Example 1 with Contributor

use of com.morihacky.android.rxjava.retrofit.Contributor in project RxJava-Android-Samples by kaushikgopal.

the class PseudoCacheMergeFragment method _getCachedData.

private Observable<Pair<Contributor, Long>> _getCachedData() {
    List<Pair<Contributor, Long>> list = new ArrayList<>();
    Pair<Contributor, Long> dataWithAgePair;
    for (String username : _contributionMap.keySet()) {
        Contributor c = new Contributor();
        c.login = username;
        c.contributions = _contributionMap.get(username);
        dataWithAgePair = new Pair<>(c, System.currentTimeMillis());
        list.add(dataWithAgePair);
    }
    return Observable.fromIterable(list);
}
Also used : ArrayList(java.util.ArrayList) Contributor(com.morihacky.android.rxjava.retrofit.Contributor) Pair(android.util.Pair)

Example 2 with Contributor

use of com.morihacky.android.rxjava.retrofit.Contributor in project RxJava-Android-Samples by kaushikgopal.

the class RetrofitFragment method onListContributorsClicked.

@OnClick(R.id.btn_demo_retrofit_contributors)
public void onListContributorsClicked() {
    _adapter.clear();
    //
    _disposables.add(_githubService.contributors(_username.getText().toString(), _repo.getText().toString()).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new DisposableObserver<List<Contributor>>() {

        @Override
        public void onComplete() {
            Timber.d("Retrofit call 1 completed");
        }

        @Override
        public void onError(Throwable e) {
            Timber.e(e, "woops we got an error while getting the list of contributors");
        }

        @Override
        public void onNext(List<Contributor> contributors) {
            for (Contributor c : contributors) {
                _adapter.add(format("%s has made %d contributions to %s", c.login, c.contributions, _repo.getText().toString()));
                Timber.d("%s has made %d contributions to %s", c.login, c.contributions, _repo.getText().toString());
            }
        }
    }));
}
Also used : DisposableObserver(io.reactivex.observers.DisposableObserver) Contributor(com.morihacky.android.rxjava.retrofit.Contributor) ArrayList(java.util.ArrayList) List(java.util.List) OnClick(butterknife.OnClick)

Example 3 with Contributor

use of com.morihacky.android.rxjava.retrofit.Contributor in project RxJava-Android-Samples by kaushikgopal.

the class PseudoCacheFragment method getCachedDiskData.

private Observable<Contributor> getCachedDiskData() {
    List<Contributor> list = new ArrayList<>();
    Map<String, Long> map = dummyDiskData();
    for (String username : map.keySet()) {
        Contributor c = new Contributor();
        c.login = username;
        c.contributions = map.get(username);
        list.add(c);
    }
    return //
    Observable.fromIterable(list).doOnSubscribe((data) -> //
    new Handler(Looper.getMainLooper()).post(//
    () -> adapterSubscriptionInfo.add("(disk) cache subscribed"))).doOnComplete(() -> //
    new Handler(Looper.getMainLooper()).post(() -> adapterSubscriptionInfo.add("(disk) cache completed")));
}
Also used : Bundle(android.os.Bundle) ButterKnife(butterknife.ButterKnife) HashMap(java.util.HashMap) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) OnClick(butterknife.OnClick) R(com.morihacky.android.rxjava.R) ArrayList(java.util.ArrayList) BindView(butterknife.BindView) Handler(android.os.Handler) Looper(android.os.Looper) Map(java.util.Map) View(android.view.View) Observable(io.reactivex.Observable) Schedulers(io.reactivex.schedulers.Schedulers) Unbinder(butterknife.Unbinder) Contributor(com.morihacky.android.rxjava.retrofit.Contributor) GithubApi(com.morihacky.android.rxjava.retrofit.GithubApi) LayoutInflater(android.view.LayoutInflater) GithubService(com.morihacky.android.rxjava.retrofit.GithubService) ViewGroup(android.view.ViewGroup) Timber(timber.log.Timber) TimeUnit(java.util.concurrent.TimeUnit) ArrayAdapter(android.widget.ArrayAdapter) List(java.util.List) TextView(android.widget.TextView) DisposableObserver(io.reactivex.observers.DisposableObserver) ListView(android.widget.ListView) Nullable(android.support.annotation.Nullable) ArrayList(java.util.ArrayList) Contributor(com.morihacky.android.rxjava.retrofit.Contributor) Handler(android.os.Handler)

Example 4 with Contributor

use of com.morihacky.android.rxjava.retrofit.Contributor in project RxJava-Android-Samples by kaushikgopal.

the class PseudoCacheFragment method getFreshNetworkData.

private Observable<Contributor> getFreshNetworkData() {
    String githubToken = getResources().getString(R.string.github_oauth_token);
    GithubApi githubService = GithubService.createGithubService(githubToken);
    return githubService.contributors("square", "retrofit").flatMap(Observable::fromIterable).doOnSubscribe((data) -> //
    new Handler(Looper.getMainLooper()).post(//
    () -> adapterSubscriptionInfo.add("(network) subscribed"))).doOnComplete(() -> //
    new Handler(Looper.getMainLooper()).post(() -> adapterSubscriptionInfo.add("(network) completed")));
}
Also used : Bundle(android.os.Bundle) ButterKnife(butterknife.ButterKnife) HashMap(java.util.HashMap) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) OnClick(butterknife.OnClick) R(com.morihacky.android.rxjava.R) ArrayList(java.util.ArrayList) BindView(butterknife.BindView) Handler(android.os.Handler) Looper(android.os.Looper) Map(java.util.Map) View(android.view.View) Observable(io.reactivex.Observable) Schedulers(io.reactivex.schedulers.Schedulers) Unbinder(butterknife.Unbinder) Contributor(com.morihacky.android.rxjava.retrofit.Contributor) GithubApi(com.morihacky.android.rxjava.retrofit.GithubApi) LayoutInflater(android.view.LayoutInflater) GithubService(com.morihacky.android.rxjava.retrofit.GithubService) ViewGroup(android.view.ViewGroup) Timber(timber.log.Timber) TimeUnit(java.util.concurrent.TimeUnit) ArrayAdapter(android.widget.ArrayAdapter) List(java.util.List) TextView(android.widget.TextView) DisposableObserver(io.reactivex.observers.DisposableObserver) ListView(android.widget.ListView) Nullable(android.support.annotation.Nullable) GithubApi(com.morihacky.android.rxjava.retrofit.GithubApi) Handler(android.os.Handler) Observable(io.reactivex.Observable)

Example 5 with Contributor

use of com.morihacky.android.rxjava.retrofit.Contributor in project RxJava-Android-Samples by kaushikgopal.

the class RetrofitFragment method onListContributorsWithFullUserInfoClicked.

@OnClick(R.id.btn_demo_retrofit_contributors_with_user_info)
public void onListContributorsWithFullUserInfoClicked() {
    _adapter.clear();
    _disposables.add(_githubService.contributors(_username.getText().toString(), _repo.getText().toString()).flatMap(Observable::fromIterable).flatMap(contributor -> {
        Observable<User> _userObservable = _githubService.user(contributor.login).filter(user -> !isEmpty(user.name) && !isEmpty(user.email));
        return Observable.zip(_userObservable, Observable.just(contributor), Pair::new);
    }).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new DisposableObserver<Pair<User, Contributor>>() {

        @Override
        public void onComplete() {
            Timber.d("Retrofit call 2 completed ");
        }

        @Override
        public void onError(Throwable e) {
            Timber.e(e, "error while getting the list of contributors along with full " + "names");
        }

        @Override
        public void onNext(Pair<User, Contributor> pair) {
            User user = pair.first;
            Contributor contributor = pair.second;
            _adapter.add(format("%s(%s) has made %d contributions to %s", user.name, user.email, contributor.contributions, _repo.getText().toString()));
            _adapter.notifyDataSetChanged();
            Timber.d("%s(%s) has made %d contributions to %s", user.name, user.email, contributor.contributions, _repo.getText().toString());
        }
    }));
}
Also used : User(com.morihacky.android.rxjava.retrofit.User) DisposableObserver(io.reactivex.observers.DisposableObserver) Contributor(com.morihacky.android.rxjava.retrofit.Contributor) Pair(android.util.Pair) OnClick(butterknife.OnClick)

Aggregations

Contributor (com.morihacky.android.rxjava.retrofit.Contributor)5 OnClick (butterknife.OnClick)4 DisposableObserver (io.reactivex.observers.DisposableObserver)4 ArrayList (java.util.ArrayList)4 List (java.util.List)3 Bundle (android.os.Bundle)2 Handler (android.os.Handler)2 Looper (android.os.Looper)2 Nullable (android.support.annotation.Nullable)2 Pair (android.util.Pair)2 LayoutInflater (android.view.LayoutInflater)2 View (android.view.View)2 ViewGroup (android.view.ViewGroup)2 ArrayAdapter (android.widget.ArrayAdapter)2 ListView (android.widget.ListView)2 TextView (android.widget.TextView)2 BindView (butterknife.BindView)2 ButterKnife (butterknife.ButterKnife)2 Unbinder (butterknife.Unbinder)2 R (com.morihacky.android.rxjava.R)2