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,7 +4,6 @@ import android.app.Activity;
4 4
 import android.support.annotation.NonNull;
5 5
 import android.support.annotation.RestrictTo;
6 6
 import android.support.v4.view.ViewPager;
7
-import android.view.View;
8 7
 
9 8
 import com.reactnativenavigation.anim.NavigationAnimator;
10 9
 import com.reactnativenavigation.parse.Options;
@@ -128,8 +127,7 @@ public class StackController extends ParentController<StackLayout> {
128 127
         while (stack.size() > 1) {
129 128
             ViewController controller = stack.get(iterator.next());
130 129
             if (!stack.isTop(controller.getId())) {
131
-                stack.remove(controller.getId());
132
-                controller.destroy();
130
+                removeAndDestroyController(controller);
133 131
             }
134 132
         }
135 133
     }
@@ -140,53 +138,35 @@ public class StackController extends ParentController<StackLayout> {
140 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 143
         disappearing.onViewWillDisappear();
168 144
         appearing.onViewWillAppear();
169 145
         getView().onChildWillDisappear(disappearing.options, appearing.options, () ->
170 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 166
         if (stack.isTop(childController.getId())) {
186
-            animatePop(listener);
167
+            pop(listener);
187 168
         } else {
188
-            stack.remove(childController.getId());
189
-            childController.destroy();
169
+            removeAndDestroyController(childController);
190 170
             listener.onSuccess(childController.getId());
191 171
         }
192 172
     }
@@ -203,9 +183,9 @@ public class StackController extends ParentController<StackLayout> {
203 183
             String nextControlId = iterator.next();
204 184
             boolean animate = nextControlId.equals(viewController.getId());
205 185
             if (animate) {
206
-                animatePop(listener);
186
+                pop(listener);
207 187
             } else {
208
-                pop(new CommandListenerAdapter());
188
+                removeAndDestroyController(stack.get(currentControlId));
209 189
             }
210 190
             currentControlId = nextControlId;
211 191
         }
@@ -221,12 +201,16 @@ public class StackController extends ParentController<StackLayout> {
221 201
         while (stack.size() > 2) {
222 202
             ViewController controller = stack.get(iterator.next());
223 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 216
     ViewController peek() {
@@ -244,12 +228,16 @@ public class StackController extends ParentController<StackLayout> {
244 228
     @Override
245 229
     public boolean handleBack() {
246 230
         if (canPop()) {
247
-            animatePop(new CommandListenerAdapter());
231
+            pop(new CommandListenerAdapter());
248 232
             return true;
249 233
         }
250 234
         return false;
251 235
     }
252 236
 
237
+    boolean canPop() {
238
+        return stack.size() > 1;
239
+    }
240
+
253 241
     @Override
254 242
     public void sendOnNavigationButtonPressed(String buttonId) {
255 243
         peek().sendOnNavigationButtonPressed(buttonId);

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

@@ -145,7 +145,7 @@ public class StackControllerTest extends BaseTest {
145 145
         uut.push(child2, new CommandListenerAdapter() {
146 146
             @Override
147 147
             public void onSuccess(String childId) {
148
-                uut.animatePop(new CommandListenerAdapter() {
148
+                uut.pop(new CommandListenerAdapter() {
149 149
                     @Override
150 150
                     public void onSuccess(String childId) {
151 151
                         verify(stackLayout[0], times(1)).onChildWillDisappear(child2.options, child1.options, () -> {
@@ -343,16 +343,20 @@ public class StackControllerTest extends BaseTest {
343 343
     public void popToRoot_onlyTopChildIsAnimated() {
344 344
         child1.options.animated = new Bool(false);
345 345
         child2.options.animated = new Bool(false);
346
-        child3.options.animated = new Bool(false);
347 346
 
348 347
         uut.push(child1, new CommandListenerAdapter());
349 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 350
             @Override
354 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,7 +448,7 @@ public class StackControllerTest extends BaseTest {
444 448
 
445 449
         assertThat(uut.getTopBar().getVisibility()).isEqualTo(View.GONE);
446 450
         uut.push(child2, new CommandListenerAdapter());
447
-        uut.animatePop(new CommandListenerAdapter() {
451
+        uut.pop(new CommandListenerAdapter() {
448 452
             @Override
449 453
             public void onSuccess(String childId) {
450 454
                 verify(uut.getTopBar(), times(1)).hide();

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

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