Browse Source

Show screen after RN view has rendered

Guy Carmeli 8 years ago
parent
commit
d1598962ec

+ 5
- 0
android/app/src/main/java/com/reactnativenavigation/screens/FragmentScreen.java View File

@@ -121,4 +121,9 @@ public class FragmentScreen extends Screen {
121 121
     public void preventMountAfterReattachedToWindow() {
122 122
         // nothing
123 123
     }
124
+
125
+    @Override
126
+    public void setOnDisplayListener(OnDisplayListener onContentViewDisplayedListener) {
127
+        // nothing
128
+    }
124 129
 }

+ 6
- 0
android/app/src/main/java/com/reactnativenavigation/screens/Screen.java View File

@@ -24,6 +24,10 @@ import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
24 24
 
25 25
 public abstract class Screen extends RelativeLayout {
26 26
 
27
+    public interface OnDisplayListener {
28
+        void onDisplay();
29
+    }
30
+
27 31
     protected final AppCompatActivity activity;
28 32
     protected final ScreenParams screenParams;
29 33
     protected TopBar topBar;
@@ -163,6 +167,8 @@ public abstract class Screen extends RelativeLayout {
163 167
         }
164 168
     }
165 169
 
170
+    public abstract void setOnDisplayListener(OnDisplayListener onContentViewDisplayedListener);
171
+
166 172
     public void show() {
167 173
         screenAnimator.show(screenParams.animateScreenTransitions);
168 174
     }

+ 10
- 5
android/app/src/main/java/com/reactnativenavigation/screens/ScreenStack.java View File

@@ -62,13 +62,18 @@ public class ScreenStack {
62 62
         }
63 63
     }
64 64
 
65
-    private void pushScreenToVisibleStack(ScreenParams params, RelativeLayout.LayoutParams layoutParams,
66
-                                          Screen nextScreen, final Screen previousScreen) {
65
+    private void pushScreenToVisibleStack(final ScreenParams params, RelativeLayout.LayoutParams layoutParams,
66
+                                          final Screen nextScreen, final Screen previousScreen) {
67 67
         addScreen(nextScreen, layoutParams);
68
-        nextScreen.show(params.animateScreenTransitions, new Runnable() {
68
+        nextScreen.setOnDisplayListener(new Screen.OnDisplayListener() {
69 69
             @Override
70
-            public void run() {
71
-                removePreviousWithoutUnmount(previousScreen);
70
+            public void onDisplay() {
71
+                nextScreen.show(params.animateScreenTransitions, new Runnable() {
72
+                    @Override
73
+                    public void run() {
74
+                        removePreviousWithoutUnmount(previousScreen);
75
+                    }
76
+                });
72 77
             }
73 78
         });
74 79
     }

+ 5
- 0
android/app/src/main/java/com/reactnativenavigation/screens/SingleScreen.java View File

@@ -41,4 +41,9 @@ public class SingleScreen extends Screen {
41 41
     public void preventMountAfterReattachedToWindow() {
42 42
         contentView.preventMountAfterReattachedToWindow();
43 43
     }
44
+
45
+    @Override
46
+    public void setOnDisplayListener(OnDisplayListener onContentViewDisplayedListener) {
47
+        contentView.setOnDisplayListener(onContentViewDisplayedListener);
48
+    }
44 49
 }

+ 5
- 0
android/app/src/main/java/com/reactnativenavigation/screens/ViewPagerScreen.java View File

@@ -82,4 +82,9 @@ public class ViewPagerScreen extends Screen {
82 82
             contentView.preventMountAfterReattachedToWindow();
83 83
         }
84 84
     }
85
+
86
+    @Override
87
+    public void setOnDisplayListener(OnDisplayListener onContentViewDisplayedListener) {
88
+        contentViews.get(0).setOnDisplayListener(onContentViewDisplayedListener);
89
+    }
85 90
 }

+ 31
- 0
android/app/src/main/java/com/reactnativenavigation/views/ContentView.java View File

@@ -2,10 +2,13 @@ package com.reactnativenavigation.views;
2 2
 
3 3
 import android.content.Context;
4 4
 import android.os.Bundle;
5
+import android.view.View;
5 6
 
6 7
 import com.facebook.react.ReactRootView;
7 8
 import com.reactnativenavigation.NavigationApplication;
8 9
 import com.reactnativenavigation.react.ReactViewHacks;
10
+import com.reactnativenavigation.screens.SingleScreen;
11
+import com.reactnativenavigation.utils.ViewUtils;
9 12
 
10 13
 public class ContentView extends ReactRootView {
11 14
 
@@ -13,6 +16,9 @@ public class ContentView extends ReactRootView {
13 16
     private String navigatorEventId;
14 17
     private final Bundle navigationParams;
15 18
 
19
+    boolean isContentVisible = false;
20
+    private SingleScreen.OnDisplayListener onDisplayListener;
21
+
16 22
     public ContentView(Context context, String screenId, Bundle navigationParams) {
17 23
         this(context, screenId, navigationParams, null);
18 24
     }
@@ -41,7 +47,32 @@ public class ContentView extends ReactRootView {
41 47
         ReactViewHacks.preventMountAfterReattachedToWindow(this);
42 48
     }
43 49
 
50
+    @Override
51
+    public void onViewAdded(final View child) {
52
+        super.onViewAdded(child);
53
+        detectContentViewVisible(child);
54
+    }
55
+
56
+    private void detectContentViewVisible(View child) {
57
+        if (onDisplayListener != null) {
58
+            ViewUtils.runOnPreDraw(child, new Runnable() {
59
+                @Override
60
+                public void run() {
61
+                    if (!isContentVisible) {
62
+                        isContentVisible = true;
63
+                        onDisplayListener.onDisplay();
64
+                        onDisplayListener = null;
65
+                    }
66
+                }
67
+            });
68
+        }
69
+    }
70
+
44 71
     private void attachToJS() {
45 72
         startReactApplication(NavigationApplication.instance.getReactGateway().getReactInstanceManager(), screenId, navigationParams);
46 73
     }
74
+
75
+    public void setOnDisplayListener(SingleScreen.OnDisplayListener onDisplayListener) {
76
+        this.onDisplayListener = onDisplayListener;
77
+    }
47 78
 }