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

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

64
 
64
 
65
     public void setContentLayout(ViewGroup contentLayout) {
65
     public void setContentLayout(ViewGroup contentLayout) {
66
         this.contentLayout = contentLayout;
66
         this.contentLayout = contentLayout;
67
-        overlayManager.setContentLayout(contentLayout);
68
         contentLayout.addView(rootLayout);
67
         contentLayout.addView(rootLayout);
69
         contentLayout.addView(modalsLayout);
68
         contentLayout.addView(modalsLayout);
70
     }
69
     }
201
     }
200
     }
202
 
201
 
203
     public void showOverlay(ViewController overlay, CommandListener listener) {
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
     public void dismissOverlay(final String componentId, CommandListener listener) {
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
     public void setContentLayout() {
123
     public void setContentLayout() {
124
         ViewGroup contentLayout = Mockito.mock(ViewGroup.class);
124
         ViewGroup contentLayout = Mockito.mock(ViewGroup.class);
125
         uut.setContentLayout(contentLayout);
125
         uut.setContentLayout(contentLayout);
126
-
127
-        verify(overlayManager).setContentLayout(contentLayout);
128
     }
126
     }
129
 
127
 
130
     @Test
128
     @Test

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

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