Skip to content

Commit

Permalink
Run only on UI
Browse files Browse the repository at this point in the history
  • Loading branch information
piaskowyk committed Nov 2, 2023
1 parent 6e12838 commit 6bf54d1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 47 deletions.
15 changes: 4 additions & 11 deletions android/src/main/java/com/swmansion/rnscreens/ScreensModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,19 @@ class ScreensModule(private val mReactContext: ReactApplicationContext) : ReactC

@ReactMethod
override fun startTransition(reactTag: Int): IntArray {
if (UiThreadUtil.isOnUiThread()) {
return startTransitionUI(reactTag)
} else {
UiThreadUtil.runOnUiThread { startTransitionUI(reactTag) }
}
return intArrayOf(-1, -1)
return startTransitionUI(reactTag)
}

@ReactMethod
override fun updateTransition(reactTag: Int, progress: Double) {}

@ReactMethod
override fun finishTransition(reactTag: Int, canceled: Boolean) {
if (UiThreadUtil.isOnUiThread()) {
return finishTransitionUI(reactTag, canceled)
} else {
UiThreadUtil.runOnUiThread { finishTransitionUI(reactTag, canceled) }
}
finishTransitionUI(reactTag, canceled)
}

private fun startTransitionUI(reactTag: Int): IntArray {
UiThreadUtil.assertOnUiThread()
if (isActiveTransition.get()) {
return intArrayOf(-1, -1)
}
Expand All @@ -63,6 +55,7 @@ class ScreensModule(private val mReactContext: ReactApplicationContext) : ReactC
}

private fun finishTransitionUI(reactTag: Int, canceled: Boolean) {
UiThreadUtil.assertOnUiThread()
if (!isActiveTransition.get()) {
return
}
Expand Down
47 changes: 11 additions & 36 deletions ios/RNSModule.mm
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ - (dispatch_queue_t)methodQueue
// for the blocks to be dispatched before the batch is completed
return dispatch_get_main_queue();
}

RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(startTransition:(nonnull NSNumber *)stackTag)
{
return [self _startTransition:stackTag];
Expand All @@ -38,19 +39,12 @@ - (dispatch_queue_t)methodQueue

- (RNSScreenStackView *)getScreenStackView:(NSNumber *)reactTag
{
__block RNSScreenStackView *view;
RCTAssertMainQueue();
RNSScreenStackView *view;
#ifdef RCT_NEW_ARCH_ENABLED
dispatch_sync(dispatch_get_main_queue(), ^{
view = [self.viewRegistry_DEPRECATED viewForReactTag:reactTag];
});
view = [self.viewRegistry_DEPRECATED viewForReactTag:reactTag];
#else
if ([[NSThread currentThread] isMainThread]) {
view = [self.bridge.uiManager viewForReactTag:reactTag];
} else {
dispatch_sync(dispatch_get_main_queue(), ^{
view = [self.bridge.uiManager viewForReactTag:reactTag];
});
}
view = (RNSScreenStackView *)[self.bridge.uiManager viewForReactTag:reactTag];
#endif // RCT_NEW_ARCH_ENABLED
return view;
}
Expand All @@ -65,6 +59,7 @@ - (RNSScreenStackView *)getScreenStackView:(NSNumber *)reactTag

- (nonnull NSArray<NSNumber *> *)_startTransition:(nonnull NSNumber *)stackTag
{
RCTAssertMainQueue();
RNSScreenStackView *stackView = [self getStackView:stackTag];
if (stackView == nil || isActiveTransition) {
return @[];
Expand All @@ -79,49 +74,29 @@ - (RNSScreenStackView *)getScreenStackView:(NSNumber *)reactTag
isActiveTransition = true;
screenTags = @[topScreen, belowTopScreen];
}

if ([[NSThread currentThread] isMainThread]) {
[stackView startScreenTransition];
} else {
dispatch_sync(dispatch_get_main_queue(), ^{
[stackView startScreenTransition];
});
}

[stackView startScreenTransition];
return screenTags;
}

- (bool)_updateTransition:(nonnull NSNumber *)stackTag progress:(double)progress
{
RCTAssertMainQueue();
RNSScreenStackView *stackView = [self getStackView:stackTag];
if (stackView == nil || !isActiveTransition) {
return false;
}

if ([[NSThread currentThread] isMainThread]) {
[stackView updateScreenTransition:progress];
} else {
dispatch_sync(dispatch_get_main_queue(), ^{
[stackView updateScreenTransition:progress];
});
}
[stackView updateScreenTransition:progress];
return true;
}

- (bool)_finishTransition:(nonnull NSNumber *)stackTag canceled:(bool)canceled
{
RCTAssertMainQueue();
RNSScreenStackView *stackView = [self getStackView:stackTag];
if (stackView == nil || !isActiveTransition) {
return false;
}

if ([[NSThread currentThread] isMainThread]) {
[stackView finishScreenTransition:canceled];
} else {
dispatch_sync(dispatch_get_main_queue(), ^{
[stackView finishScreenTransition:canceled];
});
}
[stackView finishScreenTransition:canceled];
isActiveTransition = false;
return true;
}
Expand Down

0 comments on commit 6bf54d1

Please sign in to comment.