Browse Source

Reset animation values when showing topBar

Guy Carmeli 6 years ago
parent
commit
8d4b92cf9c

+ 1
- 0
lib/android/app/src/main/java/com/reactnativenavigation/anim/TopBarAnimator.java View File

@@ -57,6 +57,7 @@ public class TopBarAnimator {
57 57
                 topBar.setVisibility(View.VISIBLE);
58 58
             }
59 59
         });
60
+        topBar.resetAnimationOptions();
60 61
         showAnimator.start();
61 62
     }
62 63
 

+ 6
- 5
lib/android/app/src/main/java/com/reactnativenavigation/presentation/OptionsPresenter.java View File

@@ -9,6 +9,7 @@ import com.reactnativenavigation.parse.OrientationOptions;
9 9
 import com.reactnativenavigation.parse.TopBarOptions;
10 10
 import com.reactnativenavigation.parse.TopTabOptions;
11 11
 import com.reactnativenavigation.parse.TopTabsOptions;
12
+import com.reactnativenavigation.parse.params.Bool;
12 13
 import com.reactnativenavigation.parse.params.Button;
13 14
 import com.reactnativenavigation.utils.UiUtils;
14 15
 import com.reactnativenavigation.viewcontrollers.IReactView;
@@ -34,7 +35,7 @@ public class OptionsPresenter {
34 35
     public void applyChildOptions(Options options, Component child) {
35 36
         applyOrientation(options.orientationOptions);
36 37
         applyButtons(options.topBarOptions.leftButtons, options.topBarOptions.rightButtons);
37
-        applyTopBarOptions(options.topBarOptions, options.animationsOptions, child);
38
+        applyTopBarOptions(options.topBarOptions, options.animationsOptions, child, options.animated);
38 39
         applyTopTabsOptions(options.topTabsOptions);
39 40
         applyTopTabOptions(options.topTabOptions);
40 41
     }
@@ -43,7 +44,7 @@ public class OptionsPresenter {
43 44
         ((Activity) topBar.getContext()).setRequestedOrientation(options.getValue());
44 45
     }
45 46
 
46
-    private void applyTopBarOptions(TopBarOptions options, AnimationsOptions animationOptions, Component component) {
47
+    private void applyTopBarOptions(TopBarOptions options, AnimationsOptions animationOptions, Component component, Bool animated) {
47 48
         topBar.setTitle(options.title.text.get(""));
48 49
         if (options.title.component.hasValue()) topBar.setTitleComponent(options.title.component);
49 50
         topBar.setTitleFontSize(options.title.fontSize.get(defaultTitleFontSize));
@@ -62,14 +63,14 @@ public class OptionsPresenter {
62 63
         if (options.testId.hasValue()) topBar.setTestId(options.testId.get());
63 64
 
64 65
         if (options.visible.isFalse()) {
65
-            if (options.animate.isTrueOrUndefined()) {
66
+            if (options.animate.isTrueOrUndefined() && animated.isTrueOrUndefined()) {
66 67
                 topBar.hideAnimate(animationOptions.pop.topBar);
67 68
             } else {
68 69
                 topBar.hide();
69 70
             }
70 71
         }
71 72
         if (options.visible.isTrueOrUndefined()) {
72
-            if (options.animate.isTrueOrUndefined()) {
73
+            if (options.animate.isTrueOrUndefined() && animated.isTrueOrUndefined()) {
73 74
                 topBar.showAnimate(animationOptions.push.topBar);
74 75
             } else {
75 76
                 topBar.show();
@@ -106,7 +107,7 @@ public class OptionsPresenter {
106 107
 
107 108
     public void onChildWillDisappear(Options disappearing, Options appearing) {
108 109
         if (disappearing.topBarOptions.visible.isTrueOrUndefined() && appearing.topBarOptions.visible.isFalse()) {
109
-            if (disappearing.topBarOptions.animate.isTrueOrUndefined()) {
110
+            if (disappearing.topBarOptions.animate.isTrueOrUndefined() && disappearing.animated.isTrueOrUndefined()) {
110 111
                 topBar.hideAnimate(disappearing.animationsOptions.pop.topBar);
111 112
             } else {
112 113
                 topBar.hide();

+ 12
- 0
lib/android/app/src/main/java/com/reactnativenavigation/views/topbar/TopBar.java View File

@@ -180,6 +180,7 @@ public class TopBar extends AppBarLayout implements ScrollEventListener.ScrollAw
180 180
 
181 181
     public void show() {
182 182
         if (visible() || animator.isAnimatingShow()) return;
183
+        resetAnimationOptions();
183 184
         setVisibility(View.VISIBLE);
184 185
     }
185 186
 
@@ -240,4 +241,15 @@ public class TopBar extends AppBarLayout implements ScrollEventListener.ScrollAw
240 241
     public TextView getTitleTextView() {
241 242
         return titleBar.findTitleTextView();
242 243
     }
244
+
245
+    public void resetAnimationOptions() {
246
+        setTranslationY(0);
247
+        setTranslationX(0);
248
+        setAlpha(1);
249
+        setScaleY(1);
250
+        setScaleX(1);
251
+        setRotationX(0);
252
+        setRotationY(0);
253
+        setRotation(0);
254
+    }
243 255
 }

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

@@ -1,6 +1,7 @@
1 1
 package com.reactnativenavigation.viewcontrollers;
2 2
 
3 3
 import android.app.Activity;
4
+import android.content.Context;
4 5
 import android.support.annotation.NonNull;
5 6
 import android.view.View;
6 7
 
@@ -21,6 +22,8 @@ import com.reactnativenavigation.viewcontrollers.topbar.TopBarController;
21 22
 import com.reactnativenavigation.views.Component;
22 23
 import com.reactnativenavigation.views.ReactComponent;
23 24
 import com.reactnativenavigation.views.StackLayout;
25
+import com.reactnativenavigation.views.titlebar.TitleBarReactViewCreator;
26
+import com.reactnativenavigation.views.topbar.TopBar;
24 27
 
25 28
 import org.assertj.core.api.iterable.Extractor;
26 29
 import org.json.JSONObject;
@@ -170,17 +173,6 @@ public class StackControllerTest extends BaseTest {
170 173
         assertThat(uut.isEmpty()).isFalse();
171 174
     }
172 175
 
173
-    @Test
174
-    public void pushAssignsRefToSelfOnPushedController() {
175
-        assertThat(child1.getParentController()).isNull();
176
-        uut.push(child1, new CommandListenerAdapter());
177
-        assertThat(child1.getParentController()).isEqualTo(uut);
178
-
179
-        StackController anotherNavController = createStackController("another");
180
-        anotherNavController.push(child2, new CommandListenerAdapter());
181
-        assertThat(child2.getParentController()).isEqualTo(anotherNavController);
182
-    }
183
-
184 176
     @Test
185 177
     public void handleBack_PopsUnlessSingleChild() {
186 178
         assertThat(uut.isEmpty()).isTrue();
@@ -226,14 +218,14 @@ public class StackControllerTest extends BaseTest {
226 218
     }
227 219
 
228 220
     @Test
229
-    public void pushAddsToViewTree() {
221
+    public void push_addsToViewTree() {
230 222
         assertThat(uut.getView().findViewById(child1.getView().getId())).isNull();
231 223
         uut.push(child1, new CommandListenerAdapter());
232 224
         assertThat(uut.getView().findViewById(child1.getView().getId())).isNotNull();
233 225
     }
234 226
 
235 227
     @Test
236
-    public void pushRemovesPreviousFromTree() {
228
+    public void push_removesPreviousFromTree() {
237 229
         assertThat(uut.getView().findViewById(child1.getView().getId())).isNull();
238 230
         uut.push(child1, new CommandListenerAdapter());
239 231
         assertThat(uut.getView().findViewById(child1.getView().getId())).isNotNull();
@@ -247,7 +239,60 @@ public class StackControllerTest extends BaseTest {
247 239
     }
248 240
 
249 241
     @Test
250
-    public void popReplacesViewWithPrevious() {
242
+    public void push_assignsRefToSelfOnPushedController() {
243
+        assertThat(child1.getParentController()).isNull();
244
+        uut.push(child1, new CommandListenerAdapter());
245
+        assertThat(child1.getParentController()).isEqualTo(uut);
246
+
247
+        StackController anotherNavController = createStackController("another");
248
+        anotherNavController.push(child2, new CommandListenerAdapter());
249
+        assertThat(child2.getParentController()).isEqualTo(anotherNavController);
250
+    }
251
+
252
+    @Test
253
+    public void push_doesNotAnimateTopBarIfScreenIsPushedWithoutAnimation() {
254
+        uut.ensureViewIsCreated();
255
+        child1.ensureViewIsCreated();
256
+
257
+        child1.options.topBarOptions.visible = new Bool(false);
258
+        child1.options.topBarOptions.animate = new Bool(false);
259
+        child1.options.animated = new Bool(false);
260
+        child2.options.animated = new Bool(false);
261
+
262
+        uut.push(child1, new CommandListenerAdapter() {
263
+            @Override
264
+            public void onSuccess(String childId) {
265
+                child1.onViewAppeared();
266
+                assertThat(uut.getTopBar().getVisibility()).isEqualTo(View.GONE);
267
+
268
+                uut.push(child2, new CommandListenerAdapter());
269
+                child2.onViewAppeared();
270
+                verify(uut.getTopBar(), times(0)).showAnimate(child2.options.animationsOptions.push.topBar);
271
+                assertThat(uut.getTopBar().getVisibility()).isEqualTo(View.VISIBLE);
272
+                verify(uut.getTopBar(), times(1)).resetAnimationOptions();
273
+            }
274
+        });
275
+    }
276
+
277
+    @Test
278
+    public void push_animatesAndClearsPreviousAnimationValues() {
279
+        uut.ensureViewIsCreated();
280
+
281
+        child1.options.topBarOptions.visible = new Bool(false);
282
+        child1.options.topBarOptions.animate = new Bool(false);
283
+        child1.options.animated = new Bool(false);
284
+
285
+        uut.push(child1, new CommandListenerAdapter());
286
+        uut.push(child2, new CommandListenerAdapter() {
287
+            @Override
288
+            public void onSuccess(String childId) {
289
+                verify(uut.getTopBar(), times(1)).resetAnimationOptions();
290
+            }
291
+        });
292
+    }
293
+
294
+    @Test
295
+    public void pop_replacesViewWithPrevious() {
251 296
         final View child2View = child2.getView();
252 297
         final View child1View = child1.getView();
253 298
 
@@ -265,7 +310,7 @@ public class StackControllerTest extends BaseTest {
265 310
     }
266 311
 
267 312
     @Test
268
-    public void popSpecificWhenTopIsRegularPop() {
313
+    public void pop_specificWhenTopIsRegularPop() {
269 314
         uut.push(child1, new CommandListenerAdapter());
270 315
         uut.push(child2, new CommandListenerAdapter() {
271 316
             @Override
@@ -282,7 +327,7 @@ public class StackControllerTest extends BaseTest {
282 327
     }
283 328
 
284 329
     @Test
285
-    public void popSpecificDeepInStack() {
330
+    public void popSpecific_deepInStack() {
286 331
         uut.push(child1, new CommandListenerAdapter());
287 332
         uut.push(child2, new CommandListenerAdapter());
288 333
         assertIsChildById(uut.getView(), child2.getView());
@@ -460,29 +505,52 @@ public class StackControllerTest extends BaseTest {
460 505
     }
461 506
 
462 507
     @Test
463
-    public void pop_animatesTopBarIfNeeded() {
508
+    public void pop_animatesTopBar() {
464 509
         uut.ensureViewIsCreated();
465 510
 
466 511
         child1.options.topBarOptions.visible = new Bool(false);
467
-        child1.options.topBarOptions.animate = new Bool(false);
468
-        child2.options.topBarOptions.visible = new Bool(true);
469
-        uut.push(child1, new CommandListenerAdapter());
470
-        child1.onViewAppeared();
471
-
472
-        assertThat(uut.getTopBar().getVisibility()).isEqualTo(View.GONE);
473
-        uut.push(child2, new CommandListenerAdapter() {
512
+        child1.options.animated = new Bool(false);
513
+        child2.options.animated = new Bool(true);
514
+        uut.push(child1, new CommandListenerAdapter() {
474 515
             @Override
475 516
             public void onSuccess(String childId) {
476
-                uut.pop(new CommandListenerAdapter() {
517
+                child1.onViewAppeared();
518
+                assertThat(uut.getTopBar().getVisibility()).isEqualTo(View.GONE);
519
+                uut.push(child2, new CommandListenerAdapter() {
477 520
                     @Override
478 521
                     public void onSuccess(String childId) {
479
-                        verify(uut.getTopBar(), times(1)).hide();
522
+                        uut.pop(new CommandListenerAdapter() {
523
+                            @Override
524
+                            public void onSuccess(String childId) {
525
+                                verify(uut.getTopBar(), times(1)).hideAnimate(child2.options.animationsOptions.pop.topBar);
526
+                            }
527
+                        });
480 528
                     }
481 529
                 });
482 530
             }
483 531
         });
484 532
     }
485 533
 
534
+    @Test
535
+    public void pop_doesNotAnimateTopBarIfScreenIsPushedWithoutAnimation() {
536
+        uut.ensureViewIsCreated();
537
+
538
+        child1.options.topBarOptions.visible = new Bool(false);
539
+        child2.options.animated = new Bool(false);
540
+        child1.ensureViewIsCreated();
541
+        uut.push(child1, new CommandListenerAdapter() {
542
+            @Override
543
+            public void onSuccess(String childId) {
544
+                uut.push(child2, new CommandListenerAdapter());
545
+                assertThat(uut.getTopBar().getVisibility()).isEqualTo(View.VISIBLE);
546
+
547
+                uut.pop(new CommandListenerAdapter());
548
+                verify(uut.getTopBar(), times(0)).hideAnimate(child2.options.animationsOptions.pop.topBar);
549
+                assertThat(uut.getTopBar().getVisibility()).isEqualTo(View.GONE);
550
+            }
551
+        });
552
+    }
553
+
486 554
     @Test
487 555
     public void popSpecific_CallsDestroyOnPoppedChild() {
488 556
         child1 = spy(child1);
@@ -656,10 +724,20 @@ public class StackControllerTest extends BaseTest {
656 724
     }
657 725
 
658 726
     private StackController createStackController(String id) {
659
-        topBarController = spy(new TopBarController());
727
+        topBarController = spy(new TopBarController() {
728
+            @Override
729
+            protected TopBar createTopBar(Context context, ReactViewCreator buttonCreator, TitleBarReactViewCreator titleBarReactViewCreator, TopBarBackgroundViewController topBarBackgroundViewController, TopBarButtonController.OnClickListener topBarButtonClickListener, StackLayout stackLayout) {
730
+                TopBar topBar = spy(super.createTopBar(context, buttonCreator, titleBarReactViewCreator, topBarBackgroundViewController, topBarButtonClickListener, stackLayout));
731
+                return topBar;
732
+            }
733
+        });
660 734
         return new StackController(activity,
661
-                new TopBarButtonCreatorMock(), new TitleBarReactViewCreatorMock(), new TopBarBackgroundViewController(activity, new TopBarBackgroundViewCreatorMock()),
662
-                topBarController, id, new Options()) {
735
+                new TopBarButtonCreatorMock(),
736
+                new TitleBarReactViewCreatorMock(),
737
+                new TopBarBackgroundViewController(activity, new TopBarBackgroundViewCreatorMock()),
738
+                topBarController,
739
+                id,
740
+                new Options()) {
663 741
             @Override
664 742
             NavigationAnimator createAnimator() {
665 743
                 animator = Mockito.mock(NavigationAnimator.class);