Browse Source

[Breaking] implement overCurrentContext

When modalPresentationStyle is overCurrentContext - previous view is not detached when showing modal.
Guy Carmeli 6 years ago
parent
commit
346753b944

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

116
         return view;
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
     public String getId() {
127
     public String getId() {
120
         return id;
128
         return id;
121
     }
129
     }

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

3
 import android.animation.Animator;
3
 import android.animation.Animator;
4
 import android.animation.AnimatorListenerAdapter;
4
 import android.animation.AnimatorListenerAdapter;
5
 import android.view.ViewGroup;
5
 import android.view.ViewGroup;
6
-import android.view.ViewManager;
7
 
6
 
8
 import com.reactnativenavigation.anim.ModalAnimator;
7
 import com.reactnativenavigation.anim.ModalAnimator;
8
+import com.reactnativenavigation.parse.ModalPresentationStyle;
9
 import com.reactnativenavigation.viewcontrollers.Navigator.CommandListener;
9
 import com.reactnativenavigation.viewcontrollers.Navigator.CommandListener;
10
 import com.reactnativenavigation.viewcontrollers.ViewController;
10
 import com.reactnativenavigation.viewcontrollers.ViewController;
11
 
11
 
37
     }
37
     }
38
 
38
 
39
     private void onShowModalEnd(ViewController toAdd, ViewController toRemove, CommandListener listener) {
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
         listener.onSuccess(toAdd.getId());
43
         listener.onSuccess(toAdd.getId());
42
     }
44
     }
43
 
45
 
44
     public void dismissModal(ViewController toDismiss, ViewController toAdd, CommandListener listener) {
46
     public void dismissModal(ViewController toDismiss, ViewController toAdd, CommandListener listener) {
45
-        content.addView(toAdd.getView(), 0);
47
+        toAdd.attachView(content, 0);
46
         if (toDismiss.options.animations.dismissModal.enable.isTrueOrUndefined()) {
48
         if (toDismiss.options.animations.dismissModal.enable.isTrueOrUndefined()) {
47
             animator.dismiss(toDismiss.getView(), new AnimatorListenerAdapter() {
49
             animator.dismiss(toDismiss.getView(), new AnimatorListenerAdapter() {
48
                 @Override
50
                 @Override

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

7
 import com.reactnativenavigation.BaseTest;
7
 import com.reactnativenavigation.BaseTest;
8
 import com.reactnativenavigation.anim.ModalAnimator;
8
 import com.reactnativenavigation.anim.ModalAnimator;
9
 import com.reactnativenavigation.mocks.SimpleViewController;
9
 import com.reactnativenavigation.mocks.SimpleViewController;
10
+import com.reactnativenavigation.parse.ModalPresentationStyle;
10
 import com.reactnativenavigation.parse.Options;
11
 import com.reactnativenavigation.parse.Options;
11
 import com.reactnativenavigation.utils.CommandListenerAdapter;
12
 import com.reactnativenavigation.utils.CommandListenerAdapter;
12
 import com.reactnativenavigation.viewcontrollers.Navigator.CommandListener;
13
 import com.reactnativenavigation.viewcontrollers.Navigator.CommandListener;
122
 
123
 
123
     @Test
124
     @Test
124
     public void dismissModal_previousViewIsAddedAtIndex0() {
125
     public void dismissModal_previousViewIsAddedAtIndex0() {
126
+        modal2.ensureViewIsCreated();
125
         FrameLayout spy = spy(new FrameLayout(newActivity()));
127
         FrameLayout spy = spy(new FrameLayout(newActivity()));
126
         uut.setContentLayout(spy);
128
         uut.setContentLayout(spy);
127
         uut.dismissModal(modal1, modal2, new CommandListenerAdapter());
129
         uut.dismissModal(modal1, modal2, new CommandListenerAdapter());
150
         uut.dismissModal(modal2, modal1, new CommandListenerAdapter());
152
         uut.dismissModal(modal2, modal1, new CommandListenerAdapter());
151
         verify(modal1, times(2)).onViewAppeared();
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
 }