|
@@ -1,12 +1,16 @@
|
1
|
1
|
package com.reactnativenavigation.views;
|
2
|
2
|
|
3
|
3
|
import android.os.Bundle;
|
|
4
|
+import android.view.View;
|
|
5
|
+import android.view.ViewGroup;
|
4
|
6
|
import android.view.ViewTreeObserver;
|
5
|
7
|
import android.widget.FrameLayout;
|
|
8
|
+import android.widget.ScrollView;
|
6
|
9
|
|
7
|
10
|
import com.facebook.react.ReactInstanceManager;
|
8
|
11
|
import com.facebook.react.ReactRootView;
|
9
|
12
|
import com.reactnativenavigation.activities.BaseReactActivity;
|
|
13
|
+import com.reactnativenavigation.activities.BottomTabActivity;
|
10
|
14
|
import com.reactnativenavigation.core.objects.Screen;
|
11
|
15
|
import com.reactnativenavigation.utils.BridgeUtils;
|
12
|
16
|
import com.reactnativenavigation.utils.ReflectionUtils;
|
|
@@ -16,7 +20,58 @@ import com.reactnativenavigation.utils.ReflectionUtils;
|
16
|
20
|
*/
|
17
|
21
|
public class RctView extends FrameLayout {
|
18
|
22
|
|
19
|
|
- private ReactRootView mReactRootView;
|
|
23
|
+ private BottomTabActivity context;
|
|
24
|
+ private ReactRootView reactRootView;
|
|
25
|
+ private ScrollView scrollView;
|
|
26
|
+ private int lastScrollY = -1;
|
|
27
|
+ private final ViewTreeObserver.OnScrollChangedListener scrollChangedListener = new ViewTreeObserver.OnScrollChangedListener() {
|
|
28
|
+ @Override
|
|
29
|
+ public void onScrollChanged() {
|
|
30
|
+ if (!scrollView.getViewTreeObserver().isAlive()) {
|
|
31
|
+ return;
|
|
32
|
+ }
|
|
33
|
+
|
|
34
|
+ final int scrollY = scrollView.getScrollY();
|
|
35
|
+ if (scrollY != lastScrollY && // Scroll position changed
|
|
36
|
+ scrollY > 0 && // Ignore top overscroll
|
|
37
|
+ scrollY < (scrollView.getChildAt(0).getHeight() - scrollView.getHeight())) { // Ignore bottom overscroll
|
|
38
|
+ int direction = scrollY > lastScrollY ?
|
|
39
|
+ BottomNavigation.SCROLL_DIRECTION_DOWN :
|
|
40
|
+ BottomNavigation.SCROLL_DIRECTION_UP;
|
|
41
|
+ lastScrollY = scrollY;
|
|
42
|
+ context.onScrollChanged(direction);
|
|
43
|
+ }
|
|
44
|
+ }
|
|
45
|
+ };
|
|
46
|
+ private boolean isScrollEventListenerRegistered = false;
|
|
47
|
+
|
|
48
|
+ private final View.OnAttachStateChangeListener stateChangeListener =
|
|
49
|
+ new View.OnAttachStateChangeListener() {
|
|
50
|
+ @Override
|
|
51
|
+ public void onViewAttachedToWindow(View v) {
|
|
52
|
+ scrollView = getScrollView((ViewGroup) getParent());
|
|
53
|
+
|
|
54
|
+ if (scrollView != null && !isScrollEventListenerRegistered) {
|
|
55
|
+ addScrollListener();
|
|
56
|
+ }
|
|
57
|
+ }
|
|
58
|
+
|
|
59
|
+ @Override
|
|
60
|
+ public void onViewDetachedFromWindow(final View detachedView) {
|
|
61
|
+ removeScrollListener();
|
|
62
|
+
|
|
63
|
+ post(new Runnable() {
|
|
64
|
+ @Override
|
|
65
|
+ public void run() {
|
|
66
|
+ scrollView = getScrollView((ViewGroup) getParent());
|
|
67
|
+ if (scrollView != null && !isScrollEventListenerRegistered) {
|
|
68
|
+ isScrollEventListenerRegistered = true;
|
|
69
|
+ addScrollListener();
|
|
70
|
+ }
|
|
71
|
+ }
|
|
72
|
+ });
|
|
73
|
+ }
|
|
74
|
+ };
|
20
|
75
|
|
21
|
76
|
/**
|
22
|
77
|
* Interface used to run some code when the {@link ReactRootView} is visible.
|
|
@@ -29,15 +84,33 @@ public class RctView extends FrameLayout {
|
29
|
84
|
}
|
30
|
85
|
|
31
|
86
|
@SuppressWarnings("unchecked")
|
32
|
|
- public RctView(BaseReactActivity ctx, ReactInstanceManager rctInstanceManager, Screen screen,
|
|
87
|
+ public RctView(BaseReactActivity ctx, ReactInstanceManager rctInstanceManager, final Screen screen,
|
33
|
88
|
final OnDisplayedListener onDisplayedListener) {
|
34
|
89
|
super(ctx);
|
35
|
90
|
setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
|
36
|
91
|
|
37
|
|
- mReactRootView = new ReactRootView(ctx);
|
38
|
|
- mReactRootView.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
|
|
92
|
+ final OnDisplayedListener onDisplayedListenerInternal = screen.bottomTabsHiddenOnScroll ?
|
|
93
|
+ new OnDisplayedListener() {
|
|
94
|
+ @Override
|
|
95
|
+ public void onDisplayed() {
|
|
96
|
+ if (onDisplayedListener != null) {
|
|
97
|
+ onDisplayedListener.onDisplayed();
|
|
98
|
+ }
|
|
99
|
+
|
|
100
|
+ setupScrollViewWithBottomTabs();
|
|
101
|
+ }
|
|
102
|
+ } : onDisplayedListener;
|
39
|
103
|
|
|
104
|
+ reactRootView = new RnnReactRootView(ctx, onDisplayedListenerInternal);
|
|
105
|
+ reactRootView.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
|
|
106
|
+ Bundle passProps = createPassProps(screen);
|
40
|
107
|
String componentName = screen.screenId;
|
|
108
|
+ reactRootView.startReactApplication(rctInstanceManager, componentName, passProps);
|
|
109
|
+
|
|
110
|
+ addView(reactRootView);
|
|
111
|
+ }
|
|
112
|
+
|
|
113
|
+ private Bundle createPassProps(Screen screen) {
|
41
|
114
|
Bundle passProps = new Bundle();
|
42
|
115
|
passProps.putString(Screen.KEY_SCREEN_INSTANCE_ID, screen.screenInstanceId);
|
43
|
116
|
passProps.putString(Screen.KEY_NAVIGATOR_ID, screen.navigatorId);
|
|
@@ -45,20 +118,44 @@ public class RctView extends FrameLayout {
|
45
|
118
|
if (screen.passedProps != null) {
|
46
|
119
|
BridgeUtils.addMapToBundle(screen.passedProps, passProps);
|
47
|
120
|
}
|
|
121
|
+ return passProps;
|
|
122
|
+ }
|
48
|
123
|
|
49
|
|
- mReactRootView.startReactApplication(rctInstanceManager, componentName, passProps);
|
|
124
|
+ private void setupScrollViewWithBottomTabs() {
|
|
125
|
+ scrollView = getScrollView((ViewGroup) getParent());
|
|
126
|
+ if (scrollView != null) {
|
|
127
|
+ context = (BottomTabActivity) getContext();
|
|
128
|
+ attachStateChangeListener(scrollView);
|
|
129
|
+ addScrollListener();
|
|
130
|
+ }
|
|
131
|
+ }
|
50
|
132
|
|
51
|
|
- if (onDisplayedListener != null) {
|
52
|
|
- mReactRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
|
53
|
|
- @Override
|
54
|
|
- public void onGlobalLayout() {
|
55
|
|
- onDisplayedListener.onDisplayed();
|
56
|
|
- mReactRootView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
|
57
|
|
- }
|
58
|
|
- });
|
|
133
|
+ private ScrollView getScrollView(ViewGroup parent) {
|
|
134
|
+ for (int i = 0; i < parent.getChildCount(); i++) {
|
|
135
|
+ View child = parent.getChildAt(i);
|
|
136
|
+
|
|
137
|
+ if (child instanceof ScrollView) {
|
|
138
|
+ return (ScrollView) child;
|
|
139
|
+ }
|
|
140
|
+
|
|
141
|
+ if (child instanceof ViewGroup) {
|
|
142
|
+ return getScrollView((ViewGroup) child);
|
|
143
|
+ }
|
59
|
144
|
}
|
60
|
145
|
|
61
|
|
- addView(mReactRootView);
|
|
146
|
+ return null;
|
|
147
|
+ }
|
|
148
|
+
|
|
149
|
+ private void attachStateChangeListener(ScrollView scrollView) {
|
|
150
|
+ scrollView.addOnAttachStateChangeListener(stateChangeListener);
|
|
151
|
+ }
|
|
152
|
+
|
|
153
|
+ private void addScrollListener() {
|
|
154
|
+ scrollView.getViewTreeObserver().addOnScrollChangedListener(scrollChangedListener);
|
|
155
|
+ }
|
|
156
|
+
|
|
157
|
+ private void removeScrollListener() {
|
|
158
|
+ scrollView.getViewTreeObserver().removeOnScrollChangedListener(scrollChangedListener);
|
62
|
159
|
}
|
63
|
160
|
|
64
|
161
|
/**
|
|
@@ -67,7 +164,8 @@ public class RctView extends FrameLayout {
|
67
|
164
|
*/
|
68
|
165
|
public void onTemporallyRemovedFromScreen() {
|
69
|
166
|
// Hack in order to prevent the react view from getting unmounted
|
70
|
|
- ReflectionUtils.setField(mReactRootView, "mAttachScheduled", true);
|
|
167
|
+
|
|
168
|
+ ReflectionUtils.setField(reactRootView, "mAttachScheduled", true);
|
71
|
169
|
}
|
72
|
170
|
|
73
|
171
|
/**
|
|
@@ -75,7 +173,7 @@ public class RctView extends FrameLayout {
|
75
|
173
|
* executed and componentWillUnmount is called
|
76
|
174
|
*/
|
77
|
175
|
public void onRemoveFromScreen() {
|
78
|
|
- ReflectionUtils.setField(mReactRootView, "mAttachScheduled", false);
|
|
176
|
+ ReflectionUtils.setField(reactRootView, "mAttachScheduled", false);
|
79
|
177
|
}
|
80
|
178
|
|
81
|
179
|
/**
|
|
@@ -83,11 +181,11 @@ public class RctView extends FrameLayout {
|
83
|
181
|
* executed and componentWillUnmount is called
|
84
|
182
|
*/
|
85
|
183
|
public void onReAddToScreen() {
|
86
|
|
- ReflectionUtils.setField(mReactRootView, "mAttachScheduled", false);
|
|
184
|
+ ReflectionUtils.setField(reactRootView, "mAttachScheduled", false);
|
87
|
185
|
}
|
88
|
186
|
|
89
|
187
|
public void detachFromScreen() {
|
90
|
|
- ReflectionUtils.invoke(mReactRootView, "onDetachedFromWindow");
|
|
188
|
+ ReflectionUtils.invoke(reactRootView, "onDetachedFromWindow");
|
91
|
189
|
}
|
92
|
190
|
}
|
93
|
191
|
|