Skip to content

Commit

Permalink
Dagger2 demo retrofit2 activity
Browse files Browse the repository at this point in the history
#22
- rxjava2 retrofit call
  • Loading branch information
eurosecom committed Jul 11, 2017
1 parent d0d5743 commit f625750
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 4 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
<activity android:name=".CompanyChooseActivity"/>
<activity android:name=".DaggerMainActivity"/>
<activity android:name=".DemoDaggerActivity"/>
<activity android:name=".DemoDaggerRxActivity"/>

<service android:name=".MyDownloadService" android:exported="false"/>
<!-- Android N SDK has new restrictions around sharing file:// URIs https://developer.android.com/reference/android/support/v4/content/FileProvider.html#ProviderDefinition -->
Expand Down
14 changes: 12 additions & 2 deletions app/src/main/java/com/eusecom/attendance/DemoDaggerActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.eusecom.attendance.models.Repository;
import com.eusecom.attendance.retrofit.GitHubApiInterface;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
Expand Down Expand Up @@ -56,7 +58,7 @@ public void onResponse(Call<ArrayList<Repository>> call, Response<ArrayList<Repo
Log.i("DEBUG", response.body().toString());
Log.i("NAME 0", response.body().get(0).getName());

Snackbar.make(view,"Retrieved 1 " + response.body().get(1).getName(), Snackbar.LENGTH_LONG)
Snackbar.make(view,"Retrieved 0 " + response.body().get(0).getName(), Snackbar.LENGTH_LONG)
.setAction("Action",null).show();


Expand All @@ -79,7 +81,7 @@ public void onFailure(Call<ArrayList<Repository>> call, Throwable t) {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
getMenuInflater().inflate(R.menu.menu_demodaggertwomain, menu);
return true;
}

Expand All @@ -95,6 +97,14 @@ public boolean onOptionsItemSelected(MenuItem item) {
return true;
}

if (id == R.id.action_rxretrofit) {

Intent is = new Intent(getApplicationContext(), DemoDaggerRxActivity.class);
startActivity(is);

return true;
}

return super.onOptionsItemSelected(item);
}
}
119 changes: 119 additions & 0 deletions app/src/main/java/com/eusecom/attendance/DemoDaggerRxActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package com.eusecom.attendance;

import com.eusecom.attendance.models.Repository;
import com.eusecom.attendance.retrofit.GitHubApiInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import org.reactivestreams.Subscription;
import java.util.List;
import javax.inject.Inject;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;

//by https://github.com/codepath/dagger2-example
//edited to retrofit2 and okhttp3


public class DemoDaggerRxActivity extends AppCompatActivity {

@Inject
SharedPreferences mSharedPreferences;

@Inject
Retrofit mRetrofit;

@Inject
GitHubApiInterface mGitHubApiInterface;

private Subscription mSubscription;
private Disposable searchDisposable;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.daggertworxactivity_main);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {

//public void onNext(List<Repository> repos)


searchDisposable = mGitHubApiInterface.getRxRepository("codepath")
.subscribeOn(Schedulers.computation())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<List<Repository>>() {
@Override
public void accept(List<Repository> result) {
Log.i("NAME 0", result.get(1).getName());
Snackbar.make(view,"Retrieved 1 " + result.get(1).getName(), Snackbar.LENGTH_LONG)
.setAction("Action",null).show();
}
});




}



});

((AttendanceApplication) getApplication()).getGitHubComponent().inject(this);
}

public void onDestroy() {
super.onDestroy();


searchDisposable.dispose();


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_demodaggertwomain, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

if (id == R.id.action_rxretrofit) {

Intent is = new Intent(getApplicationContext(), DemoDaggerRxActivity.class);
startActivity(is);

return true;
}

return super.onOptionsItemSelected(item);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.eusecom.attendance.dagger.components;

import com.eusecom.attendance.DemoDaggerActivity;
import com.eusecom.attendance.DemoDaggerRxActivity;
import com.eusecom.attendance.dagger.modules.GitHubModule;
import com.eusecom.attendance.dagger.scopes.UserScope;
import dagger.Component;
Expand All @@ -9,4 +10,5 @@
@Component(dependencies = NetComponent.class, modules = GitHubModule.class)
public interface GitHubComponent {
void inject(DemoDaggerActivity activity);
void inject(DemoDaggerRxActivity activity);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

import android.app.Application;
import android.content.SharedPreferences;
Expand Down Expand Up @@ -62,6 +63,7 @@ OkHttpClient provideOkHttpClient(Cache cache) {
Retrofit provideRetrofit(Gson gson, OkHttpClient okHttpClient) {
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.baseUrl(mBaseUrl)
.client(okHttpClient)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@

import com.eusecom.attendance.models.Repository;
import java.util.ArrayList;
import java.util.List;
import io.reactivex.Observable;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Path;
import retrofit2.http.Query;

//APIinterface for demo dagger2 retrofit

public interface GitHubApiInterface {

@GET("/users/{user}/repos")
Call<ArrayList<Repository>> getRepository(@Path("user") String userName);


@GET("/users/{user}/repos")
Observable<List<Repository>> getRxRepository(@Path("user") String userName);

}
17 changes: 17 additions & 0 deletions app/src/main/res/layout/daggertworxactivity_main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DemoDaggerRxActivity">


<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="16dp"/>

</RelativeLayout>
11 changes: 11 additions & 0 deletions app/src/main/res/menu/menu_demodaggertwomain.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".DemoDaggerActivity">

<item android:id="@+id/action_rxretrofit" android:title="@string/action_rxretrofit"
android:orderInCategory="100" app:showAsAction="never" />

<item android:id="@+id/action_settings" android:title="@string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />

</menu>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@
<string name="daggeradd2">Add item 2 to cart</string>
<string name="daggeradd3">Add item 3 to cart</string>
<string name="daggerclear">Clear cart</string>
<string name="action_rxretrofit">Rx Retrofit</string>


</resources>

0 comments on commit f625750

Please sign in to comment.