Skip to content

Commit

Permalink
fix: fixed navigation.setParams not working
Browse files Browse the repository at this point in the history
  • Loading branch information
IjzerenHein committed Aug 31, 2019
1 parent 9c4136d commit c0a5f6a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 46 deletions.
39 changes: 9 additions & 30 deletions src/SharedElementRendererData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
SharedElementsConfig,
SharedElementAnimatedValue,
SharedElementTransitionProps,
NavigationProp,
} from './types';
import { normalizeSharedElementsConfig } from './utils';

Expand All @@ -13,35 +12,27 @@ export type SharedElementRendererUpdateHandler = () => any;
export interface ISharedElementRendererData {
startTransition(animValue: SharedElementAnimatedValue): void;
endTransition(): void;
willActivateScene(
sceneData: SharedElementSceneData,
navigation: NavigationProp
): void;
didActivateScene(
sceneData: SharedElementSceneData,
navigation: NavigationProp
): void;
willActivateScene(sceneData: SharedElementSceneData): void;
didActivateScene(sceneData: SharedElementSceneData): void;
}

function getSharedElements(
sceneData: SharedElementSceneData,
navigation: NavigationProp,
otherNavigation: NavigationProp,
otherSceneData: SharedElementSceneData,
show: boolean
): SharedElementsConfig | null {
const { sharedElements } = sceneData.Component;
if (!sharedElements) return null;
// TODO push/pop distinction?
return normalizeSharedElementsConfig(
sharedElements(navigation, otherNavigation, show)
sharedElements(sceneData.navigation, otherSceneData.navigation, show)
);
}

export default class SharedElementRendererData
implements ISharedElementRendererData {
private sceneData: SharedElementSceneData | null = null;
private prevSceneData: SharedElementSceneData | null = null;
private prevNavigation: NavigationProp | null = null;
private updateSubscribers = new Set<SharedElementRendererUpdateHandler>();
private sceneSubscription: SharedElementEventSubscription | null = null;
private sharedElements: SharedElementsConfig = [];
Expand All @@ -55,25 +46,17 @@ export default class SharedElementRendererData
// Nothing to do
}

willActivateScene(
sceneData: SharedElementSceneData,
navigation: NavigationProp
): void {
willActivateScene(sceneData: SharedElementSceneData): void {
/*console.log(
'SharedElementRendererData.willActivateScene: ',
sceneData.name,
', previous: ',
this.prevSceneData ? this.prevSceneData.name : ''
);*/
if (!this.prevSceneData || !this.prevNavigation) return;
if (!this.prevSceneData) return;
const sharedElements =
getSharedElements(sceneData, navigation, this.prevNavigation, true) ||
getSharedElements(
this.prevSceneData,
this.prevNavigation,
navigation,
false
);
getSharedElements(sceneData, this.prevSceneData, true) ||
getSharedElements(this.prevSceneData, sceneData, false);
if (sharedElements && sharedElements.length) {
// console.log('sharedElements: ', sharedElements, sceneData);
this.sceneData = sceneData;
Expand All @@ -86,17 +69,13 @@ export default class SharedElementRendererData
}
}

didActivateScene(
sceneData: SharedElementSceneData,
navigation: NavigationProp
): void {
didActivateScene(sceneData: SharedElementSceneData): void {
//console.log('SharedElementRendererData.didActivateScene: ', sceneData.name);
if (this.sceneSubscription) {
this.sceneSubscription.remove();
this.sceneSubscription = null;
}
this.prevSceneData = sceneData;
this.prevNavigation = navigation;
if (this.sceneData) {
this.sceneData = null;
if (this.sharedElements.length) {
Expand Down
16 changes: 5 additions & 11 deletions src/SharedElementRendererProxy.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import SharedElementRendererData, {
ISharedElementRendererData,
} from './SharedElementRendererData';
import { SharedElementAnimatedValue, NavigationProp } from './types';
import { SharedElementAnimatedValue } from './types';
import SharedElementSceneData from './SharedElementSceneData';

export class SharedElementRendererProxy implements ISharedElementRendererData {
Expand All @@ -27,30 +27,24 @@ export class SharedElementRendererProxy implements ISharedElementRendererData {
return this.data.endTransition();
}

willActivateScene(
sceneData: SharedElementSceneData,
navigation: NavigationProp
) {
willActivateScene(sceneData: SharedElementSceneData) {
if (!this.data) {
console.warn(
'SharedElementRendererProxy.willActivateScene called before Proxy was initialized'
);
return;
}
return this.data.willActivateScene(sceneData, navigation);
return this.data.willActivateScene(sceneData);
}

didActivateScene(
sceneData: SharedElementSceneData,
navigation: NavigationProp
) {
didActivateScene(sceneData: SharedElementSceneData) {
if (!this.data) {
console.warn(
'SharedElementRendererProxy.didActivateScene called before Proxy was initialized'
);
return;
}
return this.data.didActivateScene(sceneData, navigation);
return this.data.didActivateScene(sceneData);
}

get source(): SharedElementRendererData | null {
Expand Down
8 changes: 7 additions & 1 deletion src/SharedElementSceneData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
SharedElementNode,
SharedElementEventSubscription,
SharedElementSceneComponent,
NavigationProp,
} from './types';

export type SharedElementSceneUpdateHandlerEventType =
Expand All @@ -23,9 +24,14 @@ export default class SharedElementSceneData {
} = {};
public readonly Component: SharedElementSceneComponent;
public readonly name: string;
public navigation: NavigationProp;

constructor(Component: SharedElementSceneComponent) {
constructor(
Component: SharedElementSceneComponent,
navigation: NavigationProp
) {
this.Component = Component;
this.navigation = navigation;
this.name =
Component.displayName ||
Component.name ||
Expand Down
12 changes: 8 additions & 4 deletions src/createSharedElementScene.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ function createSharedElementScene(
[key: string]: SharedElementEventSubscription;
} = {};
private sceneData: SharedElementSceneData = new SharedElementSceneData(
Component
Component,
this.props.navigation
);

componentDidMount() {
Expand Down Expand Up @@ -74,21 +75,24 @@ function createSharedElementScene(
);
}

componentDidUpdate() {
this.sceneData.navigation = this.props.navigation;
}

private onSetRef = (ref: any) => {
this.sceneData.setAncestor(nodeFromRef(ref));
};

private onWillFocus = () => {
const { navigation } = this.props;
rendererData.willActivateScene(this.sceneData, navigation);
rendererData.willActivateScene(this.sceneData);
};

private onDidFocus = () => {
const { navigation } = this.props;
const activeRoute = getActiveRouteState(navigation.state);
if (navigation.state.routeName === activeRoute.routeName) {
// console.log('onDidFocus: ', this.sceneData.name, navigation);
rendererData.didActivateScene(this.sceneData, navigation);
rendererData.didActivateScene(this.sceneData);
}
};
}
Expand Down

0 comments on commit c0a5f6a

Please sign in to comment.