use of mortar.MortarScope in project mortar by square.
the class BundleServiceTest method cannotGetBundleServiceFromContextOfDestroyed.
@Test
public void cannotGetBundleServiceFromContextOfDestroyed() {
MortarScope child = activityScope.buildChild().build("child");
Context context = mockContext(child);
child.destroy();
IllegalStateException caught = null;
try {
getBundleService(context);
} catch (IllegalStateException e) {
caught = e;
}
assertThat(caught).isNotNull();
}
use of mortar.MortarScope in project mortar by square.
the class BundleServiceTest method handlesDestroyFromOnSave.
@Test
public void handlesDestroyFromOnSave() {
final AtomicInteger saves = new AtomicInteger(0);
final AtomicInteger destroys = new AtomicInteger(0);
class Destroyer implements Bundler {
@Override
public String getMortarBundleKey() {
return "k";
}
@Override
public void onEnterScope(MortarScope scope) {
}
@Override
public void onLoad(Bundle savedInstanceState) {
}
@Override
public void onSave(Bundle outState) {
saves.incrementAndGet();
activityScope.destroy();
}
@Override
public void onExitScope() {
destroys.incrementAndGet();
}
}
BundleService bundleService = getBundleService(activityScope);
bundleService.register(new Destroyer());
bundleService.register(new Destroyer());
Bundle b = new Bundle();
BundleServiceRunner bundleServiceRunner = getBundleServiceRunner(activityScope);
bundleServiceRunner.onCreate(b);
bundleServiceRunner.onSaveInstanceState(b);
assertThat(destroys.get()).isEqualTo(2);
assertThat(saves.get()).isEqualTo(1);
}
use of mortar.MortarScope in project mortar by square.
the class BundleServiceTest method destroyingWhileSaving.
/** https://github.com/square/mortar/issues/131 */
@Test
public void destroyingWhileSaving() {
final MortarScope[] currentScreen = new MortarScope[] { null };
MortarScope screenSwapperScope = activityScope.buildChild().build("screenOne");
getBundleService(screenSwapperScope).register(new MyBundler("screenSwapper") {
@Override
public void onSave(Bundle outState) {
currentScreen[0].destroy();
}
});
final MortarScope screenOneScope = screenSwapperScope.buildChild().build("screenOne");
getBundleService(screenOneScope).register(new MyBundler("bundlerOne"));
currentScreen[0] = screenOneScope;
final MortarScope screenTwoScope = screenSwapperScope.buildChild().build("screenTwo");
getBundleService(screenTwoScope).register(new MyBundler("bundlerTwo"));
getBundleServiceRunner(activityScope).onSaveInstanceState(new Bundle());
}
use of mortar.MortarScope in project mortar by square.
the class MortarDemoActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GsonParceler parceler = new GsonParceler(new Gson());
@SuppressWarnings("deprecation") FlowDelegate.NonConfigurationInstance nonConfig = (FlowDelegate.NonConfigurationInstance) getLastNonConfigurationInstance();
MortarScope parentScope = MortarScope.getScope(getApplication());
String scopeName = getLocalClassName() + "-task-" + getTaskId();
activityScope = parentScope.findChild(scopeName);
if (activityScope == null) {
activityScope = parentScope.buildChild().withService(BundleServiceRunner.SERVICE_NAME, new BundleServiceRunner()).build(scopeName);
}
ObjectGraphService.inject(this, this);
getBundleServiceRunner(activityScope).onCreate(savedInstanceState);
actionBarOwner.takeView(this);
setContentView(R.layout.root_layout);
container = (PathContainerView) findViewById(R.id.container);
containerAsHandlesBack = (HandlesBack) container;
flowDelegate = FlowDelegate.onCreate(nonConfig, getIntent(), savedInstanceState, parceler, History.single(new ChatListScreen()), this);
}
use of mortar.MortarScope in project mortar by square.
the class ScreenScoper method getScreenScope.
/**
* Finds or creates the scope for the given screen, honoring its optional {@link
* WithModuleFactory} or {@link WithModule} annotation. Note that scopes are also created
* for unannotated screens.
*/
public MortarScope getScreenScope(Resources resources, MortarScope parentScope, final String name, final Object screen) {
ModuleFactory moduleFactory = getModuleFactory(screen);
Object[] childModule;
if (moduleFactory != NO_FACTORY) {
childModule = new Object[] { moduleFactory.createDaggerModule(resources, screen) };
} else {
// We need every screen to have a scope, so that anything it injects is scoped. We need
// this even if the screen doesn't declare a module, because Dagger allows injection of
// objects that are annotated even if they don't appear in a module.
childModule = new Object[0];
}
MortarScope childScope = parentScope.findChild(name);
if (childScope == null) {
childScope = parentScope.buildChild().withService(ObjectGraphService.SERVICE_NAME, ObjectGraphService.create(parentScope, childModule)).build(name);
}
return childScope;
}
Aggregations