react-native-navigation的迁移库

ParentController.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package com.reactnativenavigation.viewcontrollers;
  2. import android.app.Activity;
  3. import android.support.annotation.CallSuper;
  4. import android.support.annotation.CheckResult;
  5. import android.support.annotation.NonNull;
  6. import android.support.annotation.Nullable;
  7. import android.support.v4.view.ViewPager;
  8. import android.view.ViewGroup;
  9. import com.reactnativenavigation.parse.Options;
  10. import com.reactnativenavigation.parse.params.Bool;
  11. import com.reactnativenavigation.presentation.OptionsPresenter;
  12. import com.reactnativenavigation.utils.CollectionUtils;
  13. import com.reactnativenavigation.views.Component;
  14. import java.util.Collection;
  15. public abstract class ParentController<T extends ViewGroup> extends ChildController {
  16. public ParentController(Activity activity, ChildControllersRegistry childRegistry, String id, OptionsPresenter presenter, Options initialOptions) {
  17. super(activity, childRegistry, id, presenter, initialOptions);
  18. }
  19. @Override
  20. public void setWaitForRender(Bool waitForRender) {
  21. super.setWaitForRender(waitForRender);
  22. applyOnController(getCurrentChild(), controller -> ((ViewController) controller).setWaitForRender(waitForRender));
  23. }
  24. @Override
  25. public void setDefaultOptions(Options defaultOptions) {
  26. Collection<? extends ViewController> children = getChildControllers();
  27. if (!CollectionUtils.isNullOrEmpty(children)) {
  28. for (ViewController child : children) {
  29. child.setDefaultOptions(defaultOptions);
  30. }
  31. }
  32. }
  33. @Override
  34. @CheckResult
  35. public Options resolveCurrentOptions() {
  36. if (CollectionUtils.isNullOrEmpty(getChildControllers())) return options;
  37. return getCurrentChild()
  38. .resolveCurrentOptions()
  39. .copy()
  40. .mergeWith(options);
  41. }
  42. @Override
  43. @CheckResult
  44. public Options resolveCurrentOptions(Options defaultOptions) {
  45. return resolveCurrentOptions().withDefaultOptions(defaultOptions);
  46. }
  47. protected abstract ViewController getCurrentChild();
  48. @NonNull
  49. @Override
  50. public T getView() {
  51. return (T) super.getView();
  52. }
  53. @NonNull
  54. @Override
  55. protected abstract T createView();
  56. @NonNull
  57. public abstract Collection<? extends ViewController> getChildControllers();
  58. @Nullable
  59. @Override
  60. public ViewController findControllerById(final String id) {
  61. ViewController fromSuper = super.findControllerById(id);
  62. if (fromSuper != null) return fromSuper;
  63. for (ViewController child : getChildControllers()) {
  64. ViewController fromChild = child.findControllerById(id);
  65. if (fromChild != null) return fromChild;
  66. }
  67. return null;
  68. }
  69. @Override
  70. public boolean containsComponent(Component component) {
  71. if (super.containsComponent(component)) {
  72. return true;
  73. }
  74. for (ViewController child : getChildControllers()) {
  75. if (child.containsComponent(component)) return true;
  76. }
  77. return false;
  78. }
  79. @CallSuper
  80. public void applyChildOptions(Options options, Component child) {
  81. this.options = this.options.mergeWith(options);
  82. if (isRoot()) {
  83. presenter.applyRootOptions(getView(), options);
  84. }
  85. }
  86. @CallSuper
  87. public void mergeChildOptions(Options options, Component child) {
  88. }
  89. @Override
  90. public void destroy() {
  91. super.destroy();
  92. for (ViewController child : getChildControllers()) {
  93. child.destroy();
  94. }
  95. }
  96. @CallSuper
  97. protected void clearOptions() {
  98. applyOnParentController(parent -> ((ParentController) parent).clearOptions());
  99. options = initialOptions.copy().clearOneTimeOptions();
  100. }
  101. public void setupTopTabsWithViewPager(ViewPager viewPager) {
  102. }
  103. public void clearTopTabs() {
  104. }
  105. @Override
  106. public boolean isRendered() {
  107. return getCurrentChild() != null && getCurrentChild().isRendered();
  108. }
  109. }