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

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

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

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

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

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

51
         if (hasKey(params, "icon")) {
51
         if (hasKey(params, "icon")) {
52
             result.tabIcon = ImageLoader.loadImage(params.getString("icon"));
52
             result.tabIcon = ImageLoader.loadImage(params.getString("icon"));
53
         }
53
         }
54
+        result.animateScreenTransitions = params.getBoolean("animated", true);
54
         return result;
55
         return result;
55
     }
56
     }
56
 
57
 

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

30
     protected TopBar topBar;
30
     protected TopBar topBar;
31
     private final TitleBarBackButtonListener titleBarBackButtonListener;
31
     private final TitleBarBackButtonListener titleBarBackButtonListener;
32
     private VisibilityAnimator topBarVisibilityAnimator;
32
     private VisibilityAnimator topBarVisibilityAnimator;
33
+    private ScreenAnimator screenAnimator;
33
 
34
 
34
     public Screen(AppCompatActivity activity, ScreenParams screenParams, TitleBarBackButtonListener titleBarBackButtonListener) {
35
     public Screen(AppCompatActivity activity, ScreenParams screenParams, TitleBarBackButtonListener titleBarBackButtonListener) {
35
         super(activity);
36
         super(activity);
37
         this.screenParams = screenParams;
38
         this.screenParams = screenParams;
38
         this.titleBarBackButtonListener = titleBarBackButtonListener;
39
         this.titleBarBackButtonListener = titleBarBackButtonListener;
39
 
40
 
41
+        screenAnimator = new ScreenAnimator(this);
40
         createViews();
42
         createViews();
41
         setStyle(screenParams.styleParams);
43
         setStyle(screenParams.styleParams);
42
     }
44
     }
159
             titleBarButtonParam.setColorFromScreenStyle(screenParams.styleParams.titleBarButtonColor);
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
 import android.view.animation.DecelerateInterpolator;
9
 import android.view.animation.DecelerateInterpolator;
10
 import android.view.animation.LinearInterpolator;
10
 import android.view.animation.LinearInterpolator;
11
 
11
 
12
+import com.reactnativenavigation.NavigationApplication;
12
 import com.reactnativenavigation.utils.ViewUtils;
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
         alpha.setInterpolator(new DecelerateInterpolator());
54
         alpha.setInterpolator(new DecelerateInterpolator());
66
         alpha.setDuration(200);
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
         translationY.setInterpolator(new DecelerateInterpolator());
58
         translationY.setInterpolator(new DecelerateInterpolator());
70
         translationY.setDuration(280);
59
         translationY.setDuration(280);
71
 
60
 
74
         set.addListener(new AnimatorListenerAdapter() {
63
         set.addListener(new AnimatorListenerAdapter() {
75
             @Override
64
             @Override
76
             public void onAnimationStart(Animator animation) {
65
             public void onAnimationStart(Animator animation) {
77
-                screenToShow.setVisibility(View.VISIBLE);
66
+                screen.setVisibility(View.VISIBLE);
78
             }
67
             }
79
 
68
 
80
             @Override
69
             @Override
81
             public void onAnimationEnd(Animator animation) {
70
             public void onAnimationEnd(Animator animation) {
82
-                onScreenRemoved.run();
71
+                if (onAnimationEnd != null) {
72
+                    onAnimationEnd.run();
73
+                }
83
             }
74
             }
84
         });
75
         });
85
         return set;
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
         alpha.setInterpolator(new LinearInterpolator());
81
         alpha.setInterpolator(new LinearInterpolator());
93
         alpha.setStartDelay(100);
82
         alpha.setStartDelay(100);
94
         alpha.setDuration(150);
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
         translationY.setInterpolator(new AccelerateInterpolator());
86
         translationY.setInterpolator(new AccelerateInterpolator());
98
         translationY.setDuration(250);
87
         translationY.setDuration(250);
99
 
88
 
102
         set.addListener(new AnimatorListenerAdapter() {
91
         set.addListener(new AnimatorListenerAdapter() {
103
             @Override
92
             @Override
104
             public void onAnimationEnd(Animator animation) {
93
             public void onAnimationEnd(Animator animation) {
105
-                onScreenRemoved.run();
94
+                onAnimationEnd.run();
106
             }
95
             }
107
         });
96
         });
108
         return set;
97
         return set;

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

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