Skip to content

Commit

Permalink
Using the dagger.android.support lib for compatibility with AppCompat…
Browse files Browse the repository at this point in the history
…Activity and support Fragment with minSdkVersion changed from 17 to 14. Closes #48
  • Loading branch information
vestrel00 committed Aug 14, 2017
1 parent 11c97a7 commit 12253ce
Show file tree
Hide file tree
Showing 21 changed files with 63 additions and 63 deletions.
7 changes: 5 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ android {
versionCode 1
versionName '1.0.0'

minSdkVersion 17
minSdkVersion 14
targetSdkVersion 26

testInstrumentationRunner = "android.support.test.runner.AndroidJUnitRunner"
Expand All @@ -37,12 +37,15 @@ dependencies {
// However, that is a different topic.
def daggerVersion = '2.11'
def butterKnifeVersion = '8.7.0'
def supportVersion = '26.0.1'

annotationProcessor "com.google.dagger:dagger-compiler:$daggerVersion"
annotationProcessor "com.google.dagger:dagger-android-processor:$daggerVersion"
annotationProcessor "com.jakewharton:butterknife-compiler:$butterKnifeVersion"

compile "com.google.dagger:dagger:$daggerVersion"
compile "com.google.dagger:dagger-android:$daggerVersion"
compile "com.google.dagger:dagger-android-support:$daggerVersion"
compile "com.jakewharton:butterknife:$butterKnifeVersion"
}
compile "com.android.support:appcompat-v7:$supportVersion"
}
5 changes: 4 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
<application
android:name=".App"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name">
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat">

<!-- Theme.AppCompat (or descendant) is required for AppCompatActivity -->

<activity android:name=".ui.main.MainActivity">
<intent-filter>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* The Android {@link Application}.
* <p>
* <b>DEPENDENCY INJECTION</b>
* We could extend {@link dagger.android.DaggerApplication} so we can get the boilerplate
* We could extend {@link dagger.android.support.DaggerApplication} so we can get the boilerplate
* dagger code for free. However, we want to avoid inheritance (if possible and it is in this case)
* so that we have to option to inherit from something else later on if needed
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@

import dagger.Binds;
import dagger.Module;
import dagger.android.AndroidInjectionModule;
import dagger.android.ContributesAndroidInjector;
import dagger.android.support.AndroidSupportInjectionModule;

/**
* Provides application-wide dependencies.
*/
@Module(includes = AndroidInjectionModule.class)
@Module(includes = AndroidSupportInjectionModule.class)
abstract class AppModule {

@Binds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

package com.vestrel00.daggerbutterknifemvp.ui.common;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;

import com.vestrel00.daggerbutterknifemvp.navigation.Navigator;

Expand All @@ -31,17 +31,17 @@
import dagger.android.AndroidInjection;
import dagger.android.AndroidInjector;
import dagger.android.DispatchingAndroidInjector;
import dagger.android.HasFragmentInjector;
import dagger.android.support.HasSupportFragmentInjector;

/**
* Abstract Activity for all Activities to extend.
* <p>
* <b>DEPENDENCY INJECTION</b>
* We could extend {@link dagger.android.DaggerActivity} so we can get the boilerplate
* We could extend {@link dagger.android.support.DaggerAppCompatActivity} so we can get the boilerplate
* dagger code for free. However, we want to avoid inheritance (if possible and it is in this case)
* so that we have to option to inherit from something else later on if needed.
*/
public abstract class BaseActivity extends Activity implements HasFragmentInjector {
public abstract class BaseActivity extends AppCompatActivity implements HasSupportFragmentInjector {

@Inject
protected Navigator navigator;
Expand All @@ -60,7 +60,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
}

@Override
public final AndroidInjector<Fragment> fragmentInjector() {
public final AndroidInjector<Fragment> supportFragmentInjector() {
return fragmentInjector;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.vestrel00.daggerbutterknifemvp.ui.common;

import android.app.Activity;
import android.app.FragmentManager;
import android.content.Context;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;

import com.vestrel00.daggerbutterknifemvp.inject.PerActivity;

Expand All @@ -14,7 +15,7 @@

/**
* Provides base activity dependencies. This must be included in all activity modules, which must
* provide a concrete implementation of {@link Activity}.
* provide a concrete implementation of {@link AppCompatActivity}.
*/
@Module
public abstract class BaseActivityModule {
Expand All @@ -32,12 +33,16 @@ public abstract class BaseActivityModule {
* However, having a scope annotation makes the module easier to read. We wouldn't have to look
* at what is being provided in order to understand its scope.
*/
abstract Activity activity(AppCompatActivity appCompatActivity);

@Binds
@PerActivity
abstract Context activityContext(Activity activity);

@Provides
@Named(ACTIVITY_FRAGMENT_MANAGER)
@PerActivity
static FragmentManager activityFragmentManager(Activity activity) {
return activity.getFragmentManager();
static FragmentManager activityFragmentManager(AppCompatActivity activity) {
return activity.getSupportFragmentManager();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,36 @@

package com.vestrel00.daggerbutterknifemvp.ui.common.view;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;

import javax.inject.Inject;
import javax.inject.Named;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import dagger.android.AndroidInjection;
import dagger.android.AndroidInjector;
import dagger.android.DispatchingAndroidInjector;
import dagger.android.HasFragmentInjector;
import dagger.android.support.AndroidSupportInjection;
import dagger.android.support.HasSupportFragmentInjector;

/**
* Abstract Fragment for all Fragments and child Fragments to extend. This contains some boilerplate
* dependency injection code and activity {@link Context}.
* <p>
* <b>DEPENDENCY INJECTION</b>
* We could extend {@link dagger.android.DaggerFragment} so we can get the boilerplate
* We could extend {@link dagger.android.support.DaggerFragment} so we can get the boilerplate
* dagger code for free. However, we want to avoid inheritance (if possible and it is in this case)
* so that we have to option to inherit from something else later on if needed.
* <p>
* <b>VIEW BINDING</b>
* This fragment handles view bind and unbinding.
*/
public abstract class BaseFragment extends Fragment implements HasFragmentInjector {
public abstract class BaseFragment extends Fragment implements HasSupportFragmentInjector {

@Inject
protected Context activityContext;
Expand All @@ -63,23 +61,10 @@ public abstract class BaseFragment extends Fragment implements HasFragmentInject
@Nullable
private Unbinder viewUnbinder;

@SuppressWarnings("deprecation")
@Override
public void onAttach(Activity activity) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
// Perform injection here before M, L (API 22) and below because onAttach(Context)
// is not yet available at L.
AndroidInjection.inject(this);
}
super.onAttach(activity);
}

@Override
public void onAttach(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// Perform injection here for M (API 23) due to deprecation of onAttach(Activity).
AndroidInjection.inject(this);
}
// This is called even for API levels below 17.
AndroidSupportInjection.inject(this);
super.onAttach(context);
}

Expand Down Expand Up @@ -130,7 +115,7 @@ public void onDestroyView() {
}

@Override
public final AndroidInjector<Fragment> fragmentInjector() {
public final AndroidInjector<Fragment> supportFragmentInjector() {
return childFragmentInjector;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

package com.vestrel00.daggerbutterknifemvp.ui.common.view;

import android.app.Fragment;
import android.app.FragmentManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;

import com.vestrel00.daggerbutterknifemvp.inject.PerFragment;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.vestrel00.daggerbutterknifemvp.ui.example_1;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;

import com.vestrel00.daggerbutterknifemvp.inject.PerActivity;
import com.vestrel00.daggerbutterknifemvp.inject.PerFragment;
Expand Down Expand Up @@ -44,7 +44,7 @@ public abstract class Example1ActivityModule {

/**
* As per the contract specified in {@link BaseActivityModule}; "This must be included in all
* activity modules, which must rovide a concrete implementation of {@link Activity}."
* activity modules, which must rovide a concrete implementation of {@link AppCompatActivity}."
* <p>
* This provides the activity required to inject the
* {@link BaseActivityModule#ACTIVITY_FRAGMENT_MANAGER} into the
Expand All @@ -55,5 +55,5 @@ public abstract class Example1ActivityModule {
*/
@Binds
@PerActivity
abstract Activity activity(Example1Activity example1Activity);
abstract AppCompatActivity appCompatActivity(Example1Activity example1Activity);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.vestrel00.daggerbutterknifemvp.ui.example_1.fragment.view;

import android.app.Fragment;
import android.support.v4.app.Fragment;

import com.vestrel00.daggerbutterknifemvp.inject.PerFragment;
import com.vestrel00.daggerbutterknifemvp.ui.common.view.BaseFragmentModule;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.vestrel00.daggerbutterknifemvp.ui.example_2;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;

import com.vestrel00.daggerbutterknifemvp.inject.PerActivity;
import com.vestrel00.daggerbutterknifemvp.inject.PerFragment;
Expand Down Expand Up @@ -54,7 +54,7 @@ public abstract class Example2ActivityModule {

/**
* As per the contract specified in {@link BaseActivityModule}; "This must be included in all
* activity modules, which must rovide a concrete implementation of {@link Activity}."
* activity modules, which must rovide a concrete implementation of {@link AppCompatActivity}."
* <p>
* This provides the activity required to inject the
* {@link BaseActivityModule#ACTIVITY_FRAGMENT_MANAGER} into the
Expand All @@ -65,5 +65,5 @@ public abstract class Example2ActivityModule {
*/
@Binds
@PerActivity
abstract Activity activity(Example2Activity example2Activity);
abstract AppCompatActivity appCompatActivity(Example2Activity example2Activity);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.vestrel00.daggerbutterknifemvp.ui.example_2.fragment_a.view;

import android.app.Fragment;
import android.support.v4.app.Fragment;

import com.vestrel00.daggerbutterknifemvp.inject.PerFragment;
import com.vestrel00.daggerbutterknifemvp.ui.common.view.BaseFragmentModule;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.vestrel00.daggerbutterknifemvp.ui.example_2.fragment_b.view;

import android.app.Fragment;
import android.support.v4.app.Fragment;

import com.vestrel00.daggerbutterknifemvp.inject.PerFragment;
import com.vestrel00.daggerbutterknifemvp.ui.common.view.BaseFragmentModule;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.vestrel00.daggerbutterknifemvp.ui.example_3;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;

import com.vestrel00.daggerbutterknifemvp.inject.PerActivity;
import com.vestrel00.daggerbutterknifemvp.inject.PerFragment;
Expand Down Expand Up @@ -44,7 +44,7 @@ public abstract class Example3ActivityModule {

/**
* As per the contract specified in {@link BaseActivityModule}; "This must be included in all
* activity modules, which must rovide a concrete implementation of {@link Activity}."
* activity modules, which must rovide a concrete implementation of {@link AppCompatActivity}."
* <p>
* This provides the activity required to inject the
* {@link BaseActivityModule#ACTIVITY_FRAGMENT_MANAGER} into the
Expand All @@ -55,5 +55,5 @@ public abstract class Example3ActivityModule {
*/
@Binds
@PerActivity
abstract Activity activity(Example3Activity example3Activity);
abstract AppCompatActivity appCompatActivity(Example3Activity example3Activity);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.vestrel00.daggerbutterknifemvp.ui.example_3.child_fragment.view;

import android.app.Fragment;
import android.support.v4.app.Fragment;

import com.vestrel00.daggerbutterknifemvp.inject.PerChildFragment;
import com.vestrel00.daggerbutterknifemvp.ui.common.view.BaseChildFragmentModule;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.vestrel00.daggerbutterknifemvp.ui.example_3.parent_fragment.view;

import android.app.Fragment;
import android.support.v4.app.Fragment;

import com.vestrel00.daggerbutterknifemvp.inject.PerChildFragment;
import com.vestrel00.daggerbutterknifemvp.inject.PerFragment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.vestrel00.daggerbutterknifemvp.ui.main;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;

import com.vestrel00.daggerbutterknifemvp.inject.PerActivity;
import com.vestrel00.daggerbutterknifemvp.inject.PerFragment;
Expand Down Expand Up @@ -45,7 +45,7 @@ public abstract class MainActivityModule {

/**
* As per the contract specified in {@link BaseActivityModule}; "This must be included in all
* activity modules, which must rovide a concrete implementation of {@link Activity}."
* activity modules, which must rovide a concrete implementation of {@link AppCompatActivity}."
* <p>
* This provides the activity required to inject the
* {@link BaseActivityModule#ACTIVITY_FRAGMENT_MANAGER} into the
Expand All @@ -56,7 +56,7 @@ public abstract class MainActivityModule {
*/
@Binds
@PerActivity
abstract Activity activity(MainActivity mainActivity);
abstract AppCompatActivity appCompatActivity(MainActivity mainActivity);

/**
* The main activity listens to the events in the {@link MainFragment}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.vestrel00.daggerbutterknifemvp.ui.main.view;

import android.app.Fragment;
import android.support.v4.app.Fragment;

import com.vestrel00.daggerbutterknifemvp.inject.PerFragment;
import com.vestrel00.daggerbutterknifemvp.ui.common.view.BaseFragmentModule;
Expand Down
Loading

0 comments on commit 12253ce

Please sign in to comment.