Skip to content

Commit

Permalink
Component type (#5820)
Browse files Browse the repository at this point in the history
Send componentType field in componentDidAppear and componentDidDisappear events.

The new field is either:
- TopBarButton
- TopBarTitle
- TopBarBackground
- Component
  • Loading branch information
guyca authored Jan 2, 2020
1 parent bfd32cc commit 3878b68
Show file tree
Hide file tree
Showing 72 changed files with 549 additions and 292 deletions.
28 changes: 24 additions & 4 deletions e2e/StaticLifecycleEvents.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ describe('static lifecycle events', () => {
});

it('didAppear didDisappear', async () => {
await expect(elementByLabel('componentDidAppear | EventsOverlay')).toBeVisible();
await expect(elementByLabel('componentDidAppear | EventsOverlay | Component')).toBeVisible();
await elementById(TestIDs.PUSH_BTN).tap();
await expect(elementByLabel('componentDidAppear | Pushed')).toBeVisible();
await expect(elementByLabel('componentDidDisappear | EventsScreen')).toBeVisible();
await expect(elementByLabel('componentDidAppear | Pushed | Component')).toBeVisible();
await expect(elementByLabel('componentDidDisappear | EventsScreen | Component')).toBeVisible();
});

it('pushing and popping screen dispatch static event', async () => {
await expect(elementByLabel('Static Lifecycle Events Overlay')).toBeVisible();
await expect(elementByLabel('componentDidAppear | EventsOverlay')).toBeVisible();
await expect(elementByLabel('componentDidAppear | EventsOverlay | Component')).toBeVisible();
await elementById(TestIDs.PUSH_BTN).tap();
await expect(elementByLabel('push')).toBeVisible();
await elementById(TestIDs.POP_BTN).tap();
Expand All @@ -40,4 +40,24 @@ describe('static lifecycle events', () => {
await expect(elementByLabel('Overlay Unmounted')).toBeVisible();
await elementByLabel('OK').tap();
});

it('top bar buttons didAppear didDisappear', async () => {
await elementById(TestIDs.PUSH_BTN).tap();
await elementById(TestIDs.PUSH_OPTIONS_BUTTON).tap();
await elementById(TestIDs.CLEAR_OVERLAY_EVENTS_BTN).tap();
await elementById(TestIDs.GOTO_BUTTONS_SCREEN).tap();
await expect(elementByLabel('componentDidAppear | CustomRoundedButton | TopBarButton')).toBeVisible();
await elementById(TestIDs.RESET_BUTTONS).tap();
await expect(elementByLabel('componentDidDisappear | CustomRoundedButton | TopBarButton')).toBeVisible();
});

it('top bar title didAppear didDisappear', async () => {
await elementById(TestIDs.PUSH_BTN).tap();
await elementById(TestIDs.PUSH_OPTIONS_BUTTON).tap();
await elementById(TestIDs.CLEAR_OVERLAY_EVENTS_BTN).tap();
await elementById(TestIDs.SET_REACT_TITLE_VIEW).tap();
await expect(elementByLabel('componentDidAppear | ReactTitleView | TopBarTitle')).toBeVisible();
await elementById(TestIDs.PUSH_BTN).tap();
await expect(elementByLabel('componentDidDisappear | ReactTitleView | TopBarTitle')).toBeVisible();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import com.reactnativenavigation.presentation.RenderChecker;
import com.reactnativenavigation.presentation.SideMenuPresenter;
import com.reactnativenavigation.presentation.StackPresenter;
import com.reactnativenavigation.react.EventEmitter;
import com.reactnativenavigation.react.events.EventEmitter;
import com.reactnativenavigation.utils.Assertions;
import com.reactnativenavigation.utils.ImageLoader;
import com.reactnativenavigation.utils.TypefaceLoader;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.reactnativenavigation.react;

import com.reactnativenavigation.react.events.EventEmitter;
import com.reactnativenavigation.utils.LaunchArgsParser;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.bridge.Arguments;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.reactnativenavigation.react;

import androidx.annotation.NonNull;

import com.facebook.react.ReactInstanceManager;
import com.facebook.react.bridge.ReactContext;
import com.reactnativenavigation.NavigationActivity;
import com.reactnativenavigation.react.events.EventEmitter;

import androidx.annotation.NonNull;

public class NavigationReactInitializer implements ReactInstanceManager.ReactInstanceEventListener {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import com.facebook.react.ReactInstanceManager;
import com.reactnativenavigation.viewcontrollers.ReactViewCreator;
import com.reactnativenavigation.viewcontrollers.IReactView;

public class ReactComponentViewCreator implements ReactViewCreator {
private ReactInstanceManager reactInstanceManager;
Expand All @@ -14,7 +13,7 @@ public ReactComponentViewCreator(final ReactInstanceManager reactInstanceManager
}

@Override
public IReactView create(final Activity activity, final String componentId, final String componentName) {
public ReactView create(final Activity activity, final String componentId, final String componentName) {
return new ReactView(activity, reactInstanceManager, componentId, componentName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import androidx.annotation.RestrictTo;
import android.view.MotionEvent;

import com.facebook.react.ReactInstanceManager;
Expand All @@ -13,13 +12,17 @@
import com.facebook.react.uimanager.UIManagerModule;
import com.facebook.react.uimanager.events.EventDispatcher;
import com.reactnativenavigation.interfaces.ScrollEventListener;
import com.reactnativenavigation.react.events.ComponentType;
import com.reactnativenavigation.react.events.EventEmitter;
import com.reactnativenavigation.viewcontrollers.IReactView;
import com.reactnativenavigation.views.Renderable;
import com.reactnativenavigation.views.element.Element;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.RestrictTo;

@SuppressLint("ViewConstructor")
public class ReactView extends ReactRootView implements IReactView, Renderable {

Expand Down Expand Up @@ -64,19 +67,17 @@ public void destroy() {
unmountReactApplication();
}

@Override
public void sendComponentStart() {
public void sendComponentStart(ComponentType type) {
ReactContext currentReactContext = reactInstanceManager.getCurrentReactContext();
if (currentReactContext != null) {
new EventEmitter(currentReactContext).emitComponentDidAppear(componentId, componentName);
new EventEmitter(currentReactContext).emitComponentDidAppear(componentId, componentName, type);
}
}

@Override
public void sendComponentStop() {
public void sendComponentStop(ComponentType type) {
ReactContext currentReactContext = reactInstanceManager.getCurrentReactContext();
if (currentReactContext != null) {
new EventEmitter(currentReactContext).emitComponentDidDisappear(componentId, componentName);
new EventEmitter(currentReactContext).emitComponentDidDisappear(componentId, componentName, type);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.reactnativenavigation.react.events;

public enum ComponentType {
Component("Component"),
Button("TopBarButton"),
Title("TopBarTitle"),
Background("TopBarBackground");

private String name;

public String getName() {
return name;
}

ComponentType(String name) {
this.name = name;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.reactnativenavigation.react;
package com.reactnativenavigation.react.events;

import android.util.Log;

Expand Down Expand Up @@ -28,20 +28,22 @@ public EventEmitter(@Nullable ReactContext reactContext) {
}

public void appLaunched() {
emit(AppLaunched);
emit(EventEmitter.AppLaunched, Arguments.createMap());
}

public void emitComponentDidDisappear(String id, String componentName) {
public void emitComponentDidDisappear(String id, String componentName, ComponentType type) {
WritableMap event = Arguments.createMap();
event.putString("componentId", id);
event.putString("componentName", componentName);
event.putString("componentType", type.getName());
emit(ComponentDidDisappear, event);
}

public void emitComponentDidAppear(String id, String componentName) {
public void emitComponentDidAppear(String id, String componentName, ComponentType type) {
WritableMap event = Arguments.createMap();
event.putString("componentId", id);
event.putString("componentName", componentName);
event.putString("componentType", type.getName());
emit(ComponentDidAppear, event);
}

Expand Down Expand Up @@ -80,10 +82,6 @@ public void emitScreenPoppedEvent(String componentId) {
emit(ScreenPopped, event);
}

private void emit(String eventName) {
emit(eventName, Arguments.createMap());
}

private void emit(String eventName, WritableMap data) {
if (reactContext == null) {
Log.e("RNN", "Could not send event " + eventName + ". React context is null!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import androidx.annotation.Nullable;

import com.facebook.react.bridge.Promise;
import com.reactnativenavigation.react.EventEmitter;
import com.reactnativenavigation.react.events.EventEmitter;

public class NativeCommandListener extends CommandListenerAdapter {
private String commandId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ public interface IReactView extends Destroyable {

View asView();

void sendComponentStart();

void sendComponentStop();

void sendOnNavigationButtonPressed(String buttonId);

ScrollEventListener getScrollEventListener();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package com.reactnativenavigation.viewcontrollers;
package com.reactnativenavigation.viewcontrollers;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import androidx.annotation.NonNull;
import androidx.annotation.RestrictTo;
import androidx.appcompat.widget.ActionMenuView;
import androidx.appcompat.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageButton;
Expand All @@ -16,6 +12,7 @@
import com.reactnativenavigation.parse.Options;
import com.reactnativenavigation.parse.params.Button;
import com.reactnativenavigation.parse.params.Text;
import com.reactnativenavigation.react.events.ComponentType;
import com.reactnativenavigation.utils.ArrayUtils;
import com.reactnativenavigation.utils.ButtonPresenter;
import com.reactnativenavigation.utils.ImageLoader;
Expand All @@ -28,6 +25,11 @@
import java.util.Collections;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.RestrictTo;
import androidx.appcompat.widget.ActionMenuView;
import androidx.appcompat.widget.Toolbar;

public class TitleBarButtonController extends ViewController<TitleBarReactButtonView> implements MenuItem.OnMenuItemClickListener {
public interface OnClickListener {
void onPress(String buttonId);
Expand Down Expand Up @@ -69,13 +71,13 @@ public TitleBarButtonController(Activity activity,
@SuppressLint("MissingSuperCall")
@Override
public void onViewAppeared() {
getView().sendComponentStart();
getView().sendComponentStart(ComponentType.Button);
}

@SuppressLint("MissingSuperCall")
@Override
public void onViewDisappear() {
getView().sendComponentStop();
getView().sendComponentStop(ComponentType.Button);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import com.reactnativenavigation.parse.Component;
import com.reactnativenavigation.parse.Options;
import com.reactnativenavigation.react.events.ComponentType;
import com.reactnativenavigation.utils.CompatUtils;
import com.reactnativenavigation.views.titlebar.TitleBarReactView;
import com.reactnativenavigation.views.titlebar.TitleBarReactViewCreator;
Expand All @@ -23,13 +24,13 @@ public void onViewAppeared() {
super.onViewAppeared();
if (!isDestroyed()) {
runOnPreDraw(view -> view.setLayoutParams(view.getLayoutParams()));
getView().sendComponentStart();
getView().sendComponentStart(ComponentType.Title);
}
}

@Override
public void onViewDisappear() {
getView().sendComponentStop();
getView().sendComponentStop(ComponentType.Title);
super.onViewDisappear();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import com.reactnativenavigation.presentation.BottomTabPresenter;
import com.reactnativenavigation.presentation.BottomTabsPresenter;
import com.reactnativenavigation.presentation.Presenter;
import com.reactnativenavigation.react.EventEmitter;
import com.reactnativenavigation.react.events.EventEmitter;
import com.reactnativenavigation.utils.CommandListener;
import com.reactnativenavigation.utils.ImageLoader;
import com.reactnativenavigation.viewcontrollers.ChildControllersRegistry;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
package com.reactnativenavigation.viewcontrollers.externalcomponent;

import android.app.Activity;
import androidx.fragment.app.FragmentActivity;
import androidx.core.view.ViewCompat;
import android.view.View;

import com.facebook.react.ReactInstanceManager;
import com.reactnativenavigation.parse.ExternalComponent;
import com.reactnativenavigation.parse.Options;
import com.reactnativenavigation.presentation.ExternalComponentPresenter;
import com.reactnativenavigation.react.EventEmitter;
import com.reactnativenavigation.react.events.ComponentType;
import com.reactnativenavigation.react.events.EventEmitter;
import com.reactnativenavigation.utils.CoordinatorLayoutUtils;
import com.reactnativenavigation.utils.StatusBarUtils;
import com.reactnativenavigation.viewcontrollers.NoOpYellowBoxDelegate;
import com.reactnativenavigation.viewcontrollers.ViewController;
import com.reactnativenavigation.views.BehaviourDelegate;
import com.reactnativenavigation.views.ExternalComponentLayout;

import androidx.core.view.ViewCompat;
import androidx.fragment.app.FragmentActivity;

import static com.reactnativenavigation.utils.ObjectUtils.perform;

public class ExternalComponentViewController extends ViewController<ExternalComponentLayout> {
Expand Down Expand Up @@ -60,13 +62,13 @@ public void mergeOptions(Options options) {
@Override
public void onViewAppeared() {
super.onViewAppeared();
emitter.emitComponentDidAppear(getId(), externalComponent.name.get());
emitter.emitComponentDidAppear(getId(), externalComponent.name.get(), ComponentType.Component);
}

@Override
public void onViewDisappear() {
super.onViewDisappear();
emitter.emitComponentDidDisappear(getId(), externalComponent.name.get());
emitter.emitComponentDidDisappear(getId(), externalComponent.name.get(), ComponentType.Component);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import com.reactnativenavigation.anim.ModalAnimator;
import com.reactnativenavigation.parse.Options;
import com.reactnativenavigation.react.EventEmitter;
import com.reactnativenavigation.react.events.EventEmitter;
import com.reactnativenavigation.utils.CommandListener;
import com.reactnativenavigation.utils.CommandListenerAdapter;
import com.reactnativenavigation.viewcontrollers.ViewController;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import com.reactnativenavigation.presentation.OverlayManager;
import com.reactnativenavigation.presentation.Presenter;
import com.reactnativenavigation.presentation.RootPresenter;
import com.reactnativenavigation.react.EventEmitter;
import com.reactnativenavigation.react.events.EventEmitter;
import com.reactnativenavigation.utils.CommandListener;
import com.reactnativenavigation.utils.CommandListenerAdapter;
import com.reactnativenavigation.utils.CompatUtils;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import com.reactnativenavigation.presentation.Presenter;
import com.reactnativenavigation.presentation.StackPresenter;
import com.reactnativenavigation.react.Constants;
import com.reactnativenavigation.react.EventEmitter;
import com.reactnativenavigation.react.events.EventEmitter;
import com.reactnativenavigation.utils.CommandListener;
import com.reactnativenavigation.utils.CommandListenerAdapter;
import com.reactnativenavigation.utils.CompatUtils;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.reactnativenavigation.parse.Options;
import com.reactnativenavigation.presentation.Presenter;
import com.reactnativenavigation.presentation.StackPresenter;
import com.reactnativenavigation.react.EventEmitter;
import com.reactnativenavigation.react.events.EventEmitter;
import com.reactnativenavigation.viewcontrollers.ChildControllersRegistry;
import com.reactnativenavigation.viewcontrollers.ViewController;
import com.reactnativenavigation.viewcontrollers.topbar.TopBarController;
Expand Down
Loading

0 comments on commit 3878b68

Please sign in to comment.