Explorar el Código

Fix dismissModal root issue

Add root only if a modal was actually dismissed
Guy Carmeli hace 6 años
padre
commit
ac13fc6560

+ 1
- 1
e2e/Modals.test.js Ver fichero

@@ -32,7 +32,7 @@ describe('modal', () => {
32 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 36
     await elementById(testIDs.SHOW_MODAL_BUTTON).tap();
37 37
     await expect(elementByLabel('Modal Stack Position: 1')).toBeVisible();
38 38
     await elementById(testIDs.DISMISS_UNKNOWN_MODAL_BUTTON).tap();

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

@@ -72,7 +72,7 @@ public class Navigator extends ParentController {
72 72
 
73 73
     @Override
74 74
     public void destroy() {
75
-        modalStack.dismissAllModals(new CommandListenerAdapter());
75
+        modalStack.dismissAllModals(new CommandListenerAdapter(), () -> {});
76 76
         super.destroy();
77 77
     }
78 78
 
@@ -124,7 +124,11 @@ public class Navigator extends ParentController {
124 124
         if (from != null) {
125 125
             from.performOnParentStack(stack -> ((StackController) stack).push(viewController, listener));
126 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,13 +187,18 @@ public class Navigator extends ParentController {
183 187
     }
184 188
 
185 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 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 204
     public void showOverlay(ViewController overlay) {

+ 5
- 5
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/modal/ModalStack.java Ver fichero

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

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

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