Kaynağa Gözat

Modal refactor

Add previous view (modal or root) back to hierarchy in ModalPresenter.
Later on this will be controlled by modalPresentationStyle
Guy Carmeli 6 yıl önce
ebeveyn
işleme
a9aa70a7b6

+ 4
- 13
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/Navigator.java Dosyayı Görüntüle

@@ -65,14 +65,12 @@ public class Navigator extends ParentController {
65 65
     @Override
66 66
     public boolean handleBack(CommandListener listener) {
67 67
         if (modalStack.isEmpty()) return root.handleBack(listener);
68
-        return modalStack.handleBack(listener, () -> {
69
-            if (modalStack.size() == 1) contentLayout.addView(root.getView(), 0);
70
-        });
68
+        return modalStack.handleBack(listener, root);
71 69
     }
72 70
 
73 71
     @Override
74 72
     public void destroy() {
75
-        modalStack.dismissAllModals(new CommandListenerAdapter(), () -> {});
73
+        modalStack.dismissAllModals(new CommandListenerAdapter(), root);
76 74
         super.destroy();
77 75
     }
78 76
 
@@ -176,18 +174,11 @@ public class Navigator extends ParentController {
176 174
     }
177 175
 
178 176
     public void dismissModal(final String componentId, CommandListener listener) {
179
-        modalStack.dismissModal(componentId,
180
-                () -> {
181
-                    if (modalStack.size() == 1) contentLayout.addView(root.getView());
182
-                },
183
-                listener
184
-        );
177
+        modalStack.dismissModal(componentId, root, listener);
185 178
     }
186 179
 
187 180
     public void dismissAllModals(CommandListener listener) {
188
-        modalStack.dismissAllModals(listener, () -> {
189
-            if (!modalStack.isEmpty()) contentLayout.addView(root.getView(), 0);
190
-        });
181
+        modalStack.dismissAllModals(listener, root);
191 182
     }
192 183
 
193 184
     public void showOverlay(ViewController overlay) {

+ 2
- 4
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/modal/ModalPresenter.java Dosyayı Görüntüle

@@ -9,8 +9,6 @@ import com.reactnativenavigation.anim.ModalAnimator;
9 9
 import com.reactnativenavigation.viewcontrollers.Navigator.CommandListener;
10 10
 import com.reactnativenavigation.viewcontrollers.ViewController;
11 11
 
12
-import javax.annotation.Nullable;
13
-
14 12
 public class ModalPresenter {
15 13
 
16 14
     private ViewGroup content;
@@ -43,8 +41,8 @@ public class ModalPresenter {
43 41
         listener.onSuccess(toAdd.getId());
44 42
     }
45 43
 
46
-    public void dismissModal(ViewController toDismiss, @Nullable ViewController toAdd, CommandListener listener) {
47
-        if (toAdd != null) content.addView(toAdd.getView(), 0);
44
+    public void dismissModal(ViewController toDismiss, ViewController toAdd, CommandListener listener) {
45
+        content.addView(toAdd.getView(), 0);
48 46
         if (toDismiss.options.animations.dismissModal.enable.isTrueOrUndefined()) {
49 47
             animator.dismiss(toDismiss.getView(), new AnimatorListenerAdapter() {
50 48
                 @Override

+ 6
- 7
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/modal/ModalStack.java Dosyayı Görüntüle

@@ -29,11 +29,10 @@ public class ModalStack {
29 29
         presenter.showModal(viewController, toRemove, listener);
30 30
     }
31 31
 
32
-    public void dismissModal(String componentId, Runnable onModalWilDismiss, CommandListener listener) {
32
+    public void dismissModal(String componentId, ViewController root, CommandListener listener) {
33 33
         ViewController toDismiss = findModalByComponentId(componentId);
34 34
         if (toDismiss != null) {
35
-            onModalWilDismiss.run();
36
-            ViewController toAdd = isTop(toDismiss) ? get(size() - 2) : null;
35
+            ViewController toAdd = isTop(toDismiss) ? get(size() - 2) : root;
37 36
             modals.remove(toDismiss);
38 37
             presenter.dismissModal(toDismiss, toAdd, listener);
39 38
         } else {
@@ -41,7 +40,7 @@ public class ModalStack {
41 40
         }
42 41
     }
43 42
 
44
-    public void dismissAllModals(CommandListener listener, Runnable onModalWilDismiss) {
43
+    public void dismissAllModals(CommandListener listener, ViewController root) {
45 44
         if (modals.isEmpty()) {
46 45
             listener.onError("Nothing to dismiss");
47 46
             return;
@@ -49,7 +48,7 @@ public class ModalStack {
49 48
 
50 49
         while (!modals.isEmpty()) {
51 50
             if (modals.size() == 1) {
52
-                dismissModal(modals.get(0).getId(), onModalWilDismiss, listener);
51
+                dismissModal(modals.get(0).getId(), root, listener);
53 52
             } else {
54 53
                 modals.get(0).destroy();
55 54
                 modals.remove(0);
@@ -57,12 +56,12 @@ public class ModalStack {
57 56
         }
58 57
     }
59 58
 
60
-    public boolean handleBack(CommandListener listener, Runnable onModalWillDismiss) {
59
+    public boolean handleBack(CommandListener listener, ViewController root) {
61 60
         if (isEmpty()) return false;
62 61
         if (peek().handleBack(listener)) {
63 62
             return true;
64 63
         }
65
-        dismissModal(peek().getId(), onModalWillDismiss, listener);
64
+        dismissModal(peek().getId(), root, listener);
66 65
         return true;
67 66
     }
68 67
 

+ 0
- 17
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/NavigatorTest.java Dosyayı Görüntüle

@@ -412,23 +412,6 @@ public class NavigatorTest extends BaseTest {
412 412
         verify(parentController, times(2)).onViewAppeared();
413 413
     }
414 414
 
415
-    @Test
416
-    public void dismissModal_rootIsAttachedBeforeModalIsDismissed() {
417
-        disableShowModalAnimation(child1, child2);
418
-        disableDismissModalAnimation(child2);
419
-
420
-        uut.setRoot(parentController, new MockPromise());
421
-        uut.showModal(child1, new CommandListenerAdapter());
422
-        uut.showModal(child2, new CommandListenerAdapter());
423
-
424
-        uut.dismissModal(child2.getId(), new CommandListenerAdapter());
425
-        assertThat(parentController.getView().getParent()).isNull();
426
-
427
-        uut.dismissModal(child1.getId(), new CommandListenerAdapter());
428
-        assertThat(parentController.getView().getParent()).isNotNull();
429
-        verify(parentController, times(2)).onViewAppeared();
430
-    }
431
-
432 415
     @Test
433 416
     public void dismissAllModals_onViewAppearedInvokedOnRoot() {
434 417
         disableShowModalAnimation(child1);

+ 1
- 1
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/modal/ModalPresenterTest.java Dosyayı Görüntüle

@@ -121,7 +121,7 @@ public class ModalPresenterTest extends BaseTest {
121 121
     }
122 122
 
123 123
     @Test
124
-    public void dismissModal_previousModalIsAddedAtIndex0() {
124
+    public void dismissModal_previousViewIsAddedAtIndex0() {
125 125
         FrameLayout spy = spy(new FrameLayout(newActivity()));
126 126
         uut.setContentLayout(spy);
127 127
         uut.dismissModal(modal1, modal2, new CommandListenerAdapter());

+ 13
- 27
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/modal/ModalStackTest.java Dosyayı Görüntüle

@@ -87,16 +87,9 @@ public class ModalStackTest extends BaseTest {
87 87
     public void dismissModal() {
88 88
         uut.showModal(modal1, rootController, new CommandListenerAdapter());
89 89
         CommandListener listener = new CommandListenerAdapter();
90
-        Runnable onModalWillDismiss = spy(new Runnable() {
91
-            @Override
92
-            public void run() {
93
-                assertThat(modal1.getView().getParent()).isNotNull();
94
-            }
95
-        });
96
-        uut.dismissModal(modal1.getId(), onModalWillDismiss, listener);
90
+        uut.dismissModal(modal1.getId(), rootController, listener);
97 91
         assertThat(findModal(modal1.getId())).isNull();
98
-        verify(onModalWillDismiss, times(1)).run();
99
-        verify(presenter, times(1)).dismissModal(modal1, null, listener);
92
+        verify(presenter, times(1)).dismissModal(modal1, rootController, listener);
100 93
     }
101 94
 
102 95
     @SuppressWarnings("Convert2Lambda")
@@ -109,7 +102,7 @@ public class ModalStackTest extends BaseTest {
109 102
 
110 103
             }
111 104
         });
112
-        uut.dismissModal(MODAL_ID_1, onModalWillDismiss, listener);
105
+        uut.dismissModal(MODAL_ID_1, rootController, listener);
113 106
         verify(onModalWillDismiss, times(0)).run();
114 107
         verify(listener, times(1)).onError(anyString());
115 108
         verifyZeroInteractions(listener);
@@ -127,7 +120,7 @@ public class ModalStackTest extends BaseTest {
127 120
                 assertThat(uut.isEmpty()).isTrue();
128 121
             }
129 122
         });
130
-        uut.dismissAllModals(listener, () -> {});
123
+        uut.dismissAllModals(listener, rootController);
131 124
         verify(listener, times(1)).onSuccess(anyString());
132 125
         verifyZeroInteractions(listener);
133 126
     }
@@ -135,7 +128,7 @@ public class ModalStackTest extends BaseTest {
135 128
     @Test
136 129
     public void dismissAllModals_rejectIfEmpty() {
137 130
         CommandListener spy = spy(new CommandListenerAdapter());
138
-        uut.dismissAllModals(spy, () -> {});
131
+        uut.dismissAllModals(spy, rootController);
139 132
         verify(spy, times(1)).onError(any());
140 133
     }
141 134
 
@@ -148,15 +141,8 @@ public class ModalStackTest extends BaseTest {
148 141
         ViewGroup view1 = modal1.getView();
149 142
         ViewGroup view2 = modal2.getView();
150 143
         CommandListener listener = spy(new CommandListenerAdapter());
151
-        Runnable onModalWillDismiss = spy(new Runnable() {
152
-            @Override
153
-            public void run() {
154
-
155
-            }
156
-        });
157
-        uut.dismissAllModals(listener, onModalWillDismiss);
158
-        verify(onModalWillDismiss, times(1)).run();
159
-        verify(presenter, times(1)).dismissModal(modal2, null, listener);
144
+        uut.dismissAllModals(listener, rootController);
145
+        verify(presenter, times(1)).dismissModal(modal2, rootController, listener);
160 146
         verify(animator, times(0)).dismiss(eq(view1), any());
161 147
         verify(animator, times(1)).dismiss(eq(view2), any());
162 148
         assertThat(uut.size()).isEqualTo(0);
@@ -167,7 +153,7 @@ public class ModalStackTest extends BaseTest {
167 153
         uut.showModal(modal1, rootController, new CommandListenerAdapter());
168 154
         uut.showModal(modal2, rootController, new CommandListenerAdapter());
169 155
 
170
-        uut.dismissAllModals(new CommandListenerAdapter(), () -> {});
156
+        uut.dismissAllModals(new CommandListenerAdapter(), rootController);
171 157
 
172 158
         verify(modal1, times(1)).destroy();
173 159
         verify(modal1, times(1)).onViewDisappear();
@@ -179,7 +165,7 @@ public class ModalStackTest extends BaseTest {
179 165
         assertThat(uut.isEmpty()).isTrue();
180 166
         uut.showModal(modal1, rootController, new CommandListenerAdapter());
181 167
         assertThat(uut.isEmpty()).isFalse();
182
-        uut.dismissAllModals(new CommandListenerAdapter(), () -> {});
168
+        uut.dismissAllModals(new CommandListenerAdapter(), rootController);
183 169
         assertThat(uut.isEmpty()).isTrue();
184 170
     }
185 171
 
@@ -201,7 +187,7 @@ public class ModalStackTest extends BaseTest {
201 187
 
202 188
         uut.showModal(modal1, rootController, new CommandListenerAdapter());
203 189
         uut.showModal(modal2, rootController, new CommandListenerAdapter());
204
-        uut.dismissModal(modal2.getId(), () -> {}, new CommandListenerAdapter());
190
+        uut.dismissModal(modal2.getId(), rootController, new CommandListenerAdapter());
205 191
         verify(modal1, times(2)).onViewAppeared();
206 192
     }
207 193
 
@@ -214,7 +200,7 @@ public class ModalStackTest extends BaseTest {
214 200
         uut.showModal(modal2, rootController, new CommandListenerAdapter());
215 201
         uut.showModal(modal3, rootController, new CommandListenerAdapter());
216 202
 
217
-        uut.dismissModal(modal2.getId(), () -> {}, new CommandListenerAdapter());
203
+        uut.dismissModal(modal2.getId(), rootController, new CommandListenerAdapter());
218 204
         assertThat(uut.size()).isEqualTo(2);
219 205
         verify(modal2, times(1)).onViewDisappear();
220 206
         verify(modal2, times(1)).destroy();
@@ -224,14 +210,14 @@ public class ModalStackTest extends BaseTest {
224 210
     @Test
225 211
     public void handleBack_doesNothingIfModalStackIsEmpty() {
226 212
         assertThat(uut.isEmpty()).isTrue();
227
-        assertThat(uut.handleBack(new CommandListenerAdapter(), () -> {})).isFalse();
213
+        assertThat(uut.handleBack(new CommandListenerAdapter(), rootController)).isFalse();
228 214
     }
229 215
 
230 216
     @Test
231 217
     public void handleBack_dismissModal() {
232 218
         disableDismissModalAnimation(modal1);
233 219
         uut.showModal(modal1, rootController, new CommandListenerAdapter());
234
-        assertThat(uut.handleBack(new CommandListenerAdapter(), () -> {})).isTrue();
220
+        assertThat(uut.handleBack(new CommandListenerAdapter(), rootController)).isTrue();
235 221
         verify(modal1, times(1)).onViewDisappear();
236 222
 
237 223
     }