react-native-navigation的迁移库

ModalController.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package com.reactnativenavigation.controllers;
  2. import android.support.v7.app.AppCompatActivity;
  3. import com.facebook.react.bridge.Callback;
  4. import com.reactnativenavigation.events.EventBus;
  5. import com.reactnativenavigation.events.ModalDismissedEvent;
  6. import com.reactnativenavigation.layouts.ScreenStackContainer;
  7. import com.reactnativenavigation.params.ContextualMenuParams;
  8. import com.reactnativenavigation.params.ScreenParams;
  9. import com.reactnativenavigation.params.TitleBarButtonParams;
  10. import com.reactnativenavigation.params.TitleBarLeftButtonParams;
  11. import java.util.List;
  12. import java.util.Stack;
  13. public class ModalController implements ScreenStackContainer, Modal.OnModalDismissedListener {
  14. private final AppCompatActivity activity;
  15. private Stack<Modal> stack = new Stack<>();
  16. public ModalController(AppCompatActivity activity) {
  17. this.activity = activity;
  18. }
  19. public boolean containsNavigator(String navigatorId) {
  20. for (Modal modal : stack) {
  21. if (modal.containsNavigator(navigatorId)) {
  22. return true;
  23. }
  24. }
  25. return false;
  26. }
  27. public void showModal(ScreenParams screenParams) {
  28. Modal modal = new Modal(activity, this, screenParams);
  29. modal.show();
  30. stack.add(modal);
  31. }
  32. public void dismissTopModal() {
  33. if (isShowing()) {
  34. stack.pop().dismiss();
  35. }
  36. }
  37. public void dismissAllModals() {
  38. for (Modal modal : stack) {
  39. modal.dismiss();
  40. }
  41. stack.clear();
  42. }
  43. public boolean isShowing() {
  44. return !stack.empty();
  45. }
  46. public void push(ScreenParams params) {
  47. stack.peek().push(params);
  48. }
  49. @Override
  50. public void pop(ScreenParams screenParams) {
  51. stack.peek().pop(screenParams);
  52. }
  53. @Override
  54. public void popToRoot(ScreenParams params) {
  55. stack.peek().popToRoot(params);
  56. }
  57. @Override
  58. public void newStack(ScreenParams params) {
  59. stack.peek().newStack(params);
  60. }
  61. @Override
  62. public void destroy() {
  63. for (Modal modal : stack) {
  64. modal.destroy();
  65. modal.dismiss();
  66. }
  67. stack.clear();
  68. }
  69. @Override
  70. public void onModalDismissed(Modal modal) {
  71. stack.remove(modal);
  72. if (isShowing()) {
  73. stack.peek().onModalDismissed();
  74. }
  75. EventBus.instance.post(new ModalDismissedEvent());
  76. }
  77. public void setTopBarVisible(String screenInstanceId, boolean hidden, boolean animated) {
  78. for (Modal modal : stack) {
  79. modal.setTopBarVisible(screenInstanceId, hidden, animated);
  80. }
  81. }
  82. public void setTitleBarTitle(String screenInstanceId, String title) {
  83. for (Modal modal : stack) {
  84. modal.setTitleBarTitle(screenInstanceId, title);
  85. }
  86. }
  87. public void setTitleBarSubtitle(String screenInstanceId, String subtitle) {
  88. for (Modal modal : stack) {
  89. modal.setTitleBarSubtitle(screenInstanceId, subtitle);
  90. }
  91. }
  92. public void setTitleBarRightButtons(String screenInstanceId, String navigatorEventId, List<TitleBarButtonParams> titleBarButtons) {
  93. for (Modal modal : stack) {
  94. modal.setTitleBarRightButtons(screenInstanceId, navigatorEventId, titleBarButtons);
  95. }
  96. }
  97. public void setTitleBarLeftButton(String screenInstanceId, String navigatorEventId, TitleBarLeftButtonParams titleBarLeftButton) {
  98. for (Modal modal : stack) {
  99. modal.setTitleBarLeftButton(screenInstanceId, navigatorEventId, titleBarLeftButton);
  100. }
  101. }
  102. public void showContextualMenu(String screenInstanceId, ContextualMenuParams params, Callback onButtonClicked) {
  103. for (Modal modal : stack) {
  104. modal.showContextualMenu(screenInstanceId, params, onButtonClicked);
  105. }
  106. }
  107. public void dismissContextualMenu(String screenInstanceId) {
  108. for (Modal modal : stack) {
  109. modal.dismissContextualMenu(screenInstanceId);
  110. }
  111. }
  112. @Override
  113. public boolean onTitleBarBackButtonClick() {
  114. // Do nothing and let the layout handle the back button click
  115. return false;
  116. }
  117. @Override
  118. public void onSideMenuButtonClick() {
  119. // Do nothing and let the layout handle the click
  120. }
  121. }