react-native-navigation的迁移库

ReactView.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package com.reactnativenavigation.react;
  2. import android.annotation.SuppressLint;
  3. import android.content.Context;
  4. import android.os.Bundle;
  5. import android.view.MotionEvent;
  6. import com.facebook.react.ReactInstanceManager;
  7. import com.facebook.react.ReactRootView;
  8. import com.facebook.react.bridge.ReactContext;
  9. import com.facebook.react.uimanager.JSTouchDispatcher;
  10. import com.facebook.react.uimanager.UIManagerModule;
  11. import com.facebook.react.uimanager.events.EventDispatcher;
  12. import com.reactnativenavigation.interfaces.ScrollEventListener;
  13. import com.reactnativenavigation.react.events.ComponentType;
  14. import com.reactnativenavigation.react.events.EventEmitter;
  15. import com.reactnativenavigation.viewcontrollers.IReactView;
  16. import com.reactnativenavigation.views.Renderable;
  17. import androidx.annotation.RestrictTo;
  18. @SuppressLint("ViewConstructor")
  19. public class ReactView extends ReactRootView implements IReactView, Renderable {
  20. private final ReactInstanceManager reactInstanceManager;
  21. private final String componentId;
  22. private final String componentName;
  23. private boolean isAttachedToReactInstance = false;
  24. private final JSTouchDispatcher jsTouchDispatcher;
  25. public ReactView(final Context context, ReactInstanceManager reactInstanceManager, String componentId, String componentName) {
  26. super(context);
  27. this.reactInstanceManager = reactInstanceManager;
  28. this.componentId = componentId;
  29. this.componentName = componentName;
  30. jsTouchDispatcher = new JSTouchDispatcher(this);
  31. }
  32. @Override
  33. protected void onAttachedToWindow() {
  34. super.onAttachedToWindow();
  35. start();
  36. }
  37. private void start() {
  38. if (isAttachedToReactInstance) return;
  39. isAttachedToReactInstance = true;
  40. final Bundle opts = new Bundle();
  41. opts.putString("componentId", componentId);
  42. startReactApplication(reactInstanceManager, componentName, opts);
  43. }
  44. @Override
  45. public boolean isReady() {
  46. return isAttachedToReactInstance;
  47. }
  48. @Override
  49. public ReactView asView() {
  50. return this;
  51. }
  52. @Override
  53. public void destroy() {
  54. unmountReactApplication();
  55. }
  56. public void sendComponentStart(ComponentType type) {
  57. ReactContext currentReactContext = reactInstanceManager.getCurrentReactContext();
  58. if (currentReactContext != null) {
  59. new EventEmitter(currentReactContext).emitComponentDidAppear(componentId, componentName, type);
  60. }
  61. }
  62. public void sendComponentStop(ComponentType type) {
  63. ReactContext currentReactContext = reactInstanceManager.getCurrentReactContext();
  64. if (currentReactContext != null) {
  65. new EventEmitter(currentReactContext).emitComponentDidDisappear(componentId, componentName, type);
  66. }
  67. }
  68. @Override
  69. public void sendOnNavigationButtonPressed(String buttonId) {
  70. ReactContext currentReactContext = reactInstanceManager.getCurrentReactContext();
  71. if (currentReactContext != null) {
  72. new EventEmitter(currentReactContext).emitOnNavigationButtonPressed(componentId, buttonId);
  73. }
  74. }
  75. @Override
  76. public ScrollEventListener getScrollEventListener() {
  77. return new ScrollEventListener(getEventDispatcher());
  78. }
  79. @Override
  80. public void dispatchTouchEventToJs(MotionEvent event) {
  81. jsTouchDispatcher.handleTouchEvent(event, getEventDispatcher());
  82. }
  83. @Override
  84. public boolean isRendered() {
  85. return getChildCount() >= 1;
  86. }
  87. public EventDispatcher getEventDispatcher() {
  88. ReactContext reactContext = reactInstanceManager.getCurrentReactContext();
  89. return reactContext == null ? null : reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher();
  90. }
  91. @RestrictTo(RestrictTo.Scope.TESTS)
  92. public String getComponentName() {
  93. return componentName;
  94. }
  95. }