Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. public class TreeKeyDispatcher extends KeyChanger implements Dispatcher {
  2. private Activity mActivity;
  3. private Object inKey; // скрин который должен появиться на экране пользователя
  4. @Nullable
  5. private Object outKey; // скрин который должен уйти с экрана пользователя
  6. private FrameLayout mRootFrame;
  7.  
  8.  
  9. public TreeKeyDispatcher(RootActivity rootActivity) {
  10. mActivity = rootActivity;
  11. }
  12.  
  13. @Override
  14. public void dispatch(Traversal traversal, TraversalCallback callback) {
  15. Map<Object, Context> contexts;
  16. State inState = traversal.getState(traversal.destination.top());
  17. inKey = inState.getKey();
  18. State outState = traversal.origin == null ? null : traversal.getState(traversal.origin.top());
  19. outKey = outState == null ? null : outState.getKey();
  20.  
  21. mRootFrame = (FrameLayout) mActivity.findViewById(R.id.root_frame);
  22.  
  23. /**
  24. * Проверка для исключения помещения в историю перемещений одинкаовых скринов
  25. */
  26. if (inKey.equals(outKey)) {
  27. callback.onTraversalCompleted();
  28. return;
  29. }
  30.  
  31. if (inKey instanceof TreeKey) {
  32.  
  33. }
  34.  
  35. Context flowContext = traversal.createContext(inKey, mActivity);
  36. Context mortarContext = ScreenScoper.getScreenScope(((AbstractScreen) inKey)).createContext(flowContext);
  37. contexts = Collections.singletonMap(inKey, mortarContext);
  38. changeKey(outState, inState, traversal.direction, contexts, callback);
  39. }
  40.  
  41.  
  42. @Override
  43. public void changeKey(@Nullable State outgoingState, State incomingState, Direction direction, Map<Object, Context> incomingContexts, TraversalCallback callback) {
  44.  
  45. Context context = incomingContexts.get(inKey);
  46.  
  47. /**
  48. * Сохраняем состояние экрана
  49. */
  50.  
  51. if (outgoingState != null) {
  52. outgoingState.save(mRootFrame.getChildAt(0));
  53. }
  54.  
  55. /**
  56. * Создаем новый экран
  57. */
  58. Screen screen;
  59. screen = inKey.getClass().getAnnotation(Screen.class);
  60. if (screen == null) {
  61. throw new IllegalStateException("@Screen annotation is missing in screen " + ((AbstractScreen) inKey).getClass());
  62. } else {
  63. int layout = screen.value();
  64. LayoutInflater inflater = LayoutInflater.from(context);
  65. View newView = inflater.inflate(layout, mRootFrame, false);
  66. View oldView = mRootFrame.getChildAt(0);
  67.  
  68. /**
  69. * restore state new view
  70. */
  71.  
  72. incomingState.restore(newView);
  73. // TODO: 27.11.2016 Unregister screen scope
  74. /**
  75. * delete old view
  76. */
  77.  
  78. if (outKey != null && !(inKey instanceof TreeKey)) {
  79. ((AbstractScreen) outKey).unregisterScope();
  80. }
  81.  
  82. if (mRootFrame.getChildAt(0) != null) {
  83. mRootFrame.removeView(mRootFrame.getChildAt(0));
  84. }
  85.  
  86.  
  87. mRootFrame.addView(newView);
  88. callback.onTraversalCompleted();
  89.  
  90. }
  91. }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement