react-native-navigation的迁移库

ViewController.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. package com.reactnativenavigation.viewcontrollers;
  2. import android.app.Activity;
  3. import android.view.View;
  4. import android.view.ViewGroup;
  5. import android.view.ViewManager;
  6. import android.view.ViewTreeObserver;
  7. import com.reactnativenavigation.interfaces.ScrollEventListener;
  8. import com.reactnativenavigation.parse.Options;
  9. import com.reactnativenavigation.parse.params.Bool;
  10. import com.reactnativenavigation.parse.params.NullBool;
  11. import com.reactnativenavigation.presentation.FabPresenter;
  12. import com.reactnativenavigation.utils.CommandListener;
  13. import com.reactnativenavigation.utils.Functions.Func1;
  14. import com.reactnativenavigation.utils.StringUtils;
  15. import com.reactnativenavigation.utils.UiThread;
  16. import com.reactnativenavigation.utils.UiUtils;
  17. import com.reactnativenavigation.viewcontrollers.stack.StackController;
  18. import com.reactnativenavigation.viewcontrollers.viewcontrolleroverlay.ViewControllerOverlay;
  19. import com.reactnativenavigation.views.BehaviourAdapter;
  20. import com.reactnativenavigation.views.Component;
  21. import com.reactnativenavigation.views.Renderable;
  22. import java.util.ArrayList;
  23. import java.util.List;
  24. import androidx.annotation.CallSuper;
  25. import androidx.annotation.CheckResult;
  26. import androidx.annotation.NonNull;
  27. import androidx.annotation.Nullable;
  28. import androidx.annotation.VisibleForTesting;
  29. import androidx.coordinatorlayout.widget.CoordinatorLayout;
  30. import static com.reactnativenavigation.utils.CollectionUtils.*;
  31. import static com.reactnativenavigation.utils.ObjectUtils.perform;
  32. public abstract class ViewController<T extends ViewGroup> implements ViewTreeObserver.OnGlobalLayoutListener,
  33. ViewGroup.OnHierarchyChangeListener,
  34. BehaviourAdapter {
  35. private final List<Runnable> onAppearedListeners = new ArrayList<>();
  36. private boolean appearEventPosted;
  37. private boolean isFirstLayout = true;
  38. private Bool waitForRender = new NullBool();
  39. public interface ViewVisibilityListener {
  40. /**
  41. * @return true if the event is consumed, false otherwise
  42. */
  43. boolean onViewAppeared(View view);
  44. /**
  45. * @return true if the event is consumed, false otherwise
  46. */
  47. boolean onViewDisappear(View view);
  48. }
  49. public Options initialOptions;
  50. public Options options;
  51. private final Activity activity;
  52. private final String id;
  53. private YellowBoxDelegate yellowBoxDelegate;
  54. @Nullable protected T view;
  55. @Nullable private ParentController<T> parentController;
  56. private boolean isShown;
  57. private boolean isDestroyed;
  58. private ViewVisibilityListener viewVisibilityListener = new ViewVisibilityListenerAdapter();
  59. protected FabPresenter fabOptionsPresenter;
  60. private ViewControllerOverlay overlay;
  61. public boolean isDestroyed() {
  62. return isDestroyed;
  63. }
  64. public ViewController(Activity activity, String id, YellowBoxDelegate yellowBoxDelegate, Options initialOptions, ViewControllerOverlay overlay) {
  65. this.activity = activity;
  66. this.id = id;
  67. this.yellowBoxDelegate = yellowBoxDelegate;
  68. fabOptionsPresenter = new FabPresenter();
  69. this.initialOptions = initialOptions;
  70. this.overlay = overlay;
  71. options = initialOptions.copy();
  72. }
  73. public void setWaitForRender(Bool waitForRender) {
  74. this.waitForRender = waitForRender;
  75. }
  76. public ScrollEventListener getScrollEventListener() {
  77. return null;
  78. }
  79. public void addOnAppearedListener(Runnable onAppearedListener) {
  80. if (isShown) {
  81. onAppearedListener.run();
  82. } else {
  83. onAppearedListeners.add(onAppearedListener);
  84. }
  85. }
  86. public void removeOnAppearedListener(Runnable onAppearedListener) {
  87. onAppearedListeners.remove(onAppearedListener);
  88. }
  89. protected abstract T createView();
  90. public void setViewVisibilityListener(ViewVisibilityListener viewVisibilityListener) {
  91. this.viewVisibilityListener = viewVisibilityListener;
  92. }
  93. @VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE)
  94. public void ensureViewIsCreated() {
  95. getView();
  96. }
  97. public boolean handleBack(CommandListener listener) {
  98. return false;
  99. }
  100. public void addOverlay(View v) {
  101. perform(view, view -> overlay.add(view, v));
  102. }
  103. public void removeOverlay(View view) {
  104. overlay.remove(view);
  105. }
  106. @CheckResult
  107. public Options resolveCurrentOptions() {
  108. return options;
  109. }
  110. @CheckResult
  111. public Options resolveCurrentOptions(Options defaultOptions) {
  112. return options.copy().withDefaultOptions(defaultOptions);
  113. }
  114. @CallSuper
  115. public void mergeOptions(Options options) {
  116. this.initialOptions = this.initialOptions.mergeWith(options);
  117. this.options = this.options.mergeWith(options);
  118. if (getParentController() != null) {
  119. this.options.clearOneTimeOptions();
  120. initialOptions.clearOneTimeOptions();
  121. }
  122. }
  123. @CallSuper
  124. public void applyOptions(Options options) {
  125. }
  126. public void setDefaultOptions(Options defaultOptions) {
  127. }
  128. public Activity getActivity() {
  129. return activity;
  130. }
  131. public void performOnView(Func1<View> task) {
  132. if (view != null) task.run(view);
  133. }
  134. public void performOnParentController(Func1<ParentController> task) {
  135. if (parentController != null) task.run(parentController);
  136. }
  137. @VisibleForTesting(otherwise = VisibleForTesting.PROTECTED)
  138. public ParentController getParentController() {
  139. return parentController;
  140. }
  141. public ParentController requireParentController() {
  142. return parentController;
  143. }
  144. public void setParentController(@NonNull final ParentController parentController) {
  145. this.parentController = parentController;
  146. }
  147. public void performOnParentStack(Func1<StackController> task) {
  148. if (parentController instanceof StackController) {
  149. task.run((StackController) parentController);
  150. } else if (this instanceof StackController) {
  151. task.run((StackController) this);
  152. } else performOnParentController(parent -> parent.performOnParentStack(task));
  153. }
  154. public T getView() {
  155. if (view == null) {
  156. if (isDestroyed) {
  157. throw new RuntimeException("Tried to create view after it has already been destroyed");
  158. }
  159. view = createView();
  160. view.setOnHierarchyChangeListener(this);
  161. view.getViewTreeObserver().addOnGlobalLayoutListener(this);
  162. }
  163. return view;
  164. }
  165. public void detachView() {
  166. if (view == null || view.getParent() == null) return;
  167. ((ViewManager) view.getParent()).removeView(view);
  168. }
  169. public void attachView(ViewGroup parent, int index) {
  170. if (view == null) return;
  171. if (view.getParent() == null) parent.addView(view, index);
  172. }
  173. public String getId() {
  174. return id;
  175. }
  176. boolean isSameId(final String id) {
  177. return StringUtils.isEqual(this.id, id);
  178. }
  179. @Nullable
  180. public ViewController findController(String id) {
  181. return isSameId(id) ? this : null;
  182. }
  183. @Nullable
  184. public ViewController findController(View child) {
  185. return view == child ? this : null;
  186. }
  187. public boolean containsComponent(Component component) {
  188. return getView().equals(component);
  189. }
  190. public void onViewWillAppear() {
  191. }
  192. @CallSuper
  193. public void onViewAppeared() {
  194. isShown = true;
  195. applyOptions(options);
  196. performOnParentController(parentController -> {
  197. parentController.clearOptions();
  198. if (getView() instanceof Component) parentController.applyChildOptions(options, this);
  199. });
  200. if (!onAppearedListeners.isEmpty() && !appearEventPosted) {
  201. appearEventPosted = true;
  202. UiThread.post(() -> {
  203. forEach(onAppearedListeners, Runnable::run);
  204. onAppearedListeners.clear();
  205. });
  206. }
  207. }
  208. public void onViewWillDisappear() {
  209. }
  210. @CallSuper
  211. public void onViewDisappear() {
  212. isShown = false;
  213. }
  214. @CallSuper
  215. public void destroy() {
  216. if (isShown) {
  217. isShown = false;
  218. onViewDisappear();
  219. }
  220. yellowBoxDelegate.destroy();
  221. if (view instanceof Destroyable) {
  222. ((Destroyable) view).destroy();
  223. }
  224. if (view != null) {
  225. view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
  226. view.setOnHierarchyChangeListener(null);
  227. if (view.getParent() instanceof ViewGroup) {
  228. ((ViewManager) view.getParent()).removeView(view);
  229. }
  230. view = null;
  231. isDestroyed = true;
  232. }
  233. }
  234. @Override
  235. public void onGlobalLayout() {
  236. if (isFirstLayout) {
  237. onAttachToParent();
  238. isFirstLayout = false;
  239. }
  240. if (!isShown && isViewShown()) {
  241. if (!viewVisibilityListener.onViewAppeared(view)) {
  242. isShown = true;
  243. onViewAppeared();
  244. }
  245. } else if (isShown && !isViewShown()) {
  246. if (!viewVisibilityListener.onViewDisappear(view)) {
  247. isShown = false;
  248. onViewDisappear();
  249. }
  250. }
  251. }
  252. public void onAttachToParent() {
  253. }
  254. @Override
  255. public void onChildViewAdded(View parent, View child) {
  256. yellowBoxDelegate.onChildViewAdded(parent, child);
  257. }
  258. @Override
  259. public void onChildViewRemoved(View view, View view1) {
  260. }
  261. void runOnPreDraw(Func1<T> task) {
  262. if (!isDestroyed) UiUtils.runOnPreDrawOnce(getView(), task);
  263. }
  264. public abstract void sendOnNavigationButtonPressed(String buttonId);
  265. public boolean isViewShown() {
  266. return !isDestroyed &&
  267. getView().isShown() &&
  268. view != null &&
  269. isRendered();
  270. }
  271. public boolean isRendered() {
  272. return view != null && (
  273. waitForRender.isFalseOrUndefined() ||
  274. !(view instanceof Renderable) ||
  275. ((Renderable) view).isRendered()
  276. );
  277. }
  278. void applyOnController(ViewController controller, Func1<ViewController> task) {
  279. if (controller != null) task.run(controller);
  280. }
  281. @Override
  282. @CallSuper
  283. public boolean onMeasureChild(CoordinatorLayout parent, ViewGroup child, int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed) {
  284. perform(findController(child), ViewController::applyTopInset);
  285. return false;
  286. }
  287. @Override
  288. public boolean onDependentViewChanged(CoordinatorLayout parent, ViewGroup child, View dependency) {
  289. return false;
  290. }
  291. public void applyTopInset() {
  292. }
  293. public int getTopInset() {
  294. return 0;
  295. }
  296. public void applyBottomInset() {
  297. }
  298. public int getBottomInset() {
  299. return perform(parentController, 0, p -> p.getBottomInset(this));
  300. }
  301. }