public void destroy() { final Set<Map.Entry<String, Scoop>> entries = this.children.entrySet(); final Set<Map.Entry<String, Scoop>> entriesCopy = new HashSet<>(entries); for (Map.Entry<String, Scoop> entry : entriesCopy) { entry.getValue().destroy(); } this.destroyed = true; if (parent != null) { parent.children.remove(this.getName()); } }
public static <T extends Screen> T fromScoop(Scoop scoop) { if (scoop == null) { return null; } return scoop.findService("screen"); }
public static DaggerInjector fromContext(Context context) { return Scoop.fromContext(context).findService(SERVICE_NAME); }
public static DaggerInjector fromView(View view) { return Scoop.fromView(view).findService(SERVICE_NAME); }
@Test public void destroyChildScoop() { FooService service1 = new FooService(); Scoop rootScoop = new Scoop.Builder("root") .service("foo_service_1", service1) .build(); FooService service2 = new FooService(); Scoop childScoop = new Scoop.Builder("child", rootScoop) .service("foo_service_2", service2) .build(); FooService service3 = new FooService(); Scoop grandChildScoop = new Scoop.Builder("grand_child", childScoop) .service("foo_service_3", service3) .build(); childScoop.destroy(); Assert.assertFalse(rootScoop.isDestroyed()); assertNull(rootScoop.findChild("child")); Assert.assertNotNull(rootScoop.findService("foo_service_1")); Assert.assertTrue(childScoop.isDestroyed()); Assert.assertNotNull(childScoop.getParent()); assertNull(childScoop.findChild("grand_child")); Assert.assertNotNull(childScoop.findService("foo_service_1")); Assert.assertNotNull(childScoop.findService("foo_service_2")); Assert.assertTrue(grandChildScoop.isDestroyed()); Assert.assertNotNull(grandChildScoop.getParent()); Assert.assertNotNull(grandChildScoop.findService("foo_service_1")); Assert.assertNotNull(grandChildScoop.findService("foo_service_2")); Assert.assertNotNull(grandChildScoop.findService("foo_service_3")); }
@Test public void createScoopFromAPathToABCPathNullCurrentScoop() { List<Screen> fromPath = Arrays.<Screen>asList(new ScreenA()); List<Screen> toPath = Arrays.<Screen>asList(new ScreenA(), new ScreenB(), new ScreenC()); Scoop cScoop = screenScooper.create(rootScoop, null, fromPath, toPath); //A assertFalse(cScoop.getParent().getParent().isDestroyed()); //B assertFalse(cScoop.getParent().isDestroyed()); assertFalse(cScoop.isDestroyed()); assertFalse(rootScoop.isDestroyed()); assertEquals(rootScoop, cScoop.getParent().getParent().getParent()); }
@Test public void createRootScoop() { FooService service = new FooService(); Scoop scoop = new Scoop.Builder("root") .service("foo_service", service) .build(); Assert.assertEquals("root", scoop.getName()); assertNull(scoop.getParent()); Assert.assertEquals(service, scoop.findService("foo_service")); }
public View inflateView( Scoop scoop, Screen screen, ViewGroup viewGroup) { View view = scoop.inflate(screen.getLayout(), viewGroup, false); return view; } }
@Override protected void onDestroy() { Timber.d("onDestroy"); subscriptions.clear(); getActivityScoop().destroy(); super.onDestroy(); }
@Test public void createChildScoop() { Scoop rootScoop = new Scoop.Builder("root").build(); Scoop childScoop = new Scoop.Builder("child", rootScoop) .build(); Assert.assertEquals("child", childScoop.getName()); Assert.assertEquals(rootScoop, childScoop.getParent()); Assert.assertEquals(childScoop, rootScoop.findChild("child")); }
public static Scoop fromView(View view) { if (view == null) { return null; } return fromContext(view.getContext()); }
private ArrayList<Scoop> getCurrentScoops(List<Screen> fromPath, Scoop currentScreenScoop) { ArrayList<Scoop> scoops = new ArrayList<>(); Scoop startScoop = currentScreenScoop; if (startScoop != null) { for (int i = 0; i < fromPath.size(); i++) { scoops.add(0, startScoop); startScoop = startScoop.getParent(); } } return scoops; }
public Scoop build() { return new Scoop(name, parent, services); } }
public LayoutInflater inflater(Context context) { return LayoutInflater.from(context).cloneInContext(createContext(context)); }
public View inflate(int layoutId, ViewGroup viewGroup, boolean attachToRoot) { return inflater(viewGroup.getContext()).inflate(layoutId, viewGroup, attachToRoot); }
.build(); rootScoop.destroy(); Assert.assertTrue(rootScoop.isDestroyed()); Assert.assertNull(rootScoop.findChild("child")); Assert.assertNotNull(rootScoop.findService("foo_service_1")); Assert.assertTrue(childScoop1.isDestroyed()); Assert.assertNotNull(childScoop1.getParent()); Assert.assertNull(childScoop1.findChild("grand_child")); Assert.assertNotNull(childScoop1.findService("foo_service_1")); Assert.assertNotNull(childScoop1.findService("child_service_1")); Assert.assertTrue(grandChildScoop.isDestroyed()); Assert.assertNotNull(grandChildScoop.getParent()); Assert.assertNotNull(grandChildScoop.findService("foo_service_1")); Assert.assertNotNull(grandChildScoop.findService("child_service_1")); Assert.assertNotNull(grandChildScoop.findService("foo_service_3"));
@Test public void createScoopFromABPathToABCPathNullCurrentScoop() { List<Screen> fromPath = Arrays.<Screen>asList(new ScreenA(), new ScreenB()); List<Screen> toPath = Arrays.<Screen>asList(new ScreenA(), new ScreenB(), new ScreenC()); Scoop cScoop = screenScooper.create(rootScoop, null, fromPath, toPath); assertFalse(rootScoop.isDestroyed()); assertFalse(cScoop.getParent().getParent().isDestroyed()); assertFalse(cScoop.getParent().isDestroyed()); assertFalse(cScoop.isDestroyed()); assertEquals(rootScoop, cScoop.getParent().getParent().getParent()); }
public View inflateView( Scoop scoop, Screen screen, ViewGroup viewGroup) { View view = scoop.inflate(screen.getLayout(), viewGroup, false); return view; } }
public Scoop create(Scoop rootScoop, Scoop currentScreenScoop, List<Screen> fromPath, List<Screen> toPath) { Scoop finalScoop = null; List<Scoop> scoops = getCurrentScoops(fromPath, currentScreenScoop); int index = 0; while (index < fromPath.size() && currentScreenScoop != null) { Screen fromScreen = safeElementGet(fromPath, index); Screen toScreen = safeElementGet(toPath, index); Scoop scoop = scoops.get(index); if (Screen.equals(fromScreen, toScreen)) { finalScoop = scoop; } else { scoop.destroy(); break; } index++; } while (index < toPath.size()) { Screen toScreen = safeElementGet(toPath, index); if (finalScoop == null) { finalScoop = rootScoop; } finalScoop = screenScoopFactory.createScreenScoop(toScreen, finalScoop); index++; } return finalScoop; }