|
| 1 | +package com.rcttabview |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.view.View |
| 5 | +import com.facebook.react.bridge.ReactApplicationContext |
| 6 | +import com.facebook.react.bridge.ReadableArray |
| 7 | +import com.facebook.react.bridge.ReadableMap |
| 8 | +import com.facebook.react.module.annotations.ReactModule |
| 9 | +import com.facebook.react.uimanager.PixelUtil.toDIPFromPixel |
| 10 | +import com.facebook.react.uimanager.SimpleViewManager |
| 11 | +import com.facebook.react.uimanager.ThemedReactContext |
| 12 | +import com.facebook.react.uimanager.UIManagerHelper |
| 13 | +import com.facebook.react.uimanager.ViewManagerDelegate |
| 14 | +import com.facebook.react.viewmanagers.RNCTabViewManagerDelegate |
| 15 | +import com.facebook.react.viewmanagers.RNCTabViewManagerInterface |
| 16 | +import com.facebook.yoga.YogaMeasureMode |
| 17 | +import com.facebook.yoga.YogaMeasureOutput |
| 18 | + |
| 19 | + |
| 20 | +@ReactModule(name = RCTTabViewImpl.NAME) |
| 21 | +class RCTTabViewManager(context: ReactApplicationContext) : |
| 22 | + SimpleViewManager<ReactBottomNavigationView>(), |
| 23 | + RNCTabViewManagerInterface<ReactBottomNavigationView> { |
| 24 | + |
| 25 | + private val contextInner: ReactApplicationContext = context |
| 26 | + private val delegate: RNCTabViewManagerDelegate<ReactBottomNavigationView, RCTTabViewManager> = |
| 27 | + RNCTabViewManagerDelegate(this) |
| 28 | + private val tabViewImpl: RCTTabViewImpl = RCTTabViewImpl() |
| 29 | + |
| 30 | + override fun createViewInstance(context: ThemedReactContext): ReactBottomNavigationView { |
| 31 | + val view = ReactBottomNavigationView(context) |
| 32 | + val eventDispatcher = UIManagerHelper.getEventDispatcherForReactTag(context, view.id) |
| 33 | + view.onTabSelectedListener = { data -> |
| 34 | + data.getString("key")?.let { |
| 35 | + eventDispatcher?.dispatchEvent(PageSelectedEvent(viewTag = view.id, key = it)) |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + view.onTabLongPressedListener = { data -> |
| 40 | + data.getString("key")?.let { |
| 41 | + eventDispatcher?.dispatchEvent(TabLongPressEvent(viewTag = view.id, key = it)) |
| 42 | + } |
| 43 | + } |
| 44 | + return view |
| 45 | + |
| 46 | + } |
| 47 | + |
| 48 | + override fun getName(): String { |
| 49 | + return tabViewImpl.getName() |
| 50 | + } |
| 51 | + |
| 52 | + override fun setItems(view: ReactBottomNavigationView?, value: ReadableArray?) { |
| 53 | + if (view != null && value != null) |
| 54 | + tabViewImpl.setItems(view, value) |
| 55 | + } |
| 56 | + |
| 57 | + override fun setSelectedPage(view: ReactBottomNavigationView?, value: String?) { |
| 58 | + if (view != null && value != null) |
| 59 | + tabViewImpl.setSelectedPage(view, value) |
| 60 | + } |
| 61 | + |
| 62 | + override fun setIcons(view: ReactBottomNavigationView?, value: ReadableArray?) { |
| 63 | + if (view != null) |
| 64 | + tabViewImpl.setIcons(view, value) |
| 65 | + } |
| 66 | + |
| 67 | + override fun setLabeled(view: ReactBottomNavigationView?, value: Boolean) { |
| 68 | + if (view != null) |
| 69 | + tabViewImpl.setLabeled(view, value) |
| 70 | + } |
| 71 | + |
| 72 | + override fun setRippleColor(view: ReactBottomNavigationView?, value: Int?) { |
| 73 | + if (view != null && value != null) |
| 74 | + tabViewImpl.setRippleColor(view, value) |
| 75 | + } |
| 76 | + |
| 77 | + override fun setBarTintColor(view: ReactBottomNavigationView?, value: Int?) { |
| 78 | + if (view != null && value != null) |
| 79 | + tabViewImpl.setBarTintColor(view, value) |
| 80 | + } |
| 81 | + |
| 82 | + override fun setActiveTintColor(view: ReactBottomNavigationView?, value: Int?) { |
| 83 | + if (view != null && value != null) |
| 84 | + tabViewImpl.setActiveTintColor(view, value) |
| 85 | + } |
| 86 | + |
| 87 | + override fun setInactiveTintColor(view: ReactBottomNavigationView?, value: Int?) { |
| 88 | + if (view != null && value != null) |
| 89 | + tabViewImpl.setInactiveTintColor(view, value) |
| 90 | + } |
| 91 | + |
| 92 | + override fun getDelegate(): ViewManagerDelegate<ReactBottomNavigationView> { |
| 93 | + return delegate |
| 94 | + } |
| 95 | + |
| 96 | + public override fun measure( |
| 97 | + context: Context?, |
| 98 | + localData: ReadableMap?, |
| 99 | + props: ReadableMap?, |
| 100 | + state: ReadableMap?, |
| 101 | + width: Float, |
| 102 | + widthMode: YogaMeasureMode?, |
| 103 | + height: Float, |
| 104 | + heightMode: YogaMeasureMode?, |
| 105 | + attachmentsPositions: FloatArray? |
| 106 | + ): Long { |
| 107 | + val view = ReactBottomNavigationView(context ?: contextInner) |
| 108 | + val measureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED) |
| 109 | + view.measure(measureSpec, measureSpec) |
| 110 | + |
| 111 | + return YogaMeasureOutput.make( |
| 112 | + toDIPFromPixel(view.measuredWidth.toFloat()), |
| 113 | + toDIPFromPixel(view.measuredHeight.toFloat()) |
| 114 | + ) |
| 115 | + } |
| 116 | + |
| 117 | + // iOS Methods |
| 118 | + |
| 119 | + override fun setTranslucent(view: ReactBottomNavigationView?, value: Boolean) { |
| 120 | + } |
| 121 | + |
| 122 | + override fun setIgnoresTopSafeArea(view: ReactBottomNavigationView?, value: Boolean) { |
| 123 | + } |
| 124 | + |
| 125 | + override fun setDisablePageAnimations(view: ReactBottomNavigationView?, value: Boolean) { |
| 126 | + } |
| 127 | + |
| 128 | + override fun setSidebarAdaptable(view: ReactBottomNavigationView?, value: Boolean) { |
| 129 | + } |
| 130 | + |
| 131 | + override fun setScrollEdgeAppearance(view: ReactBottomNavigationView?, value: String?) { |
| 132 | + } |
| 133 | +} |
0 commit comments