Преглед на файлове

Emit modalDismissed event when dismissingAllModals as well

Guy Carmeli преди 6 години
родител
ревизия
2002020aae

+ 2
- 1
lib/android/app/src/main/java/com/reactnativenavigation/react/EventEmitter.java Целия файл

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

+ 19
- 2
lib/android/app/src/main/java/com/reactnativenavigation/utils/CommandListenerAdapter.java Целия файл

1
 package com.reactnativenavigation.utils;
1
 package com.reactnativenavigation.utils;
2
 
2
 
3
+import android.support.annotation.Nullable;
4
+
3
 public class CommandListenerAdapter implements CommandListener {
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
     @Override
21
     @Override
5
     public void onSuccess(String childId) {
22
     public void onSuccess(String childId) {
6
-
23
+        if (listener != null) listener.onSuccess(childId);
7
     }
24
     }
8
 
25
 
9
     @Override
26
     @Override
10
     public void onError(String message) {
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 Целия файл

86
 
86
 
87
     private void onDismissEnd(ViewController toDismiss, CommandListener listener) {
87
     private void onDismissEnd(ViewController toDismiss, CommandListener listener) {
88
         toDismiss.destroy();
88
         toDismiss.destroy();
89
-        eventEmitter.emitModalDismissed(toDismiss.getId());
90
         listener.onSuccess(toDismiss.getId());
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 Целия файл

8
 import com.reactnativenavigation.parse.Options;
8
 import com.reactnativenavigation.parse.Options;
9
 import com.reactnativenavigation.react.EventEmitter;
9
 import com.reactnativenavigation.react.EventEmitter;
10
 import com.reactnativenavigation.utils.CommandListener;
10
 import com.reactnativenavigation.utils.CommandListener;
11
+import com.reactnativenavigation.utils.CommandListenerAdapter;
11
 import com.reactnativenavigation.viewcontrollers.ViewController;
12
 import com.reactnativenavigation.viewcontrollers.ViewController;
12
 
13
 
13
 import java.util.ArrayList;
14
 import java.util.ArrayList;
19
 public class ModalStack {
20
 public class ModalStack {
20
     private List<ViewController> modals = new ArrayList<>();
21
     private List<ViewController> modals = new ArrayList<>();
21
     private final ModalPresenter presenter;
22
     private final ModalPresenter presenter;
23
+    private EventEmitter eventEmitter;
24
+
25
+    public void setEventEmitter(EventEmitter eventEmitter) {
26
+        this.eventEmitter = eventEmitter;
27
+    }
22
 
28
 
23
     public ModalStack(Activity activity) {
29
     public ModalStack(Activity activity) {
24
         this.presenter = new ModalPresenter(new ModalAnimator(activity));
30
         this.presenter = new ModalPresenter(new ModalAnimator(activity));
49
             boolean isTop = isTop(toDismiss);
55
             boolean isTop = isTop(toDismiss);
50
             modals.remove(toDismiss);
56
             modals.remove(toDismiss);
51
             ViewController toAdd = isEmpty() ? root : isTop ? get(size() - 1) : null;
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
             if (isTop) {
65
             if (isTop) {
53
-                presenter.dismissTopModal(toDismiss, toAdd, listener);
66
+                presenter.dismissTopModal(toDismiss, toAdd, onDismiss);
54
             } else {
67
             } else {
55
-                presenter.dismissModal(toDismiss, listener);
68
+                presenter.dismissModal(toDismiss, onDismiss);
56
             }
69
             }
57
         } else {
70
         } else {
58
             listener.onError("Nothing to dismiss");
71
             listener.onError("Nothing to dismiss");
65
             return;
78
             return;
66
         }
79
         }
67
 
80
 
81
+        String topModalId = peek().getId();
82
+        int modalsDismissed = size();
83
+
68
         while (!modals.isEmpty()) {
84
         while (!modals.isEmpty()) {
69
             if (modals.size() == 1) {
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
             } else {
93
             } else {
72
                 modals.get(0).destroy();
94
                 modals.get(0).destroy();
73
                 modals.remove(0);
95
                 modals.remove(0);
126
         }
148
         }
127
         return null;
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 Целия файл

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

+ 5
- 3
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/modal/ModalStackTest.java Целия файл

87
     @Test
87
     @Test
88
     public void dismissModal() {
88
     public void dismissModal() {
89
         uut.showModal(modal1, rootController, new CommandListenerAdapter());
89
         uut.showModal(modal1, rootController, new CommandListenerAdapter());
90
-        CommandListener listener = new CommandListenerAdapter();
90
+        CommandListener listener = spy(new CommandListenerAdapter());
91
         uut.dismissModal(modal1.getId(), rootController, listener);
91
         uut.dismissModal(modal1.getId(), rootController, listener);
92
         assertThat(findModal(modal1.getId())).isNull();
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
     @SuppressWarnings("Convert2Lambda")
97
     @SuppressWarnings("Convert2Lambda")
162
         ViewGroup view2 = modal2.getView();
163
         ViewGroup view2 = modal2.getView();
163
         CommandListener listener = spy(new CommandListenerAdapter());
164
         CommandListener listener = spy(new CommandListenerAdapter());
164
         uut.dismissAllModals(listener, rootController);
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
         verify(animator, times(0)).dismiss(eq(view1), eq(modal1.options.animations.dismissModal), any());
168
         verify(animator, times(0)).dismiss(eq(view1), eq(modal1.options.animations.dismissModal), any());
167
         verify(animator, times(1)).dismiss(eq(view2), eq(modal2.options.animations.dismissModal), any());
169
         verify(animator, times(1)).dismiss(eq(view2), eq(modal2.options.animations.dismissModal), any());
168
         assertThat(uut.size()).isEqualTo(0);
170
         assertThat(uut.size()).isEqualTo(0);