Daniel Zlotin пре 8 година
родитељ
комит
652e9bc716

+ 33
- 0
lib/android/app/src/main/java/com/reactnativenavigation/anim/StackAnimator.java Прегледај датотеку

@@ -0,0 +1,33 @@
1
+package com.reactnativenavigation.anim;
2
+
3
+import android.animation.Animator;
4
+import android.animation.AnimatorListenerAdapter;
5
+import android.animation.AnimatorSet;
6
+import android.animation.ObjectAnimator;
7
+import android.view.View;
8
+import android.view.animation.DecelerateInterpolator;
9
+
10
+public class StackAnimator {
11
+	public void animatePush(final View target, final Runnable onComplete) {
12
+		target.setAlpha(0);
13
+		target.setTranslationY(0.08f * ((View) target.getParent()).getHeight());
14
+
15
+		ObjectAnimator alpha = ObjectAnimator.ofFloat(target, View.ALPHA, 1);
16
+		alpha.setInterpolator(new DecelerateInterpolator());
17
+		alpha.setDuration(200);
18
+
19
+		ObjectAnimator translationY = ObjectAnimator.ofFloat(target, View.TRANSLATION_Y, 0);
20
+		translationY.setInterpolator(new DecelerateInterpolator());
21
+		translationY.setDuration(350);
22
+
23
+		AnimatorSet set = new AnimatorSet();
24
+		set.playTogether(translationY, alpha);
25
+		set.addListener(new AnimatorListenerAdapter() {
26
+			@Override
27
+			public void onAnimationEnd(final Animator animation) {
28
+				if (onComplete != null) onComplete.run();
29
+			}
30
+		});
31
+		set.start();
32
+	}
33
+}

+ 15
- 1
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/StackController.java Прегледај датотеку

@@ -6,13 +6,21 @@ import android.support.annotation.Nullable;
6 6
 import android.view.ViewGroup;
7 7
 import android.widget.FrameLayout;
8 8
 
9
+import com.reactnativenavigation.anim.StackAnimator;
10
+
9 11
 import java.util.Collection;
10 12
 
11 13
 public class StackController extends ParentController {
12 14
 	private final IndexedStack<ViewController> stack = new IndexedStack<>();
15
+	private StackAnimator animator;
13 16
 
14 17
 	public StackController(final Activity activity, String id) {
18
+		this(activity, id, new StackAnimator());
19
+	}
20
+
21
+	public StackController(final Activity activity, String id, StackAnimator animator) {
15 22
 		super(activity, id);
23
+		this.animator = animator;
16 24
 	}
17 25
 
18 26
 	public void push(final ViewController child) {
@@ -22,8 +30,14 @@ public class StackController extends ParentController {
22 30
 		stack.push(child.getId(), child);
23 31
 
24 32
 		getView().addView(child.getView());
33
+
25 34
 		if (previousTop != null) {
26
-			getView().removeView(previousTop.getView());
35
+			animator.animatePush(child.getView(), new Runnable() {
36
+				@Override
37
+				public void run() {
38
+					getView().removeView(previousTop.getView());
39
+				}
40
+			});
27 41
 		}
28 42
 	}
29 43
 

+ 12
- 0
lib/android/app/src/test/java/com/reactnativenavigation/mocks/TestStackAnimator.java Прегледај датотеку

@@ -0,0 +1,12 @@
1
+package com.reactnativenavigation.mocks;
2
+
3
+import android.view.View;
4
+
5
+import com.reactnativenavigation.anim.StackAnimator;
6
+
7
+public class TestStackAnimator extends StackAnimator {
8
+	@Override
9
+	public void animatePush(final View target, final Runnable onComplete) {
10
+		if (onComplete != null) onComplete.run();
11
+	}
12
+}

+ 12
- 11
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/NavigatorTest.java Прегледај датотеку

@@ -5,6 +5,7 @@ import android.view.ViewGroup;
5 5
 
6 6
 import com.reactnativenavigation.BaseTest;
7 7
 import com.reactnativenavigation.mocks.SimpleViewController;
8
+import com.reactnativenavigation.mocks.TestStackAnimator;
8 9
 
9 10
 import org.junit.Test;
10 11
 import org.robolectric.Shadows;
@@ -71,7 +72,7 @@ public class NavigatorTest extends BaseTest {
71 72
 
72 73
 	@Test
73 74
 	public void push() throws Exception {
74
-		StackController stackController = new StackController(activity, "stack1");
75
+		StackController stackController = new StackController(activity, "stack1", new TestStackAnimator());
75 76
 		stackController.push(child1);
76 77
 		uut.setRoot(stackController);
77 78
 
@@ -94,8 +95,8 @@ public class NavigatorTest extends BaseTest {
94 95
 	@Test
95 96
 	public void push_OnCorrectStackByFindingChildId() throws Exception {
96 97
 		BottomTabsController bottomTabsController = new BottomTabsController(activity, "tabsController");
97
-		StackController stack1 = new StackController(activity, "stack1");
98
-		StackController stack2 = new StackController(activity, "stack2");
98
+		StackController stack1 = new StackController(activity, "stack1", new TestStackAnimator());
99
+		StackController stack2 = new StackController(activity, "stack2", new TestStackAnimator());
99 100
 		stack1.push(child1);
100 101
 		stack2.push(child2);
101 102
 		bottomTabsController.setTabs(Arrays.<ViewController>asList(stack1, stack2));
@@ -118,8 +119,8 @@ public class NavigatorTest extends BaseTest {
118 119
 	@Test
119 120
 	public void pop_FromCorrectStackByFindingChildId() throws Exception {
120 121
 		BottomTabsController bottomTabsController = new BottomTabsController(activity, "tabsController");
121
-		StackController stack1 = new StackController(activity, "stack1");
122
-		StackController stack2 = new StackController(activity, "stack2");
122
+		StackController stack1 = new StackController(activity, "stack1", new TestStackAnimator());
123
+		StackController stack2 = new StackController(activity, "stack2", new TestStackAnimator());
123 124
 		stack1.push(child1);
124 125
 		stack2.push(child2);
125 126
 		stack2.push(child3);
@@ -135,8 +136,8 @@ public class NavigatorTest extends BaseTest {
135 136
 	@Test
136 137
 	public void popSpecific() throws Exception {
137 138
 		BottomTabsController bottomTabsController = new BottomTabsController(activity, "tabsController");
138
-		StackController stack1 = new StackController(activity, "stack1");
139
-		StackController stack2 = new StackController(activity, "stack2");
139
+		StackController stack1 = new StackController(activity, "stack1", new TestStackAnimator());
140
+		StackController stack2 = new StackController(activity, "stack2", new TestStackAnimator());
140 141
 		stack1.push(child1);
141 142
 		stack2.push(child2);
142 143
 		stack2.push(child3);
@@ -153,8 +154,8 @@ public class NavigatorTest extends BaseTest {
153 154
 	@Test
154 155
 	public void popTo_FromCorrectStackUpToChild() throws Exception {
155 156
 		BottomTabsController bottomTabsController = new BottomTabsController(activity, "tabsController");
156
-		StackController stack1 = new StackController(activity, "stack1");
157
-		StackController stack2 = new StackController(activity, "stack2");
157
+		StackController stack1 = new StackController(activity, "stack1", new TestStackAnimator());
158
+		StackController stack2 = new StackController(activity, "stack2", new TestStackAnimator());
158 159
 		stack1.push(child1);
159 160
 		stack2.push(child2);
160 161
 		stack2.push(child3);
@@ -171,8 +172,8 @@ public class NavigatorTest extends BaseTest {
171 172
 	@Test
172 173
 	public void popToRoot() throws Exception {
173 174
 		BottomTabsController bottomTabsController = new BottomTabsController(activity, "tabsController");
174
-		StackController stack1 = new StackController(activity, "stack1");
175
-		StackController stack2 = new StackController(activity, "stack2");
175
+		StackController stack1 = new StackController(activity, "stack1", new TestStackAnimator());
176
+		StackController stack2 = new StackController(activity, "stack2", new TestStackAnimator());
176 177
 		stack1.push(child1);
177 178
 		stack2.push(child2);
178 179
 		stack2.push(child3);

+ 4
- 3
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/StackControllerTest.java Прегледај датотеку

@@ -6,6 +6,7 @@ import android.widget.FrameLayout;
6 6
 
7 7
 import com.reactnativenavigation.BaseTest;
8 8
 import com.reactnativenavigation.mocks.SimpleViewController;
9
+import com.reactnativenavigation.mocks.TestStackAnimator;
9 10
 
10 11
 import org.junit.Test;
11 12
 
@@ -23,7 +24,7 @@ public class StackControllerTest extends BaseTest {
23 24
 	public void beforeEach() {
24 25
 		super.beforeEach();
25 26
 		activity = newActivity();
26
-		uut = new StackController(activity, "uut");
27
+		uut = new StackController(activity, "uut", new TestStackAnimator());
27 28
 		child1 = new SimpleViewController(activity, "child1");
28 29
 		child2 = new SimpleViewController(activity, "child2");
29 30
 		child3 = new SimpleViewController(activity, "child3");
@@ -77,7 +78,7 @@ public class StackControllerTest extends BaseTest {
77 78
 		uut.push(child1);
78 79
 		assertThat(child1.getParentStackController()).isEqualTo(uut);
79 80
 
80
-		StackController anotherNavController = new StackController(activity, "another");
81
+		StackController anotherNavController = new StackController(activity, "another", new TestStackAnimator());
81 82
 		anotherNavController.push(child2);
82 83
 		assertThat(child2.getParentStackController()).isEqualTo(anotherNavController);
83 84
 	}
@@ -235,7 +236,7 @@ public class StackControllerTest extends BaseTest {
235 236
 
236 237
 	@Test
237 238
 	public void findControllerById_Deeply() throws Exception {
238
-		StackController stack = new StackController(activity, "stack2");
239
+		StackController stack = new StackController(activity, "stack2", new TestStackAnimator());
239 240
 		stack.push(child2);
240 241
 		uut.push(stack);
241 242
 		assertThat(uut.findControllerById(child2.getId())).isEqualTo(child2);