forked from software-mansion/react-native-screens
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refactor snapshots when going back on Fabric (software-mansion#…
…2134) PR adding snapshots when going back on Fabric on Android and changing the behavior a bit on iOS.
- Loading branch information
Showing
20 changed files
with
341 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
android/src/fabric/java/com/swmansion/rnscreens/NativeProxy.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.swmansion.rnscreens | ||
|
||
import android.util.Log | ||
import com.facebook.jni.HybridData | ||
import com.facebook.proguard.annotations.DoNotStrip | ||
import com.facebook.react.fabric.FabricUIManager | ||
import java.lang.ref.WeakReference | ||
import java.util.concurrent.ConcurrentHashMap | ||
|
||
class NativeProxy { | ||
@DoNotStrip | ||
@Suppress("unused") | ||
private val mHybridData: HybridData | ||
|
||
init { | ||
mHybridData = initHybrid() | ||
} | ||
|
||
private external fun initHybrid(): HybridData | ||
|
||
external fun nativeAddMutationsListener(fabricUIManager: FabricUIManager) | ||
|
||
companion object { | ||
// we use ConcurrentHashMap here since it will be read on the JS thread, | ||
// and written to on the UI thread. | ||
private val viewsMap = ConcurrentHashMap<Int, WeakReference<Screen>>() | ||
|
||
fun addScreenToMap( | ||
tag: Int, | ||
view: Screen, | ||
) { | ||
viewsMap[tag] = WeakReference(view) | ||
} | ||
|
||
fun removeScreenFromMap(tag: Int) { | ||
viewsMap.remove(tag) | ||
} | ||
|
||
fun clearMapOnInvalidate() { | ||
viewsMap.clear() | ||
} | ||
} | ||
|
||
@DoNotStrip | ||
public fun notifyScreenRemoved(screenTag: Int) { | ||
val screen = viewsMap[screenTag]?.get() | ||
if (screen is Screen) { | ||
screen.startRemovalTransition() | ||
} else { | ||
Log.w("[RNScreens]", "Did not find view with tag $screenTag.") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#include <fbjni/fbjni.h> | ||
#include <react/fabric/Binding.h> | ||
#include <react/renderer/scheduler/Scheduler.h> | ||
|
||
#include <string> | ||
|
||
#include "NativeProxy.h" | ||
|
||
using namespace facebook; | ||
using namespace react; | ||
|
||
namespace rnscreens { | ||
|
||
NativeProxy::NativeProxy(jni::alias_ref<NativeProxy::javaobject> jThis) | ||
: javaPart_(jni::make_global(jThis)) {} | ||
|
||
NativeProxy::~NativeProxy() {} | ||
|
||
void NativeProxy::registerNatives() { | ||
registerHybrid( | ||
{makeNativeMethod("initHybrid", NativeProxy::initHybrid), | ||
makeNativeMethod( | ||
"nativeAddMutationsListener", | ||
NativeProxy::nativeAddMutationsListener)}); | ||
} | ||
|
||
void NativeProxy::nativeAddMutationsListener( | ||
jni::alias_ref<facebook::react::JFabricUIManager::javaobject> | ||
fabricUIManager) { | ||
auto uiManager = | ||
fabricUIManager->getBinding()->getScheduler()->getUIManager(); | ||
screenRemovalListener_ = | ||
std::make_shared<RNSScreenRemovalListener>([this](int tag) { | ||
static const auto method = | ||
javaPart_->getClass()->getMethod<void(jint)>("notifyScreenRemoved"); | ||
method(javaPart_, tag); | ||
}); | ||
|
||
uiManager->getShadowTreeRegistry().enumerate( | ||
[this](const facebook::react::ShadowTree &shadowTree, bool &stop) { | ||
shadowTree.getMountingCoordinator()->setMountingOverrideDelegate( | ||
screenRemovalListener_); | ||
}); | ||
} | ||
|
||
jni::local_ref<NativeProxy::jhybriddata> NativeProxy::initHybrid( | ||
jni::alias_ref<jhybridobject> jThis) { | ||
return makeCxxInstance(jThis); | ||
} | ||
|
||
} // namespace rnscreens |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#pragma once | ||
|
||
#include <fbjni/fbjni.h> | ||
#include <react/fabric/JFabricUIManager.h> | ||
#include "RNSScreenRemovalListener.h" | ||
|
||
#include <string> | ||
|
||
namespace rnscreens { | ||
using namespace facebook; | ||
using namespace facebook::jni; | ||
|
||
class NativeProxy : public jni::HybridClass<NativeProxy> { | ||
public: | ||
std::shared_ptr<RNSScreenRemovalListener> screenRemovalListener_; | ||
static auto constexpr kJavaDescriptor = | ||
"Lcom/swmansion/rnscreens/NativeProxy;"; | ||
static jni::local_ref<jhybriddata> initHybrid( | ||
jni::alias_ref<jhybridobject> jThis); | ||
static void registerNatives(); | ||
|
||
~NativeProxy(); | ||
|
||
private: | ||
friend HybridBase; | ||
jni::global_ref<NativeProxy::javaobject> javaPart_; | ||
|
||
explicit NativeProxy(jni::alias_ref<NativeProxy::javaobject> jThis); | ||
|
||
void nativeAddMutationsListener( | ||
jni::alias_ref<facebook::react::JFabricUIManager::javaobject> | ||
fabricUIManager); | ||
}; | ||
|
||
} // namespace rnscreens |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#include <fbjni/fbjni.h> | ||
|
||
#include "NativeProxy.h" | ||
|
||
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *) { | ||
return facebook::jni::initialize( | ||
vm, [] { rnscreens::NativeProxy::registerNatives(); }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.