-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#13 - layout, activity and fragment
- Loading branch information
Showing
15 changed files
with
523 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
app/src/main/java/com/eusecom/attendance/RxzipActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
|
||
|
||
|
||
} |
168 changes: 168 additions & 0 deletions
168
app/src/main/java/com/eusecom/attendance/fragments/RetrofitFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
})); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
app/src/main/java/com/eusecom/attendance/fragments/RxbusMainFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
app/src/main/java/com/eusecom/attendance/fragments/RxzipMainFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
app/src/main/java/com/eusecom/attendance/retrofit/RfContributor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
32
app/src/main/java/com/eusecom/attendance/retrofit/RfGithubApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.