|
| 1 | +package com.swmansion.reanimated; |
| 2 | + |
| 3 | +import static com.swmansion.reanimated.Utils.simplifyStringNumbersList; |
| 4 | + |
| 5 | +import androidx.annotation.OptIn; |
| 6 | +import com.facebook.jni.HybridData; |
| 7 | +import com.facebook.proguard.annotations.DoNotStrip; |
| 8 | +import com.facebook.react.bridge.ReactApplicationContext; |
| 9 | +import com.facebook.react.bridge.queue.MessageQueueThread; |
| 10 | +import com.facebook.react.common.annotations.FrameworkAPI; |
| 11 | +import com.facebook.react.turbomodule.core.CallInvokerHolderImpl; |
| 12 | +import com.swmansion.reanimated.layoutReanimation.LayoutAnimations; |
| 13 | +import com.swmansion.reanimated.layoutReanimation.NativeMethodsHolder; |
| 14 | +import com.swmansion.reanimated.nativeProxy.NativeProxyCommon; |
| 15 | +import java.lang.ref.WeakReference; |
| 16 | +import java.util.HashMap; |
| 17 | +import java.util.Objects; |
| 18 | + |
| 19 | +public class NativeProxy extends NativeProxyCommon { |
| 20 | + @DoNotStrip |
| 21 | + @SuppressWarnings("unused") |
| 22 | + private final HybridData mHybridData; |
| 23 | + |
| 24 | + @OptIn(markerClass = FrameworkAPI.class) |
| 25 | + public NativeProxy(ReactApplicationContext context, String valueUnpackerCode) { |
| 26 | + super(context); |
| 27 | + CallInvokerHolderImpl holder = |
| 28 | + (CallInvokerHolderImpl) context.getCatalystInstance().getJSCallInvokerHolder(); |
| 29 | + LayoutAnimations LayoutAnimations = new LayoutAnimations(context); |
| 30 | + ReanimatedMessageQueueThread messageQueueThread = new ReanimatedMessageQueueThread(); |
| 31 | + mHybridData = |
| 32 | + initHybrid( |
| 33 | + Objects.requireNonNull(context.getJavaScriptContextHolder()).get(), |
| 34 | + holder, |
| 35 | + mAndroidUIScheduler, |
| 36 | + LayoutAnimations, |
| 37 | + messageQueueThread, |
| 38 | + valueUnpackerCode); |
| 39 | + prepareLayoutAnimations(LayoutAnimations); |
| 40 | + installJSIBindings(); |
| 41 | + if (BuildConfig.DEBUG) { |
| 42 | + checkCppVersion(); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + @OptIn(markerClass = FrameworkAPI.class) |
| 47 | + private native HybridData initHybrid( |
| 48 | + long jsContext, |
| 49 | + CallInvokerHolderImpl jsCallInvokerHolder, |
| 50 | + AndroidUIScheduler androidUIScheduler, |
| 51 | + LayoutAnimations LayoutAnimations, |
| 52 | + MessageQueueThread messageQueueThread, |
| 53 | + String valueUnpackerCode); |
| 54 | + |
| 55 | + public native boolean isAnyHandlerWaitingForEvent(String eventName, int emitterReactTag); |
| 56 | + |
| 57 | + public native void performOperations(); |
| 58 | + |
| 59 | + @Override |
| 60 | + protected HybridData getHybridData() { |
| 61 | + return mHybridData; |
| 62 | + } |
| 63 | + |
| 64 | + public static NativeMethodsHolder createNativeMethodsHolder(LayoutAnimations layoutAnimations) { |
| 65 | + WeakReference<LayoutAnimations> weakLayoutAnimations = new WeakReference<>(layoutAnimations); |
| 66 | + return new NativeMethodsHolder() { |
| 67 | + @Override |
| 68 | + public void startAnimation(int tag, int type, HashMap<String, Object> values) { |
| 69 | + LayoutAnimations layoutAnimations = weakLayoutAnimations.get(); |
| 70 | + if (layoutAnimations != null) { |
| 71 | + HashMap<String, String> preparedValues = new HashMap<>(); |
| 72 | + for (String key : values.keySet()) { |
| 73 | + String stringValue = values.get(key).toString(); |
| 74 | + if (key.endsWith("TransformMatrix")) { |
| 75 | + preparedValues.put(key, simplifyStringNumbersList(stringValue)); |
| 76 | + } else { |
| 77 | + preparedValues.put(key, stringValue); |
| 78 | + } |
| 79 | + } |
| 80 | + layoutAnimations.startAnimationForTag(tag, type, preparedValues); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public boolean shouldAnimateExiting(int tag, boolean shouldAnimate) { |
| 86 | + LayoutAnimations layoutAnimations = weakLayoutAnimations.get(); |
| 87 | + if (layoutAnimations != null) { |
| 88 | + return layoutAnimations.shouldAnimateExiting(tag, shouldAnimate); |
| 89 | + } |
| 90 | + return false; |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public boolean isLayoutAnimationEnabled() { |
| 95 | + LayoutAnimations layoutAnimations = weakLayoutAnimations.get(); |
| 96 | + if (layoutAnimations != null) { |
| 97 | + return layoutAnimations.isLayoutAnimationEnabled(); |
| 98 | + } |
| 99 | + return false; |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public boolean hasAnimation(int tag, int type) { |
| 104 | + LayoutAnimations layoutAnimations = weakLayoutAnimations.get(); |
| 105 | + if (layoutAnimations != null) { |
| 106 | + return layoutAnimations.hasAnimationForTag(tag, type); |
| 107 | + } |
| 108 | + return false; |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public void clearAnimationConfig(int tag) { |
| 113 | + LayoutAnimations layoutAnimations = weakLayoutAnimations.get(); |
| 114 | + if (layoutAnimations != null) { |
| 115 | + layoutAnimations.clearAnimationConfigForTag(tag); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public void cancelAnimation(int tag) { |
| 121 | + LayoutAnimations layoutAnimations = weakLayoutAnimations.get(); |
| 122 | + if (layoutAnimations != null) { |
| 123 | + layoutAnimations.cancelAnimationForTag(tag); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public int findPrecedingViewTagForTransition(int tag) { |
| 129 | + LayoutAnimations layoutAnimations = weakLayoutAnimations.get(); |
| 130 | + if (layoutAnimations != null) { |
| 131 | + return layoutAnimations.findPrecedingViewTagForTransition(tag); |
| 132 | + } |
| 133 | + return -1; |
| 134 | + } |
| 135 | + |
| 136 | + public void checkDuplicateSharedTag(int viewTag, int screenTag) { |
| 137 | + LayoutAnimations layoutAnimations = weakLayoutAnimations.get(); |
| 138 | + if (layoutAnimations != null) { |
| 139 | + layoutAnimations.checkDuplicateSharedTag(viewTag, screenTag); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + public int[] getSharedGroup(int viewTag) { |
| 144 | + LayoutAnimations layoutAnimations = weakLayoutAnimations.get(); |
| 145 | + if (layoutAnimations != null) { |
| 146 | + return layoutAnimations.getSharedGroup(viewTag); |
| 147 | + } |
| 148 | + return new int[] {}; |
| 149 | + } |
| 150 | + }; |
| 151 | + } |
| 152 | +} |
0 commit comments