react-native-navigation的迁移库

LeftButton.java 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package com.reactnativenavigation.views;
  2. import android.content.Context;
  3. import android.graphics.Color;
  4. import android.view.View;
  5. import com.balysv.materialmenu.MaterialMenuDrawable;
  6. import com.reactnativenavigation.NavigationApplication;
  7. import com.reactnativenavigation.params.TitleBarButtonParams;
  8. import com.reactnativenavigation.params.TitleBarLeftButtonParams;
  9. class LeftButton extends MaterialMenuDrawable implements View.OnClickListener {
  10. private static int getColor(TitleBarButtonParams params) {
  11. return params != null && params.color.hasColor() ?
  12. params.color.getColor() :
  13. Color.BLACK;
  14. }
  15. private TitleBarLeftButtonParams params;
  16. private final LeftButtonOnClickListener onClickListener;
  17. private final String navigatorEventId;
  18. private final boolean overrideBackPressInJs;
  19. LeftButton(Context context,
  20. TitleBarLeftButtonParams params,
  21. LeftButtonOnClickListener onClickListener,
  22. String navigatorEventId,
  23. boolean overrideBackPressInJs) {
  24. super(context, getColor(params), Stroke.THIN);
  25. this.params = params;
  26. this.onClickListener = onClickListener;
  27. this.navigatorEventId = navigatorEventId;
  28. this.overrideBackPressInJs = overrideBackPressInJs;
  29. setInitialState();
  30. }
  31. void setIconState(TitleBarLeftButtonParams params) {
  32. this.params = params;
  33. if (params.color.hasColor()) {
  34. setColor(params.color.getColor());
  35. }
  36. animateIconState(params.iconState);
  37. }
  38. @Override
  39. public void onClick(View v) {
  40. if (isBackButton()) {
  41. handleBackButtonClick();
  42. } else if (isSideMenuButton()) {
  43. onClickListener.onSideMenuButtonClick();
  44. } else {
  45. sendClickEvent();
  46. }
  47. }
  48. private void handleBackButtonClick() {
  49. if (overrideBackPressInJs) {
  50. NavigationApplication.instance.getEventEmitter().sendNavigatorEvent("backPress", navigatorEventId);
  51. } else {
  52. onClickListener.onTitleBarBackButtonClick();
  53. }
  54. }
  55. private void setInitialState() {
  56. if (params != null) {
  57. setIconState(params.iconState);
  58. } else {
  59. setVisible(false);
  60. }
  61. }
  62. private boolean isBackButton() {
  63. return getIconState() == IconState.ARROW;
  64. }
  65. private boolean isSideMenuButton() {
  66. return getIconState() == IconState.BURGER;
  67. }
  68. private void sendClickEvent() {
  69. NavigationApplication.instance.getEventEmitter().sendNavigatorEvent(params.eventId, navigatorEventId);
  70. }
  71. }