react-native-navigation的迁移库

ComponentViewController.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package com.reactnativenavigation.viewcontrollers;
  2. import android.app.Activity;
  3. import android.view.View;
  4. import com.reactnativenavigation.interfaces.ScrollEventListener;
  5. import com.reactnativenavigation.parse.Options;
  6. import com.reactnativenavigation.presentation.ComponentPresenter;
  7. import com.reactnativenavigation.presentation.Presenter;
  8. import com.reactnativenavigation.utils.StatusBarUtils;
  9. import com.reactnativenavigation.views.ComponentLayout;
  10. import com.reactnativenavigation.views.ReactComponent;
  11. import androidx.annotation.NonNull;
  12. import androidx.core.view.ViewCompat;
  13. import androidx.core.view.WindowInsetsCompat;
  14. import static com.reactnativenavigation.utils.ObjectUtils.perform;
  15. public class ComponentViewController extends ChildController<ComponentLayout> {
  16. private final String componentName;
  17. private ComponentPresenter presenter;
  18. private final ReactViewCreator viewCreator;
  19. ReactComponent getComponent() {
  20. return view;
  21. }
  22. public ComponentViewController(final Activity activity,
  23. final ChildControllersRegistry childRegistry,
  24. final String id,
  25. final String componentName,
  26. final ReactViewCreator viewCreator,
  27. final Options initialOptions,
  28. final Presenter presenter,
  29. final ComponentPresenter componentPresenter) {
  30. super(activity, childRegistry, id, presenter, initialOptions);
  31. this.componentName = componentName;
  32. this.viewCreator = viewCreator;
  33. this.presenter = componentPresenter;
  34. }
  35. @Override
  36. public void setDefaultOptions(Options defaultOptions) {
  37. super.setDefaultOptions(defaultOptions);
  38. presenter.setDefaultOptions(defaultOptions);
  39. }
  40. @Override
  41. public ScrollEventListener getScrollEventListener() {
  42. return perform(view, null, ComponentLayout::getScrollEventListener);
  43. }
  44. @Override
  45. public void onViewAppeared() {
  46. super.onViewAppeared();
  47. if (view != null) view.sendComponentStart();
  48. }
  49. @Override
  50. public void onViewDisappear() {
  51. if (view != null) view.sendComponentStop();
  52. super.onViewDisappear();
  53. }
  54. @Override
  55. public void sendOnNavigationButtonPressed(String buttonId) {
  56. getView().sendOnNavigationButtonPressed(buttonId);
  57. }
  58. @Override
  59. public void applyOptions(Options options) {
  60. if (isRoot()) applyTopInset();
  61. super.applyOptions(options);
  62. getView().applyOptions(options);
  63. presenter.applyOptions(getView(), resolveCurrentOptions(presenter.defaultOptions));
  64. }
  65. @Override
  66. public boolean isViewShown() {
  67. return super.isViewShown() && view != null && view.isReady();
  68. }
  69. @NonNull
  70. @Override
  71. protected ComponentLayout createView() {
  72. ComponentLayout view = (ComponentLayout) viewCreator.create(getActivity(), getId(), componentName);
  73. return (ComponentLayout) view.asView();
  74. }
  75. @Override
  76. public void mergeOptions(Options options) {
  77. if (options == Options.EMPTY) return;
  78. presenter.mergeOptions(getView(), options);
  79. super.mergeOptions(options);
  80. }
  81. @Override
  82. public void applyTopInset() {
  83. if (view != null) presenter.applyTopInsets(view, getTopInset());
  84. }
  85. @Override
  86. public int getTopInset() {
  87. int statusBarInset = resolveCurrentOptions().statusBar.isHiddenOrDrawBehind() ? 0 : StatusBarUtils.getStatusBarHeight(getActivity());
  88. return statusBarInset + perform(getParentController(), 0, p -> p.getTopInset(this));
  89. }
  90. @Override
  91. public void applyBottomInset() {
  92. if (view != null) presenter.applyBottomInset(view, getBottomInset());
  93. }
  94. @Override
  95. protected WindowInsetsCompat applyWindowInsets(ViewController view, WindowInsetsCompat insets) {
  96. ViewCompat.onApplyWindowInsets(view.getView(), insets.replaceSystemWindowInsets(
  97. insets.getSystemWindowInsetLeft(),
  98. insets.getSystemWindowInsetTop(),
  99. insets.getSystemWindowInsetRight(),
  100. Math.max(insets.getSystemWindowInsetBottom() - getBottomInset(), 0)
  101. ));
  102. return insets;
  103. }
  104. @Override
  105. public void destroy() {
  106. final boolean blurOnUnmount = options != null && options.modal.blurOnUnmount.isTrue();
  107. if (blurOnUnmount) {
  108. blurActivityFocus();
  109. }
  110. super.destroy();
  111. }
  112. private void blurActivityFocus() {
  113. final Activity activity = getActivity();
  114. final View focusView = activity != null ? activity.getCurrentFocus() : null;
  115. if (focusView != null) {
  116. focusView.clearFocus();
  117. }
  118. }
  119. }