Created
September 20, 2017 07:07
-
-
Save MaksTuev/5a6ee84e51484cb28c557ee0a075053d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Базвый нафигатор фрагметов | |
* | |
* @param <T> | |
*/ | |
abstract class BaseFragmentNavigator<T extends Provider<?>> implements Navigator { | |
protected final T provider; | |
BaseFragmentNavigator(T provider) { | |
this.provider = provider; | |
} | |
@IntDef({TRANSIT_NONE, TRANSIT_FRAGMENT_OPEN, TRANSIT_FRAGMENT_CLOSE, TRANSIT_FRAGMENT_FADE}) | |
@Retention(RetentionPolicy.SOURCE) | |
private @interface Transit { | |
} | |
abstract FragmentManager getFragmentManager(); | |
public void add(FragmentRoute route, boolean stackable, @Transit int transition) { | |
int viewContainerId = getViewContainerIdOrThrow(); | |
FragmentManager fragmentManager = getFragmentManager(); | |
fragmentManager.executePendingTransactions(); | |
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); | |
fragmentTransaction.add(viewContainerId, route.createFragment(), route.getTag()); | |
fragmentTransaction.setTransition(transition); | |
if (stackable) { | |
fragmentTransaction.addToBackStack(route.getTag()); | |
} | |
fragmentTransaction.commit(); | |
} | |
public void replace(FragmentRoute route, boolean stackable, @Transit int transition) { | |
int viewContainerId = getViewContainerIdOrThrow(); | |
FragmentManager fragmentManager = getFragmentManager(); | |
fragmentManager.executePendingTransactions(); | |
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); | |
fragmentTransaction.replace(viewContainerId, route.createFragment(), route.getTag()); | |
fragmentTransaction.setTransition(transition); | |
if (stackable) { | |
fragmentTransaction.addToBackStack(route.getTag()); | |
} | |
fragmentTransaction.commit(); | |
} | |
/** | |
* @return возвращает {@code true} если фрагмент был удален успешно | |
*/ | |
public boolean remove(FragmentRoute route, @Transit int transition) { | |
FragmentManager fragmentManager = getFragmentManager(); | |
fragmentManager.executePendingTransactions(); | |
Fragment fragment = fragmentManager.findFragmentByTag(route.getTag()); | |
if (fragment == null) { | |
return false; | |
} | |
fragmentManager.beginTransaction() | |
.setTransition(transition) | |
.remove(fragment) | |
.commit(); | |
return true; | |
} | |
/** | |
* @return возвращает {@code true} если фрагмент успешно отобразился | |
*/ | |
public boolean show(FragmentRoute route, @Transit int transition) { | |
return toggleVisibility(route, true, transition); | |
} | |
/** | |
* @return возвращает {@code true} если фрагмент был скрыт успешно | |
*/ | |
public boolean hide(FragmentRoute route, @Transit int transition) { | |
return toggleVisibility(route, false, transition); | |
} | |
} | |
/** | |
* базовый маршрут для открытия фрагмента | |
* см {@link Route} | |
*/ | |
public abstract class FragmentRoute implements Route { | |
protected abstract Class<? extends Fragment> getFragmentClass(); | |
public String getTag(){ | |
return getFragmentClass().getCanonicalName(); | |
} | |
public Fragment createFragment(){ | |
try { | |
return getFragmentClass().newInstance(); | |
} catch (InstantiationException | IllegalAccessException e) { | |
throw new IllegalStateException(e); | |
} | |
} | |
} | |
/** | |
* см {@link Route} | |
*/ | |
public abstract class FragmentWithParamsRoute extends FragmentRoute { | |
public FragmentWithParamsRoute(){ | |
} | |
public FragmentWithParamsRoute(Bundle bundle){ | |
} | |
protected abstract Bundle prepareBundle(); | |
@Override | |
public Fragment createFragment(){ | |
Fragment fragment = super.createFragment(); | |
fragment.setArguments(prepareBundle()); | |
return fragment; | |
} | |
} | |
/** | |
* Маршрут экрана списка заказов | |
*/ | |
@Data | |
public class OrdersFragmentRoute extends FragmentRoute { | |
public OrdersFragmentRoute() { | |
} | |
@Override | |
protected Class<? extends Fragment> getFragmentClass() { | |
return OrdersFragmentView.class; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment