Browse Source

Fix crash when dismissAllModals is called before root is set

Guy Carmeli 4 years ago
parent
commit
52bcd5ba50

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

@@ -14,11 +14,12 @@ import java.util.ArrayList;
14 14
 import java.util.EmptyStackException;
15 15
 import java.util.List;
16 16
 
17
-import javax.annotation.Nullable;
18
-
17
+import androidx.annotation.Nullable;
19 18
 import androidx.annotation.RestrictTo;
20 19
 import androidx.coordinatorlayout.widget.CoordinatorLayout;
21 20
 
21
+import static com.reactnativenavigation.utils.ObjectUtils.perform;
22
+
22 23
 public class ModalStack {
23 24
     private List<ViewController> modals = new ArrayList<>();
24 25
     private final ModalPresenter presenter;
@@ -82,9 +83,9 @@ public class ModalStack {
82 83
         }
83 84
     }
84 85
 
85
-    public void dismissAllModals(ViewController root, Options mergeOptions, CommandListener listener) {
86
+    public void dismissAllModals(@Nullable ViewController root, Options mergeOptions, CommandListener listener) {
86 87
         if (modals.isEmpty()) {
87
-            listener.onSuccess(root.getId());
88
+            listener.onSuccess(perform(root, "", ViewController::getId));
88 89
             return;
89 90
         }
90 91
         String topModalId = peek().getId();

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

@@ -173,6 +173,13 @@ public class ModalStackTest extends BaseTest {
173 173
         verifyZeroInteractions(listener);
174 174
     }
175 175
 
176
+    @Test
177
+    public void dismissAllModal_resolvesPromiseSuccessfullyWhenCalledBeforeRootIsSet() {
178
+        CommandListenerAdapter spy = new CommandListenerAdapter();
179
+        uut.dismissAllModals(null, Options.EMPTY, spy);
180
+        verify(spy).onSuccess("");
181
+    }
182
+
176 183
     @Test
177 184
     public void dismissAllModals_resolveSuccessfullyIfEmpty() {
178 185
         CommandListener spy = spy(new CommandListenerAdapter());