Skip to content

Commit

Permalink
Approve Activity
Browse files Browse the repository at this point in the history
#12
- prepare RxJava Retrofit call for sending absences to Mysql server
  • Loading branch information
eurosecom committed Apr 1, 2017
1 parent a08ec0d commit 62bbcea
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -16,10 +17,17 @@
import android.widget.TextView;
import android.widget.Toast;

import com.eusecom.attendance.Constants;
import com.eusecom.attendance.NewPostActivity;
import com.eusecom.attendance.SettingsActivity;
import com.eusecom.attendance.models.Attendance;
import com.eusecom.attendance.models.DeletedAbs;
import com.eusecom.attendance.retrofit.RfContributor;
import com.eusecom.attendance.retrofit.RfEtestApi;
import com.eusecom.attendance.retrofit.RfEtestService;
import com.eusecom.attendance.retrofit.RfGithubApi;
import com.eusecom.attendance.retrofit.RfGithubService;
import com.eusecom.attendance.retrofit.RfUser;
import com.eusecom.attendance.viewholder.ApproveViewHolder;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.auth.FirebaseAuth;
Expand All @@ -39,6 +47,15 @@
import java.util.HashMap;
import java.util.Map;

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 abstract class ApproveListFragment extends Fragment {

private static final String TAG = "ApproveListFragment";
Expand All @@ -58,6 +75,18 @@ public ApproveListFragment() {}
boolean isCancelable, isrunning;
String timestampx;

private RfEtestApi _githubService;
private CompositeDisposable _disposables;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String githubToken = Constants.GITHUB_API_KEY;
_githubService = RfEtestService.createGithubService(githubToken);

_disposables = new CompositeDisposable();
}

@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Expand Down Expand Up @@ -259,6 +288,7 @@ public void onDestroy() {
if (mAdapter != null) {
mAdapter.cleanup();
}
_disposables.dispose();
}

public String getUid() {
Expand All @@ -271,7 +301,43 @@ public String getUid() {
// [START deletefan_out]
private void approvePost(String postkey, int anodaj) {

_disposables.add(_githubService.contributors("square", "retrofit")
.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() {
Log.d(TAG, "Retrofit call 2 completed ");

}

@Override
public void onError(Throwable e) {
Log.d(TAG, "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;


String snext = " " + user.name + " "
+ user.email + " "
+ contributor.contributions;

Log.d(TAG, "onnext " + snext);
}
}));

}
// [END delete_fan_out]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import android.widget.ListView;


import com.eusecom.attendance.Constants;
import com.eusecom.attendance.R;
import com.eusecom.attendance.retrofit.RfContributor;
import com.eusecom.attendance.retrofit.RfGithubApi;
Expand Down Expand Up @@ -47,7 +48,7 @@ public class RetrofitFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String githubToken = getResources().getString(R.string.github_oauth_token);
String githubToken = Constants.GITHUB_API_KEY;
_githubService = RfGithubService.createGithubService(githubToken);

_disposables = new CompositeDisposable();
Expand Down
108 changes: 108 additions & 0 deletions app/src/main/java/com/eusecom/attendance/retrofit/RfEtestApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package com.eusecom.attendance.retrofit;

import java.util.List;

import io.reactivex.Observable;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.Path;
import retrofit2.http.Query;

/*
<?php
//read headers
$headerStringValue="";
$headers = apache_request_headers();
foreach ($headers as $header => $value) {
$headerStringValue = " ".$headerStringValue.$value." ";
}
$user = $_GET['user'];
$sqlt = <<<ico
{
"login": "JakeWharton",
"id": 66577,
"avatar_url": "https://avatars3.githubusercontent.com/u/66577?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/JakeWharton",
"html_url": "https://github.com/JakeWharton",
"followers_url": "https://api.github.com/users/JakeWharton/followers",
"following_url": "https://api.github.com/users/JakeWharton/following{/other_user}",
"gists_url": "https://api.github.com/users/JakeWharton/gists{/gist_id}",
"starred_url": "https://api.github.com/users/JakeWharton/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/JakeWharton/subscriptions",
"organizations_url": "https://api.github.com/users/JakeWharton/orgs",
"repos_url": "https://api.github.com/users/JakeWharton/repos",
"events_url": "https://api.github.com/users/JakeWharton/events{/privacy}",
"received_events_url": "https://api.github.com/users/JakeWharton/received_events",
"type": "User",
"site_admin": false,
"name": "Jake Wharton",
"company": "Square, Inc.",
"blog": "http://jakewharton.com",
"location": "Pittsburgh, PA, USA",
"email": "jakewharton@gmail.com
ico;
$prem=" from php prem";
$sqlt1 = <<<ico1
",
"hireable": null,
"bio": null,
"public_repos": 92,
"public_gists": 54,
"followers": 33187,
"following": 12,
"created_at": "2009-03-24T16:09:53Z",
"updated_at": "2017-03-26T15:19:18Z"
}
ico1;
echo $sqlt.$prem.$headerStringValue.$user.$sqlt1;
?>
*/

public interface RfEtestApi {

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

@GET("/attendance/contributor.json")
Observable<List<RfContributor>> contributors_json(@Query("owner") String owner,
@Query("repo") String repo);

@GET("/attendance/{owner}/{repo}/contributor.json")
Observable<List<RfContributor>> contributors_withPath(@Path("owner") String owner,
@Path("repo") String repo);

@GET("/repos/{owner}/{repo}/contributors")
Observable<List<RfContributor>> contributors_orig(@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("/attendance/user.php")
Observable<RfUser> user(@Query("user") String user);

@GET("/users/{user}")
Observable<RfUser> user_orig(@Path("user") String user);

/**
* See https://developer.github.com/v3/users/
*/
@GET("/users/{user}")
RfUser getUser(@Path("user") String user);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.eusecom.attendance.retrofit;

import android.text.TextUtils;

import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static java.lang.String.format;

public class RfEtestService {

private RfEtestService() {
}

public static RfEtestApi createGithubService(final String githubToken) {
Retrofit.Builder builder = new Retrofit.Builder().addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.baseUrl("http://www.eshoptest.sk");
//.baseUrl("https://api.github.com");

if (!TextUtils.isEmpty(githubToken)) {

OkHttpClient client = new OkHttpClient.Builder().addInterceptor(chain -> {
Request request = chain.request();
Request newReq = request.newBuilder()
.addHeader("Authorization", format("token %s", githubToken))
.build();
return chain.proceed(newReq);
}).build();

builder.client(client);
}

return builder.build().create(RfEtestApi.class);
}
}
3 changes: 1 addition & 2 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,7 @@
<!-- chees finder-->
<string name="nothing_found">No cheeses found</string>
<string name="search">search</string>
<!-- rx zip-->
<string name="github_oauth_token">e4e7cedff598bca8000d164864a5826edf09572b</string>




Expand Down

0 comments on commit 62bbcea

Please sign in to comment.