/** * Go to detail view. * @param sender */ public void goToDetailScreen(Object sender) { //Navigate to CounterDetailController which is paired by CounterDetailScreen navigationManager.navigate(sender).to(CounterDetailController.class); }
public void goBackToBasicView(Object sender) { navigationManager.navigate(sender).back(); }
/** * Prepare the instance subject to being injected with no qualifier for the fragment being * navigated to. This instance will be not be released until the navigation is settled. To * config the instance try {@link #with(Class, Preparer)} or {@link #with(Class, Annotation, Preparer)} * * @param type The class type of the instance needs to be prepared * @return This navigator * @throws MvcGraphException Raised when the required injectable object cannot be injected */ public <T> Navigator with(Class<T> type) throws MvcGraphException { with(type, null, null); return this; }
/** * Navigate to the location represented by the controller. Navigation only takes effect when the * given locationId is different from the current location and raises {@link NavigationManager.Event.OnLocationForward} * <p/> * <p> * To set argument for the next location navigating to, use {@link #with(Class, Annotation, Preparer)} * to prepare the controller injecting into the next fragment. * </p> * * @param controllerClass The controller of which screen the app is navigating to. */ public void to(@NotNull Class<? extends Controller> controllerClass) { doNavigateTo(controllerClass, null); go(); }
@Test public void should_not_raise_navigate_back_event_when_navigate_to_first_location_from_the_first_location() throws Exception { // Arrange BackListener backListener = mock(BackListener.class); eventBusC.register(backListener); navigationManager.navigate(this).to(locId1); // Arrange Navigator navigator = navigationManager.navigate(this); navigator.back(null); // Verify ArgumentCaptor<NavigationManager.Event.OnLocationBack> event = ArgumentCaptor.forClass(NavigationManager.Event.OnLocationBack.class); verify(backListener, times(0)).onEvent(event.capture()); }
@Test public void should_retain_prepared_instance_until_navigation_settled_without_qualifier() throws Exception { // Arrange final long fiveMinutes = 60 * 5; // Act Navigator navigator = navigationManager.navigate(this).with(TimerController.class, new Preparer<TimerController>() { @Override public void prepare(TimerController instance) { instance.setInitialValue(fiveMinutes); } }); navigator.to(TimerController.class); Mvc.graph().use(TimerController.class, null, new Consumer<TimerController>() { @Override public void consume(TimerController instance) { //Controller should not have been released yet Assert.assertEquals(fiveMinutes, instance.getInitialValue()); } }); //destroy the navigator navigator.destroy(); Mvc.graph().use(TimerController.class, new Consumer<TimerController>() { @Override public void consume(TimerController instance) { //Controller should have now been released Assert.assertEquals(0, instance.getInitialValue()); } }); }
@Test public void should_invoke_on_settled_when_navigation_is_done() throws Exception { // Arrange Navigator.OnSettled onSettled = mock(Navigator.OnSettled.class); // Act Navigator navigator = navigationManager.navigate(this).with(TimerController.class) .onSettled(onSettled); navigator.to(TimerController.class); verify(onSettled, times(0)).run(); //destroy the navigator navigator.destroy(); verify(onSettled, times(1)).run(); }
@Test public void should_throw_MvcGraphException_when_mvcGraph_destroy_method_encounters_PokeException() throws ProvideException, ProviderConflictException, ProviderMissingException { navigationManager.logger = mock(Logger.class); Navigator navigator = navigationManager.navigate(this); Mvc.graph().getRootComponent().register(new Object(){ @Provides public X x() { return new X_1(); } }); navigator.with(X.class); Mvc.graph().getRootComponent().unregister(X.class, null); navigator.destroy(); verify(navigationManager.logger).warn(anyString(), anyString()); }
/** * Sends out the navigation event to execute the navigation */ private void go() { if (navigateEvent != null) { navigationManager.postEvent2C(navigateEvent); if (navigationManager.logger.isTraceEnabled()) { if (navigateEvent instanceof NavigationManager.Event.OnLocationForward) { NavigationManager.Event.OnLocationForward event = (NavigationManager.Event.OnLocationForward) navigateEvent; String lastLocId = event.getLastValue() == null ? null : event.getLastValue().getLocationId(); navigationManager.logger.trace("Nav Manager: Forward: {} -> {}", lastLocId, event.getCurrentValue().getLocationId()); } } if (navigateEvent instanceof NavigationManager.Event.OnLocationBack) { if (navigationManager.logger.isTraceEnabled()) { NavigationManager.Event.OnLocationBack event = (NavigationManager.Event.OnLocationBack) navigateEvent; NavLocation lastLoc = event.getLastValue(); NavLocation currentLoc = event.getCurrentValue(); navigationManager.logger.trace("Nav Manager: Backward: {} -> {}", lastLoc.getLocationId(), currentLoc == null ? "null" : currentLoc.getLocationId()); } checkAppExit(sender); } } dumpHistory(); }
/** * Initiates a {@link Navigator} to start navigation. * @param sender Who wants to navigate * @return A new instance of {@link Navigator} */ public Navigator navigate(Object sender) { return new Navigator(sender, this); }
@Test public void should_return_correct_sender_by_navigator() throws Exception { // Act Navigator navigator = navigationManager.navigate(this); Assert.assertTrue(this == navigator.getSender()); }
Navigator navigator = navigationManager.navigate(this).with(TimerController.class, slower2Qualifier, new Preparer<TimerController>() { @Override public void prepare(TimerController instance) { navigator.to(TimerController.class); navigator.destroy(); navigator = navigationManager.navigate(this).with(TimerController.class, slower3Qualifier, new Preparer<TimerController>() { @Override public void prepare(TimerController instance) { navigator.to(TimerController.class); navigator.destroy();
/** * Navigate to the location represented by the controller. Navigation only takes effect when the * given locationId is different from the current location and raises {@link NavigationManager.Event.OnLocationForward} * <p/> * <p> * To set argument for the next location navigating to, use {@link #with(Class, Annotation, Preparer)} * to prepare the controller injecting into the next fragment. * </p> * * @param controllerClass The controller class type. * @param forwarder The configuration by {@link Forwarder} of the forward navigation. */ public void to(@NotNull Class<? extends Controller> controllerClass, @NotNull Forwarder forwarder) { doNavigateTo(controllerClass, forwarder); go(); }
public void startApp(Object sender) { navigationManager.navigate(sender).to(CounterMasterController.class); }
public void navigateBack(Object sender) { navigationManager.navigate(sender).back(); }
with(type, null, preparer); return this;
@Test public void should_skip_interim_location_on_back_navigation() throws Exception { //mock the subscriber BackListener backListener = mock(BackListener.class); eventBusC.register(backListener); prepareLocationHistory(); reset(backListener); navigationManager.navigate(this).to(locId1, new Forwarder().setInterim(true)); navigationManager.navigate(this).to(locId2, new Forwarder().setInterim(true)); navigationManager.navigate(this).back(); ArgumentCaptor<NavigationManager.Event.OnLocationBack> event = ArgumentCaptor.forClass(NavigationManager.Event.OnLocationBack.class); verify(backListener).onEvent(event.capture()); assertEquals(event.getValue().getLastValue().getLocationId(), locId2.getName()); assertEquals(event.getValue().getCurrentValue().getLocationId(), locId4.getName()); NavLocation currentLoc = navigationManager.getModel().getCurrentLocation(); assertEquals(currentLoc.getLocationId(), locId4.getName()); Assert.assertTrue(event.getValue().isFastRewind()); }
@Test public void shouldClearAllNavigationHistory() throws Exception { ForwardListener forwardListener = prepareLocationHistory(); reset(forwardListener); navigationManager.navigate(this).to(locId5, new Forwarder().clearAll()); ArgumentCaptor<NavigationManager.Event.OnLocationForward> event = ArgumentCaptor.forClass(NavigationManager.Event.OnLocationForward.class); verify(forwardListener).onEvent(event.capture()); assertEquals(event.getValue().getLastValue().getLocationId(), locId4.getName()); assertEquals(event.getValue().getCurrentValue().getLocationId(), locId5.getName()); assertEquals(event.getValue().getLocationWhereHistoryClearedUpTo(), null); assertEquals(event.getValue().isClearHistory(), true); //Now the history should be loc1->loc2->loc5 NavLocation currentLoc = navigationManager.getModel().getCurrentLocation(); assertEquals(currentLoc.getLocationId(), locId5.getName()); assertEquals(currentLoc.getPreviousLocation(), null); }