Browse Source

Refactor NavigationAnimator

Guy Carmeli 6 years ago
parent
commit
c34a3483d8

+ 2
- 3
lib/android/app/src/main/java/com/reactnativenavigation/anim/BaseAnimator.java View File

@@ -5,7 +5,6 @@ import android.animation.AnimatorSet;
5 5
 import android.animation.ObjectAnimator;
6 6
 import android.content.Context;
7 7
 import android.support.annotation.NonNull;
8
-import android.support.annotation.RestrictTo;
9 8
 import android.view.View;
10 9
 import android.view.animation.AccelerateInterpolator;
11 10
 import android.view.animation.DecelerateInterpolator;
@@ -15,12 +14,12 @@ import com.reactnativenavigation.utils.UiUtils;
15 14
 
16 15
 class BaseAnimator {
17 16
 
18
-    protected AnimationsOptions options = new AnimationsOptions();
19
-
20 17
     private static final int DURATION = 300;
21 18
     private static final DecelerateInterpolator DECELERATE_INTERPOLATOR = new DecelerateInterpolator();
22 19
     private static final AccelerateInterpolator ACCELERATE_INTERPOLATOR = new AccelerateInterpolator();
23 20
 
21
+    protected AnimationsOptions options = new AnimationsOptions();
22
+
24 23
     private float translationY;
25 24
 
26 25
     BaseAnimator(Context context) {

+ 8
- 23
lib/android/app/src/main/java/com/reactnativenavigation/anim/NavigationAnimator.java View File

@@ -4,7 +4,6 @@ import android.animation.Animator;
4 4
 import android.animation.AnimatorListenerAdapter;
5 5
 import android.animation.AnimatorSet;
6 6
 import android.content.Context;
7
-import android.support.annotation.Nullable;
8 7
 import android.view.View;
9 8
 
10 9
 import com.reactnativenavigation.parse.AnimationsOptions;
@@ -21,14 +20,9 @@ public class NavigationAnimator extends BaseAnimator {
21 20
         this.options = options;
22 21
     }
23 22
 
24
-    public void animatePush(final View view, @Nullable final AnimationListener animationListener) {
23
+    public void push(final View view, AnimationListener animationListener) {
25 24
         view.setVisibility(View.INVISIBLE);
26
-        AnimatorSet set;
27
-        if (options.push.content.hasValue()) {
28
-            set = options.push.content.getAnimation(view);
29
-        } else {
30
-            set = getDefaultPushAnimation(view);
31
-        }
25
+        AnimatorSet set = options.push.content.getAnimation(view, getDefaultPushAnimation(view));
32 26
         set.addListener(new AnimatorListenerAdapter() {
33 27
             @Override
34 28
             public void onAnimationStart(Animator animation) {
@@ -37,35 +31,26 @@ public class NavigationAnimator extends BaseAnimator {
37 31
 
38 32
             @Override
39 33
             public void onAnimationEnd(Animator animation) {
40
-                if (animationListener != null) {
41
-                    animationListener.onAnimationEnd();
42
-                }
34
+                animationListener.onAnimationEnd();
43 35
             }
44 36
         });
45 37
         set.start();
46 38
     }
47 39
 
48
-    public void animatePop(View view, @Nullable final AnimationListener animationListener) {
49
-        AnimatorSet set;
50
-        if (options.pop.content.hasValue()) {
51
-            set = options.pop.content.getAnimation(view);
52
-        } else {
53
-            set = getDefaultPopAnimation(view);
54
-        }
40
+    public void pop(View view, AnimationListener animationListener) {
41
+        AnimatorSet set = options.pop.content.getAnimation(view, getDefaultPopAnimation(view));
55 42
         set.addListener(new AnimatorListenerAdapter() {
56 43
             @Override
57 44
             public void onAnimationEnd(Animator animation) {
58
-                if (animationListener != null) {
59
-                    animationListener.onAnimationEnd();
60
-                }
45
+                animationListener.onAnimationEnd();
61 46
             }
62 47
         });
63 48
         set.start();
64 49
     }
65 50
 
66
-    public void animateStartApp(View view, @Nullable final AnimationListener animationListener) {
51
+    public void animateStartApp(View view, AnimationListener animationListener) {
67 52
         view.setVisibility(View.INVISIBLE);
68
-        AnimatorSet set = options.startApp.getAnimation(view);
53
+        AnimatorSet set = options.startApp.getAnimation(view, null);
69 54
         set.addListener(new AnimatorListenerAdapter() {
70 55
             @Override
71 56
             public void onAnimationStart(Animator animation) {

+ 2
- 2
lib/android/app/src/main/java/com/reactnativenavigation/parse/AnimationOptions.java View File

@@ -3,7 +3,6 @@ package com.reactnativenavigation.parse;
3 3
 
4 4
 import android.animation.Animator;
5 5
 import android.animation.AnimatorSet;
6
-import android.util.Log;
7 6
 import android.util.Property;
8 7
 import android.view.View;
9 8
 
@@ -62,7 +61,8 @@ public class AnimationOptions {
62 61
         return hasValue;
63 62
     }
64 63
 
65
-    public AnimatorSet getAnimation(View view) {
64
+    public AnimatorSet getAnimation(View view, AnimatorSet defaultAnimation) {
65
+        if (!hasValue()) return defaultAnimation;
66 66
         AnimatorSet animationSet = new AnimatorSet();
67 67
         List<Animator> animators = new ArrayList<>();
68 68
         for (ValueAnimationOptions options : valueOptions) {

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

@@ -97,7 +97,7 @@ public class StackController extends ParentController<StackLayout> {
97 97
 
98 98
         if (toRemove != null) {
99 99
             if (child.options.animated.isTrueOrUndefined()) {
100
-                animator.animatePush(child.getView(), () -> {
100
+                animator.push(child.getView(), () -> {
101 101
                     getView().removeView(toRemove.getView());
102 102
                     listener.onSuccess(child.getId());
103 103
                 });
@@ -143,7 +143,7 @@ public class StackController extends ParentController<StackLayout> {
143 143
         );
144 144
 
145 145
         if (disappearing.options.animated.isTrueOrUndefined()) {
146
-            animator.animatePop(
146
+            animator.pop(
147 147
                     disappearing.getView(),
148 148
                     () -> finishPopping(disappearing, listener)
149 149
             );

+ 2
- 2
lib/android/app/src/test/java/com/reactnativenavigation/mocks/TestNavigationAnimator.java View File

@@ -13,12 +13,12 @@ public class TestNavigationAnimator extends NavigationAnimator {
13 13
     }
14 14
 
15 15
     @Override
16
-    public void animatePush(final View enteringView, AnimationListener animationListener) {
16
+    public void push(final View enteringView, AnimationListener animationListener) {
17 17
         if (animationListener != null) animationListener.onAnimationEnd();
18 18
     }
19 19
 
20 20
     @Override
21
-    public void animatePop(final View enteringView, AnimationListener animationListener) {
21
+    public void pop(final View enteringView, AnimationListener animationListener) {
22 22
         if (animationListener != null) animationListener.onAnimationEnd();
23 23
     }
24 24
 }

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

@@ -328,9 +328,9 @@ public class StackControllerTest extends BaseTest {
328 328
                 uut.popTo(child2, new CommandListenerAdapter() {
329 329
                     @Override
330 330
                     public void onSuccess(String childId) {
331
-                        verify(animator, times(0)).animatePop(eq(child1.getView()), any());
332
-                        verify(animator, times(0)).animatePop(eq(child2.getView()), any());
333
-                        verify(animator, times(1)).animatePop(eq(child4.getView()), any());
331
+                        verify(animator, times(0)).pop(eq(child1.getView()), any());
332
+                        verify(animator, times(0)).pop(eq(child2.getView()), any());
333
+                        verify(animator, times(1)).pop(eq(child4.getView()), any());
334 334
                     }
335 335
                 });
336 336
             }
@@ -376,7 +376,7 @@ public class StackControllerTest extends BaseTest {
376 376
                 uut.popToRoot(new CommandListenerAdapter() {
377 377
                     @Override
378 378
                     public void onSuccess(String childId) {
379
-                        verify(animator, times(1)).animatePop(eq(child3.getView()), any());
379
+                        verify(animator, times(1)).pop(eq(child3.getView()), any());
380 380
                     }
381 381
                 });
382 382
             }