Skip to content

Commit

Permalink
RxJava and Retrofit demo
Browse files Browse the repository at this point in the history
#13
- layout, activity and fragment
  • Loading branch information
eurosecom committed Mar 28, 2017
1 parent e69f987 commit a08ec0d
Show file tree
Hide file tree
Showing 15 changed files with 523 additions and 3 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<activity android:name=".MapActivity"/>
<activity android:name=".RxbusActivity"/>
<activity android:name=".CheeseActivity"/>
<activity android:name=".RxzipActivity"/>



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public void onClick(View v, int pos, boolean isLongClick) {
case 7:

// View v at position pos is clicked.
Intent i7 = new Intent(mContext, RxbusActivity.class);
Intent i7 = new Intent(mContext, RxzipActivity.class);
v.getContext().startActivity(i7);

break;
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/eusecom/attendance/RxbusActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import com.eusecom.attendance.fragments.MainFragment;
import com.eusecom.attendance.fragments.RxbusMainFragment;
import com.eusecom.attendance.rxbus.RxBus;

public class RxbusActivity
Expand All @@ -27,7 +27,7 @@ protected void onCreate(Bundle savedInstanceState) {

if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.replace(android.R.id.content, new MainFragment(), this.toString())
.replace(android.R.id.content, new RxbusMainFragment(), this.toString())
.commit();
}
}
Expand Down
33 changes: 33 additions & 0 deletions app/src/main/java/com/eusecom/attendance/RxzipActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.eusecom.attendance;

/**
* send events by https://github.com/kaushikgopal/RxJava-Android-Samples
*/

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.eusecom.attendance.fragments.RxzipMainFragment;

public class RxzipActivity extends FragmentActivity {



@Override
public void onBackPressed() {
super.onBackPressed();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.replace(android.R.id.content, new RxzipMainFragment(), this.toString())
.commit();
}
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package com.eusecom.attendance.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;


import com.eusecom.attendance.R;
import com.eusecom.attendance.retrofit.RfContributor;
import com.eusecom.attendance.retrofit.RfGithubApi;
import com.eusecom.attendance.retrofit.RfGithubService;
import com.eusecom.attendance.retrofit.RfUser;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;


import static android.text.TextUtils.isEmpty;
import static java.lang.String.format;

public class RetrofitFragment extends Fragment {

@Bind(R.id.demo_retrofit_contributors_username) EditText _username;
@Bind(R.id.demo_retrofit_contributors_repository) EditText _repo;
@Bind(R.id.log_list) ListView _resultList;

private ArrayAdapter<String> _adapter;
private RfGithubApi _githubService;
private CompositeDisposable _disposables;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String githubToken = getResources().getString(R.string.github_oauth_token);
_githubService = RfGithubService.createGithubService(githubToken);

_disposables = new CompositeDisposable();
}

@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {

View layout = inflater.inflate(R.layout.fragment_retrofit, container, false);
ButterKnife.bind(this, layout);

_adapter = new ArrayAdapter<>(getActivity(), R.layout.item_retrofitlog, R.id.item_log, new ArrayList<>());
//_adapter.setNotifyOnChange(true);
_resultList.setAdapter(_adapter);

return layout;
}

@Override
public void onDestroyView() {
super.onDestroyView();
ButterKnife.unbind(this);
}

@Override
public void onDestroy() {
super.onDestroy();
_disposables.dispose();
}

@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<RfContributor>>() {

@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<RfContributor> contributors) {
for (RfContributor 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());
}
}
}));
}

@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<RfUser> _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<RfUser,RfContributor>>() {
@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 pair) {
RfUser user = ((Pair<RfUser, RfContributor>)pair).first;
RfContributor contributor = ((Pair<RfUser, RfContributor>)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());
}
}));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.eusecom.attendance.fragments;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import butterknife.ButterKnife;
import butterknife.OnClick;

import com.eusecom.attendance.R;
import com.eusecom.attendance.rxbus.RxBusDemoFragment;

public class RxbusMainFragment extends BaseFragment {

@Override
public View onCreateView(@NonNull LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.fragment_rxbus, container, false);
ButterKnife.bind(this, layout);
return layout;
}

@Override
public void onDestroyView() {
super.onDestroyView();
ButterKnife.unbind(this);
}


@OnClick(R.id.btn_demo_rxbus)
void demoRxBus() {
clickedOn(new RxBusDemoFragment());
}

private void clickedOn(@NonNull Fragment fragment) {
final String tag = fragment.getClass().toString();
getActivity().getSupportFragmentManager()
.beginTransaction()
.addToBackStack(tag)
.replace(android.R.id.content, fragment, tag)
.commit();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.eusecom.attendance.fragments;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import butterknife.ButterKnife;
import butterknife.OnClick;

import com.eusecom.attendance.R;
import com.eusecom.attendance.rxbus.RxBusDemoFragment;

public class RxzipMainFragment extends BaseFragment {

@Override
public View onCreateView(@NonNull LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.fragment_rxzip, container, false);
ButterKnife.bind(this, layout);
return layout;
}

@Override
public void onDestroyView() {
super.onDestroyView();
ButterKnife.unbind(this);
}


@OnClick(R.id.btn_demo_rxzip)
void demoRxZip() {
clickedOn(new RetrofitFragment());
}

private void clickedOn(@NonNull Fragment fragment) {
final String tag = fragment.getClass().toString();
getActivity().getSupportFragmentManager()
.beginTransaction()
.addToBackStack(tag)
.replace(android.R.id.content, fragment, tag)
.commit();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.eusecom.attendance.retrofit;

public class RfContributor {
public String login;
public long contributions;
}
32 changes: 32 additions & 0 deletions app/src/main/java/com/eusecom/attendance/retrofit/RfGithubApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.eusecom.attendance.retrofit;

import java.util.List;

import io.reactivex.Observable;
import retrofit2.http.GET;
import retrofit2.http.Path;

public interface RfGithubApi {

/**
* See https://developer.github.com/v3/repos/#list-contributors
*/
@GET("/repos/{owner}/{repo}/contributors")
Observable<List<RfContributor>> contributors(@Path("owner") String owner,
@Path("repo") String repo);

@GET("/repos/{owner}/{repo}/contributors")
List<RfContributor> getContributors(@Path("owner") String owner, @Path("repo") String repo);

/**
* See https://developer.github.com/v3/users/
*/
@GET("/users/{user}")
Observable<RfUser> user(@Path("user") String user);

/**
* See https://developer.github.com/v3/users/
*/
@GET("/users/{user}")
RfUser getUser(@Path("user") String user);
}
Loading

0 comments on commit a08ec0d

Please sign in to comment.