react-native-navigation的迁移库

BottomTabsLayout.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. package com.reactnativenavigation.layouts;
  2. import android.support.annotation.NonNull;
  3. import android.support.annotation.Nullable;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.view.View;
  6. import android.widget.RelativeLayout;
  7. import com.aurelhubert.ahbottomnavigation.AHBottomNavigation;
  8. import com.facebook.react.bridge.Arguments;
  9. import com.facebook.react.bridge.Callback;
  10. import com.facebook.react.bridge.WritableMap;
  11. import com.reactnativenavigation.NavigationApplication;
  12. import com.reactnativenavigation.events.EventBus;
  13. import com.reactnativenavigation.events.ScreenChangedEvent;
  14. import com.reactnativenavigation.params.ActivityParams;
  15. import com.reactnativenavigation.params.ContextualMenuParams;
  16. import com.reactnativenavigation.params.FabParams;
  17. import com.reactnativenavigation.params.ScreenParams;
  18. import com.reactnativenavigation.params.SideMenuParams;
  19. import com.reactnativenavigation.params.SnackbarParams;
  20. import com.reactnativenavigation.params.TitleBarButtonParams;
  21. import com.reactnativenavigation.params.TitleBarLeftButtonParams;
  22. import com.reactnativenavigation.screens.Screen;
  23. import com.reactnativenavigation.screens.ScreenStack;
  24. import com.reactnativenavigation.views.BottomTabs;
  25. import com.reactnativenavigation.views.SideMenu;
  26. import com.reactnativenavigation.views.SideMenu.Side;
  27. import com.reactnativenavigation.views.SnackbarAndFabContainer;
  28. import java.util.List;
  29. import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
  30. import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
  31. public class BottomTabsLayout extends RelativeLayout implements Layout, AHBottomNavigation.OnTabSelectedListener {
  32. private final AppCompatActivity activity;
  33. private ActivityParams params;
  34. private SnackbarAndFabContainer snackbarAndFabContainer;
  35. private BottomTabs bottomTabs;
  36. private ScreenStack[] screenStacks;
  37. private final SideMenuParams leftSideMenuParams;
  38. private final SideMenuParams rightSideMenuParams;
  39. private @Nullable SideMenu sideMenu;
  40. private int currentStackIndex = 0;
  41. public BottomTabsLayout(AppCompatActivity activity, ActivityParams params) {
  42. super(activity);
  43. this.activity = activity;
  44. this.params = params;
  45. leftSideMenuParams = params.leftSideMenuParams;
  46. rightSideMenuParams = params.rightSideMenuParams;
  47. screenStacks = new ScreenStack[params.tabParams.size()];
  48. createLayout();
  49. }
  50. private void createLayout() {
  51. createSideMenu();
  52. createBottomTabs();
  53. addBottomTabs();
  54. addScreenStacks();
  55. createSnackbarContainer();
  56. showInitialScreenStack();
  57. }
  58. private void createSideMenu() {
  59. if (leftSideMenuParams == null && rightSideMenuParams == null) {
  60. return;
  61. }
  62. sideMenu = new SideMenu(getContext(), leftSideMenuParams, rightSideMenuParams);
  63. RelativeLayout.LayoutParams lp = new LayoutParams(MATCH_PARENT, MATCH_PARENT);
  64. addView(sideMenu, lp);
  65. }
  66. private void addScreenStacks() {
  67. for (int i = screenStacks.length - 1; i >= 0; i--) {
  68. createAndAddScreens(i);
  69. }
  70. }
  71. private void createAndAddScreens(int position) {
  72. ScreenParams screenParams = params.tabParams.get(position);
  73. ScreenStack newStack = new ScreenStack(activity, getScreenStackParent(), screenParams.getNavigatorId(), this);
  74. newStack.pushInitialScreen(screenParams, createScreenLayoutParams(screenParams));
  75. screenStacks[position] = newStack;
  76. }
  77. private RelativeLayout getScreenStackParent() {
  78. return sideMenu == null ? this : sideMenu.getContentContainer();
  79. }
  80. @NonNull
  81. private LayoutParams createScreenLayoutParams(ScreenParams params) {
  82. LayoutParams lp = new LayoutParams(MATCH_PARENT, MATCH_PARENT);
  83. if (params.styleParams.drawScreenAboveBottomTabs) {
  84. lp.addRule(RelativeLayout.ABOVE, bottomTabs.getId());
  85. }
  86. return lp;
  87. }
  88. private void createBottomTabs() {
  89. bottomTabs = new BottomTabs(getContext());
  90. bottomTabs.addTabs(params.tabParams, this);
  91. }
  92. private void addBottomTabs() {
  93. LayoutParams lp = new LayoutParams(MATCH_PARENT, WRAP_CONTENT);
  94. lp.addRule(ALIGN_PARENT_BOTTOM);
  95. getScreenStackParent().addView(bottomTabs, lp);
  96. }
  97. private void createSnackbarContainer() {
  98. snackbarAndFabContainer = new SnackbarAndFabContainer(getContext(), this);
  99. RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT);
  100. lp.addRule(ABOVE, bottomTabs.getId());
  101. getScreenStackParent().addView(snackbarAndFabContainer, lp);
  102. }
  103. private void showInitialScreenStack() {
  104. showStackAndUpdateStyle(screenStacks[0]);
  105. EventBus.instance.post(new ScreenChangedEvent(screenStacks[0].peek().getScreenParams()));
  106. }
  107. @Override
  108. public View asView() {
  109. return this;
  110. }
  111. @Override
  112. public boolean onBackPressed() {
  113. if (getCurrentScreenStack().handleBackPressInJs()) {
  114. return true;
  115. }
  116. if (getCurrentScreenStack().canPop()) {
  117. getCurrentScreenStack().pop(true);
  118. setBottomTabsStyleFromCurrentScreen();
  119. EventBus.instance.post(new ScreenChangedEvent(getCurrentScreenStack().peek().getScreenParams()));
  120. return true;
  121. } else {
  122. return false;
  123. }
  124. }
  125. @Override
  126. public void setTopBarVisible(String screenInstanceId, boolean hidden, boolean animated) {
  127. for (int i = 0; i < bottomTabs.getItemsCount(); i++) {
  128. screenStacks[i].setScreenTopBarVisible(screenInstanceId, hidden, animated);
  129. }
  130. }
  131. public void setBottomTabsVisible(boolean hidden, boolean animated) {
  132. bottomTabs.setVisibility(hidden, animated);
  133. }
  134. @Override
  135. public void setTitleBarTitle(String screenInstanceId, String title) {
  136. for (int i = 0; i < bottomTabs.getItemsCount(); i++) {
  137. screenStacks[i].setScreenTitleBarTitle(screenInstanceId, title);
  138. }
  139. }
  140. @Override
  141. public void setTitleBarSubtitle(String screenInstanceId, String subtitle) {
  142. for (int i = 0; i < bottomTabs.getItemsCount(); i++) {
  143. screenStacks[i].setScreenTitleBarSubtitle(screenInstanceId, subtitle);
  144. }
  145. }
  146. @Override
  147. public void setTitleBarRightButtons(String screenInstanceId, String navigatorEventId, List<TitleBarButtonParams> titleBarButtons) {
  148. for (int i = 0; i < bottomTabs.getItemsCount(); i++) {
  149. screenStacks[i].setScreenTitleBarRightButtons(screenInstanceId, navigatorEventId, titleBarButtons);
  150. }
  151. }
  152. @Override
  153. public void setTitleBarLeftButton(String screenInstanceId, String navigatorEventId, TitleBarLeftButtonParams titleBarLeftButtonParams) {
  154. for (int i = 0; i < bottomTabs.getItemsCount(); i++) {
  155. screenStacks[i].setScreenTitleBarLeftButton(screenInstanceId, navigatorEventId, titleBarLeftButtonParams);
  156. }
  157. }
  158. @Override
  159. public void setFab(String screenInstanceId, String navigatorEventId, FabParams fabParams) {
  160. for (int i = 0; i < bottomTabs.getItemsCount(); i++) {
  161. screenStacks[i].setFab(screenInstanceId, navigatorEventId, fabParams);
  162. }
  163. }
  164. @Override
  165. public void toggleSideMenuVisible(boolean animated, Side side) {
  166. if (sideMenu != null) {
  167. sideMenu.toggleVisible(animated, side);
  168. }
  169. }
  170. @Override
  171. public void setSideMenuVisible(boolean animated, boolean visible, Side side) {
  172. if (sideMenu != null) {
  173. sideMenu.setVisible(visible, animated, side);
  174. }
  175. }
  176. @Override
  177. public void showSnackbar(SnackbarParams params) {
  178. final String eventId = getCurrentScreenStack().peek().getNavigatorEventId();
  179. snackbarAndFabContainer.showSnackbar(eventId, params);
  180. }
  181. @Override
  182. public void onModalDismissed() {
  183. EventBus.instance.post(new ScreenChangedEvent(getCurrentScreenStack().peek().getScreenParams()));
  184. }
  185. @Override
  186. public boolean containsNavigator(String navigatorId) {
  187. // Unused
  188. return false;
  189. }
  190. @Override
  191. public void showContextualMenu(String screenInstanceId, ContextualMenuParams params, Callback onButtonClicked) {
  192. getCurrentScreenStack().peek().showContextualMenu(params, onButtonClicked);
  193. }
  194. @Override
  195. public void dismissContextualMenu(String screenInstanceId) {
  196. getCurrentScreenStack().peek().dismissContextualMenu();
  197. }
  198. @Override
  199. public Screen getCurrentScreen() {
  200. return getCurrentScreenStack().peek();
  201. }
  202. public void selectBottomTabByTabIndex(Integer index) {
  203. bottomTabs.setCurrentItem(index);
  204. }
  205. public void selectBottomTabByNavigatorId(String navigatorId) {
  206. bottomTabs.setCurrentItem(getScreenStackIndex(navigatorId));
  207. }
  208. @Override
  209. public void push(ScreenParams params) {
  210. ScreenStack screenStack = getScreenStack(params.getNavigatorId());
  211. screenStack.push(params, createScreenLayoutParams(params));
  212. if (isCurrentStack(screenStack)) {
  213. bottomTabs.setStyleFromScreen(params.styleParams);
  214. EventBus.instance.post(new ScreenChangedEvent(params));
  215. }
  216. }
  217. @Override
  218. public void pop(ScreenParams params) {
  219. getCurrentScreenStack().pop(params.animateScreenTransitions, new ScreenStack.OnScreenPop() {
  220. @Override
  221. public void onScreenPopAnimationEnd() {
  222. setBottomTabsStyleFromCurrentScreen();
  223. }
  224. });
  225. EventBus.instance.post(new ScreenChangedEvent(getCurrentScreenStack().peek().getScreenParams()));
  226. }
  227. @Override
  228. public void popToRoot(ScreenParams params) {
  229. getCurrentScreenStack().popToRoot(params.animateScreenTransitions);
  230. setBottomTabsStyleFromCurrentScreen();
  231. EventBus.instance.post(new ScreenChangedEvent(getCurrentScreenStack().peek().getScreenParams()));
  232. }
  233. @Override
  234. public void newStack(ScreenParams params) {
  235. ScreenStack currentScreenStack = getCurrentScreenStack();
  236. removeView(currentScreenStack.peek());
  237. currentScreenStack.destroy();
  238. ScreenStack newStack = new ScreenStack(activity, getScreenStackParent(), params.getNavigatorId(), this);
  239. LayoutParams lp = createScreenLayoutParams(params);
  240. newStack.pushInitialScreenWithAnimation(params, lp);
  241. screenStacks[currentStackIndex] = newStack;
  242. bottomTabs.setStyleFromScreen(params.styleParams);
  243. EventBus.instance.post(new ScreenChangedEvent(params));
  244. }
  245. @Override
  246. public void destroy() {
  247. snackbarAndFabContainer.destroy();
  248. for (ScreenStack screenStack : screenStacks) {
  249. screenStack.destroy();
  250. }
  251. if (sideMenu != null) {
  252. sideMenu.destroy();
  253. }
  254. }
  255. @Override
  256. public boolean onTabSelected(int position, boolean wasSelected) {
  257. if (wasSelected) {
  258. return false;
  259. }
  260. hideCurrentStack();
  261. showNewStack(position);
  262. EventBus.instance.post(new ScreenChangedEvent(getCurrentScreenStack().peek().getScreenParams()));
  263. sendTabSelectedEventToJs();
  264. return true;
  265. }
  266. private void sendTabSelectedEventToJs() {
  267. WritableMap data = Arguments.createMap();
  268. String navigatorEventId = getCurrentScreenStack().peek().getNavigatorEventId();
  269. NavigationApplication.instance.getEventEmitter().sendNavigatorEvent("bottomTabSelected", navigatorEventId, data);
  270. }
  271. private void showNewStack(int position) {
  272. showStackAndUpdateStyle(screenStacks[position]);
  273. currentStackIndex = position;
  274. }
  275. private void showStackAndUpdateStyle(ScreenStack newStack) {
  276. newStack.show();
  277. bottomTabs.setStyleFromScreen(newStack.getCurrentScreenStyleParams());
  278. }
  279. private void hideCurrentStack() {
  280. ScreenStack currentScreenStack = getCurrentScreenStack();
  281. currentScreenStack.hide();
  282. }
  283. private ScreenStack getCurrentScreenStack() {
  284. return screenStacks[currentStackIndex];
  285. }
  286. private @NonNull ScreenStack getScreenStack(String navigatorId) {
  287. int index = getScreenStackIndex(navigatorId);
  288. return screenStacks[index];
  289. }
  290. public void setBottomTabBadgeByIndex(Integer index, String badge) {
  291. bottomTabs.setNotification(badge, index);
  292. }
  293. public void setBottomTabBadgeByNavigatorId(String navigatorId, String badge) {
  294. bottomTabs.setNotification(badge, getScreenStackIndex(navigatorId));
  295. }
  296. private int getScreenStackIndex(String navigatorId) throws ScreenStackNotFoundException {
  297. for (int i = 0; i < screenStacks.length; i++) {
  298. if (screenStacks[i].getNavigatorId().equals(navigatorId)) {
  299. return i;
  300. }
  301. }
  302. throw new ScreenStackNotFoundException("Stack " + navigatorId + " not found");
  303. }
  304. private class ScreenStackNotFoundException extends RuntimeException {
  305. ScreenStackNotFoundException(String navigatorId) {
  306. super(navigatorId);
  307. }
  308. }
  309. private boolean isCurrentStack(ScreenStack screenStack) {
  310. return getCurrentScreenStack() == screenStack;
  311. }
  312. private void setBottomTabsStyleFromCurrentScreen() {
  313. bottomTabs.setStyleFromScreen(getCurrentScreenStack().getCurrentScreenStyleParams());
  314. }
  315. @Override
  316. public boolean onTitleBarBackButtonClick() {
  317. if (getCurrentScreenStack().canPop()) {
  318. getCurrentScreenStack().pop(true, new ScreenStack.OnScreenPop() {
  319. @Override
  320. public void onScreenPopAnimationEnd() {
  321. setBottomTabsStyleFromCurrentScreen();
  322. EventBus.instance.post(new ScreenChangedEvent(getCurrentScreenStack().peek().getScreenParams()));
  323. }
  324. });
  325. return true;
  326. }
  327. return false;
  328. }
  329. @Override
  330. public void onSideMenuButtonClick() {
  331. final String navigatorEventId = getCurrentScreenStack().peek().getNavigatorEventId();
  332. NavigationApplication.instance.getEventEmitter().sendNavigatorEvent("sideMenu", navigatorEventId);
  333. if (sideMenu != null) {
  334. sideMenu.openDrawer(Side.Left);
  335. }
  336. }
  337. }