FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix(transition): prevent circular refs on TransitionListener by edusperoni · Pull Request #11136 · NativeScript/NativeScript · GitHub

Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .ts  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
186 changes: 102 additions & 84 deletions packages/core/ui/frame/fragment.transitions.android.ts
This file contains hidden or 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
Expand Up @@ -23,17 +23,87 @@ const animFadeOut = 17432577; // android.R.anim.fade_out
export const waitingQueue = new Map<number, Set<ExpandedEntry>>();
export const completedEntries = new Map<number, ExpandedEntry>();

let TransitionListener: TransitionListener;
let TransitionListener = lazy(() => {
@NativeClass
@Interfaces([(<any>androidx).transition.Transition.TransitionListener])
class TransitionListenerImpl extends java.lang.Object implements androidx.transition.Transition.TransitionListener {
public entry: WeakRef<ExpandedEntry>;
constructor(
entry: ExpandedEntry,
public transition: androidx.transition.Transition,
) {
super();
this.entry = new WeakRef(entry);

return global.__native(this);
}

public onTransitionStart(transition: androidx.transition.Transition): void {
const entry = this.entry.deref();
if (!entry) {
return;
}
entry.isAnimationRunning = true;
addToWaitingQueue(entry);
if (Trace.isEnabled()) {
Trace.write(`START ${toShortString(transition)} transition for ${entry.fragmentTag}`, Trace.categories.Transition);
}
if (entry?.transition) {
notifySharedTransition(entry.transition?.id, SharedTransition.startedEvent);
}
}

onTransitionEnd(transition: androidx.transition.Transition): void {
const entry = this.entry.deref();
if (!entry) {
return;
}
if (Trace.isEnabled()) {
Trace.write(`END ${toShortString(transition)} transition for ${entry.fragmentTag}`, Trace.categories.Transition);
}
entry.isAnimationRunning = false;
transitionOrAnimationCompleted(entry, undefined);
if (entry?.transition) {
notifySharedTransition(entry.transition?.id, SharedTransition.finishedEvent);
}
}

onTransitionResume(transition: androidx.transition.Transition): void {
if (Trace.isEnabled()) {
const fragment = this.entry.deref()?.fragmentTag;
Trace.write(`RESUME ${toShortString(transition)} transition for ${fragment}`, Trace.categories.Transition);
}
}

onTransitionPause(transition: androidx.transition.Transition): void {
if (Trace.isEnabled()) {
Trace.write(`PAUSE ${toShortString(transition)} transition for ${this.entry.deref()?.fragmentTag}`, Trace.categories.Transition);
}
}

onTransitionCancel(transition: androidx.transition.Transition): void {
const entry = this.entry.deref();
if (!entry) {
return;
}
entry.isAnimationRunning = false;
if (Trace.isEnabled()) {
Trace.write(`CANCEL ${toShortString(transition)} transition for ${entry.fragmentTag}`, Trace.categories.Transition);
}
}
}
return TransitionListenerImpl;
});
let AnimationListener: android.animation.Animator.AnimatorListener;

interface ExpandedTransitionListener extends androidx.transition.Transition.TransitionListener {
entry: ExpandedEntry;
entry: WeakRef<ExpandedEntry>;
transition: androidx.transition.Transition;
}

interface ExpandedAnimator extends android.animation.Animator {
entry: ExpandedEntry;
backEntry?: BackstackEntry;
entry: WeakRef<ExpandedEntry>;
backEntry?: WeakRef<BackstackEntry>;
transitionType?: string;
}

Expand Down Expand Up @@ -187,13 +257,13 @@ function setupAllAnimation(entry: ExpandedEntry, transition: Transition): void {
// need to clearAnimationListener for enter & popExit animators.
const enterAnimator = <ExpandedAnimator>transition.createAndroidAnimator(Transition.AndroidTransitionType.enter);
enterAnimator.transitionType = Transition.AndroidTransitionType.enter;
enterAnimator.entry = entry;
enterAnimator.entry = new WeakRef(entry);
enterAnimator.addListener(listener);
entry.enterAnimator = enterAnimator;

const popExitAnimator = <ExpandedAnimator>transition.createAndroidAnimator(Transition.AndroidTransitionType.popExit);
popExitAnimator.transitionType = Transition.AndroidTransitionType.popExit;
popExitAnimator.entry = entry;
popExitAnimator.entry = new WeakRef(entry);
popExitAnimator.addListener(listener);
entry.popExitAnimator = popExitAnimator;
}
Expand All @@ -207,13 +277,13 @@ function setupExitAndPopEnterAnimation(entry: ExpandedEntry, transition: Transit

const exitAnimator = <ExpandedAnimator>transition.createAndroidAnimator(Transition.AndroidTransitionType.exit);
exitAnimator.transitionType = Transition.AndroidTransitionType.exit;
exitAnimator.entry = entry;
exitAnimator.entry = new WeakRef(entry);
exitAnimator.addListener(listener);
entry.exitAnimator = exitAnimator;

const popEnterAnimator = <ExpandedAnimator>transition.createAndroidAnimator(Transition.AndroidTransitionType.popEnter);
popEnterAnimator.transitionType = Transition.AndroidTransitionType.popEnter;
popEnterAnimator.entry = entry;
popEnterAnimator.entry = new WeakRef(entry);
popEnterAnimator.addListener(listener);
entry.popEnterAnimator = popEnterAnimator;
}
Expand All @@ -230,33 +300,44 @@ function getAnimationListener(): android.animation.Animator.AnimatorListener {
}

onAnimationStart(animator: ExpandedAnimator): void {
const entry = animator.entry;
addToWaitingQueue(entry);
const entry = animator.entry.deref();
if (Trace.isEnabled()) {
Trace.write(`START ${animator.transitionType} for ${entry.fragmentTag}`, Trace.categories.Transition);
Trace.write(`START ${animator.transitionType} for ${entry?.fragmentTag}`, Trace.categories.Transition);
}
if (!entry) {
return;
}
addToWaitingQueue(entry);
entry.isAnimationRunning = true;
}

onAnimationRepeat(animator: ExpandedAnimator): void {
if (Trace.isEnabled()) {
Trace.write(`REPEAT ${animator.transitionType} for ${animator.entry.fragmentTag}`, Trace.categories.Transition);
Trace.write(`REPEAT ${animator.transitionType} for ${animator.entry.deref()?.fragmentTag}`, Trace.categories.Transition);
}
}

onAnimationEnd(animator: ExpandedAnimator): void {
if (Trace.isEnabled()) {
Trace.write(`END ${animator.transitionType} for ${animator.entry.fragmentTag}`, Trace.categories.Transition);
Trace.write(`END ${animator.transitionType} for ${animator.entry.deref()?.fragmentTag}`, Trace.categories.Transition);
}
const entry = animator.entry.deref();
if (!entry) {
return;
}
animator.entry.isAnimationRunning = false;
transitionOrAnimationCompleted(animator.entry, animator.backEntry);
entry.isAnimationRunning = false;
transitionOrAnimationCompleted(entry, animator.backEntry?.deref());
}

onAnimationCancel(animator: ExpandedAnimator): void {
if (Trace.isEnabled()) {
Trace.write(`CANCEL ${animator.transitionType} for ${animator.entry.fragmentTag}`, Trace.categories.Transition);
Trace.write(`CANCEL ${animator.transitionType} for ${animator.entry.deref()?.fragmentTag}`, Trace.categories.Transition);
}
const entry = animator.entry.deref();
if (!entry) {
return;
}
animator.entry.isAnimationRunning = false;
entry.isAnimationRunning = false;
}
}

Expand All @@ -273,8 +354,8 @@ function clearAnimationListener(animator: ExpandedAnimator, listener: android.an

animator.removeListener(listener);

if (animator.entry && Trace.isEnabled()) {
const entry = animator.entry;
if (animator.entry?.deref() && Trace.isEnabled()) {
const entry = animator.entry.deref();
Trace.write(`Clear ${animator.transitionType} - ${entry.transition} for ${entry.fragmentTag}`, Trace.categories.Transition);
}

Expand Down Expand Up @@ -361,70 +442,7 @@ function notifySharedTransition(id: number, eventName: string) {
// Transition listener can't be static because
// android is cloning transitions and we can't expand them :(
function getTransitionListener(entry: ExpandedEntry, transition: androidx.transition.Transition): ExpandedTransitionListener {
if (!TransitionListener) {
@NativeClass
@Interfaces([(<any>androidx).transition.Transition.TransitionListener])
class TransitionListenerImpl extends java.lang.Object implements androidx.transition.Transition.TransitionListener {
public backEntry?: BackstackEntry;
constructor(
public entry: ExpandedEntry,
public transition: androidx.transition.Transition,
) {
super();

return global.__native(this);
}

public onTransitionStart(transition: androidx.transition.Transition): void {
const entry = this.entry;
entry.isAnimationRunning = true;
addToWaitingQueue(entry);
if (Trace.isEnabled()) {
Trace.write(`START ${toShortString(transition)} transition for ${entry.fragmentTag}`, Trace.categories.Transition);
}
if (entry?.transition) {
notifySharedTransition(entry.transition?.id, SharedTransition.startedEvent);
}
}

onTransitionEnd(transition: androidx.transition.Transition): void {
const entry = this.entry;
if (Trace.isEnabled()) {
Trace.write(`END ${toShortString(transition)} transition for ${entry.fragmentTag}`, Trace.categories.Transition);
}
entry.isAnimationRunning = false;
transitionOrAnimationCompleted(entry, this.backEntry);
if (entry?.transition) {
notifySharedTransition(entry.transition?.id, SharedTransition.finishedEvent);
}
}

onTransitionResume(transition: androidx.transition.Transition): void {
if (Trace.isEnabled()) {
const fragment = this.entry.fragmentTag;
Trace.write(`RESUME ${toShortString(transition)} transition for ${fragment}`, Trace.categories.Transition);
}
}

onTransitionPause(transition: androidx.transition.Transition): void {
if (Trace.isEnabled()) {
Trace.write(`PAUSE ${toShortString(transition)} transition for ${this.entry.fragmentTag}`, Trace.categories.Transition);
}
}

onTransitionCancel(transition: androidx.transition.Transition): void {
const entry = this.entry;
entry.isAnimationRunning = false;
if (Trace.isEnabled()) {
Trace.write(`CANCEL ${toShortString(transition)} transition for ${this.entry.fragmentTag}`, Trace.categories.Transition);
}
}
}

TransitionListener = TransitionListenerImpl;
}

return new TransitionListener(entry, transition);
return new (TransitionListener())(entry, transition);
}

function addToWaitingQueue(entry: ExpandedEntry): void {
Expand All @@ -445,7 +463,7 @@ function cloneExpandedTransitionListener(expandedTransitionListener: ExpandedTra

const cloneTransition = expandedTransitionListener.transition.clone();

return addNativeTransitionListener(expandedTransitionListener.entry, cloneTransition);
return addNativeTransitionListener(expandedTransitionListener.entry.deref(), cloneTransition);
}

function clearExitAndReenterTransitions(entry: ExpandedEntry, removeListener: boolean): void {
Expand Down
Loading

Back | FazBrowse Home | New Git URL