From d0a17fabf4f5360b0f54797867a7a49960a1937d Mon Sep 17 00:00:00 2001 From: Jasu Date: Tue, 3 Sep 2019 10:11:05 +0300 Subject: [PATCH] Add animation support for setStackRoot (#5357) Fixed Android's native setStackRoot animation and added animation support for Android's setStackRoot. --- .../parse/AnimationsOptions.java | 4 ++ .../stack/StackController.java | 49 +++++++++++++------ package.json | 2 +- 3 files changed, 39 insertions(+), 16 deletions(-) diff --git a/lib/android/app/src/main/java/com/reactnativenavigation/parse/AnimationsOptions.java b/lib/android/app/src/main/java/com/reactnativenavigation/parse/AnimationsOptions.java index 738c13446ac..148bad8b96f 100644 --- a/lib/android/app/src/main/java/com/reactnativenavigation/parse/AnimationsOptions.java +++ b/lib/android/app/src/main/java/com/reactnativenavigation/parse/AnimationsOptions.java @@ -11,6 +11,7 @@ public static AnimationsOptions parse(JSONObject json) { options.push = NestedAnimationsOptions.parse(json.optJSONObject("push")); options.pop = NestedAnimationsOptions.parse(json.optJSONObject("pop")); + options.setStackRoot = NestedAnimationsOptions.parse(json.optJSONObject("setStackRoot")); options.setRoot = AnimationOptions.parse(json.optJSONObject("setRoot")); options.showModal = AnimationOptions.parse(json.optJSONObject("showModal")); options.dismissModal = AnimationOptions.parse(json.optJSONObject("dismissModal")); @@ -20,6 +21,7 @@ public static AnimationsOptions parse(JSONObject json) { public NestedAnimationsOptions push = new NestedAnimationsOptions(); public NestedAnimationsOptions pop = new NestedAnimationsOptions(); + public NestedAnimationsOptions setStackRoot = new NestedAnimationsOptions(); public AnimationOptions setRoot = new AnimationOptions(); public AnimationOptions showModal = new AnimationOptions(); public AnimationOptions dismissModal = new AnimationOptions(); @@ -28,6 +30,7 @@ public void mergeWith(AnimationsOptions other) { push.mergeWith(other.push); pop.mergeWith(other.pop); setRoot.mergeWith(other.setRoot); + setStackRoot.mergeWith(other.setStackRoot); showModal.mergeWith(other.showModal); dismissModal.mergeWith(other.dismissModal); } @@ -35,6 +38,7 @@ public void mergeWith(AnimationsOptions other) { void mergeWithDefault(AnimationsOptions defaultOptions) { push.mergeWithDefault(defaultOptions.push); pop.mergeWithDefault(defaultOptions.pop); + setStackRoot.mergeWithDefault(defaultOptions.setStackRoot); setRoot.mergeWithDefault(defaultOptions.setRoot); showModal.mergeWithDefault(defaultOptions.showModal); dismissModal.mergeWithDefault(defaultOptions.dismissModal); diff --git a/lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/stack/StackController.java b/lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/stack/StackController.java index 751ced0ccc9..7e74c4b4e70 100644 --- a/lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/stack/StackController.java +++ b/lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/stack/StackController.java @@ -183,23 +183,27 @@ private void addChildToStack(ViewController child, View view, Options resolvedOp public void setRoot(List children, CommandListener listener) { animator.cancelPushAnimations(); + final ViewController toRemove = stack.peek(); IdStack stackToDestroy = stack; stack = new IdStack<>(); + + ViewController child = last(children); if (children.size() == 1) { - backButtonHelper.clear(last(children)); - push(last(children), new CommandListenerAdapter() { - @Override - public void onSuccess(String childId) { - destroyStack(stackToDestroy); - listener.onSuccess(childId); - } - }); + backButtonHelper.clear(child); } else { - backButtonHelper.addToPushedChild(last(children)); - push(last(children), new CommandListenerAdapter() { - @Override - public void onSuccess(String childId) { - destroyStack(stackToDestroy); + backButtonHelper.addToPushedChild(child); + } + + child.setParentController(this); + stack.push(child.getId(), child); + Options resolvedOptions = resolveCurrentOptions(presenter.getDefaultOptions()); + addChildToStack(child, child.getView(), resolvedOptions); + + CommandListener listenerAdapter = new CommandListenerAdapter() { + @Override + public void onSuccess(String childId) { + destroyStack(stackToDestroy); + if (children.size() > 1) { for (int i = 0; i < children.size() - 1; i++) { stack.set(children.get(i).getId(), children.get(i), i); children.get(i).setParentController(StackController.this); @@ -209,9 +213,24 @@ public void onSuccess(String childId) { backButtonHelper.addToPushedChild(children.get(i)); } } - listener.onSuccess(childId); } - }); + listener.onSuccess(childId); + } + }; + + if (toRemove != null && resolvedOptions.animations.setStackRoot.enabled.isTrueOrUndefined()) { + if (resolvedOptions.animations.setStackRoot.waitForRender.isTrue()) { + child.getView().setAlpha(0); + child.addOnAppearedListener(() -> animator.push(child.getView(), resolvedOptions.animations.setStackRoot, resolvedOptions.transitions, toRemove.getElements(), child.getElements(), () -> { + listenerAdapter.onSuccess(child.getId()); + })); + } else { + animator.push(child.getView(), resolvedOptions.animations.setStackRoot, () -> { + listenerAdapter.onSuccess(child.getId()); + }); + } + } else { + listenerAdapter.onSuccess(child.getId()); } } diff --git a/package.json b/package.json index 3189fac5e7c..98da014a0b1 100644 --- a/package.json +++ b/package.json @@ -166,4 +166,4 @@ } } } -} \ No newline at end of file +}