Skip to content
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

move to scopes #5

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
import butterknife.ButterKnife;
import com.example.smoothie.deps.ContextNamer;
import javax.inject.Inject;
import toothpick.Injector;
import toothpick.Scope;
import toothpick.ToothPick;
import toothpick.smoothie.module.DefaultActivityModule;
import toothpick.smoothie.module.ActivityModule;

public class LessSimpleActivity extends Activity {

Expand All @@ -24,7 +24,7 @@ public class LessSimpleActivity extends Activity {
@Inject AlarmManager alarmManager;
@Inject FragmentManager fragmentManager;
@Inject Activity activity;
private Injector injector;
private Scope scope;

@Inject ContextNamer contextNamer;
@Bind(R.id.title) TextView title;
Expand All @@ -33,11 +33,10 @@ public class LessSimpleActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Injector appInjector = ToothPick.openInjector(getApplication());
injector = ToothPick.openInjector(this);
injector.installModules(new DefaultActivityModule(this));
appInjector.addChild(injector);
injector.inject(this);
scope = ToothPick.openScopes(getApplication(), this);
scope.installModules(new ActivityModule(this));
scope.installModules(new ActivityModule(this));
ToothPick.inject(this, scope);
setContentView(R.layout.simple_activity);
ButterKnife.bind(this);
title.setText(contextNamer.getApplicationName());
Expand All @@ -46,7 +45,7 @@ protected void onCreate(Bundle savedInstanceState) {

@Override
protected void onDestroy() {
ToothPick.closeInjector(this);
ToothPick.closeScope(this);
super.onDestroy();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
import butterknife.ButterKnife;
import com.example.smoothie.deps.ContextNamer;
import javax.inject.Inject;
import toothpick.Injector;
import toothpick.Scope;
import toothpick.ToothPick;
import toothpick.config.Module;
import toothpick.smoothie.module.DefaultActivityModule;
import toothpick.smoothie.module.ActivityModule;

public class PersistActivity extends Activity {

public static final String PRESENTER_SCOPE = "PRESENTER_SCOPE";
private Injector injector;
private Scope scope;

@Inject ContextNamer contextNamer;
@Bind(R.id.title) TextView title;
Expand All @@ -27,14 +27,12 @@ public class PersistActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Injector appInjector = ToothPick.openInjector(getApplication());
Injector metaInjector = ToothPick.openInjector(PRESENTER_SCOPE);
appInjector.addChild(metaInjector);
injector = ToothPick.openInjector(this);
metaInjector.addChild(injector);
injector.installModules(new DefaultActivityModule(this));
injector.inject(this);
metaInjector.installModules(new PresenterModule());

scope = ToothPick.openScopes(getApplication(), PRESENTER_SCOPE, this);
scope.installModules(new ActivityModule(this));
ToothPick.inject(this, scope);
ToothPick.openScope(PRESENTER_SCOPE).installModules(new PresenterModule());

setContentView(R.layout.simple_activity);
ButterKnife.bind(this);
title.setText("Persist");
Expand All @@ -44,13 +42,15 @@ protected void onCreate(Bundle savedInstanceState) {

@Override
protected void onDestroy() {
ToothPick.closeInjector(this);
ToothPick.closeScope(this);
super.onDestroy();
}

@Override
public void onBackPressed() {
ToothPick.closeInjector(PRESENTER_SCOPE);
//when we leave the presenter flow,
//we close its scope
ToothPick.closeScope(PRESENTER_SCOPE);
super.onBackPressed();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
import butterknife.OnClick;
import com.example.smoothie.deps.ContextNamer;
import javax.inject.Inject;
import toothpick.Injector;
import toothpick.Scope;
import toothpick.ToothPick;
import toothpick.smoothie.module.DefaultActivityModule;
import toothpick.smoothie.module.ActivityModule;

public class SimpleActivity extends Activity {

private Injector injector;
private Scope scope;

@Inject ContextNamer contextNamer;
@Bind(R.id.title) TextView title;
Expand All @@ -26,11 +26,9 @@ public class SimpleActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Injector appInjector = ToothPick.openInjector(getApplication());
injector = ToothPick.openInjector(this);
injector.installModules(new DefaultActivityModule(this));
appInjector.addChild(injector);
injector.inject(this);
scope = ToothPick.openScopes(getApplication(), this);
scope.installModules(new ActivityModule(this));
ToothPick.inject(this, scope);
setContentView(R.layout.simple_activity);
ButterKnife.bind(this);
title.setText(contextNamer.getApplicationName());
Expand All @@ -46,7 +44,7 @@ void startNewActivity() {

@Override
protected void onDestroy() {
ToothPick.closeInjector(this);
ToothPick.closeScope(this);
super.onDestroy();
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.example.smoothie;

import android.app.Application;
import toothpick.Injector;
import toothpick.Scope;
import toothpick.ToothPick;
import toothpick.smoothie.module.DefaultApplicationModule;
import toothpick.smoothie.module.ApplicationModule;

public class SimpleApp extends Application {
@Override
public void onCreate() {
super.onCreate();
Injector appInjector = ToothPick.openInjector(this);
appInjector.installModules(new DefaultApplicationModule(this));
Scope appScope = ToothPick.openScope(this);
appScope.installModules(new ApplicationModule(this));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ public class ContextNamer {

public ContextNamer() {
countInstances++;
try {
throw new RuntimeException();
} catch (RuntimeException e) {
e.printStackTrace();
}
}

public String getApplicationName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.util.ActivityController;
import toothpick.Injector;
import toothpick.Scope;
import toothpick.ToothPick;
import toothpick.config.Module;

Expand All @@ -29,8 +29,8 @@ public void verifyInjectionAtOnCreate() {
expect(mockContextNamer.getActivityName()).andReturn("bar");
ActivityController<SimpleActivity> controllerSimpleActivity = Robolectric.buildActivity(SimpleActivity.class);
SimpleActivity activity = controllerSimpleActivity.get();
Injector injector = ToothPick.openInjector(activity);
injector.installTestModules(new TestModule(mockContextNamer));
Scope scope = ToothPick.openScope(activity);
scope.installTestModules(new TestModule(mockContextNamer));
replay(mockContextNamer);

//WHEN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import toothpick.smoothie.provider.FragmentManagerProvider;
import toothpick.smoothie.provider.LoaderManagerProvider;

public class DefaultActivityModule extends Module {
public DefaultActivityModule(Activity activity) {
public class ActivityModule extends Module {
public ActivityModule(Activity activity) {
bind(Activity.class).to(activity);
bind(FragmentManager.class).toProvider(FragmentManagerProvider.class);
bind(LoaderManager.class).toProvider(LoaderManagerProvider.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
import static android.content.Context.VIBRATOR_SERVICE;
import static android.content.Context.WINDOW_SERVICE;

public class DefaultApplicationModule extends Module {
public DefaultApplicationModule(Application application) {
public class ApplicationModule extends Module {
public ApplicationModule(Application application) {
bind(Application.class).to(application);
bind(AccountManager.class).toProvider(AccountManagerProvider.class);
bind(AssetManager.class).toProvider(AssetManagerProvider.class);
Expand All @@ -61,23 +61,27 @@ public DefaultApplicationModule(Application application) {

// TODO check min sdk and refactor
private void bindSystemServices(Application application) {
bind(LocationManager.class).toProvider(new SystemServiceProvider<LocationManager>(application, LOCATION_SERVICE));
bind(WindowManager.class).toProvider(new SystemServiceProvider<WindowManager>(application, WINDOW_SERVICE));
bind(ActivityManager.class).toProvider(new SystemServiceProvider<ActivityManager>(application, ACTIVITY_SERVICE));
bind(PowerManager.class).toProvider(new SystemServiceProvider<PowerManager>(application, POWER_SERVICE));
bind(AlarmManager.class).toProvider(new SystemServiceProvider<AlarmManager>(application, ALARM_SERVICE));
bind(NotificationManager.class).toProvider(new SystemServiceProvider<NotificationManager>(application, NOTIFICATION_SERVICE));
bind(KeyguardManager.class).toProvider(new SystemServiceProvider<KeyguardManager>(application, KEYGUARD_SERVICE));
bind(Vibrator.class).toProvider(new SystemServiceProvider<Vibrator>(application, VIBRATOR_SERVICE));
bind(ConnectivityManager.class).toProvider(new SystemServiceProvider<ConnectivityManager>(application, CONNECTIVITY_SERVICE));
bind(WifiManager.class).toProvider(new SystemServiceProvider<WifiManager>(application, WINDOW_SERVICE));
bind(InputMethodManager.class).toProvider(new SystemServiceProvider<InputMethodManager>(application, INPUT_METHOD_SERVICE));
bind(SensorManager.class).toProvider(new SystemServiceProvider<SensorManager>(application, SENSOR_SERVICE));
bind(TelephonyManager.class).toProvider(new SystemServiceProvider<TelephonyManager>(application, TELEPHONY_SERVICE));
bind(AudioManager.class).toProvider(new SystemServiceProvider<AudioManager>(application, AUDIO_SERVICE));
bindSystemService(application, LocationManager.class, LOCATION_SERVICE);
bindSystemService(application, WindowManager.class, WINDOW_SERVICE);
bindSystemService(application, ActivityManager.class, ACTIVITY_SERVICE);
bindSystemService(application, PowerManager.class, POWER_SERVICE);
bindSystemService(application, AlarmManager.class, ALARM_SERVICE);
bindSystemService(application, NotificationManager.class, NOTIFICATION_SERVICE);
bindSystemService(application, KeyguardManager.class, KEYGUARD_SERVICE);
bindSystemService(application, Vibrator.class, VIBRATOR_SERVICE);
bindSystemService(application, ConnectivityManager.class, CONNECTIVITY_SERVICE);
bindSystemService(application, WifiManager.class, WINDOW_SERVICE);
bindSystemService(application, InputMethodManager.class, INPUT_METHOD_SERVICE);
bindSystemService(application, SensorManager.class, SENSOR_SERVICE);
bindSystemService(application, TelephonyManager.class, TELEPHONY_SERVICE);
bindSystemService(application, AudioManager.class, AUDIO_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
bind(DownloadManager.class).toProvider(new SystemServiceProvider<DownloadManager>(application, DOWNLOAD_SERVICE));
bindSystemService(application, DownloadManager.class, DOWNLOAD_SERVICE);
}
bind(Application.class).to(application);
}

private <T> void bindSystemService(Application application, Class<T> serviceClass, String serviceName) {
bind(serviceClass).toProvider(new SystemServiceProvider<T>(application, serviceName));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import toothpick.smoothie.provider.SupportFragmentManagerProvider;
import toothpick.smoothie.provider.SupportLoaderManagerProvider;

public class DefaultSupportActivityModule extends Module {
public DefaultSupportActivityModule(FragmentActivity activity) {
public class SupportActivityModule extends Module {
public SupportActivityModule(FragmentActivity activity) {
bind(Activity.class).to(activity);
bind(FragmentManager.class).toProvider(SupportFragmentManagerProvider.class);
bind(LoaderManager.class).toProvider(SupportLoaderManagerProvider.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public abstract class ToothpickProcessor extends AbstractProcessor {
public static final String PARAMETER_REGISTRY_PACKAGE_NAME = "toothpick_registry_package_name";

/**
* The name of the annotation processor option to exclude classes from the creation of member injectors & factories.
* The name of the annotation processor option to exclude classes from the creation of member scopes & factories.
*/
public static final String PARAMETER_EXCLUDES = "toothpick_excludes";

Expand Down Expand Up @@ -161,7 +161,7 @@ protected boolean isValidInjectField(VariableElement fieldElement) {
valid = false;
}

// Verify parentInjector modifiers.
// Verify parentScope modifiers.
Set<Modifier> parentModifiers = enclosingElement.getModifiers();
//TODO should not be a non static inner class neither
if (parentModifiers.contains(PRIVATE)) {
Expand All @@ -184,7 +184,7 @@ protected boolean isValidInjectMethod(Element methodElement) {
valid = false;
}

// Verify parentInjector modifiers.
// Verify parentScope modifiers.
Set<Modifier> parentModifiers = enclosingElement.getModifiers();
//TODO should not be a non static inner class neither
if (parentModifiers.contains(PRIVATE)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ private boolean isValidInjectConstructor(Element element) {
valid = false;
}

// Verify parentInjector modifiers.
// Verify parentScope modifiers.
Set<Modifier> parentModifiers = enclosingElement.getModifiers();
//TODO should not be a non static inner class neither
if (!parentModifiers.contains(PUBLIC)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import javax.lang.model.element.Modifier;
import javax.lang.model.type.TypeMirror;
import toothpick.Factory;
import toothpick.Injector;
import toothpick.Scope;
import toothpick.compiler.CodeGenerator;
import toothpick.compiler.factory.targets.FactoryInjectionTarget;

Expand Down Expand Up @@ -46,7 +46,8 @@ public String brewJava() {
return javaFile.toString();
}

@Override public String getFqcn() {
@Override
public String getFqcn() {
return factoryInjectionTarget.builtClass.getQualifiedName().toString() + FACTORY_SUFFIX;
}

Expand All @@ -55,7 +56,7 @@ private void emitCreateInstance(TypeSpec.Builder builder) {
MethodSpec.Builder createInstanceBuilder = MethodSpec.methodBuilder("createInstance")
.addAnnotation(Override.class)
.addModifiers(Modifier.PUBLIC)
.addParameter(ClassName.get(Injector.class), "injector")
.addParameter(ClassName.get(Scope.class), "scope")
.returns(className);

StringBuilder localVarStatement = new StringBuilder("");
Expand All @@ -71,7 +72,7 @@ private void emitCreateInstance(TypeSpec.Builder builder) {
for (TypeMirror typeMirror : factoryInjectionTarget.parameters) {
String paramName = "param" + counter++;
TypeName paramType = TypeName.get(typeMirror);
createInstanceBuilder.addStatement("$T $L = injector.getInstance($T.class)", paramType, paramName, paramType);
createInstanceBuilder.addStatement("$T $L = scope.getInstance($T.class)", paramType, paramName, paramType);
localVarStatement.append(prefix);
localVarStatement.append(paramName);
prefix = ", ";
Expand All @@ -80,14 +81,9 @@ private void emitCreateInstance(TypeSpec.Builder builder) {
localVarStatement.append(")");
createInstanceBuilder.addStatement(localVarStatement.toString());
if (factoryInjectionTarget.needsMemberInjection) {
StringBuilder injectStatement = new StringBuilder("injector.inject(");
injectStatement.append(varName);
injectStatement.append(")");
createInstanceBuilder.addStatement(injectStatement.toString());
createInstanceBuilder.addStatement("new $L$$$$MemberInjector().inject($L, scope)", className, varName);
}
StringBuilder returnStatement = new StringBuilder("return ");
returnStatement.append(varName);
createInstanceBuilder.addStatement(returnStatement.toString());
createInstanceBuilder.addStatement("return $L", varName);

builder.addMethod(createInstanceBuilder.build());
}
Expand Down
Loading