-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
A: 3 - Created the App, BaseActivity, BaseFragment, and custom PerActivity, PerFragment, and PerChildFragment Scopes. Closes #3 #20
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright 2017 Vandolf Estrellado | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.vestrel00.daggerbutterknifemvp; | ||
|
||
import android.app.Activity; | ||
import android.app.Application; | ||
|
||
import javax.inject.Inject; | ||
|
||
import dagger.android.AndroidInjector; | ||
import dagger.android.DispatchingAndroidInjector; | ||
import dagger.android.HasActivityInjector; | ||
|
||
/** | ||
* The Android {@link Application}. | ||
*/ | ||
public class App extends Application implements HasActivityInjector { | ||
|
||
@Inject | ||
DispatchingAndroidInjector<Activity> activityInjector; | ||
|
||
@Override | ||
public void onCreate() { | ||
super.onCreate(); | ||
DaggerAppComponent.create().inject(this); | ||
} | ||
|
||
@Override | ||
public AndroidInjector<Activity> activityInjector() { | ||
return activityInjector; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright 2017 Vandolf Estrellado | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.vestrel00.daggerbutterknifemvp; | ||
|
||
import javax.inject.Singleton; | ||
|
||
import dagger.Component; | ||
|
||
/** | ||
* Injects application dependencies. | ||
*/ | ||
@Singleton | ||
@Component(modules = AppModule.class) | ||
interface AppComponent { | ||
void inject(App app); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright 2017 Vandolf Estrellado | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.vestrel00.daggerbutterknifemvp; | ||
|
||
import dagger.Module; | ||
import dagger.android.AndroidInjectionModule; | ||
|
||
/** | ||
* Provides application-wide dependencies. | ||
*/ | ||
@Module(includes = AndroidInjectionModule.class) | ||
abstract class AppModule { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright 2017 Vandolf Estrellado | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.vestrel00.daggerbutterknifemvp.inject; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
|
||
import javax.inject.Scope; | ||
|
||
/** | ||
* A custom scoping annotation that specifies that the lifespan of a dependency be the same as that | ||
* of an Activity. | ||
* | ||
* This is used to annotate dependencies that behave like a singleton within the lifespan of an | ||
* Activity, Fragment, and child Fragments instead of the entire Application. | ||
*/ | ||
@Scope | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface PerActivity { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright 2017 Vandolf Estrellado | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.vestrel00.daggerbutterknifemvp.inject; | ||
|
||
import android.app.Fragment; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
|
||
import javax.inject.Scope; | ||
|
||
/** | ||
* A custom scoping annotation that specifies that the lifespan of a dependency be the same as that | ||
* of a child Fragment (a fragment inside a fragment that is added using | ||
* {@link Fragment#getChildFragmentManager()}). | ||
* <p> | ||
* This is used to annotate dependencies that behave like a singleton within the lifespan of a | ||
* child Fragment instead of the entire Application, Activity, or parent Fragment. | ||
* <p> | ||
* Note that this does not support a child fragment within a child fragment as conflicting scopes | ||
* will occur. Child fragments within child fragments should usually be avoided. However, if | ||
* another level of child fragment is required, then another scope would need to be created | ||
* (perhaps PerGrandChild custom scope annotation). | ||
*/ | ||
@Scope | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface PerChildFragment { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright 2017 Vandolf Estrellado | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.vestrel00.daggerbutterknifemvp.inject; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
|
||
import javax.inject.Scope; | ||
|
||
/** | ||
* A custom scoping annotation that specifies that the lifespan of a dependency be the same as that | ||
* of a Fragment. | ||
* | ||
* This is used to annotate dependencies that behave like a singleton within the lifespan of a | ||
* Fragment and child Fragments instead of the entire Application or Activity. | ||
*/ | ||
@Scope | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface PerFragment { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright 2017 Vandolf Estrellado | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/** | ||
* Contains the different custom scopes used. | ||
* <p> | ||
* Note that the standard {@link javax.inject.Singleton} scope is used as the application scope | ||
* (there is no PerApplication scope annotation). This is done in order to support singleton | ||
* scoped classes from other libraries (which do not have access to this project's custom scopes). | ||
*/ | ||
package com.vestrel00.daggerbutterknifemvp.inject; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* Copyright 2017 Vandolf Estrellado | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/** | ||
* Contains the {@link android.app.Application}. | ||
*/ | ||
package com.vestrel00.daggerbutterknifemvp; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright 2017 Vandolf Estrellado | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
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.Nullable; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Named; | ||
|
||
import dagger.android.AndroidInjection; | ||
import dagger.android.AndroidInjector; | ||
import dagger.android.DispatchingAndroidInjector; | ||
import dagger.android.HasFragmentInjector; | ||
|
||
/** | ||
* 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 | ||
* 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 { | ||
|
||
@Inject | ||
@Named(BaseActivityModule.ACTIVITY_FRAGMENT_MANAGER) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why use
|
||
protected FragmentManager fragmentManager; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "Why is FragmentManager injected into BaseActivity? Why not just use getFragmentManager() method?" See #52 |
||
|
||
@Inject | ||
DispatchingAndroidInjector<Fragment> fragmentInjector; | ||
|
||
@Override | ||
protected void onCreate(@Nullable Bundle savedInstanceState) { | ||
AndroidInjection.inject(this); | ||
super.onCreate(savedInstanceState); | ||
} | ||
|
||
@Override | ||
public final AndroidInjector<Fragment> fragmentInjector() { | ||
return fragmentInjector; | ||
} | ||
|
||
protected final void addFragment(int containerViewId, Fragment fragment) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can annotate it with (@idres id containerViewId,...) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice catch! Thanks. Fixed: 88f9f5b I also added a quick tip about the support annotations in the article. |
||
fragmentManager.beginTransaction() | ||
.add(containerViewId, fragment) | ||
.commit(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.vestrel00.daggerbutterknifemvp.ui.common; | ||
|
||
import android.app.Activity; | ||
import android.app.FragmentManager; | ||
import android.content.Context; | ||
|
||
import com.vestrel00.daggerbutterknifemvp.inject.PerActivity; | ||
|
||
import javax.inject.Named; | ||
|
||
import dagger.Binds; | ||
import dagger.Module; | ||
import dagger.Provides; | ||
|
||
/** | ||
* Provides base activity dependencies. This must be included in all activity modules, which must | ||
* provide a concrete implementation of {@link Activity}. | ||
*/ | ||
@Module | ||
public abstract class BaseActivityModule { | ||
|
||
static final String ACTIVITY_FRAGMENT_MANAGER = "BaseActivityModule.activityFragmentManager"; | ||
|
||
@Binds | ||
@PerActivity | ||
abstract Context activityContext(Activity activity); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Scoping the The same thing applies to However, using scope annotations in these cases makes the module easier to read. We wouldn’t have to look at what is being provided in order to understand its scope. I choose readability here over (negligible) “performance/optimization”. |
||
|
||
@Provides | ||
@Named(ACTIVITY_FRAGMENT_MANAGER) | ||
@PerActivity | ||
static FragmentManager activityFragmentManager(Activity activity) { | ||
return activity.getFragmentManager(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing documentation: