Browse Source

Fix dismissModal root issue

Add root only if a modal was actually dismissed
Guy Carmeli 6 years ago
parent
commit
ac13fc6560

+ 1
- 1
e2e/Modals.test.js View File

32
     await expect(elementById(testIDs.WELCOME_SCREEN_HEADER)).toBeVisible();
32
     await expect(elementById(testIDs.WELCOME_SCREEN_HEADER)).toBeVisible();
33
   });
33
   });
34
 
34
 
35
-  xit('dismiss unknown screen id', async () => {
35
+  it('dismiss unknown screen id', async () => {
36
     await elementById(testIDs.SHOW_MODAL_BUTTON).tap();
36
     await elementById(testIDs.SHOW_MODAL_BUTTON).tap();
37
     await expect(elementByLabel('Modal Stack Position: 1')).toBeVisible();
37
     await expect(elementByLabel('Modal Stack Position: 1')).toBeVisible();
38
     await elementById(testIDs.DISMISS_UNKNOWN_MODAL_BUTTON).tap();
38
     await elementById(testIDs.DISMISS_UNKNOWN_MODAL_BUTTON).tap();

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

72
 
72
 
73
     @Override
73
     @Override
74
     public void destroy() {
74
     public void destroy() {
75
-        modalStack.dismissAllModals(new CommandListenerAdapter());
75
+        modalStack.dismissAllModals(new CommandListenerAdapter(), () -> {});
76
         super.destroy();
76
         super.destroy();
77
     }
77
     }
78
 
78
 
124
         if (from != null) {
124
         if (from != null) {
125
             from.performOnParentStack(stack -> ((StackController) stack).push(viewController, listener));
125
             from.performOnParentStack(stack -> ((StackController) stack).push(viewController, listener));
126
         } else {
126
         } else {
127
-            listener.onError("Could not push component: " + viewController.getId() + ". Stack with id " + fromId + " was not found.");
127
+            listener.onError("Could not push component: " +
128
+                             viewController.getId() +
129
+                             ". Stack with id " +
130
+                             fromId +
131
+                             " was not found.");
128
         }
132
         }
129
     }
133
     }
130
 
134
 
183
     }
187
     }
184
 
188
 
185
     public void dismissModal(final String componentId, CommandListener listener) {
189
     public void dismissModal(final String componentId, CommandListener listener) {
186
-        if (modalStack.size() == 1) contentLayout.addView(root.getView());
187
-        modalStack.dismissModal(componentId, listener);
190
+        modalStack.dismissModal(componentId,
191
+                () -> {
192
+                    if (modalStack.size() == 1) contentLayout.addView(root.getView());
193
+                },
194
+                listener
195
+        );
188
     }
196
     }
189
 
197
 
190
     public void dismissAllModals(CommandListener listener) {
198
     public void dismissAllModals(CommandListener listener) {
191
-        if (!modalStack.isEmpty()) contentLayout.addView(root.getView(), 0);
192
-        modalStack.dismissAllModals(listener);
199
+        modalStack.dismissAllModals(listener, () -> {
200
+            if (!modalStack.isEmpty()) contentLayout.addView(root.getView(), 0);
201
+        });
193
     }
202
     }
194
 
203
 
195
     public void showOverlay(ViewController overlay) {
204
     public void showOverlay(ViewController overlay) {

+ 5
- 5
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, CommandListener listener) {
32
+    public void dismissModal(String componentId, Runnable onModalWilDismiss, CommandListener listener) {
33
         ViewController toDismiss = findModalByComponentId(componentId);
33
         ViewController toDismiss = findModalByComponentId(componentId);
34
         if (toDismiss != null) {
34
         if (toDismiss != null) {
35
+            onModalWilDismiss.run();
35
             ViewController toAdd = isTop(toDismiss) ? get(size() - 2) : null;
36
             ViewController toAdd = isTop(toDismiss) ? get(size() - 2) : null;
36
             modals.remove(toDismiss);
37
             modals.remove(toDismiss);
37
             presenter.dismissModal(toDismiss, toAdd, listener);
38
             presenter.dismissModal(toDismiss, toAdd, listener);
40
         }
41
         }
41
     }
42
     }
42
 
43
 
43
-    public void dismissAllModals(CommandListener listener) {
44
+    public void dismissAllModals(CommandListener listener, Runnable onModalWilDismiss) {
44
         if (modals.isEmpty()) {
45
         if (modals.isEmpty()) {
45
             listener.onError("Nothing to dismiss");
46
             listener.onError("Nothing to dismiss");
46
             return;
47
             return;
48
 
49
 
49
         while (!modals.isEmpty()) {
50
         while (!modals.isEmpty()) {
50
             if (modals.size() == 1) {
51
             if (modals.size() == 1) {
51
-                dismissModal(modals.get(0).getId(), listener);
52
+                dismissModal(modals.get(0).getId(), onModalWilDismiss, listener);
52
             } else {
53
             } else {
53
                 modals.get(0).destroy();
54
                 modals.get(0).destroy();
54
                 modals.remove(0);
55
                 modals.remove(0);
61
         if (peek().handleBack(listener)) {
62
         if (peek().handleBack(listener)) {
62
             return true;
63
             return true;
63
         }
64
         }
64
-        onModalWillDismiss.run();
65
-        dismissModal(peek().getId(), listener);
65
+        dismissModal(peek().getId(), onModalWillDismiss, listener);
66
         return true;
66
         return true;
67
     }
67
     }
68
 
68
 

+ 32
- 9
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/modal/ModalStackTest2.java View File

72
         assertThat(findModal(MODAL_ID_1)).isNotNull();
72
         assertThat(findModal(MODAL_ID_1)).isNotNull();
73
     }
73
     }
74
 
74
 
75
+    @SuppressWarnings("Convert2Lambda")
75
     @Test
76
     @Test
76
     public void dismissModal() {
77
     public void dismissModal() {
77
         uut.showModal(modal1, new CommandListenerAdapter());
78
         uut.showModal(modal1, new CommandListenerAdapter());
78
         CommandListener listener = new CommandListenerAdapter();
79
         CommandListener listener = new CommandListenerAdapter();
79
-        uut.dismissModal(modal1.getId(), listener);
80
+        Runnable onModalWillDismiss = spy(new Runnable() {
81
+            @Override
82
+            public void run() {
83
+                assertThat(modal1.getView().getParent()).isNotNull();
84
+            }
85
+        });
86
+        uut.dismissModal(modal1.getId(), onModalWillDismiss, listener);
80
         assertThat(findModal(modal1.getId())).isNull();
87
         assertThat(findModal(modal1.getId())).isNull();
88
+        verify(onModalWillDismiss, times(1)).run();
81
         verify(presenter, times(1)).dismissModal(modal1, null, listener);
89
         verify(presenter, times(1)).dismissModal(modal1, null, listener);
82
     }
90
     }
83
 
91
 
92
+    @SuppressWarnings("Convert2Lambda")
84
     @Test
93
     @Test
85
     public void dismissModal_rejectIfModalNotFound() {
94
     public void dismissModal_rejectIfModalNotFound() {
86
         CommandListener listener = spy(new CommandListenerAdapter());
95
         CommandListener listener = spy(new CommandListenerAdapter());
87
-        uut.dismissModal(MODAL_ID_1, listener);
96
+        Runnable onModalWillDismiss = spy(new Runnable() {
97
+            @Override
98
+            public void run() {
99
+
100
+            }
101
+        });
102
+        uut.dismissModal(MODAL_ID_1, onModalWillDismiss, listener);
103
+        verify(onModalWillDismiss, times(0)).run();
88
         verify(listener, times(1)).onError(anyString());
104
         verify(listener, times(1)).onError(anyString());
89
         verifyZeroInteractions(listener);
105
         verifyZeroInteractions(listener);
90
     }
106
     }
101
                 assertThat(uut.isEmpty()).isTrue();
117
                 assertThat(uut.isEmpty()).isTrue();
102
             }
118
             }
103
         });
119
         });
104
-        uut.dismissAllModals(listener);
120
+        uut.dismissAllModals(listener, () -> {});
105
         verify(listener, times(1)).onSuccess(anyString());
121
         verify(listener, times(1)).onSuccess(anyString());
106
         verifyZeroInteractions(listener);
122
         verifyZeroInteractions(listener);
107
     }
123
     }
109
     @Test
125
     @Test
110
     public void dismissAllModals_rejectIfEmpty() {
126
     public void dismissAllModals_rejectIfEmpty() {
111
         CommandListener spy = spy(new CommandListenerAdapter());
127
         CommandListener spy = spy(new CommandListenerAdapter());
112
-        uut.dismissAllModals(spy);
128
+        uut.dismissAllModals(spy, () -> {});
113
         verify(spy, times(1)).onError(any());
129
         verify(spy, times(1)).onError(any());
114
     }
130
     }
115
 
131
 
132
+    @SuppressWarnings("Convert2Lambda")
116
     @Test
133
     @Test
117
     public void dismissAllModals_onlyTopModalIsAnimated() {
134
     public void dismissAllModals_onlyTopModalIsAnimated() {
118
         uut.showModal(modal1, new CommandListenerAdapter());
135
         uut.showModal(modal1, new CommandListenerAdapter());
121
         ViewGroup view1 = modal1.getView();
138
         ViewGroup view1 = modal1.getView();
122
         ViewGroup view2 = modal2.getView();
139
         ViewGroup view2 = modal2.getView();
123
         CommandListener listener = spy(new CommandListenerAdapter());
140
         CommandListener listener = spy(new CommandListenerAdapter());
124
-        uut.dismissAllModals(listener);
141
+        Runnable onModalWillDismiss = spy(new Runnable() {
142
+            @Override
143
+            public void run() {
125
 
144
 
145
+            }
146
+        });
147
+        uut.dismissAllModals(listener, onModalWillDismiss);
148
+        verify(onModalWillDismiss, times(1)).run();
126
         verify(presenter, times(1)).dismissModal(modal2, null, listener);
149
         verify(presenter, times(1)).dismissModal(modal2, null, listener);
127
         verify(animator, times(0)).dismiss(eq(view1), any());
150
         verify(animator, times(0)).dismiss(eq(view1), any());
128
         verify(animator, times(1)).dismiss(eq(view2), any());
151
         verify(animator, times(1)).dismiss(eq(view2), any());
134
         uut.showModal(modal1, new CommandListenerAdapter());
157
         uut.showModal(modal1, new CommandListenerAdapter());
135
         uut.showModal(modal2, new CommandListenerAdapter());
158
         uut.showModal(modal2, new CommandListenerAdapter());
136
 
159
 
137
-        uut.dismissAllModals(new CommandListenerAdapter());
160
+        uut.dismissAllModals(new CommandListenerAdapter(), () -> {});
138
 
161
 
139
         verify(modal1, times(1)).destroy();
162
         verify(modal1, times(1)).destroy();
140
         verify(modal1, times(1)).onViewDisappear();
163
         verify(modal1, times(1)).onViewDisappear();
146
         assertThat(uut.isEmpty()).isTrue();
169
         assertThat(uut.isEmpty()).isTrue();
147
         uut.showModal(modal1, new CommandListenerAdapter());
170
         uut.showModal(modal1, new CommandListenerAdapter());
148
         assertThat(uut.isEmpty()).isFalse();
171
         assertThat(uut.isEmpty()).isFalse();
149
-        uut.dismissAllModals(new CommandListenerAdapter());
172
+        uut.dismissAllModals(new CommandListenerAdapter(), () -> {});
150
         assertThat(uut.isEmpty()).isTrue();
173
         assertThat(uut.isEmpty()).isTrue();
151
     }
174
     }
152
 
175
 
168
 
191
 
169
         uut.showModal(modal1, new CommandListenerAdapter());
192
         uut.showModal(modal1, new CommandListenerAdapter());
170
         uut.showModal(modal2, new CommandListenerAdapter());
193
         uut.showModal(modal2, new CommandListenerAdapter());
171
-        uut.dismissModal(modal2.getId(), new CommandListenerAdapter());
194
+        uut.dismissModal(modal2.getId(), () -> {}, new CommandListenerAdapter());
172
         verify(modal1, times(2)).onViewAppeared();
195
         verify(modal1, times(2)).onViewAppeared();
173
     }
196
     }
174
 
197
 
181
         uut.showModal(modal2, new CommandListenerAdapter());
204
         uut.showModal(modal2, new CommandListenerAdapter());
182
         uut.showModal(modal3, new CommandListenerAdapter());
205
         uut.showModal(modal3, new CommandListenerAdapter());
183
 
206
 
184
-        uut.dismissModal(modal2.getId(), new CommandListenerAdapter());
207
+        uut.dismissModal(modal2.getId(), () -> {}, new CommandListenerAdapter());
185
         assertThat(uut.size()).isEqualTo(2);
208
         assertThat(uut.size()).isEqualTo(2);
186
         verify(modal2, times(1)).onViewDisappear();
209
         verify(modal2, times(1)).onViewDisappear();
187
         verify(modal2, times(1)).destroy();
210
         verify(modal2, times(1)).destroy();