Browse Source

Add ability to push pop screens without animation

Guy Carmeli 8 years ago
parent
commit
1996de6d1b

+ 7
- 14
android/app/src/main/java/com/reactnativenavigation/layouts/BottomTabsLayout.java View File

@@ -9,7 +9,6 @@ import com.reactnativenavigation.params.ActivityParams;
9 9
 import com.reactnativenavigation.params.ScreenParams;
10 10
 import com.reactnativenavigation.params.TitleBarButtonParams;
11 11
 import com.reactnativenavigation.params.TitleBarLeftButtonParams;
12
-import com.reactnativenavigation.screens.ScreenAnimator;
13 12
 import com.reactnativenavigation.screens.ScreenStack;
14 13
 import com.reactnativenavigation.views.BottomTabs;
15 14
 
@@ -25,7 +24,6 @@ public class BottomTabsLayout extends RelativeLayout implements Layout, AHBottom
25 24
     private BottomTabs bottomTabs;
26 25
     private ScreenStack[] screenStacks;
27 26
     private int currentStackIndex = 0;
28
-    private ScreenAnimator screenAnimator;
29 27
 
30 28
     public BottomTabsLayout(AppCompatActivity activity, ActivityParams params) {
31 29
         super(activity);
@@ -37,7 +35,6 @@ public class BottomTabsLayout extends RelativeLayout implements Layout, AHBottom
37 35
 
38 36
     private void createLayout() {
39 37
         createBottomTabs();
40
-        createScreenAnimator();
41 38
         addBottomTabsToScreen();
42 39
         addScreenStacks();
43 40
         showInitialScreenStack();
@@ -50,7 +47,7 @@ public class BottomTabsLayout extends RelativeLayout implements Layout, AHBottom
50 47
     }
51 48
 
52 49
     private void createAndAddScreenStack(int position) {
53
-        ScreenStack newStack = new ScreenStack(activity, params.tabParams.get(position), this, screenAnimator);
50
+        ScreenStack newStack = new ScreenStack(activity, params.tabParams.get(position), this);
54 51
         screenStacks[position] = newStack;
55 52
         newStack.setVisibility(INVISIBLE);
56 53
         addScreenStack(newStack);
@@ -73,10 +70,6 @@ public class BottomTabsLayout extends RelativeLayout implements Layout, AHBottom
73 70
         bottomTabs.addTabs(params.tabParams, this);
74 71
     }
75 72
 
76
-    private void createScreenAnimator() {
77
-        screenAnimator = new ScreenAnimator(bottomTabs);
78
-    }
79
-
80 73
     private void addBottomTabsToScreen() {
81 74
         LayoutParams lp = new LayoutParams(MATCH_PARENT, WRAP_CONTENT);
82 75
         lp.addRule(ALIGN_PARENT_BOTTOM);
@@ -95,7 +88,7 @@ public class BottomTabsLayout extends RelativeLayout implements Layout, AHBottom
95 88
     @Override
96 89
     public boolean onBackPressed() {
97 90
         if (getCurrentScreenStack().canPop()) {
98
-            getCurrentScreenStack().pop(screenAnimator);
91
+            getCurrentScreenStack().pop(true);
99 92
             setBottomTabsStyleFromCurrentScreen();
100 93
             return true;
101 94
         } else {
@@ -137,19 +130,19 @@ public class BottomTabsLayout extends RelativeLayout implements Layout, AHBottom
137 130
 
138 131
     @Override
139 132
     public void push(ScreenParams screenParams) {
140
-        getCurrentScreenStack().push(screenAnimator, screenParams);
133
+        getCurrentScreenStack().push(screenParams);
141 134
         bottomTabs.setStyleFromScreen(screenParams.styleParams);
142 135
     }
143 136
 
144 137
     @Override
145 138
     public void pop(ScreenParams screenParams) {
146
-        getCurrentScreenStack().pop(screenAnimator);
139
+        getCurrentScreenStack().pop(screenParams.animateScreenTransitions);
147 140
         setBottomTabsStyleFromCurrentScreen();
148 141
     }
149 142
 
150 143
     @Override
151 144
     public void popToRoot(ScreenParams params) {
152
-        getCurrentScreenStack().popToRoot(screenAnimator);
145
+        getCurrentScreenStack().popToRoot(params.animateScreenTransitions);
153 146
         setBottomTabsStyleFromCurrentScreen();
154 147
     }
155 148
 
@@ -159,7 +152,7 @@ public class BottomTabsLayout extends RelativeLayout implements Layout, AHBottom
159 152
         currentScreenStack.destroy();
160 153
         removeView(currentScreenStack);
161 154
 
162
-        ScreenStack newStack = new ScreenStack(activity, params, this, screenAnimator);
155
+        ScreenStack newStack = new ScreenStack(activity, params, this);
163 156
         screenStacks[currentStackIndex] = newStack;
164 157
         addView(newStack, 0, new RelativeLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
165 158
 
@@ -205,7 +198,7 @@ public class BottomTabsLayout extends RelativeLayout implements Layout, AHBottom
205 198
     @Override
206 199
     public boolean onTitleBarBackPress() {
207 200
         if (getCurrentScreenStack().canPop()) {
208
-            getCurrentScreenStack().pop(screenAnimator);
201
+            getCurrentScreenStack().pop(true);
209 202
             setBottomTabsStyleFromCurrentScreen();
210 203
             return true;
211 204
         }

+ 5
- 10
android/app/src/main/java/com/reactnativenavigation/layouts/SingleScreenLayout.java View File

@@ -32,27 +32,22 @@ public class SingleScreenLayout extends FrameLayout implements Layout {
32 32
         super(activity);
33 33
         this.activity = activity;
34 34
         this.screenParams = screenParams;
35
-        createScreenAnimator();
36 35
         createStack();
37 36
     }
38 37
 
39
-    private void createScreenAnimator() {
40
-        screenAnimator = new ScreenAnimator();
41
-    }
42
-
43 38
     private void createStack() {
44 39
         if (stack != null) {
45 40
             stack.destroy();
46 41
             removeView(stack);
47 42
         }
48
-        stack = new ScreenStack(activity, screenParams, this, screenAnimator);
43
+        stack = new ScreenStack(activity, screenParams, this);
49 44
         addView(stack, new LayoutParams(MATCH_PARENT, MATCH_PARENT));
50 45
     }
51 46
 
52 47
     @Override
53 48
     public boolean onBackPressed() {
54 49
         if (stack.canPop()) {
55
-            stack.pop(screenAnimator);
50
+            stack.pop(true);
56 51
             return true;
57 52
         } else {
58 53
             return false;
@@ -67,17 +62,17 @@ public class SingleScreenLayout extends FrameLayout implements Layout {
67 62
 
68 63
     @Override
69 64
     public void push(ScreenParams params) {
70
-        stack.push(screenAnimator, params);
65
+        stack.push(params);
71 66
     }
72 67
 
73 68
     @Override
74 69
     public void pop(ScreenParams params) {
75
-        stack.pop(screenAnimator);
70
+        stack.pop(params.animateScreenTransitions);
76 71
     }
77 72
 
78 73
     @Override
79 74
     public void popToRoot(ScreenParams params) {
80
-        stack.popToRoot(screenAnimator);
75
+        stack.popToRoot(params.animateScreenTransitions);
81 76
     }
82 77
 
83 78
     @Override

+ 1
- 0
android/app/src/main/java/com/reactnativenavigation/params/ScreenParams.java View File

@@ -14,6 +14,7 @@ public class ScreenParams {
14 14
     public StyleParams styleParams;
15 15
     public List<TopTabParams> topTabParams;
16 16
     public String fragmentCreatorClassName;
17
+    public boolean animateScreenTransitions;
17 18
 
18 19
     //    public String tabLabel; TODO when tabs are supported move to TabParams
19 20
     //    public Drawable tabIcon;

+ 1
- 0
android/app/src/main/java/com/reactnativenavigation/params/parsers/ScreenParamsParser.java View File

@@ -51,6 +51,7 @@ public class ScreenParamsParser extends Parser {
51 51
         if (hasKey(params, "icon")) {
52 52
             result.tabIcon = ImageLoader.loadImage(params.getString("icon"));
53 53
         }
54
+        result.animateScreenTransitions = params.getBoolean("animated", true);
54 55
         return result;
55 56
     }
56 57
 

+ 14
- 0
android/app/src/main/java/com/reactnativenavigation/screens/Screen.java View File

@@ -30,6 +30,7 @@ public abstract class Screen extends RelativeLayout {
30 30
     protected TopBar topBar;
31 31
     private final TitleBarBackButtonListener titleBarBackButtonListener;
32 32
     private VisibilityAnimator topBarVisibilityAnimator;
33
+    private ScreenAnimator screenAnimator;
33 34
 
34 35
     public Screen(AppCompatActivity activity, ScreenParams screenParams, TitleBarBackButtonListener titleBarBackButtonListener) {
35 36
         super(activity);
@@ -37,6 +38,7 @@ public abstract class Screen extends RelativeLayout {
37 38
         this.screenParams = screenParams;
38 39
         this.titleBarBackButtonListener = titleBarBackButtonListener;
39 40
 
41
+        screenAnimator = new ScreenAnimator(this);
40 42
         createViews();
41 43
         setStyle(screenParams.styleParams);
42 44
     }
@@ -159,4 +161,16 @@ public abstract class Screen extends RelativeLayout {
159 161
             titleBarButtonParam.setColorFromScreenStyle(screenParams.styleParams.titleBarButtonColor);
160 162
         }
161 163
     }
164
+
165
+    public void show(boolean animated) {
166
+        screenAnimator.show(animated);
167
+    }
168
+
169
+    public void show(boolean animated, Runnable onAnimationEnd) {
170
+        screenAnimator.show(animated, onAnimationEnd);
171
+    }
172
+
173
+    public void hide(boolean animated, Runnable onAnimatedEnd) {
174
+        screenAnimator.hide(animated, onAnimatedEnd);
175
+    }
162 176
 }

+ 39
- 50
android/app/src/main/java/com/reactnativenavigation/screens/ScreenAnimator.java View File

@@ -9,63 +9,52 @@ import android.view.animation.AccelerateInterpolator;
9 9
 import android.view.animation.DecelerateInterpolator;
10 10
 import android.view.animation.LinearInterpolator;
11 11
 
12
+import com.reactnativenavigation.NavigationApplication;
12 13
 import com.reactnativenavigation.utils.ViewUtils;
13
-import com.reactnativenavigation.views.BottomTabs;
14 14
 
15
-public class ScreenAnimator {
16
-    private static final String TAG = "ScreenAnimator";
17
-    private BottomTabs bottomTabs;
18
-
19
-    public ScreenAnimator() {
15
+import javax.annotation.Nullable;
20 16
 
21
-    }
17
+public class ScreenAnimator {
18
+    private final float translationY;
19
+    private Screen screen;
22 20
 
23
-    public ScreenAnimator(BottomTabs bottomTabs) {
24
-        this.bottomTabs = bottomTabs;
21
+    public ScreenAnimator(Screen screen) {
22
+        this.screen = screen;
23
+        translationY = 0.08f * ViewUtils.getScreenHeight();
25 24
     }
26 25
 
27
-    public void show(Screen screenToShow, Runnable onScreenRemoved) {
28
-        createPushAnimator(screenToShow, onScreenRemoved).start();
26
+    public void show(boolean animate, final Runnable onAnimationEnd) {
27
+        if (animate) {
28
+            createShowAnimator(onAnimationEnd).start();
29
+        } else {
30
+            screen.setVisibility(View.VISIBLE);
31
+            NavigationApplication.instance.runOnMainThread(onAnimationEnd, 200);
32
+        }
29 33
     }
30 34
 
31
-    public void show(Screen screenToShow) {
32
-        createPushAnimator(screenToShow).start();
35
+    public void show(boolean animate) {
36
+        if (animate) {
37
+            createShowAnimator(null).start();
38
+        } else {
39
+            screen.setVisibility(View.VISIBLE);
40
+        }
33 41
     }
34 42
 
35
-    public void hide(Screen screenToHide, Runnable onScreenRemoved) {
36
-        createPopAnimator(screenToHide, onScreenRemoved).start();
43
+    public void hide(boolean animate, Runnable onAnimationEnd) {
44
+        if (animate) {
45
+            createHideAnimator(onAnimationEnd).start();
46
+        } else {
47
+            screen.setVisibility(View.INVISIBLE);
48
+            onAnimationEnd.run();
49
+        }
37 50
     }
38 51
 
39
-    private Animator createPushAnimator(final Screen screen) {
40
-        ObjectAnimator alpha = ObjectAnimator.ofFloat(screen, View.ALPHA, 0.7f, 1);
41
-        alpha.setStartDelay(100);
42
-        alpha.setInterpolator(new LinearInterpolator());
43
-        alpha.setDuration(150);
44
-
45
-        final float delta = 0.08f * ViewUtils.getScreenHeight();
46
-        ObjectAnimator translationY = ObjectAnimator.ofFloat(screen, View.TRANSLATION_Y, delta, 0);
47
-        translationY.setInterpolator(new AccelerateInterpolator());
48
-        translationY.setDuration(250);
49
-
50
-        AnimatorSet set = new AnimatorSet();
51
-        set.playTogether(translationY, alpha);
52
-        set.addListener(new AnimatorListenerAdapter() {
53
-            @Override
54
-            public void onAnimationStart(Animator animation) {
55
-                screen.setVisibility(View.VISIBLE);
56
-            }
57
-        });
58
-        return set;
59
-    }
60
-
61
-    private Animator createPushAnimator(final Screen screenToShow, final Runnable onScreenRemoved) {
62
-        final float translationYValue = 0.08f * ViewUtils.getScreenHeight();
63
-
64
-        ObjectAnimator alpha = ObjectAnimator.ofFloat(screenToShow, View.ALPHA, 0, 1);
52
+    private Animator createShowAnimator(final @Nullable Runnable onAnimationEnd) {
53
+        ObjectAnimator alpha = ObjectAnimator.ofFloat(screen, View.ALPHA, 0, 1);
65 54
         alpha.setInterpolator(new DecelerateInterpolator());
66 55
         alpha.setDuration(200);
67 56
 
68
-        ObjectAnimator translationY = ObjectAnimator.ofFloat(screenToShow, View.TRANSLATION_Y, translationYValue, 0);
57
+        ObjectAnimator translationY = ObjectAnimator.ofFloat(screen, View.TRANSLATION_Y, this.translationY, 0);
69 58
         translationY.setInterpolator(new DecelerateInterpolator());
70 59
         translationY.setDuration(280);
71 60
 
@@ -74,26 +63,26 @@ public class ScreenAnimator {
74 63
         set.addListener(new AnimatorListenerAdapter() {
75 64
             @Override
76 65
             public void onAnimationStart(Animator animation) {
77
-                screenToShow.setVisibility(View.VISIBLE);
66
+                screen.setVisibility(View.VISIBLE);
78 67
             }
79 68
 
80 69
             @Override
81 70
             public void onAnimationEnd(Animator animation) {
82
-                onScreenRemoved.run();
71
+                if (onAnimationEnd != null) {
72
+                    onAnimationEnd.run();
73
+                }
83 74
             }
84 75
         });
85 76
         return set;
86 77
     }
87 78
 
88
-    private Animator createPopAnimator(final Screen screenToHide, final Runnable onScreenRemoved) {
89
-        final float translationYValue = 0.08f * ViewUtils.getScreenHeight();
90
-
91
-        ObjectAnimator alpha = ObjectAnimator.ofFloat(screenToHide, View.ALPHA, 0);
79
+    private Animator createHideAnimator(final Runnable onAnimationEnd) {
80
+        ObjectAnimator alpha = ObjectAnimator.ofFloat(screen, View.ALPHA, 0);
92 81
         alpha.setInterpolator(new LinearInterpolator());
93 82
         alpha.setStartDelay(100);
94 83
         alpha.setDuration(150);
95 84
 
96
-        ObjectAnimator translationY = ObjectAnimator.ofFloat(screenToHide, View.TRANSLATION_Y, translationYValue);
85
+        ObjectAnimator translationY = ObjectAnimator.ofFloat(screen, View.TRANSLATION_Y, this.translationY);
97 86
         translationY.setInterpolator(new AccelerateInterpolator());
98 87
         translationY.setDuration(250);
99 88
 
@@ -102,7 +91,7 @@ public class ScreenAnimator {
102 91
         set.addListener(new AnimatorListenerAdapter() {
103 92
             @Override
104 93
             public void onAnimationEnd(Animator animation) {
105
-                onScreenRemoved.run();
94
+                onAnimationEnd.run();
106 95
             }
107 96
         });
108 97
         return set;

+ 10
- 12
android/app/src/main/java/com/reactnativenavigation/screens/ScreenStack.java View File

@@ -25,26 +25,24 @@ public class ScreenStack extends FrameLayout {
25 25
 
26 26
     public ScreenStack(AppCompatActivity activity,
27 27
                        ScreenParams initialScreenParams,
28
-                       TitleBarBackButtonListener titleBarBackButtonListener,
29
-                       ScreenAnimator screenAnimator) {
28
+                       TitleBarBackButtonListener titleBarBackButtonListener) {
30 29
         super(activity);
31 30
         this.activity = activity;
32 31
         this.titleBarBackButtonListener = titleBarBackButtonListener;
33
-//        setLayoutTransition(new LayoutTransition());
34
-        pushInitialScreen(screenAnimator, initialScreenParams);
32
+        pushInitialScreen(initialScreenParams);
35 33
     }
36 34
 
37
-    private void pushInitialScreen(ScreenAnimator screenAnimator, ScreenParams initialScreenParams) {
35
+    private void pushInitialScreen(ScreenParams initialScreenParams) {
38 36
         Screen initialScreen = ScreenFactory.create(activity, initialScreenParams, titleBarBackButtonListener);
39 37
         addScreen(initialScreen);
40
-        screenAnimator.show(initialScreen);
38
+        initialScreen.show(initialScreenParams.animateScreenTransitions);
41 39
     }
42 40
 
43
-    public void push(ScreenAnimator screenAnimator, ScreenParams params) {
41
+    public void push(final ScreenParams params) {
44 42
         Screen nextScreen = ScreenFactory.create(activity, params, titleBarBackButtonListener);
45 43
         final Screen previousScreen = stack.peek();
46 44
         addScreen(nextScreen);
47
-        screenAnimator.show(nextScreen, new Runnable() {
45
+        nextScreen.show(params.animateScreenTransitions, new Runnable() {
48 46
             @Override
49 47
             public void run() {
50 48
                 removePreviousWithoutUnmount(previousScreen);
@@ -63,7 +61,7 @@ public class ScreenStack extends FrameLayout {
63 61
         removeView(previous);
64 62
     }
65 63
 
66
-    public void pop(ScreenAnimator screenAnimator) {
64
+    public void pop(boolean animated) {
67 65
         if (!canPop()) {
68 66
             return;
69 67
         }
@@ -72,7 +70,7 @@ public class ScreenStack extends FrameLayout {
72 70
         Screen previous = stack.peek();
73 71
 
74 72
         readdPrevious(previous);
75
-        screenAnimator.hide(toRemove, new Runnable() {
73
+        toRemove.hide(animated, new Runnable() {
76 74
             @Override
77 75
             public void run() {
78 76
                 toRemove.ensureUnmountOnDetachedFromWindow();
@@ -86,9 +84,9 @@ public class ScreenStack extends FrameLayout {
86 84
         previous.preventMountAfterReattachedToWindow();
87 85
     }
88 86
 
89
-    public void popToRoot(ScreenAnimator screenAnimator) {
87
+    public void popToRoot(boolean animated) {
90 88
         while (canPop()) {
91
-            pop(screenAnimator);
89
+            pop(animated);
92 90
         }
93 91
     }
94 92