morristech
2/7/2018 - 7:56 AM

Injector.java

public final class Injector {

    private static final String INJECTOR_SERVICE = "ACTIVITY_COMPONENT";
    private static final String INJECTOR_SERVICE_APP = "INJECTOR_SERVICE_APP";

    private Injector() {
    }

    public static AppComponent obtainAppComponent(Context context) {
        //noinspection ResourceType
        return (AppComponent) context.getApplicationContext().getSystemService(INJECTOR_SERVICE_APP);
    }

    public static ActivityComponent obtain(Context context) {
        //noinspection ResourceType
        return (ActivityComponent) context.getSystemService(INJECTOR_SERVICE);
    }

    public static boolean matchesService(String name) {
        return INJECTOR_SERVICE.equals(name);
    }

    public static boolean matchesAppService(String name) {
        return INJECTOR_SERVICE_APP.equals(name);
    }

    public static ActivityComponent create(Activity activity) {
        return  Injector.obtainAppComponent(activity)
                .plusActivityComponent(new ActivityModule(activity));
    }
}