Skip to content

Commit

Permalink
feat: clean event dispatching on Android (#1014)
Browse files Browse the repository at this point in the history
Clean up event dispatching code on Android.
  • Loading branch information
WoLewicki authored Jul 15, 2021
1 parent 33d4084 commit 0f9caf1
Showing 1 changed file with 29 additions and 40 deletions.
69 changes: 29 additions & 40 deletions android/src/main/java/com/swmansion/rnscreens/ScreenFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.UiThreadUtil;
import com.facebook.react.uimanager.UIManagerModule;
import com.facebook.react.uimanager.events.Event;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -142,39 +143,46 @@ public List<ScreenContainer> getChildScreenContainers() {
}

protected void dispatchOnWillAppear() {
((ReactContext) mScreenView.getContext())
.getNativeModule(UIManagerModule.class)
.getEventDispatcher()
.dispatchEvent(new ScreenWillAppearEvent(mScreenView.getId()));

dispatchEventInChildContainers(ScreenLifecycleEvent.WillAppear);
dispatchEvent(ScreenLifecycleEvent.WillAppear, this);
}

protected void dispatchOnAppear() {
((ReactContext) mScreenView.getContext())
.getNativeModule(UIManagerModule.class)
.getEventDispatcher()
.dispatchEvent(new ScreenAppearEvent(mScreenView.getId()));

dispatchEventInChildContainers(ScreenLifecycleEvent.Appear);
dispatchEvent(ScreenLifecycleEvent.Appear, this);
}

protected void dispatchOnWillDisappear() {
((ReactContext) mScreenView.getContext())
.getNativeModule(UIManagerModule.class)
.getEventDispatcher()
.dispatchEvent(new ScreenWillDisappearEvent(mScreenView.getId()));

dispatchEventInChildContainers(ScreenLifecycleEvent.WillDisappear);
dispatchEvent(ScreenLifecycleEvent.WillDisappear, this);
}

protected void dispatchOnDisappear() {
((ReactContext) mScreenView.getContext())
dispatchEvent(ScreenLifecycleEvent.Disappear, this);
}

private void dispatchEvent(ScreenLifecycleEvent event, ScreenFragment fragment) {
Screen screen = fragment.getScreen();
Event lifecycleEvent;
switch (event) {
case WillAppear:
lifecycleEvent = new ScreenWillAppearEvent(screen.getId());
break;
case Appear:
lifecycleEvent = new ScreenAppearEvent(screen.getId());
break;
case WillDisappear:
lifecycleEvent = new ScreenWillDisappearEvent(screen.getId());
break;
case Disappear:
lifecycleEvent = new ScreenDisappearEvent(screen.getId());
break;
default:
throw new IllegalStateException("Tried to dispatch unknown event: " + event.toString());
}
((ReactContext) screen.getContext())
.getNativeModule(UIManagerModule.class)
.getEventDispatcher()
.dispatchEvent(new ScreenDisappearEvent(mScreenView.getId()));
.dispatchEvent(lifecycleEvent);

dispatchEventInChildContainers(ScreenLifecycleEvent.Disappear);
fragment.dispatchEventInChildContainers(event);
}

private void dispatchEventInChildContainers(ScreenLifecycleEvent event) {
Expand All @@ -193,25 +201,6 @@ private void dispatchEventInChildContainers(ScreenLifecycleEvent event) {
}
}

private void dispatchEvent(ScreenLifecycleEvent event, ScreenFragment fragment) {
switch (event) {
case Appear:
fragment.dispatchOnAppear();
break;
case WillAppear:
fragment.dispatchOnWillAppear();
break;
case Disappear:
fragment.dispatchOnDisappear();
break;
case WillDisappear:
fragment.dispatchOnWillDisappear();
break;
default:
break;
}
}

public void registerChildScreenContainer(ScreenContainer screenContainer) {
mChildScreenContainers.add(screenContainer);
}
Expand Down

0 comments on commit 0f9caf1

Please sign in to comment.