Add refresh on click functionality for 3 random repos

master
esensar 2016-10-04 00:16:44 +02:00
parent 17402765b6
commit 5b2ff57d5d
6 changed files with 25 additions and 41 deletions

View File

@ -1,7 +1,5 @@
package com.ensarsarajcic.reactivegithubsample.models; package com.ensarsarajcic.reactivegithubsample.models;
import com.google.gson.annotations.SerializedName;
/** /**
* Created by ensar on 03/10/16. * Created by ensar on 03/10/16.
*/ */

View File

@ -1,7 +1,5 @@
package com.ensarsarajcic.reactivegithubsample.models; package com.ensarsarajcic.reactivegithubsample.models;
import com.google.gson.annotations.SerializedName;
import java.util.List; import java.util.List;
/** /**

View File

@ -1,7 +1,5 @@
package com.ensarsarajcic.reactivegithubsample.models; package com.ensarsarajcic.reactivegithubsample.models;
import com.google.gson.annotations.SerializedName;
/** /**
* Created by ensar on 03/10/16. * Created by ensar on 03/10/16.
*/ */

View File

@ -17,12 +17,10 @@ import com.ensarsarajcic.reactivegithubsample.models.GitHubRepo;
import com.ensarsarajcic.reactivegithubsample.models.GitHubUser; import com.ensarsarajcic.reactivegithubsample.models.GitHubUser;
import com.ensarsarajcic.reactivegithubsample.network.RestClient; import com.ensarsarajcic.reactivegithubsample.network.RestClient;
import com.jakewharton.rxbinding.view.RxView; import com.jakewharton.rxbinding.view.RxView;
import com.jakewharton.rxbinding.widget.RxCompoundButton;
import com.jakewharton.rxbinding.widget.RxTextView;
import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
@ -99,7 +97,7 @@ public class GitHubUsersAdapter extends RecyclerView.Adapter<GitHubUsersAdapter.
public List<GitHubRepo> call(GitHubUser gitHubUser) { public List<GitHubRepo> call(GitHubUser gitHubUser) {
try { try {
return RestClient.getGitHubApi().getUserRepos(gitHubUser.getLogin()).execute().body(); return RestClient.getGitHubApi().getUserRepos(gitHubUser.getLogin()).execute().body();
} catch (IOException e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
Log.e(TAG, "call: ", e); Log.e(TAG, "call: ", e);
return null; return null;
@ -115,36 +113,24 @@ public class GitHubUsersAdapter extends RecyclerView.Adapter<GitHubUsersAdapter.
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()); .observeOn(AndroidSchedulers.mainThread());
// Observable<Void> randomReposObservable = RxView.clicks(holder.tvRepos).subscribeOn(AndroidSchedulers.mainThread()); Observable<Void> randomReposObservable = RxView.clicks(holder.tvRepos)
// // Very stupid way to just generate a single event because type is void
// compositeSubscription.add(randomReposObservable.subscribe(new Subscriber<Void>() { .startWith(Arrays.asList(new Void[]{null}))
// @Override .subscribeOn(AndroidSchedulers.mainThread());
// public void onCompleted() {
// Log.d(TAG, "onCompleted: ");
// }
//
// @Override
// public void onError(Throwable e) {
// Log.d(TAG, "onError: ");
// }
//
// @Override
// public void onNext(Void aVoid) {
// Log.d(TAG, "onNext: ");
// }
// }));
//
// Observable<List<GitHubRepo>> gitHubReposObservable = Observable.combineLatest(randomReposObservable, fetchUserReposObservable, new Func2<Void, List<GitHubRepo>, List<GitHubRepo>>() {
// @Override
// public List<GitHubRepo> call(Void aVoid, List<GitHubRepo> gitHubRepos) {
// return gitHubRepos;
// }
// }).subscribeOn(AndroidSchedulers.mainThread());
Observable<List<String>> repoNamesObservable = fetchUserReposObservable.map(new Func1<List<GitHubRepo>, List<String>>() { Observable<List<GitHubRepo>> gitHubReposObservable = Observable.combineLatest(randomReposObservable, fetchUserReposObservable, new Func2<Void, List<GitHubRepo>, List<GitHubRepo>>() {
@Override @Override
public List<String> call(List<GitHubRepo> gitHubRepos) { public List<GitHubRepo> call(Void aVoid, List<GitHubRepo> gitHubRepos) {
return gitHubRepos;
}
});
Observable<List<String>> repoNamesObservable = gitHubReposObservable.map(new Func1<List<GitHubRepo>, List<String>>() {
@Override
public List<String> call(List<GitHubRepo> gitHubReposOriginal) {
List<String> names = new ArrayList<String>(); List<String> names = new ArrayList<String>();
List<GitHubRepo> gitHubRepos = new ArrayList<GitHubRepo>();
gitHubRepos.addAll(gitHubReposOriginal);
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
if(gitHubRepos.isEmpty()) break; if(gitHubRepos.isEmpty()) break;
int position = new Random().nextInt(gitHubRepos.size()); int position = new Random().nextInt(gitHubRepos.size());
@ -155,8 +141,6 @@ public class GitHubUsersAdapter extends RecyclerView.Adapter<GitHubUsersAdapter.
} }
}); });
compositeSubscription.add(fetchImageObservable.subscribe(new Subscriber<Bitmap>() { compositeSubscription.add(fetchImageObservable.subscribe(new Subscriber<Bitmap>() {
@Override @Override
public void onCompleted() { public void onCompleted() {
@ -204,6 +188,7 @@ public class GitHubUsersAdapter extends RecyclerView.Adapter<GitHubUsersAdapter.
private TextView tvUserName; private TextView tvUserName;
private TextView tvUserUrl; private TextView tvUserUrl;
private ListView lvRepos; private ListView lvRepos;
private TextView tvRepos;
public GitHubUserViewHolder(View itemView) { public GitHubUserViewHolder(View itemView) {
super(itemView); super(itemView);
@ -211,6 +196,7 @@ public class GitHubUsersAdapter extends RecyclerView.Adapter<GitHubUsersAdapter.
tvUserName = (TextView) itemView.findViewById(R.id.tvUserName); tvUserName = (TextView) itemView.findViewById(R.id.tvUserName);
tvUserUrl = (TextView) itemView.findViewById(R.id.tvUserUrl); tvUserUrl = (TextView) itemView.findViewById(R.id.tvUserUrl);
lvRepos = (ListView) itemView.findViewById(R.id.lvRepos); lvRepos = (ListView) itemView.findViewById(R.id.lvRepos);
tvRepos = (TextView) itemView.findViewById(R.id.tvRepos);
} }
} }

View File

@ -1,7 +1,7 @@
package com.ensarsarajcic.reactivegithubsample.views; package com.ensarsarajcic.reactivegithubsample.views;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator; import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;

View File

@ -34,7 +34,11 @@
android:layout_below="@+id/tvUserUrl" android:layout_below="@+id/tvUserUrl"
android:layout_alignLeft="@+id/tvUserUrl" android:layout_alignLeft="@+id/tvUserUrl"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" /> android:layout_height="wrap_content"
android:textColor="@color/colorPrimaryDark"
android:textAllCaps="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="5dp"/>
<ListView <ListView
android:id="@+id/lvRepos" android:id="@+id/lvRepos"