diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 1bd09e2..72a61b6 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -53,6 +53,7 @@ + diff --git a/app/src/main/java/com/eusecom/attendance/DemoDaggerActivity.java b/app/src/main/java/com/eusecom/attendance/DemoDaggerActivity.java index 3554c71..35c9840 100644 --- a/app/src/main/java/com/eusecom/attendance/DemoDaggerActivity.java +++ b/app/src/main/java/com/eusecom/attendance/DemoDaggerActivity.java @@ -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; @@ -56,7 +58,7 @@ public void onResponse(Call> call, Response> 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; } @@ -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); } } diff --git a/app/src/main/java/com/eusecom/attendance/DemoDaggerRxActivity.java b/app/src/main/java/com/eusecom/attendance/DemoDaggerRxActivity.java new file mode 100644 index 0000000..bf8afc2 --- /dev/null +++ b/app/src/main/java/com/eusecom/attendance/DemoDaggerRxActivity.java @@ -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 repos) + + + searchDisposable = mGitHubApiInterface.getRxRepository("codepath") + .subscribeOn(Schedulers.computation()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(new Consumer>() { + @Override + public void accept(List 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); + } +} diff --git a/app/src/main/java/com/eusecom/attendance/dagger/components/GitHubComponent.java b/app/src/main/java/com/eusecom/attendance/dagger/components/GitHubComponent.java index f1f7655..5fef340 100644 --- a/app/src/main/java/com/eusecom/attendance/dagger/components/GitHubComponent.java +++ b/app/src/main/java/com/eusecom/attendance/dagger/components/GitHubComponent.java @@ -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; @@ -9,4 +10,5 @@ @Component(dependencies = NetComponent.class, modules = GitHubModule.class) public interface GitHubComponent { void inject(DemoDaggerActivity activity); + void inject(DemoDaggerRxActivity activity); } diff --git a/app/src/main/java/com/eusecom/attendance/dagger/modules/NetModule.java b/app/src/main/java/com/eusecom/attendance/dagger/modules/NetModule.java index 441fdb1..b5bcabd 100644 --- a/app/src/main/java/com/eusecom/attendance/dagger/modules/NetModule.java +++ b/app/src/main/java/com/eusecom/attendance/dagger/modules/NetModule.java @@ -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; @@ -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(); diff --git a/app/src/main/java/com/eusecom/attendance/retrofit/GitHubApiInterface.java b/app/src/main/java/com/eusecom/attendance/retrofit/GitHubApiInterface.java index f2912c1..b14e14f 100644 --- a/app/src/main/java/com/eusecom/attendance/retrofit/GitHubApiInterface.java +++ b/app/src/main/java/com/eusecom/attendance/retrofit/GitHubApiInterface.java @@ -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> getRepository(@Path("user") String userName); + + @GET("/users/{user}/repos") + Observable> getRxRepository(@Path("user") String userName); + } \ No newline at end of file diff --git a/app/src/main/res/layout/daggertworxactivity_main.xml b/app/src/main/res/layout/daggertworxactivity_main.xml new file mode 100644 index 0000000..58e352d --- /dev/null +++ b/app/src/main/res/layout/daggertworxactivity_main.xml @@ -0,0 +1,17 @@ + + + + + + + diff --git a/app/src/main/res/menu/menu_demodaggertwomain.xml b/app/src/main/res/menu/menu_demodaggertwomain.xml new file mode 100644 index 0000000..611f11b --- /dev/null +++ b/app/src/main/res/menu/menu_demodaggertwomain.xml @@ -0,0 +1,11 @@ + + + + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f733df7..530114d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -273,6 +273,7 @@ Add item 2 to cart Add item 3 to cart Clear cart + Rx Retrofit