Browse Source

Add animation support for setStackRoot (#5357)

Fixed Android's native setStackRoot animation and added animation support for Android's setStackRoot.
Jasu 5 years ago
parent
commit
d0a17fabf4

+ 4
- 0
lib/android/app/src/main/java/com/reactnativenavigation/parse/AnimationsOptions.java View File

@@ -11,6 +11,7 @@ public class AnimationsOptions {
11 11
 
12 12
         options.push = NestedAnimationsOptions.parse(json.optJSONObject("push"));
13 13
         options.pop = NestedAnimationsOptions.parse(json.optJSONObject("pop"));
14
+        options.setStackRoot = NestedAnimationsOptions.parse(json.optJSONObject("setStackRoot"));
14 15
         options.setRoot = AnimationOptions.parse(json.optJSONObject("setRoot"));
15 16
         options.showModal = AnimationOptions.parse(json.optJSONObject("showModal"));
16 17
         options.dismissModal = AnimationOptions.parse(json.optJSONObject("dismissModal"));
@@ -20,6 +21,7 @@ public class AnimationsOptions {
20 21
 
21 22
     public NestedAnimationsOptions push = new NestedAnimationsOptions();
22 23
     public NestedAnimationsOptions pop = new NestedAnimationsOptions();
24
+    public NestedAnimationsOptions setStackRoot = new NestedAnimationsOptions();
23 25
     public AnimationOptions setRoot = new AnimationOptions();
24 26
     public AnimationOptions showModal = new AnimationOptions();
25 27
     public AnimationOptions dismissModal = new AnimationOptions();
@@ -28,6 +30,7 @@ public class AnimationsOptions {
28 30
         push.mergeWith(other.push);
29 31
         pop.mergeWith(other.pop);
30 32
         setRoot.mergeWith(other.setRoot);
33
+        setStackRoot.mergeWith(other.setStackRoot);
31 34
         showModal.mergeWith(other.showModal);
32 35
         dismissModal.mergeWith(other.dismissModal);
33 36
     }
@@ -35,6 +38,7 @@ public class AnimationsOptions {
35 38
     void mergeWithDefault(AnimationsOptions defaultOptions) {
36 39
         push.mergeWithDefault(defaultOptions.push);
37 40
         pop.mergeWithDefault(defaultOptions.pop);
41
+        setStackRoot.mergeWithDefault(defaultOptions.setStackRoot);
38 42
         setRoot.mergeWithDefault(defaultOptions.setRoot);
39 43
         showModal.mergeWithDefault(defaultOptions.showModal);
40 44
         dismissModal.mergeWithDefault(defaultOptions.dismissModal);

+ 34
- 15
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/stack/StackController.java View File

@@ -183,23 +183,27 @@ public class StackController extends ParentController<StackLayout> {
183 183
 
184 184
     public void setRoot(List<ViewController> children, CommandListener listener) {
185 185
         animator.cancelPushAnimations();
186
+        final ViewController toRemove = stack.peek();
186 187
         IdStack stackToDestroy = stack;
187 188
         stack = new IdStack<>();
189
+
190
+        ViewController child = last(children);
188 191
         if (children.size() == 1) {
189
-            backButtonHelper.clear(last(children));
190
-            push(last(children), new CommandListenerAdapter() {
191
-                @Override
192
-                public void onSuccess(String childId) {
193
-                    destroyStack(stackToDestroy);
194
-                    listener.onSuccess(childId);
195
-                }
196
-            });
192
+            backButtonHelper.clear(child);
197 193
         } else {
198
-            backButtonHelper.addToPushedChild(last(children));
199
-            push(last(children), new CommandListenerAdapter() {
200
-                @Override
201
-                public void onSuccess(String childId) {
202
-                    destroyStack(stackToDestroy);
194
+            backButtonHelper.addToPushedChild(child);
195
+        }
196
+
197
+        child.setParentController(this);
198
+        stack.push(child.getId(), child);
199
+        Options resolvedOptions = resolveCurrentOptions(presenter.getDefaultOptions());
200
+        addChildToStack(child, child.getView(), resolvedOptions);
201
+
202
+        CommandListener listenerAdapter = new CommandListenerAdapter() {
203
+            @Override
204
+            public void onSuccess(String childId) {
205
+                destroyStack(stackToDestroy);
206
+                if (children.size() > 1) {
203 207
                     for (int i = 0; i < children.size() - 1; i++) {
204 208
                         stack.set(children.get(i).getId(), children.get(i), i);
205 209
                         children.get(i).setParentController(StackController.this);
@@ -209,9 +213,24 @@ public class StackController extends ParentController<StackLayout> {
209 213
                             backButtonHelper.addToPushedChild(children.get(i));
210 214
                         }
211 215
                     }
212
-                    listener.onSuccess(childId);
213 216
                 }
214
-            });
217
+                listener.onSuccess(childId);
218
+            }
219
+        };
220
+
221
+        if (toRemove != null && resolvedOptions.animations.setStackRoot.enabled.isTrueOrUndefined()) {
222
+            if (resolvedOptions.animations.setStackRoot.waitForRender.isTrue()) {
223
+                child.getView().setAlpha(0);
224
+                child.addOnAppearedListener(() -> animator.push(child.getView(), resolvedOptions.animations.setStackRoot, resolvedOptions.transitions, toRemove.getElements(), child.getElements(), () -> {
225
+                    listenerAdapter.onSuccess(child.getId());
226
+                }));
227
+            } else {
228
+                animator.push(child.getView(), resolvedOptions.animations.setStackRoot, () -> {
229
+                    listenerAdapter.onSuccess(child.getId());
230
+                });
231
+            }
232
+        } else {
233
+            listenerAdapter.onSuccess(child.getId());
215 234
         }
216 235
     }
217 236
 

+ 1
- 1
package.json View File

@@ -166,4 +166,4 @@
166 166
       }
167 167
     }
168 168
   }
169
-}
169
+}