Last active
October 23, 2017 07:39
-
-
Save imminent/5222625 to your computer and use it in GitHub Desktop.
Dagger `ObjectGraph` utility to hide the mess of injecting the different Android classes. Note this is inspired by https://github.com/pyricau/CleanAndroidCode/blob/master/cleanandroidcode/src/main/java/info/piwai/cleanandroidcode/base/GraphRetriever.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.annotation.Nonnull; | |
import android.app.Activity; | |
import android.app.Service; | |
import android.support.v4.app.Fragment; | |
import android.content.Context; | |
/** | |
* <p>Retrieves the {@link dagger.ObjectGraph} and injects dependencies.</p> | |
* @author Dandré Allison | |
*/ | |
public final class ObjectGraph { | |
/** | |
* <p>An {@link android.app.Application} that wants to inject dependencies from an | |
* {@link dagger.ObjectGraph object graph} must implement {@link ObjectGraphApplication}.</p> | |
*/ | |
public interface ObjectGraphApplication { | |
/** | |
* <p>Injects dependencies into the given Object.</p> | |
*/ | |
@Nonnull void inject(Object dependent); | |
} | |
/** | |
* <p>Injects the dependencies for the given {@link Activity}.</p> | |
* @param activity The given activity | |
*/ | |
public static void inject(@Nonnull Activity activity) { | |
((ObjectGraphApplication) activity.getApplication()).inject(activity); | |
} | |
/** | |
* <p>Injects the dependencies for the given {@link Fragment}.</p> | |
* @param fragment The given fragment | |
*/ | |
public static void inject(@Nonnull Fragment fragment) { | |
final Activity activity = fragment.getActivity(); | |
if (activity == null) | |
throw new IllegalStateException("Attempting to get Activity before it has been attached to " | |
+ fragment.getClass().getName()); | |
((ObjectGraphApplication) activity.getApplication()).inject(fragment); | |
} | |
/** | |
* <p>Injects the dependencies for the given {@link Service}.</p> | |
* @param service The given service | |
*/ | |
public static void inject(@Nonnull Service service) { | |
((ObjectGraphApplication) service.getApplication()).inject(service); | |
} | |
/** | |
* <p>Injects the dependencies for the given {@link Object} from the given {@link Context}.</p> | |
* @param context The given context | |
* @param object The given object | |
*/ | |
public static void inject(@Nonnull Context context, @Nonnull Object object) { | |
((ObjectGraphApplication) context.getApplicationContext()).inject(object); | |
} | |
/* Private Constructor */ | |
/** Blocks instantiation of the {@link ObjectGraph} class. */ | |
private ObjectGraph() { } | |
} |
I have simplified setting up the basic injection pattern in Android using a library I made called Hilt.
Now all you have to do is extend a couple classes, and set up your own modules and place them in the getActivityModules()
methods in your subclass of HiltActivity
and HiltApplication
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@nimeacuerdo it is simply a convenience method that removes the "how" from the "what". When you're in a class that has a reference to the
Context
and needs it dependencies injected, you will be calling(ObjectGraphApplication) context.getApplicationContext()
to get to theObjectGraphApplication
, so I extracted it out into the method. TheContext
passed doesn't have to be anObjectGraphApplication
in the way the method is implemented, merely aContext
that can get to theObjectGraphApplication
.