Browse Source

Consolidate pop and animatePop

Guy Carmeli 6 years ago
parent
commit
8bf55f7877

+ 32
- 44
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/StackController.java View File

4
 import android.support.annotation.NonNull;
4
 import android.support.annotation.NonNull;
5
 import android.support.annotation.RestrictTo;
5
 import android.support.annotation.RestrictTo;
6
 import android.support.v4.view.ViewPager;
6
 import android.support.v4.view.ViewPager;
7
-import android.view.View;
8
 
7
 
9
 import com.reactnativenavigation.anim.NavigationAnimator;
8
 import com.reactnativenavigation.anim.NavigationAnimator;
10
 import com.reactnativenavigation.parse.Options;
9
 import com.reactnativenavigation.parse.Options;
128
         while (stack.size() > 1) {
127
         while (stack.size() > 1) {
129
             ViewController controller = stack.get(iterator.next());
128
             ViewController controller = stack.get(iterator.next());
130
             if (!stack.isTop(controller.getId())) {
129
             if (!stack.isTop(controller.getId())) {
131
-                stack.remove(controller.getId());
132
-                controller.destroy();
130
+                removeAndDestroyController(controller);
133
             }
131
             }
134
         }
132
         }
135
     }
133
     }
140
             return;
138
             return;
141
         }
139
         }
142
 
140
 
143
-        final ViewController exitingController = stack.pop();
144
-        final ViewController enteringController = stack.peek();
145
-        popInternal(exitingController, enteringController);
146
-
147
-        finishPopping(exitingController.getView(), exitingController, listener);
148
-    }
149
-
150
-    void animatePop(CommandListener listener) {
151
-        if (!canPop()) {
152
-            listener.onError("Nothing to pop");
153
-            return;
154
-        }
155
-
156
-        final ViewController exitingController = stack.pop();
157
-        final ViewController enteringController = stack.peek();
158
-        popInternal(exitingController, enteringController);
159
-
160
-        animator.animatePop(
161
-                exitingController.getView(),
162
-                () -> finishPopping(exitingController.getView(), exitingController, listener)
163
-        );
164
-    }
165
-
166
-    private void popInternal(ViewController disappearing, ViewController appearing) {
141
+        final ViewController disappearing = stack.pop();
142
+        final ViewController appearing = stack.peek();
167
         disappearing.onViewWillDisappear();
143
         disappearing.onViewWillDisappear();
168
         appearing.onViewWillAppear();
144
         appearing.onViewWillAppear();
169
         getView().onChildWillDisappear(disappearing.options, appearing.options, () ->
145
         getView().onChildWillDisappear(disappearing.options, appearing.options, () ->
170
                 getView().addView(appearing.getView(), getView().indexOfChild(disappearing.getView()))
146
                 getView().addView(appearing.getView(), getView().indexOfChild(disappearing.getView()))
171
         );
147
         );
172
-    }
173
 
148
 
174
-    boolean canPop() {
175
-        return stack.size() > 1;
149
+        if (disappearing.options.animated.isTrueOrUndefined()) {
150
+            animator.animatePop(
151
+                    disappearing.getView(),
152
+                    () -> finishPopping(disappearing, listener)
153
+            );
154
+        } else {
155
+            finishPopping(disappearing, listener);
156
+        }
176
     }
157
     }
177
 
158
 
178
-    private void finishPopping(View exitingView, ViewController poppedTop, CommandListener listener) {
179
-        getView().removeView(exitingView);
180
-        poppedTop.destroy();
181
-        listener.onSuccess(poppedTop.getId());
159
+    private void finishPopping(ViewController disappearing, CommandListener listener) {
160
+        getView().removeView(disappearing.getView());
161
+        disappearing.destroy();
162
+        listener.onSuccess(disappearing.getId());
182
     }
163
     }
183
 
164
 
184
-    void popSpecific(final ViewController childController, CommandListener listener) {
165
+    void popSpecific(ViewController childController, CommandListener listener) {
185
         if (stack.isTop(childController.getId())) {
166
         if (stack.isTop(childController.getId())) {
186
-            animatePop(listener);
167
+            pop(listener);
187
         } else {
168
         } else {
188
-            stack.remove(childController.getId());
189
-            childController.destroy();
169
+            removeAndDestroyController(childController);
190
             listener.onSuccess(childController.getId());
170
             listener.onSuccess(childController.getId());
191
         }
171
         }
192
     }
172
     }
203
             String nextControlId = iterator.next();
183
             String nextControlId = iterator.next();
204
             boolean animate = nextControlId.equals(viewController.getId());
184
             boolean animate = nextControlId.equals(viewController.getId());
205
             if (animate) {
185
             if (animate) {
206
-                animatePop(listener);
186
+                pop(listener);
207
             } else {
187
             } else {
208
-                pop(new CommandListenerAdapter());
188
+                removeAndDestroyController(stack.get(currentControlId));
209
             }
189
             }
210
             currentControlId = nextControlId;
190
             currentControlId = nextControlId;
211
         }
191
         }
221
         while (stack.size() > 2) {
201
         while (stack.size() > 2) {
222
             ViewController controller = stack.get(iterator.next());
202
             ViewController controller = stack.get(iterator.next());
223
             if (!stack.isTop(controller.getId())) {
203
             if (!stack.isTop(controller.getId())) {
224
-                stack.remove(controller.getId());
225
-                controller.destroy();
204
+                removeAndDestroyController(controller);
226
             }
205
             }
227
         }
206
         }
228
 
207
 
229
-        animatePop(listener);
208
+        pop(listener);
209
+    }
210
+
211
+    private void removeAndDestroyController(ViewController controller) {
212
+        stack.remove(controller.getId());
213
+        controller.destroy();
230
     }
214
     }
231
 
215
 
232
     ViewController peek() {
216
     ViewController peek() {
244
     @Override
228
     @Override
245
     public boolean handleBack() {
229
     public boolean handleBack() {
246
         if (canPop()) {
230
         if (canPop()) {
247
-            animatePop(new CommandListenerAdapter());
231
+            pop(new CommandListenerAdapter());
248
             return true;
232
             return true;
249
         }
233
         }
250
         return false;
234
         return false;
251
     }
235
     }
252
 
236
 
237
+    boolean canPop() {
238
+        return stack.size() > 1;
239
+    }
240
+
253
     @Override
241
     @Override
254
     public void sendOnNavigationButtonPressed(String buttonId) {
242
     public void sendOnNavigationButtonPressed(String buttonId) {
255
         peek().sendOnNavigationButtonPressed(buttonId);
243
         peek().sendOnNavigationButtonPressed(buttonId);

+ 11
- 7
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/StackControllerTest.java View File

145
         uut.push(child2, new CommandListenerAdapter() {
145
         uut.push(child2, new CommandListenerAdapter() {
146
             @Override
146
             @Override
147
             public void onSuccess(String childId) {
147
             public void onSuccess(String childId) {
148
-                uut.animatePop(new CommandListenerAdapter() {
148
+                uut.pop(new CommandListenerAdapter() {
149
                     @Override
149
                     @Override
150
                     public void onSuccess(String childId) {
150
                     public void onSuccess(String childId) {
151
                         verify(stackLayout[0], times(1)).onChildWillDisappear(child2.options, child1.options, () -> {
151
                         verify(stackLayout[0], times(1)).onChildWillDisappear(child2.options, child1.options, () -> {
343
     public void popToRoot_onlyTopChildIsAnimated() {
343
     public void popToRoot_onlyTopChildIsAnimated() {
344
         child1.options.animated = new Bool(false);
344
         child1.options.animated = new Bool(false);
345
         child2.options.animated = new Bool(false);
345
         child2.options.animated = new Bool(false);
346
-        child3.options.animated = new Bool(false);
347
 
346
 
348
         uut.push(child1, new CommandListenerAdapter());
347
         uut.push(child1, new CommandListenerAdapter());
349
         uut.push(child2, new CommandListenerAdapter());
348
         uut.push(child2, new CommandListenerAdapter());
350
-        uut.push(child3, new CommandListenerAdapter());
351
-
352
-        uut.popToRoot(new CommandListenerAdapter() {
349
+        uut.push(child3, new CommandListenerAdapter() {
353
             @Override
350
             @Override
354
             public void onSuccess(String childId) {
351
             public void onSuccess(String childId) {
355
-                verify(animator, times(1)).animatePop(eq(child3.getView()), any());
352
+                child1.options.animated = new Bool(true);
353
+                child2.options.animated = new Bool(true);
354
+                uut.popToRoot(new CommandListenerAdapter() {
355
+                    @Override
356
+                    public void onSuccess(String childId) {
357
+                        verify(animator, times(1)).animatePop(eq(child3.getView()), any());
358
+                    }
359
+                });
356
             }
360
             }
357
         });
361
         });
358
     }
362
     }
444
 
448
 
445
         assertThat(uut.getTopBar().getVisibility()).isEqualTo(View.GONE);
449
         assertThat(uut.getTopBar().getVisibility()).isEqualTo(View.GONE);
446
         uut.push(child2, new CommandListenerAdapter());
450
         uut.push(child2, new CommandListenerAdapter());
447
-        uut.animatePop(new CommandListenerAdapter() {
451
+        uut.pop(new CommandListenerAdapter() {
448
             @Override
452
             @Override
449
             public void onSuccess(String childId) {
453
             public void onSuccess(String childId) {
450
                 verify(uut.getTopBar(), times(1)).hide();
454
                 verify(uut.getTopBar(), times(1)).hide();

+ 1
- 1
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/TopTabsViewControllerTest.java View File

241
         uut.onViewAppeared();
241
         uut.onViewAppeared();
242
 
242
 
243
         assertThat(ViewHelper.isVisible(stackController.getTopBar().getTopTabs())).isTrue();
243
         assertThat(ViewHelper.isVisible(stackController.getTopBar().getTopTabs())).isTrue();
244
-        stackController.animatePop(new CommandListenerAdapter() {
244
+        stackController.pop(new CommandListenerAdapter() {
245
             @Override
245
             @Override
246
             public void onSuccess(String childId) {
246
             public void onSuccess(String childId) {
247
                 assertThat(ViewHelper.isVisible(stackController.getTopBar().getTopTabs())).isFalse();
247
                 assertThat(ViewHelper.isVisible(stackController.getTopBar().getTopTabs())).isFalse();