Ver código fonte

[Breaking] implement overCurrentContext

When modalPresentationStyle is overCurrentContext - previous view is not detached when showing modal.
Guy Carmeli 6 anos atrás
pai
commit
346753b944

+ 8
- 0
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/ViewController.java Ver arquivo

@@ -116,6 +116,14 @@ public abstract class ViewController<T extends ViewGroup> implements ViewTreeObs
116 116
         return view;
117 117
     }
118 118
 
119
+    public void detachView() {
120
+        ((ViewManager) view.getParent()).removeView(view);
121
+    }
122
+
123
+    public void attachView(ViewGroup parent, int index) {
124
+        if (view.getParent() == null) parent.addView(view, index);
125
+    }
126
+
119 127
     public String getId() {
120 128
         return id;
121 129
     }

+ 5
- 3
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/modal/ModalPresenter.java Ver arquivo

@@ -3,9 +3,9 @@ package com.reactnativenavigation.viewcontrollers.modal;
3 3
 import android.animation.Animator;
4 4
 import android.animation.AnimatorListenerAdapter;
5 5
 import android.view.ViewGroup;
6
-import android.view.ViewManager;
7 6
 
8 7
 import com.reactnativenavigation.anim.ModalAnimator;
8
+import com.reactnativenavigation.parse.ModalPresentationStyle;
9 9
 import com.reactnativenavigation.viewcontrollers.Navigator.CommandListener;
10 10
 import com.reactnativenavigation.viewcontrollers.ViewController;
11 11
 
@@ -37,12 +37,14 @@ public class ModalPresenter {
37 37
     }
38 38
 
39 39
     private void onShowModalEnd(ViewController toAdd, ViewController toRemove, CommandListener listener) {
40
-        ((ViewManager) toRemove.getView().getParent()).removeView(toRemove.getView());
40
+        if (toAdd.options.modal.presentationStyle != ModalPresentationStyle.OverCurrentContext) {
41
+            toRemove.detachView();
42
+        }
41 43
         listener.onSuccess(toAdd.getId());
42 44
     }
43 45
 
44 46
     public void dismissModal(ViewController toDismiss, ViewController toAdd, CommandListener listener) {
45
-        content.addView(toAdd.getView(), 0);
47
+        toAdd.attachView(content, 0);
46 48
         if (toDismiss.options.animations.dismissModal.enable.isTrueOrUndefined()) {
47 49
             animator.dismiss(toDismiss.getView(), new AnimatorListenerAdapter() {
48 50
                 @Override

+ 12
- 0
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/modal/ModalPresenterTest.java Ver arquivo

@@ -7,6 +7,7 @@ import android.widget.FrameLayout;
7 7
 import com.reactnativenavigation.BaseTest;
8 8
 import com.reactnativenavigation.anim.ModalAnimator;
9 9
 import com.reactnativenavigation.mocks.SimpleViewController;
10
+import com.reactnativenavigation.parse.ModalPresentationStyle;
10 11
 import com.reactnativenavigation.parse.Options;
11 12
 import com.reactnativenavigation.utils.CommandListenerAdapter;
12 13
 import com.reactnativenavigation.viewcontrollers.Navigator.CommandListener;
@@ -122,6 +123,7 @@ public class ModalPresenterTest extends BaseTest {
122 123
 
123 124
     @Test
124 125
     public void dismissModal_previousViewIsAddedAtIndex0() {
126
+        modal2.ensureViewIsCreated();
125 127
         FrameLayout spy = spy(new FrameLayout(newActivity()));
126 128
         uut.setContentLayout(spy);
127 129
         uut.dismissModal(modal1, modal2, new CommandListenerAdapter());
@@ -150,4 +152,14 @@ public class ModalPresenterTest extends BaseTest {
150 152
         uut.dismissModal(modal2, modal1, new CommandListenerAdapter());
151 153
         verify(modal1, times(2)).onViewAppeared();
152 154
     }
155
+
156
+    @Test
157
+    public void dismissModal_previousViewIsNotDetachedIfOverCurrentContext() {
158
+        modal1.options.modal.presentationStyle = ModalPresentationStyle.OverCurrentContext;
159
+        disableShowModalAnimation(modal1, modal2);
160
+
161
+        uut.showModal(modal1, rootController, new CommandListenerAdapter());
162
+        assertThat(rootController.getView().getParent()).isNotNull();
163
+        verify(rootController, times(0)).onViewDisappear();
164
+    }
153 165
 }