Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class TreeKeyDispatcher extends KeyChanger implements Dispatcher {
- private Activity mActivity;
- private Object inKey; // скрин который должен появиться на экране пользователя
- @Nullable
- private Object outKey; // скрин который должен уйти с экрана пользователя
- private FrameLayout mRootFrame;
- public TreeKeyDispatcher(RootActivity rootActivity) {
- mActivity = rootActivity;
- }
- @Override
- public void dispatch(Traversal traversal, TraversalCallback callback) {
- Map<Object, Context> contexts;
- State inState = traversal.getState(traversal.destination.top());
- inKey = inState.getKey();
- State outState = traversal.origin == null ? null : traversal.getState(traversal.origin.top());
- outKey = outState == null ? null : outState.getKey();
- mRootFrame = (FrameLayout) mActivity.findViewById(R.id.root_frame);
- /**
- * Проверка для исключения помещения в историю перемещений одинкаовых скринов
- */
- if (inKey.equals(outKey)) {
- callback.onTraversalCompleted();
- return;
- }
- if (inKey instanceof TreeKey) {
- }
- Context flowContext = traversal.createContext(inKey, mActivity);
- Context mortarContext = ScreenScoper.getScreenScope(((AbstractScreen) inKey)).createContext(flowContext);
- contexts = Collections.singletonMap(inKey, mortarContext);
- changeKey(outState, inState, traversal.direction, contexts, callback);
- }
- @Override
- public void changeKey(@Nullable State outgoingState, State incomingState, Direction direction, Map<Object, Context> incomingContexts, TraversalCallback callback) {
- Context context = incomingContexts.get(inKey);
- /**
- * Сохраняем состояние экрана
- */
- if (outgoingState != null) {
- outgoingState.save(mRootFrame.getChildAt(0));
- }
- /**
- * Создаем новый экран
- */
- Screen screen;
- screen = inKey.getClass().getAnnotation(Screen.class);
- if (screen == null) {
- throw new IllegalStateException("@Screen annotation is missing in screen " + ((AbstractScreen) inKey).getClass());
- } else {
- int layout = screen.value();
- LayoutInflater inflater = LayoutInflater.from(context);
- View newView = inflater.inflate(layout, mRootFrame, false);
- View oldView = mRootFrame.getChildAt(0);
- /**
- * restore state new view
- */
- incomingState.restore(newView);
- // TODO: 27.11.2016 Unregister screen scope
- /**
- * delete old view
- */
- if (outKey != null && !(inKey instanceof TreeKey)) {
- ((AbstractScreen) outKey).unregisterScope();
- }
- if (mRootFrame.getChildAt(0) != null) {
- mRootFrame.removeView(mRootFrame.getChildAt(0));
- }
- mRootFrame.addView(newView);
- callback.onTraversalCompleted();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement