Browse Source

Emit modalDismissed event when dismissingAllModals as well

Guy Carmeli 6 years ago
parent
commit
2002020aae

+ 2
- 1
lib/android/app/src/main/java/com/reactnativenavigation/react/EventEmitter.java View File

@@ -61,9 +61,10 @@ public class EventEmitter {
61 61
 		emit(CommandCompleted, event);
62 62
 	}
63 63
 
64
-    public void emitModalDismissed(String id) {
64
+    public void emitModalDismissed(String id, int modalsDismissed) {
65 65
         WritableMap event = Arguments.createMap();
66 66
         event.putString("componentId", id);
67
+        event.putInt("modalsDismissed", modalsDismissed);
67 68
         emit(ModalDismissed, event);
68 69
     }
69 70
 

+ 19
- 2
lib/android/app/src/main/java/com/reactnativenavigation/utils/CommandListenerAdapter.java View File

@@ -1,13 +1,30 @@
1 1
 package com.reactnativenavigation.utils;
2 2
 
3
+import android.support.annotation.Nullable;
4
+
3 5
 public class CommandListenerAdapter implements CommandListener {
6
+
7
+    @Nullable private CommandListener listener;
8
+
9
+    @Nullable
10
+    public CommandListener getListener() {
11
+        return listener;
12
+    }
13
+
14
+    public CommandListenerAdapter() {
15
+    }
16
+
17
+    public CommandListenerAdapter(@Nullable CommandListener listener) {
18
+        this.listener = listener;
19
+    }
20
+
4 21
     @Override
5 22
     public void onSuccess(String childId) {
6
-
23
+        if (listener != null) listener.onSuccess(childId);
7 24
     }
8 25
 
9 26
     @Override
10 27
     public void onError(String message) {
11
-
28
+        if (listener != null) listener.onError(message);
12 29
     }
13 30
 }

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

@@ -86,11 +86,6 @@ public class ModalPresenter {
86 86
 
87 87
     private void onDismissEnd(ViewController toDismiss, CommandListener listener) {
88 88
         toDismiss.destroy();
89
-        eventEmitter.emitModalDismissed(toDismiss.getId());
90 89
         listener.onSuccess(toDismiss.getId());
91 90
     }
92
-
93
-    public void setEventEmitter(EventEmitter eventEmitter) {
94
-        this.eventEmitter = eventEmitter;
95
-    }
96 91
 }

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

@@ -8,6 +8,7 @@ import com.reactnativenavigation.anim.ModalAnimator;
8 8
 import com.reactnativenavigation.parse.Options;
9 9
 import com.reactnativenavigation.react.EventEmitter;
10 10
 import com.reactnativenavigation.utils.CommandListener;
11
+import com.reactnativenavigation.utils.CommandListenerAdapter;
11 12
 import com.reactnativenavigation.viewcontrollers.ViewController;
12 13
 
13 14
 import java.util.ArrayList;
@@ -19,6 +20,11 @@ import javax.annotation.Nullable;
19 20
 public class ModalStack {
20 21
     private List<ViewController> modals = new ArrayList<>();
21 22
     private final ModalPresenter presenter;
23
+    private EventEmitter eventEmitter;
24
+
25
+    public void setEventEmitter(EventEmitter eventEmitter) {
26
+        this.eventEmitter = eventEmitter;
27
+    }
22 28
 
23 29
     public ModalStack(Activity activity) {
24 30
         this.presenter = new ModalPresenter(new ModalAnimator(activity));
@@ -49,10 +55,17 @@ public class ModalStack {
49 55
             boolean isTop = isTop(toDismiss);
50 56
             modals.remove(toDismiss);
51 57
             ViewController toAdd = isEmpty() ? root : isTop ? get(size() - 1) : null;
58
+            CommandListenerAdapter onDismiss = new CommandListenerAdapter(listener) {
59
+                @Override
60
+                public void onSuccess(String childId) {
61
+                    super.onSuccess(childId);
62
+                    eventEmitter.emitModalDismissed(toDismiss.getId(), 1);
63
+                }
64
+            };
52 65
             if (isTop) {
53
-                presenter.dismissTopModal(toDismiss, toAdd, listener);
66
+                presenter.dismissTopModal(toDismiss, toAdd, onDismiss);
54 67
             } else {
55
-                presenter.dismissModal(toDismiss, listener);
68
+                presenter.dismissModal(toDismiss, onDismiss);
56 69
             }
57 70
         } else {
58 71
             listener.onError("Nothing to dismiss");
@@ -65,9 +78,18 @@ public class ModalStack {
65 78
             return;
66 79
         }
67 80
 
81
+        String topModalId = peek().getId();
82
+        int modalsDismissed = size();
83
+
68 84
         while (!modals.isEmpty()) {
69 85
             if (modals.size() == 1) {
70
-                dismissModal(modals.get(0).getId(), root, listener);
86
+                dismissModal(modals.get(0).getId(), root, new CommandListenerAdapter(listener) {
87
+                    @Override
88
+                    public void onSuccess(String childId) {
89
+                        super.onSuccess(childId);
90
+                        eventEmitter.emitModalDismissed(topModalId, modalsDismissed);
91
+                    }
92
+                });
71 93
             } else {
72 94
                 modals.get(0).destroy();
73 95
                 modals.remove(0);
@@ -126,8 +148,4 @@ public class ModalStack {
126 148
         }
127 149
         return null;
128 150
     }
129
-
130
-    public void setEventEmitter(EventEmitter eventEmitter) {
131
-        presenter.setEventEmitter(eventEmitter);
132
-    }
133 151
 }

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

@@ -10,7 +10,6 @@ import com.reactnativenavigation.parse.AnimationOptions;
10 10
 import com.reactnativenavigation.parse.ModalPresentationStyle;
11 11
 import com.reactnativenavigation.parse.Options;
12 12
 import com.reactnativenavigation.parse.params.Bool;
13
-import com.reactnativenavigation.react.EventEmitter;
14 13
 import com.reactnativenavigation.utils.CommandListener;
15 14
 import com.reactnativenavigation.utils.CommandListenerAdapter;
16 15
 import com.reactnativenavigation.viewcontrollers.ChildController;
@@ -20,7 +19,6 @@ import com.reactnativenavigation.viewcontrollers.ViewController;
20 19
 import org.json.JSONException;
21 20
 import org.json.JSONObject;
22 21
 import org.junit.Test;
23
-import org.mockito.Mockito;
24 22
 
25 23
 import static org.assertj.core.api.Java6Assertions.assertThat;
26 24
 import static org.mockito.ArgumentMatchers.any;
@@ -56,7 +54,6 @@ public class ModalPresenterTest extends BaseTest {
56 54
         uut.setContentLayout(contentLayout);
57 55
         modal1 = spy(new SimpleViewController(activity, childRegistry, MODAL_ID_1, new Options()));
58 56
         modal2 = spy(new SimpleViewController(activity, childRegistry, MODAL_ID_2, new Options()));
59
-        uut.setEventEmitter(Mockito.mock(EventEmitter.class));
60 57
     }
61 58
 
62 59
     @Test

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

@@ -87,10 +87,11 @@ public class ModalStackTest extends BaseTest {
87 87
     @Test
88 88
     public void dismissModal() {
89 89
         uut.showModal(modal1, rootController, new CommandListenerAdapter());
90
-        CommandListener listener = new CommandListenerAdapter();
90
+        CommandListener listener = spy(new CommandListenerAdapter());
91 91
         uut.dismissModal(modal1.getId(), rootController, listener);
92 92
         assertThat(findModal(modal1.getId())).isNull();
93
-        verify(presenter, times(1)).dismissTopModal(modal1, rootController, listener);
93
+        verify(presenter, times(1)).dismissTopModal(eq(modal1), eq(rootController), any());
94
+        verify(listener).onSuccess(modal1.getId());
94 95
     }
95 96
 
96 97
     @SuppressWarnings("Convert2Lambda")
@@ -162,7 +163,8 @@ public class ModalStackTest extends BaseTest {
162 163
         ViewGroup view2 = modal2.getView();
163 164
         CommandListener listener = spy(new CommandListenerAdapter());
164 165
         uut.dismissAllModals(listener, rootController);
165
-        verify(presenter, times(1)).dismissTopModal(modal2, rootController, listener);
166
+        verify(presenter, times(1)).dismissTopModal(eq(modal2), eq(rootController), any());
167
+        verify(listener).onSuccess(modal2.getId());
166 168
         verify(animator, times(0)).dismiss(eq(view1), eq(modal1.options.animations.dismissModal), any());
167 169
         verify(animator, times(1)).dismiss(eq(view2), eq(modal2.options.animations.dismissModal), any());
168 170
         assertThat(uut.size()).isEqualTo(0);