Преглед изворни кода

Show screen after RN view has rendered

Guy Carmeli пре 8 година
родитељ
комит
d1598962ec

+ 5
- 0
android/app/src/main/java/com/reactnativenavigation/screens/FragmentScreen.java Прегледај датотеку

121
     public void preventMountAfterReattachedToWindow() {
121
     public void preventMountAfterReattachedToWindow() {
122
         // nothing
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 Прегледај датотеку

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

+ 10
- 5
android/app/src/main/java/com/reactnativenavigation/screens/ScreenStack.java Прегледај датотеку

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
         addScreen(nextScreen, layoutParams);
67
         addScreen(nextScreen, layoutParams);
68
-        nextScreen.show(params.animateScreenTransitions, new Runnable() {
68
+        nextScreen.setOnDisplayListener(new Screen.OnDisplayListener() {
69
             @Override
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 Прегледај датотеку

41
     public void preventMountAfterReattachedToWindow() {
41
     public void preventMountAfterReattachedToWindow() {
42
         contentView.preventMountAfterReattachedToWindow();
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 Прегледај датотеку

82
             contentView.preventMountAfterReattachedToWindow();
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 Прегледај датотеку

2
 
2
 
3
 import android.content.Context;
3
 import android.content.Context;
4
 import android.os.Bundle;
4
 import android.os.Bundle;
5
+import android.view.View;
5
 
6
 
6
 import com.facebook.react.ReactRootView;
7
 import com.facebook.react.ReactRootView;
7
 import com.reactnativenavigation.NavigationApplication;
8
 import com.reactnativenavigation.NavigationApplication;
8
 import com.reactnativenavigation.react.ReactViewHacks;
9
 import com.reactnativenavigation.react.ReactViewHacks;
10
+import com.reactnativenavigation.screens.SingleScreen;
11
+import com.reactnativenavigation.utils.ViewUtils;
9
 
12
 
10
 public class ContentView extends ReactRootView {
13
 public class ContentView extends ReactRootView {
11
 
14
 
13
     private String navigatorEventId;
16
     private String navigatorEventId;
14
     private final Bundle navigationParams;
17
     private final Bundle navigationParams;
15
 
18
 
19
+    boolean isContentVisible = false;
20
+    private SingleScreen.OnDisplayListener onDisplayListener;
21
+
16
     public ContentView(Context context, String screenId, Bundle navigationParams) {
22
     public ContentView(Context context, String screenId, Bundle navigationParams) {
17
         this(context, screenId, navigationParams, null);
23
         this(context, screenId, navigationParams, null);
18
     }
24
     }
41
         ReactViewHacks.preventMountAfterReattachedToWindow(this);
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
     private void attachToJS() {
71
     private void attachToJS() {
45
         startReactApplication(NavigationApplication.instance.getReactGateway().getReactInstanceManager(), screenId, navigationParams);
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
 }