Browse Source

Modal refactor

Add previous view (modal or root) back to hierarchy in ModalPresenter.
Later on this will be controlled by modalPresentationStyle
Guy Carmeli 7 years ago
parent
commit
a9aa70a7b6

+ 4
- 13
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/Navigator.java View File

65
     @Override
65
     @Override
66
     public boolean handleBack(CommandListener listener) {
66
     public boolean handleBack(CommandListener listener) {
67
         if (modalStack.isEmpty()) return root.handleBack(listener);
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
     @Override
71
     @Override
74
     public void destroy() {
72
     public void destroy() {
75
-        modalStack.dismissAllModals(new CommandListenerAdapter(), () -> {});
73
+        modalStack.dismissAllModals(new CommandListenerAdapter(), root);
76
         super.destroy();
74
         super.destroy();
77
     }
75
     }
78
 
76
 
176
     }
174
     }
177
 
175
 
178
     public void dismissModal(final String componentId, CommandListener listener) {
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
     public void dismissAllModals(CommandListener listener) {
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
     public void showOverlay(ViewController overlay) {
184
     public void showOverlay(ViewController overlay) {

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

9
 import com.reactnativenavigation.viewcontrollers.Navigator.CommandListener;
9
 import com.reactnativenavigation.viewcontrollers.Navigator.CommandListener;
10
 import com.reactnativenavigation.viewcontrollers.ViewController;
10
 import com.reactnativenavigation.viewcontrollers.ViewController;
11
 
11
 
12
-import javax.annotation.Nullable;
13
-
14
 public class ModalPresenter {
12
 public class ModalPresenter {
15
 
13
 
16
     private ViewGroup content;
14
     private ViewGroup content;
43
         listener.onSuccess(toAdd.getId());
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
         if (toDismiss.options.animations.dismissModal.enable.isTrueOrUndefined()) {
46
         if (toDismiss.options.animations.dismissModal.enable.isTrueOrUndefined()) {
49
             animator.dismiss(toDismiss.getView(), new AnimatorListenerAdapter() {
47
             animator.dismiss(toDismiss.getView(), new AnimatorListenerAdapter() {
50
                 @Override
48
                 @Override

+ 6
- 7
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/modal/ModalStack.java View File

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

+ 0
- 17
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/NavigatorTest.java View File

412
         verify(parentController, times(2)).onViewAppeared();
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
     @Test
415
     @Test
433
     public void dismissAllModals_onViewAppearedInvokedOnRoot() {
416
     public void dismissAllModals_onViewAppearedInvokedOnRoot() {
434
         disableShowModalAnimation(child1);
417
         disableShowModalAnimation(child1);

+ 1
- 1
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/modal/ModalPresenterTest.java View File

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

+ 13
- 27
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/modal/ModalStackTest.java View File

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