Browse Source

Fix NPE when showing Overlay (#5909)

Co-authored-by: Yogev Ben David <yogevbd@wix.com>
Guy Carmeli 5 years ago
parent
commit
bfde34a586
No account linked to committer's email address

+ 4
- 7
lib/android/app/src/main/java/com/reactnativenavigation/presentation/OverlayManager.java View File

@@ -10,18 +10,16 @@ import com.reactnativenavigation.views.BehaviourDelegate;
10 10
 
11 11
 import java.util.HashMap;
12 12
 
13
+import androidx.annotation.Nullable;
14
+
13 15
 import static com.reactnativenavigation.utils.CollectionUtils.*;
14 16
 import static com.reactnativenavigation.utils.CoordinatorLayoutUtils.matchParentWithBehaviour;
15 17
 
16 18
 public class OverlayManager {
17 19
     private final HashMap<String, ViewController> overlayRegistry = new HashMap<>();
18
-    private ViewGroup contentLayout;
19
-
20
-    public void setContentLayout(ViewGroup contentLayout) {
21
-        this.contentLayout = contentLayout;
22
-    }
23 20
 
24
-    public void show(ViewGroup overlaysContainer, ViewController overlay, CommandListener listener) {
21
+    public void show(@Nullable ViewGroup contentLayout, ViewGroup overlaysContainer, ViewController overlay, CommandListener listener) {
22
+        if (contentLayout == null) return;
25 23
         if (overlaysContainer.getParent() == null) contentLayout.addView(overlaysContainer);
26 24
         overlayRegistry.put(overlay.getId(), overlay);
27 25
         overlay.addOnAppearedListener(() -> listener.onSuccess(overlay.getId()));
@@ -40,7 +38,6 @@ public class OverlayManager {
40 38
 
41 39
     public void destroy() {
42 40
         forEach(overlayRegistry.values(), this::destroyOverlay);
43
-        contentLayout = null;
44 41
     }
45 42
 
46 43
     public int size() {

+ 1
- 2
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/navigator/Navigator.java View File

@@ -64,7 +64,6 @@ public class Navigator extends ParentController {
64 64
 
65 65
     public void setContentLayout(ViewGroup contentLayout) {
66 66
         this.contentLayout = contentLayout;
67
-        overlayManager.setContentLayout(contentLayout);
68 67
         contentLayout.addView(rootLayout);
69 68
         contentLayout.addView(modalsLayout);
70 69
     }
@@ -201,7 +200,7 @@ public class Navigator extends ParentController {
201 200
     }
202 201
 
203 202
     public void showOverlay(ViewController overlay, CommandListener listener) {
204
-        overlayManager.show(overlaysLayout, overlay, listener);
203
+        overlayManager.show(contentLayout, overlaysLayout, overlay, listener);
205 204
     }
206 205
 
207 206
     public void dismissOverlay(final String componentId, CommandListener listener) {

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

@@ -123,8 +123,6 @@ public class NavigatorTest extends BaseTest {
123 123
     public void setContentLayout() {
124 124
         ViewGroup contentLayout = Mockito.mock(ViewGroup.class);
125 125
         uut.setContentLayout(contentLayout);
126
-
127
-        verify(overlayManager).setContentLayout(contentLayout);
128 126
     }
129 127
 
130 128
     @Test

+ 8
- 9
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/overlay/OverlayManagerTest.java View File

@@ -41,20 +41,19 @@ public class OverlayManagerTest extends BaseTest {
41 41
         overlay1 = spy(new SimpleViewController(activity, childRegistry, OVERLAY_ID_1, new Options()));
42 42
         overlay2 = spy(new SimpleViewController(activity, childRegistry, OVERLAY_ID_2, new Options()));
43 43
         uut = new OverlayManager();
44
-        uut.setContentLayout(contentLayout);
45 44
     }
46 45
 
47 46
     @Test
48 47
     public void show_attachesOverlayContainerToContentLayout() {
49
-        uut.show(overlayContainer, overlay1, new CommandListenerAdapter());
48
+        uut.show(contentLayout, overlayContainer, overlay1, new CommandListenerAdapter());
50 49
         assertThat(overlayContainer.getParent()).isEqualTo(contentLayout);
51
-        uut.show(overlayContainer, overlay2, new CommandListenerAdapter());
50
+        uut.show(contentLayout, overlayContainer, overlay2, new CommandListenerAdapter());
52 51
     }
53 52
 
54 53
     @Test
55 54
     public void show() {
56 55
         CommandListenerAdapter listener = spy(new CommandListenerAdapter());
57
-        uut.show(overlayContainer, overlay1, listener);
56
+        uut.show(contentLayout, overlayContainer, overlay1, listener);
58 57
         verify(listener, times(1)).onSuccess(OVERLAY_ID_1);
59 58
         assertThat(overlay1.getView().getParent()).isEqualTo(overlayContainer);
60 59
         assertMatchParent(overlay1.getView());
@@ -62,7 +61,7 @@ public class OverlayManagerTest extends BaseTest {
62 61
 
63 62
     @Test
64 63
     public void dismiss() {
65
-        uut.show(overlayContainer, overlay1, new CommandListenerAdapter());
64
+        uut.show(contentLayout, overlayContainer, overlay1, new CommandListenerAdapter());
66 65
         assertThat(uut.size()).isOne();
67 66
         CommandListener listener = spy(new CommandListenerAdapter());
68 67
         uut.dismiss(overlay1.getId(), listener);
@@ -80,8 +79,8 @@ public class OverlayManagerTest extends BaseTest {
80 79
 
81 80
     @Test
82 81
     public void dismiss_onViewReturnedToFront() {
83
-        uut.show(overlayContainer, overlay1, new CommandListenerAdapter());
84
-        uut.show(overlayContainer, overlay2, new CommandListenerAdapter());
82
+        uut.show(contentLayout, overlayContainer, overlay1, new CommandListenerAdapter());
83
+        uut.show(contentLayout, overlayContainer, overlay2, new CommandListenerAdapter());
85 84
         verify(overlay1, times(0)).onViewBroughtToFront();
86 85
 
87 86
         uut.dismiss(OVERLAY_ID_2, new CommandListenerAdapter());
@@ -90,8 +89,8 @@ public class OverlayManagerTest extends BaseTest {
90 89
 
91 90
     @Test
92 91
     public void dismiss_overlayContainerIsRemovedIfAllOverlaysAreDismissed() {
93
-        uut.show(overlayContainer, overlay1, new CommandListenerAdapter());
94
-        uut.show(overlayContainer, overlay2, new CommandListenerAdapter());
92
+        uut.show(contentLayout, overlayContainer, overlay1, new CommandListenerAdapter());
93
+        uut.show(contentLayout, overlayContainer, overlay2, new CommandListenerAdapter());
95 94
 
96 95
         uut.dismiss(OVERLAY_ID_2, new CommandListenerAdapter());
97 96
         assertThat(overlayContainer.getParent()).isEqualTo(contentLayout);