Sfoglia il codice sorgente

Apply stack options when stacks are attached to BottomTabs

Currently options are applied when a view is visible. This caused some noticeable flickering when
switching to tabs for the first time.
This commit adds a new callback which ViewController can override - onAttachToParent.
StackController now uses it to apply options which prevents flickering.
Guy Carmeli 6 anni fa
parent
commit
0683ccbd00

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

@@ -31,6 +31,7 @@ public abstract class ViewController<T extends ViewGroup> implements ViewTreeObs
31 31
 
32 32
     private Runnable onAppearedListener;
33 33
     private boolean appearEventPosted;
34
+    private boolean isFirstayout = true;
34 35
     private Bool waitForRender = new NullBool();
35 36
 
36 37
     public interface ViewVisibilityListener {
@@ -238,6 +239,10 @@ public abstract class ViewController<T extends ViewGroup> implements ViewTreeObs
238 239
 
239 240
     @Override
240 241
     public void onGlobalLayout() {
242
+        if (isFirstayout) {
243
+            onAttachToParent();
244
+            isFirstayout = false;
245
+        }
241 246
         if (!isShown && isViewShown()) {
242 247
             if (!viewVisibilityListener.onViewAppeared(view)) {
243 248
                 isShown = true;
@@ -251,6 +256,10 @@ public abstract class ViewController<T extends ViewGroup> implements ViewTreeObs
251 256
         }
252 257
     }
253 258
 
259
+    protected void onAttachToParent() {
260
+
261
+    }
262
+
254 263
     @Override
255 264
     public void onChildViewAdded(View parent, View child) {
256 265
         yellowBoxDelegate.onChildViewAdded(parent, child);

+ 9
- 2
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/stack/StackController.java Vedi File

@@ -68,6 +68,13 @@ public class StackController extends ParentController<StackLayout> {
68 68
         return stack.peek();
69 69
     }
70 70
 
71
+    @Override
72
+    public void onAttachToParent() {
73
+        if (!isViewShown() && !isEmpty()) {
74
+            presenter.applyChildOptions(resolveCurrentOptions(), (Component) getCurrentChild().getView());
75
+        }
76
+    }
77
+
71 78
     @Override
72 79
     public void mergeOptions(Options options) {
73 80
         presenter.mergeOptions(options, (Component) getCurrentChild().getView());
@@ -276,7 +283,7 @@ public class StackController extends ParentController<StackLayout> {
276 283
         controller.destroy();
277 284
     }
278 285
 
279
-    public ViewController peek() {
286
+    ViewController peek() {
280 287
         return stack.peek();
281 288
     }
282 289
 
@@ -298,7 +305,7 @@ public class StackController extends ParentController<StackLayout> {
298 305
     }
299 306
 
300 307
     @VisibleForTesting()
301
-    public boolean canPop() {
308
+    boolean canPop() {
302 309
         return stack.size() > 1;
303 310
     }
304 311
 

+ 16
- 0
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/stack/StackControllerTest.java Vedi File

@@ -5,6 +5,7 @@ import android.app.Activity;
5 5
 import android.content.Context;
6 6
 import android.view.View;
7 7
 import android.view.ViewGroup;
8
+import android.widget.FrameLayout;
8 9
 
9 10
 import com.reactnativenavigation.BaseTest;
10 11
 import com.reactnativenavigation.TestUtils;
@@ -915,6 +916,21 @@ public class StackControllerTest extends BaseTest {
915 916
         verify(presenter, times(0)).mergeChildOptions(any(), any(), any());
916 917
     }
917 918
 
919
+    @Test
920
+    public void resolvedOptionsAreAppliedWhenStackIsAttachedToParentAndNotVisible() {
921
+        FrameLayout parent = new FrameLayout(activity);
922
+        activity.setContentView(parent);
923
+
924
+        ViewController child = new SimpleViewController(activity, childRegistry, "child1", new Options());
925
+        StackController stack = createStack(Collections.singletonList(child));
926
+        stack.getView().setVisibility(View.INVISIBLE);
927
+
928
+        parent.addView(stack.getView());
929
+
930
+        Component component = (Component) child.getView();
931
+        verify(presenter).applyChildOptions(any(), eq(component));
932
+    }
933
+
918 934
     @Test
919 935
     public void destroy() {
920 936
         uut.ensureViewIsCreated();