mirror of
https://github.com/zhigang1992/react-navigation.git
synced 2026-01-23 12:28:23 +08:00
Compare commits
98 Commits
2.0.0-alph
...
2.0.0-beta
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fd7fcfbc5b | ||
|
|
06b928f5c1 | ||
|
|
acb038e213 | ||
|
|
22d4400b93 | ||
|
|
f78c264419 | ||
|
|
65bd436cbe | ||
|
|
a0745762ba | ||
|
|
89658d9361 | ||
|
|
074aabc33e | ||
|
|
eb2a1ae8fd | ||
|
|
9721353e7b | ||
|
|
523b3d8f3a | ||
|
|
f851c26642 | ||
|
|
f8f5d66bf9 | ||
|
|
72037e20fb | ||
|
|
69a23f1c9f | ||
|
|
d32463ee83 | ||
|
|
2da04f37e6 | ||
|
|
623f9452cb | ||
|
|
c646a4e7ba | ||
|
|
a1311cd31d | ||
|
|
f044334464 | ||
|
|
1c7ebeba39 | ||
|
|
1493d6f1b8 | ||
|
|
8cf82d1cbe | ||
|
|
e8403582ae | ||
|
|
5832593980 | ||
|
|
78c7745049 | ||
|
|
350a80c29b | ||
|
|
c460341a68 | ||
|
|
3fc74e29ab | ||
|
|
518c094657 | ||
|
|
d71aa2c6ef | ||
|
|
f21ec66cb4 | ||
|
|
27ef6dc900 | ||
|
|
ce819f6356 | ||
|
|
1ac742610b | ||
|
|
28f57240c2 | ||
|
|
ca6319d26e | ||
|
|
2b1b726723 | ||
|
|
4ce9469571 | ||
|
|
cef9b8f393 | ||
|
|
ecc4056559 | ||
|
|
71a02af309 | ||
|
|
c88da09348 | ||
|
|
9aac0b89f2 | ||
|
|
a02dd46eec | ||
|
|
0919404b2e | ||
|
|
e61b8b3bd6 | ||
|
|
77d877f0c1 | ||
|
|
15c27e3fe7 | ||
|
|
ed478a829d | ||
|
|
a3335b1384 | ||
|
|
e00a08a3dc | ||
|
|
656e82de9f | ||
|
|
1958cf37ea | ||
|
|
75fb558cd3 | ||
|
|
2b55d60780 | ||
|
|
744b37fbc3 | ||
|
|
84d684b52d | ||
|
|
6f9c504627 | ||
|
|
953763f7d9 | ||
|
|
7c351df14d | ||
|
|
da9426b4b9 | ||
|
|
4169fad8b3 | ||
|
|
4f8efd2873 | ||
|
|
cbc86bb6d8 | ||
|
|
0927e03687 | ||
|
|
efaf0cd125 | ||
|
|
21e6a9732a | ||
|
|
31192250e1 | ||
|
|
124e8acb2d | ||
|
|
2c5f95cea6 | ||
|
|
0a2336d005 | ||
|
|
58d1791d4a | ||
|
|
3d56c5d4e2 | ||
|
|
47658d4d7d | ||
|
|
258ae419de | ||
|
|
62123f16f9 | ||
|
|
a94424192b | ||
|
|
752d6c0f04 | ||
|
|
a017713efc | ||
|
|
26384b625e | ||
|
|
5d5e8bfca6 | ||
|
|
2536837795 | ||
|
|
20650a8ede | ||
|
|
ee0dbfe8ae | ||
|
|
23cbc009d4 | ||
|
|
f21a093918 | ||
|
|
1d4712acbd | ||
|
|
09c71a45a2 | ||
|
|
79e664f11d | ||
|
|
b622abc935 | ||
|
|
7d4bbb8f88 | ||
|
|
7c304a007f | ||
|
|
adf3333462 | ||
|
|
d4636d3130 | ||
|
|
4749405d64 |
@@ -219,7 +219,7 @@ PODS:
|
||||
- ReactCommon/jscallinvoker (= 0.61.2)
|
||||
- RNGestureHandler (1.3.0):
|
||||
- React
|
||||
- RNScreens (2.0.0-alpha.3):
|
||||
- RNScreens (2.0.0-alpha.23):
|
||||
- React
|
||||
- Yoga (1.14.0)
|
||||
|
||||
@@ -256,7 +256,7 @@ DEPENDENCIES:
|
||||
- Yoga (from `../node_modules/react-native/ReactCommon/yoga`)
|
||||
|
||||
SPEC REPOS:
|
||||
https://github.com/cocoapods/specs.git:
|
||||
https://github.com/CocoaPods/Specs.git:
|
||||
- boost-for-react-native
|
||||
|
||||
EXTERNAL SOURCES:
|
||||
@@ -342,9 +342,9 @@ SPEC CHECKSUMS:
|
||||
React-RCTVibration: fb54c732fd20405a76598e431aa2f8c2bf527de9
|
||||
ReactCommon: 5848032ed2f274fcb40f6b9ec24067787c42d479
|
||||
RNGestureHandler: 5329a942fce3d41c68b84c2c2276ce06a696d8b0
|
||||
RNScreens: a32a406ec4884f0ba383ef835332e8379f75e3da
|
||||
RNScreens: 55c735f525774e894be67848c250c95a9c3194c0
|
||||
Yoga: 14927e37bd25376d216b150ab2a561773d57911f
|
||||
|
||||
PODFILE CHECKSUM: 1a141b811c7076eb11c48f2e22336181f52531b5
|
||||
|
||||
COCOAPODS: 1.7.3
|
||||
COCOAPODS: 1.8.4
|
||||
|
||||
@@ -1633,7 +1633,7 @@ combined-stream@~1.0.6:
|
||||
dependencies:
|
||||
delayed-stream "~1.0.0"
|
||||
|
||||
commander@^2.19.0, commander@~2.20.0:
|
||||
commander@^2.19.0:
|
||||
version "2.20.0"
|
||||
resolved "https://registry.yarnpkg.com/commander/-/commander-2.20.0.tgz#d58bb2b5c1ee8f87b0d340027e9e94e222c5a422"
|
||||
integrity sha512-7j2y+40w61zy6YC2iRNpUe/NwhNyoXrYpHMrSunaMG64nRnaf96zO/KMQR4OyN/UnE5KLyEBnKHd4aG3rskjpQ==
|
||||
@@ -1642,6 +1642,11 @@ commander@~2.13.0:
|
||||
version "2.13.0"
|
||||
resolved "https://registry.yarnpkg.com/commander/-/commander-2.13.0.tgz#6964bca67685df7c1f1430c584f07d7597885b9c"
|
||||
|
||||
commander@~2.20.3:
|
||||
version "2.20.3"
|
||||
resolved "https://registry.yarnpkg.com/commander/-/commander-2.20.3.tgz#fd485e84c03eb4881c20722ba48035e8531aeb33"
|
||||
integrity sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==
|
||||
|
||||
commondir@^1.0.1:
|
||||
version "1.0.1"
|
||||
resolved "https://registry.yarnpkg.com/commondir/-/commondir-1.0.1.tgz#ddd800da0c66127393cca5950ea968a3aaf1253b"
|
||||
@@ -2487,9 +2492,9 @@ growly@^1.3.0:
|
||||
resolved "https://registry.yarnpkg.com/growly/-/growly-1.3.0.tgz#f10748cbe76af964b7c96c93c6bcc28af120c081"
|
||||
|
||||
handlebars@^4.0.3:
|
||||
version "4.2.0"
|
||||
resolved "https://registry.yarnpkg.com/handlebars/-/handlebars-4.2.0.tgz#57ce8d2175b9bbb3d8b3cf3e4217b1aec8ddcb2e"
|
||||
integrity sha512-Kb4xn5Qh1cxAKvQnzNWZ512DhABzyFNmsaJf3OAkWNa4NkaqWcNI8Tao8Tasi0/F4JD9oyG0YxuFyvyR57d+Gw==
|
||||
version "4.5.3"
|
||||
resolved "https://registry.yarnpkg.com/handlebars/-/handlebars-4.5.3.tgz#5cf75bd8714f7605713511a56be7c349becb0482"
|
||||
integrity sha512-3yPecJoJHK/4c6aZhSvxOyG4vJKDshV36VHp0iVCDVh7o9w2vwi3NSnL2MMPj3YdduqaBcu7cGbggJQM0br9xA==
|
||||
dependencies:
|
||||
neo-async "^2.6.0"
|
||||
optimist "^0.6.1"
|
||||
@@ -4806,7 +4811,7 @@ react-native-safe-area-view@^0.14.1:
|
||||
debounce "^1.2.0"
|
||||
|
||||
"react-native-screens@file:..":
|
||||
version "2.0.0-alpha.3"
|
||||
version "2.0.0-alpha.22"
|
||||
dependencies:
|
||||
debounce "^1.2.0"
|
||||
|
||||
@@ -5761,11 +5766,11 @@ uglify-es@^3.1.9:
|
||||
source-map "~0.6.1"
|
||||
|
||||
uglify-js@^3.1.4:
|
||||
version "3.6.0"
|
||||
resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-3.6.0.tgz#704681345c53a8b2079fb6cec294b05ead242ff5"
|
||||
integrity sha512-W+jrUHJr3DXKhrsS7NUVxn3zqMOFn0hL/Ei6v0anCIMoKC93TjcflTagwIHLW7SfMFfiQuktQyFVCFHGUE0+yg==
|
||||
version "3.7.3"
|
||||
resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-3.7.3.tgz#f918fce9182f466d5140f24bb0ff35c2d32dcc6a"
|
||||
integrity sha512-7tINm46/3puUA4hCkKYo4Xdts+JDaVC9ZPRcG8Xw9R4nhO/gZgUM3TENq8IF4Vatk8qCig4MzP/c8G4u2BkVQg==
|
||||
dependencies:
|
||||
commander "~2.20.0"
|
||||
commander "~2.20.3"
|
||||
source-map "~0.6.1"
|
||||
|
||||
ultron@1.0.x:
|
||||
|
||||
22
README.md
22
README.md
@@ -100,12 +100,12 @@ To do that react-native-screens provides you with two components documented belo
|
||||
### `<ScreenContainer/>`
|
||||
|
||||
This component is a container for one or more `Screen` components.
|
||||
It does not accept other component types are direct children.
|
||||
The role of container is to control which of its children screens should be attached to the view hierarchy.
|
||||
It does that by monitoring `active` property of each of its children.
|
||||
It it possible to have as many `active` children as you'd like but in order for the component to be the most efficient we should keep the number of active screens to the minimum.
|
||||
In a case of stack navigator or tabs navigator we only want to have one active screen (the top most view on a stack or the selected tab).
|
||||
Then for the time of transitioning between views we may want to activate a second screen for the duration of transition, and then go back to just one active screen.
|
||||
It does not accept other component types as direct children.
|
||||
The role of the container is to control which of its children screens should be attached to the view hierarchy.
|
||||
It does that by monitoring the `active` property of each of its children.
|
||||
It is possible to have as many `active` children as you'd like but in order for the component to be the most efficient we should keep the number of active screens to a minimum.
|
||||
In a case of a stack navigator or tabs navigator we only want to have one active screen (the top most view on a stack or the selected tab).
|
||||
While transitioning between views we may want to activate a second screen for the duration of the transition, and then go back to just one active screen.
|
||||
|
||||
### `<Screen/>`
|
||||
|
||||
@@ -127,11 +127,11 @@ Otherwise the views will be attached as long as the parent container is attached
|
||||
|
||||
### `<ScreenStack>`
|
||||
|
||||
Screen stack component expects one or more `Screen` components as direct children and renders them in a platform native stack container (for iOS it is `UINavigationController` and for Android inside `Fragment` container). For `Screen` components placed as children of `ScteenStack` the `active` property is ignored and instead the screen that corresponds to the last child is rendered as active. All type of updates done to the list of children are acceptable, when the top element is exchanged the container will use platform default (unless customized) animation to transition between screens.
|
||||
Screen stack component expects one or more `Screen` components as direct children and renders them in a platform native stack container (for iOS it is `UINavigationController` and for Android inside `Fragment` container). For `Screen` components placed as children of `ScreenStack` the `active` property is ignored and instead the screen that corresponds to the last child is rendered as active. All type of updates done to the list of children are acceptable, when the top element is exchanged the container will use platform default (unless customized) animation to transition between screens.
|
||||
|
||||
`StackScreen` extends the capabilities of `Screen` component to allow additional customizations and to make it possible to handle events such as using hardware back or back gesture to dismiss the top screen. Below is the list of additional properties that can be used for `Screen` component:
|
||||
|
||||
#### `onDismiss`
|
||||
#### `onDismissed`
|
||||
|
||||
A callback that gets called when the current screen is dismissed by hardware back (on Android) or dismiss gesture (swipe back or down). The callback takes no arguments.
|
||||
|
||||
@@ -140,6 +140,7 @@ A callback that gets called when the current screen is dismissed by hardware bac
|
||||
Allows for the customization of how the given screen should appear/dissapear when pushed or popped at the top of the stack. The followin values are currently supported:
|
||||
- `"default"` – uses a platform default animation
|
||||
- `"fade"` – fades screen in or out
|
||||
- `"flip"` – flips the screen, requires `stackPresentation: "modal"` (iOS only)
|
||||
- `"none"` – the screen appears/dissapears without an animation
|
||||
|
||||
#### `stackPresentation`
|
||||
@@ -154,9 +155,8 @@ Defines how the method that should be used to present the given screen. It is a
|
||||
The config component is expected to be rendered as a direct children of `<Screen>`. It provides an ability to configure native navigation header that gets rendered as a part of native screen stack. The component acts as a "virtual" element that is not directly rendered under `Screen`. You can use its properties to customize platform native header for the parent screen and also render react-native components that you'd like to be displayed inside the header (e.g. in the title are or on the side).
|
||||
|
||||
Along with this component properties that can be used to customize header behavior one can also use one or the below component containers to render custom react-native content in different areas of the native header:
|
||||
- `ScreenStackHeaderTitleView` – react native views rendered as children will appear on the navigation bar in the place of title. Note that title is aligned next to back button on Android while it is centered on iOS.
|
||||
- `ScreenStackHeaderCenterView` – the childern will render in the center of the native navigation bar (on iOS this has the same behavior as `ScreenStackHeaderTitleView` container)
|
||||
- `ScreenStackHeaderRightView` – the children will render on the right hand side of the navigation bar (or on the left hand side in case LTR locales are set on the user's device)
|
||||
- `ScreenStackHeaderCenterView` – the childern will render in the center of the native navigation bar.
|
||||
- `ScreenStackHeaderRightView` – the children will render on the right hand side of the navigation bar (or on the left hand side in case LTR locales are set on the user's device).
|
||||
- `ScreenStackHeaderLeftView` – the children will render on the left hand side of the navigation bar (or on the right hand side in case LTR locales are set on the user's device).
|
||||
|
||||
Below is a list of properties that can be set with `ScreenStackHeaderConfig` component:
|
||||
|
||||
@@ -91,12 +91,12 @@ afterEvaluate { project ->
|
||||
}
|
||||
|
||||
task androidJavadocJar(type: Jar, dependsOn: androidJavadoc) {
|
||||
archiveClassifier.set('javadoc')
|
||||
classifier = 'javadoc'
|
||||
from androidJavadoc.destinationDir
|
||||
}
|
||||
|
||||
task androidSourcesJar(type: Jar) {
|
||||
archiveClassifier.set('sources')
|
||||
classifier = 'sources'
|
||||
from android.sourceSets.main.java.srcDirs
|
||||
include '**/*.java'
|
||||
}
|
||||
|
||||
@@ -17,5 +17,6 @@ org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryErro
|
||||
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
|
||||
# org.gradle.parallel=true
|
||||
|
||||
android.useDeprecatedNdk=true
|
||||
android.useAndroidX=true
|
||||
android.enableJetifier=true
|
||||
|
||||
@@ -2,7 +2,6 @@ package com.swmansion.rnscreens;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Paint;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.view.inputmethod.InputMethodManager;
|
||||
@@ -13,12 +12,11 @@ import androidx.fragment.app.Fragment;
|
||||
|
||||
import com.facebook.react.bridge.GuardedRunnable;
|
||||
import com.facebook.react.bridge.ReactContext;
|
||||
import com.facebook.react.uimanager.MeasureSpecAssertions;
|
||||
import com.facebook.react.uimanager.PointerEvents;
|
||||
import com.facebook.react.uimanager.ReactPointerEventsView;
|
||||
import com.facebook.react.uimanager.UIManagerModule;
|
||||
|
||||
public class Screen extends ViewGroup implements ReactPointerEventsView {
|
||||
public class Screen extends ViewGroup {
|
||||
|
||||
public enum StackPresentation {
|
||||
PUSH,
|
||||
@@ -54,6 +52,7 @@ public class Screen extends ViewGroup implements ReactPointerEventsView {
|
||||
private boolean mTransitioning;
|
||||
private StackPresentation mStackPresentation = StackPresentation.PUSH;
|
||||
private StackAnimation mStackAnimation = StackAnimation.DEFAULT;
|
||||
private boolean mGestureEnabled = true;
|
||||
|
||||
public Screen(ReactContext context) {
|
||||
super(context);
|
||||
@@ -124,6 +123,10 @@ public class Screen extends ViewGroup implements ReactPointerEventsView {
|
||||
mStackAnimation = stackAnimation;
|
||||
}
|
||||
|
||||
public void setGestureEnabled(boolean gestureEnabled) {
|
||||
mGestureEnabled = gestureEnabled;
|
||||
}
|
||||
|
||||
public StackAnimation getStackAnimation() {
|
||||
return mStackAnimation;
|
||||
}
|
||||
@@ -132,11 +135,6 @@ public class Screen extends ViewGroup implements ReactPointerEventsView {
|
||||
return mStackPresentation;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PointerEvents getPointerEvents() {
|
||||
return mTransitioning ? PointerEvents.NONE : PointerEvents.AUTO;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setLayerType(int layerType, @Nullable Paint paint) {
|
||||
// ignore - layer type is controlled by `transitioning` prop
|
||||
@@ -171,4 +169,8 @@ public class Screen extends ViewGroup implements ReactPointerEventsView {
|
||||
public boolean isActive() {
|
||||
return mActive;
|
||||
}
|
||||
|
||||
public boolean isGestureEnabled() {
|
||||
return mGestureEnabled;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.swmansion.rnscreens;
|
||||
|
||||
import com.facebook.react.bridge.Arguments;
|
||||
import com.facebook.react.uimanager.events.Event;
|
||||
import com.facebook.react.uimanager.events.RCTEventEmitter;
|
||||
|
||||
public class ScreenAppearEvent extends Event<ScreenAppearEvent> {
|
||||
|
||||
public static final String EVENT_NAME = "topAppear";
|
||||
|
||||
public ScreenAppearEvent(int viewId) {
|
||||
super(viewId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getEventName() {
|
||||
return EVENT_NAME;
|
||||
}
|
||||
|
||||
@Override
|
||||
public short getCoalescingKey() {
|
||||
// All events for a given view can be coalesced.
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispatch(RCTEventEmitter rctEventEmitter) {
|
||||
rctEventEmitter.receiveEvent(getViewTag(), getEventName(), Arguments.createMap());
|
||||
}
|
||||
}
|
||||
@@ -2,11 +2,13 @@ package com.swmansion.rnscreens;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.ContextWrapper;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.view.ViewParent;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.fragment.app.FragmentActivity;
|
||||
import androidx.fragment.app.FragmentManager;
|
||||
import androidx.fragment.app.FragmentTransaction;
|
||||
|
||||
import com.facebook.react.ReactRootView;
|
||||
@@ -21,25 +23,58 @@ public class ScreenContainer<T extends ScreenFragment> extends ViewGroup {
|
||||
|
||||
protected final ArrayList<T> mScreenFragments = new ArrayList<>();
|
||||
private final Set<ScreenFragment> mActiveScreenFragments = new HashSet<>();
|
||||
private final ArrayList<Runnable> mAfterTransitionRunnables = new ArrayList<>(1);
|
||||
|
||||
protected @Nullable FragmentManager mFragmentManager;
|
||||
private @Nullable FragmentTransaction mCurrentTransaction;
|
||||
private @Nullable FragmentTransaction mProcessingTransaction;
|
||||
private boolean mNeedUpdate;
|
||||
private boolean mIsAttached;
|
||||
private boolean mIsTransitioning;
|
||||
private boolean mLayoutEnqueued = false;
|
||||
|
||||
private ChoreographerCompat.FrameCallback mFrameCallback = new ChoreographerCompat.FrameCallback() {
|
||||
|
||||
private final ChoreographerCompat.FrameCallback mFrameCallback = new ChoreographerCompat.FrameCallback() {
|
||||
@Override
|
||||
public void doFrame(long frameTimeNanos) {
|
||||
updateIfNeeded();
|
||||
}
|
||||
};
|
||||
|
||||
private final ChoreographerCompat.FrameCallback mLayoutCallback = new ChoreographerCompat.FrameCallback() {
|
||||
@Override
|
||||
public void doFrame(long frameTimeNanos) {
|
||||
mLayoutEnqueued = false;
|
||||
measure(
|
||||
MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.EXACTLY),
|
||||
MeasureSpec.makeMeasureSpec(getHeight(), MeasureSpec.EXACTLY));
|
||||
layout(getLeft(), getTop(), getRight(), getBottom());
|
||||
}
|
||||
};
|
||||
|
||||
public ScreenContainer(Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
|
||||
// no-op
|
||||
protected void onLayout(boolean changed, int l, int t, int r, int b) {
|
||||
for (int i = 0, size = getChildCount(); i < size; i++) {
|
||||
getChildAt(i).layout(0, 0, getWidth(), getHeight());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void requestLayout() {
|
||||
super.requestLayout();
|
||||
|
||||
if (!mLayoutEnqueued && mLayoutCallback != null) {
|
||||
mLayoutEnqueued = true;
|
||||
// we use NATIVE_ANIMATED_MODULE choreographer queue because it allows us to catch the current
|
||||
// looper loop instead of enqueueing the update in the next loop causing a one frame delay.
|
||||
ReactChoreographer.getInstance().postFrameCallback(
|
||||
ReactChoreographer.CallbackType.NATIVE_ANIMATED_MODULE,
|
||||
mLayoutCallback);
|
||||
}
|
||||
}
|
||||
|
||||
protected void markUpdated() {
|
||||
@@ -75,6 +110,44 @@ public class ScreenContainer<T extends ScreenFragment> extends ViewGroup {
|
||||
markUpdated();
|
||||
}
|
||||
|
||||
protected void removeAllScreens() {
|
||||
for (int i = 0, size = mScreenFragments.size(); i < size; i++) {
|
||||
mScreenFragments.get(i).getScreen().setContainer(null);
|
||||
}
|
||||
mScreenFragments.clear();
|
||||
markUpdated();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void startViewTransition(View view) {
|
||||
super.startViewTransition(view);
|
||||
mIsTransitioning = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endViewTransition(View view) {
|
||||
super.endViewTransition(view);
|
||||
if (mIsTransitioning) {
|
||||
mIsTransitioning = false;
|
||||
notifyTransitionFinished();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isTransitioning() {
|
||||
return mIsTransitioning || mProcessingTransaction != null;
|
||||
}
|
||||
|
||||
public void postAfterTransition(Runnable runnable) {
|
||||
mAfterTransitionRunnables.add(runnable);
|
||||
}
|
||||
|
||||
protected void notifyTransitionFinished() {
|
||||
for (int i = 0, size = mAfterTransitionRunnables.size(); i < size; i++) {
|
||||
mAfterTransitionRunnables.get(i).run();
|
||||
}
|
||||
mAfterTransitionRunnables.clear();
|
||||
}
|
||||
|
||||
protected int getScreenCount() {
|
||||
return mScreenFragments.size();
|
||||
}
|
||||
@@ -83,11 +156,18 @@ public class ScreenContainer<T extends ScreenFragment> extends ViewGroup {
|
||||
return mScreenFragments.get(index).getScreen();
|
||||
}
|
||||
|
||||
protected final FragmentActivity findRootFragmentActivity() {
|
||||
private FragmentManager findFragmentManager() {
|
||||
ViewParent parent = this;
|
||||
while (!(parent instanceof ReactRootView) && parent.getParent() != null) {
|
||||
// We traverse view hierarchy up until we find screen parent or a root view
|
||||
while (!(parent instanceof ReactRootView || parent instanceof Screen) && parent.getParent() != null) {
|
||||
parent = parent.getParent();
|
||||
}
|
||||
// If parent is of type Screen it means we are inside a nested fragment structure.
|
||||
// Otherwise we expect to connect directly with root view and get root fragment manager
|
||||
if (parent instanceof Screen) {
|
||||
return ((Screen) parent).getFragment().getChildFragmentManager();
|
||||
}
|
||||
|
||||
// we expect top level view to be of type ReactRootView, this isn't really necessary but in order
|
||||
// to find root view we test if parent is null. This could potentially happen also when the view
|
||||
// is detached from the hierarchy and that test would not correctly indicate the root view. So
|
||||
@@ -106,12 +186,12 @@ public class ScreenContainer<T extends ScreenFragment> extends ViewGroup {
|
||||
throw new IllegalStateException(
|
||||
"In order to use RNScreens components your app's activity need to extend ReactFragmentActivity or ReactCompatActivity");
|
||||
}
|
||||
return (FragmentActivity) context;
|
||||
return ((FragmentActivity) context).getSupportFragmentManager();
|
||||
}
|
||||
|
||||
protected FragmentTransaction getOrCreateTransaction() {
|
||||
if (mCurrentTransaction == null) {
|
||||
mCurrentTransaction = findRootFragmentActivity().getSupportFragmentManager().beginTransaction();
|
||||
mCurrentTransaction = mFragmentManager.beginTransaction();
|
||||
mCurrentTransaction.setReorderingAllowed(true);
|
||||
}
|
||||
return mCurrentTransaction;
|
||||
@@ -119,6 +199,19 @@ public class ScreenContainer<T extends ScreenFragment> extends ViewGroup {
|
||||
|
||||
protected void tryCommitTransaction() {
|
||||
if (mCurrentTransaction != null) {
|
||||
final FragmentTransaction transaction = mCurrentTransaction;
|
||||
mProcessingTransaction = transaction;
|
||||
mProcessingTransaction.runOnCommit(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (mProcessingTransaction == transaction) {
|
||||
// we need to take into account that commit is initiated with some other transaction while
|
||||
// the previous one is still processing. In this case mProcessingTransaction gets overwritten
|
||||
// and we don't want to set it to null until the second transaction is finished.
|
||||
mProcessingTransaction = null;
|
||||
}
|
||||
}
|
||||
});
|
||||
mCurrentTransaction.commitAllowingStateLoss();
|
||||
mCurrentTransaction = null;
|
||||
}
|
||||
@@ -144,10 +237,16 @@ public class ScreenContainer<T extends ScreenFragment> extends ViewGroup {
|
||||
return screenFragment.getScreen().isActive();
|
||||
}
|
||||
|
||||
protected boolean hasScreen(ScreenFragment screenFragment) {
|
||||
return mScreenFragments.contains(screenFragment);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onAttachedToWindow() {
|
||||
super.onAttachedToWindow();
|
||||
mIsAttached = true;
|
||||
mNeedUpdate = true;
|
||||
mFragmentManager = findFragmentManager();
|
||||
updateIfNeeded();
|
||||
}
|
||||
|
||||
@@ -155,6 +254,22 @@ public class ScreenContainer<T extends ScreenFragment> extends ViewGroup {
|
||||
protected void onDetachedFromWindow() {
|
||||
super.onDetachedFromWindow();
|
||||
mIsAttached = false;
|
||||
|
||||
// fragment manager is destroyed so we can't do anything with it anymore
|
||||
mFragmentManager = null;
|
||||
// so we don't add the same screen twice after re-attach
|
||||
removeAllViews();
|
||||
mActiveScreenFragments.clear();
|
||||
// after re-attach we'll update the screen and add views again
|
||||
markUpdated();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
||||
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
|
||||
for (int i = 0, size = getChildCount(); i < size; i++) {
|
||||
getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);
|
||||
}
|
||||
}
|
||||
|
||||
private void updateIfNeeded() {
|
||||
|
||||
@@ -34,6 +34,11 @@ public class ScreenContainerViewManager extends ViewGroupManager<ScreenContainer
|
||||
parent.removeScreenAt(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeAllViews(ScreenContainer parent) {
|
||||
parent.removeAllScreens();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getChildCount(ScreenContainer parent) {
|
||||
return parent.getScreenCount();
|
||||
@@ -43,4 +48,9 @@ public class ScreenContainerViewManager extends ViewGroupManager<ScreenContainer
|
||||
public View getChildAt(ScreenContainer parent, int index) {
|
||||
return parent.getScreenAt(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean needsCustomLayoutForChildren() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,12 +7,10 @@ import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.widget.Toolbar;
|
||||
import androidx.fragment.app.Fragment;
|
||||
|
||||
import com.facebook.react.bridge.ReactContext;
|
||||
import com.facebook.react.uimanager.UIManagerModule;
|
||||
import com.facebook.react.uimanager.events.EventDispatcher;
|
||||
|
||||
public class ScreenFragment extends Fragment {
|
||||
|
||||
@@ -39,12 +37,39 @@ public class ScreenFragment extends Fragment {
|
||||
return mScreenView;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
private void dispatchOnAppear() {
|
||||
((ReactContext) mScreenView.getContext())
|
||||
.getNativeModule(UIManagerModule.class)
|
||||
.getEventDispatcher()
|
||||
.dispatchEvent(new ScreenDismissedEvent(mScreenView.getId()));
|
||||
.dispatchEvent(new ScreenAppearEvent(mScreenView.getId()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
ScreenContainer container = mScreenView.getContainer();
|
||||
if (container.isTransitioning()) {
|
||||
container.postAfterTransition(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
dispatchOnAppear();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
dispatchOnAppear();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
ScreenContainer container = mScreenView.getContainer();
|
||||
if (container == null || !container.hasScreen(this)) {
|
||||
// we only send dismissed even when the screen has been removed from its container
|
||||
((ReactContext) mScreenView.getContext())
|
||||
.getNativeModule(UIManagerModule.class)
|
||||
.getEventDispatcher()
|
||||
.dispatchEvent(new ScreenDismissedEvent(mScreenView.getId()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@ package com.swmansion.rnscreens;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.fragment.app.FragmentManager;
|
||||
import androidx.fragment.app.FragmentTransaction;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -10,11 +12,33 @@ import java.util.Set;
|
||||
|
||||
public class ScreenStack extends ScreenContainer<ScreenStackFragment> {
|
||||
|
||||
private static final String BACK_STACK_TAG = "RN_SCREEN_LAST";
|
||||
|
||||
private final ArrayList<ScreenStackFragment> mStack = new ArrayList<>();
|
||||
private final Set<ScreenStackFragment> mDismissed = new HashSet<>();
|
||||
|
||||
private ScreenStackFragment mTopScreen = null;
|
||||
private boolean mLayoutEnqueued = false;
|
||||
|
||||
private final FragmentManager.OnBackStackChangedListener mBackStackListener = new FragmentManager.OnBackStackChangedListener() {
|
||||
@Override
|
||||
public void onBackStackChanged() {
|
||||
if (mFragmentManager.getBackStackEntryCount() == 0) {
|
||||
// when back stack entry count hits 0 it means the user's navigated back using hw back
|
||||
// button. As the "fake" transaction we installed on the back stack does nothing we need
|
||||
// to handle back navigation on our own.
|
||||
dismiss(mTopScreen);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private final FragmentManager.FragmentLifecycleCallbacks mLifecycleCallbacks = new FragmentManager.FragmentLifecycleCallbacks() {
|
||||
@Override
|
||||
public void onFragmentResumed(FragmentManager fm, Fragment f) {
|
||||
if (mTopScreen == f) {
|
||||
setupBackHandlerIfNeeded(mTopScreen);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
public ScreenStack(Context context) {
|
||||
super(context);
|
||||
@@ -25,6 +49,10 @@ public class ScreenStack extends ScreenContainer<ScreenStackFragment> {
|
||||
onUpdate();
|
||||
}
|
||||
|
||||
public Screen getTopScreen() {
|
||||
return mTopScreen.getScreen();
|
||||
}
|
||||
|
||||
public Screen getRootScreen() {
|
||||
for (int i = 0, size = getScreenCount(); i < size; i++) {
|
||||
Screen screen = getScreenAt(i);
|
||||
@@ -41,41 +69,25 @@ public class ScreenStack extends ScreenContainer<ScreenStackFragment> {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onLayout(boolean changed, int l, int t, int r, int b) {
|
||||
for (int i = 0, size = getChildCount(); i < size; i++) {
|
||||
getChildAt(i).layout(0, 0, getWidth(), getHeight());
|
||||
protected void onDetachedFromWindow() {
|
||||
if (mFragmentManager != null) {
|
||||
mFragmentManager.removeOnBackStackChangedListener(mBackStackListener);
|
||||
mFragmentManager.unregisterFragmentLifecycleCallbacks(mLifecycleCallbacks);
|
||||
if (!mFragmentManager.isStateSaved()) {
|
||||
// state save means that the container where fragment manager was installed has been unmounted.
|
||||
// This could happen as a result of dismissing nested stack. In such a case we don't need to
|
||||
// reset back stack as it'd result in a crash caused by the fact the fragment manager is no
|
||||
// longer attached.
|
||||
mFragmentManager.popBackStack(BACK_STACK_TAG, FragmentManager.POP_BACK_STACK_INCLUSIVE);
|
||||
}
|
||||
}
|
||||
super.onDetachedFromWindow();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
||||
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
|
||||
for (int i = 0, size = getChildCount(); i < size; i++) {
|
||||
getChildAt(i).measure(
|
||||
MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.EXACTLY),
|
||||
MeasureSpec.makeMeasureSpec(getHeight(), MeasureSpec.EXACTLY));
|
||||
}
|
||||
}
|
||||
|
||||
private final Runnable mLayoutRunnable = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
mLayoutEnqueued = false;
|
||||
measure(
|
||||
MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.EXACTLY),
|
||||
MeasureSpec.makeMeasureSpec(getHeight(), MeasureSpec.EXACTLY));
|
||||
layout(getLeft(), getTop(), getRight(), getBottom());
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
public void requestLayout() {
|
||||
super.requestLayout();
|
||||
|
||||
if (!mLayoutEnqueued) {
|
||||
mLayoutEnqueued = true;
|
||||
post(mLayoutRunnable);
|
||||
}
|
||||
protected void onAttachedToWindow() {
|
||||
super.onAttachedToWindow();
|
||||
mFragmentManager.registerFragmentLifecycleCallbacks(mLifecycleCallbacks, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -85,6 +97,17 @@ public class ScreenStack extends ScreenContainer<ScreenStackFragment> {
|
||||
super.removeScreenAt(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void removeAllScreens() {
|
||||
mDismissed.clear();
|
||||
super.removeAllScreens();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean hasScreen(ScreenFragment screenFragment) {
|
||||
return super.hasScreen(screenFragment) && !mDismissed.contains(screenFragment);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onUpdate() {
|
||||
// remove all screens previously on stack
|
||||
@@ -93,7 +116,11 @@ public class ScreenStack extends ScreenContainer<ScreenStackFragment> {
|
||||
getOrCreateTransaction().remove(screen);
|
||||
}
|
||||
}
|
||||
ScreenStackFragment newTop = null;
|
||||
|
||||
// When going back from a nested stack with a single screen on it, we may hit an edge case
|
||||
// when all screens are dismissed and no screen is to be displayed on top. We need to gracefully
|
||||
// handle the case of newTop being NULL, which happens in several places below
|
||||
ScreenStackFragment newTop = null; // newTop is nullable, see the above comment ^
|
||||
ScreenStackFragment belowTop = null; // this is only set if newTop has TRANSPARENT_MODAL presentation mode
|
||||
|
||||
for (int i = mScreenFragments.size() - 1; i >= 0; i--) {
|
||||
@@ -111,28 +138,26 @@ public class ScreenStack extends ScreenContainer<ScreenStackFragment> {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for (ScreenStackFragment screen : mScreenFragments) {
|
||||
// add all new views that weren't on stack before
|
||||
if (!mStack.contains(screen) && !mDismissed.contains(screen)) {
|
||||
getOrCreateTransaction().add(getId(), screen);
|
||||
}
|
||||
// detach all screens that should not be visible
|
||||
if (screen != newTop && screen != belowTop && !mDismissed.contains(screen)) {
|
||||
getOrCreateTransaction().hide(screen);
|
||||
getOrCreateTransaction().remove(screen);
|
||||
}
|
||||
}
|
||||
// attach "below top" screen if set
|
||||
if (belowTop != null) {
|
||||
if (belowTop != null && !belowTop.isAdded()) {
|
||||
final ScreenStackFragment top = newTop;
|
||||
getOrCreateTransaction().show(belowTop).runOnCommit(new Runnable() {
|
||||
getOrCreateTransaction().add(getId(), belowTop).runOnCommit(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
top.getScreen().bringToFront();
|
||||
}
|
||||
});
|
||||
}
|
||||
getOrCreateTransaction().show(newTop);
|
||||
|
||||
if (newTop != null && !newTop.isAdded()) {
|
||||
getOrCreateTransaction().add(getId(), newTop);
|
||||
}
|
||||
|
||||
if (!mStack.contains(newTop)) {
|
||||
// if new top screen wasn't on stack we do "open animation" so long it is not the very first screen on stack
|
||||
@@ -169,5 +194,60 @@ public class ScreenStack extends ScreenContainer<ScreenStackFragment> {
|
||||
mStack.addAll(mScreenFragments);
|
||||
|
||||
tryCommitTransaction();
|
||||
|
||||
if (mTopScreen != null) {
|
||||
setupBackHandlerIfNeeded(mTopScreen);
|
||||
}
|
||||
|
||||
for (ScreenStackFragment screen : mStack) {
|
||||
screen.onStackUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The below method sets up fragment manager's back stack in a way that it'd trigger our back
|
||||
* stack change listener when hw back button is clicked.
|
||||
*
|
||||
* Because back stack by default rolls back the transaction the stack entry is associated with we
|
||||
* generate a "fake" transaction that hides and shows the top fragment. As a result when back
|
||||
* stack entry is rolled back nothing happens and we are free to handle back navigation on our
|
||||
* own in `mBackStackListener`.
|
||||
*
|
||||
* We pop that "fake" transaction each time we update stack and we add a new one in case the top
|
||||
* screen is allowed to be dismised using hw back button. This way in the listener we can tell
|
||||
* if back button was pressed based on the count of the items on back stack. We expect 0 items
|
||||
* in case hw back is pressed becakse we try to keep the number of items at 1 by always resetting
|
||||
* and adding new items. In case we don't add a new item to back stack we remove listener so that
|
||||
* it does not get triggered.
|
||||
*
|
||||
* It is important that we don't install back handler when stack contains a single screen as in
|
||||
* that case we want the parent navigator or activity handler to take over.
|
||||
*/
|
||||
private void setupBackHandlerIfNeeded(ScreenStackFragment topScreen) {
|
||||
if (!mTopScreen.isResumed()) {
|
||||
// if the top fragment is not in a resumed state, adding back stack transaction would throw.
|
||||
// In such a case we skip installing back handler and use FragmentLifecycleCallbacks to get
|
||||
// notified when it gets resumed so that we can install the handler.
|
||||
return;
|
||||
}
|
||||
mFragmentManager.removeOnBackStackChangedListener(mBackStackListener);
|
||||
mFragmentManager.popBackStack(BACK_STACK_TAG, FragmentManager.POP_BACK_STACK_INCLUSIVE);
|
||||
ScreenStackFragment firstScreen = null;
|
||||
for (int i = 0, size = mStack.size(); i < size; i++) {
|
||||
ScreenStackFragment screen = mStack.get(i);
|
||||
if (!mDismissed.contains(screen)) {
|
||||
firstScreen = screen;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (topScreen != firstScreen && topScreen.isDismissable()) {
|
||||
mFragmentManager
|
||||
.beginTransaction()
|
||||
.show(topScreen)
|
||||
.addToBackStack(BACK_STACK_TAG)
|
||||
.setPrimaryNavigationFragment(topScreen)
|
||||
.commitAllowingStateLoss();
|
||||
mFragmentManager.addOnBackStackChangedListener(mBackStackListener);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import android.widget.LinearLayout;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.widget.Toolbar;
|
||||
import androidx.coordinatorlayout.widget.CoordinatorLayout;
|
||||
import androidx.fragment.app.Fragment;
|
||||
|
||||
import com.facebook.react.uimanager.PixelUtil;
|
||||
import com.google.android.material.appbar.AppBarLayout;
|
||||
@@ -22,6 +23,7 @@ public class ScreenStackFragment extends ScreenFragment {
|
||||
private AppBarLayout mAppBarLayout;
|
||||
private Toolbar mToolbar;
|
||||
private boolean mShadowHidden;
|
||||
private CoordinatorLayout mScreenRootView;
|
||||
|
||||
@SuppressLint("ValidFragment")
|
||||
public ScreenStackFragment(Screen screenView) {
|
||||
@@ -52,10 +54,14 @@ public class ScreenStackFragment extends ScreenFragment {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater,
|
||||
@Nullable ViewGroup container,
|
||||
@Nullable Bundle savedInstanceState) {
|
||||
public void onStackUpdate() {
|
||||
View child = mScreenView.getChildAt(0);
|
||||
if (child instanceof ScreenStackHeaderConfig) {
|
||||
((ScreenStackHeaderConfig) child).onUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
private CoordinatorLayout configureView() {
|
||||
CoordinatorLayout view = new CoordinatorLayout(getContext());
|
||||
CoordinatorLayout.LayoutParams params = new CoordinatorLayout.LayoutParams(
|
||||
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
|
||||
@@ -79,4 +85,48 @@ public class ScreenStackFragment extends ScreenFragment {
|
||||
|
||||
return view;
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater,
|
||||
@Nullable ViewGroup container,
|
||||
@Nullable Bundle savedInstanceState) {
|
||||
if (mScreenRootView == null) {
|
||||
mScreenRootView = configureView();
|
||||
}
|
||||
|
||||
return mScreenRootView;
|
||||
}
|
||||
|
||||
public boolean isDismissable() {
|
||||
return mScreenView.isGestureEnabled();
|
||||
}
|
||||
|
||||
public boolean canNavigateBack() {
|
||||
ScreenContainer container = mScreenView.getContainer();
|
||||
if (container instanceof ScreenStack) {
|
||||
if (((ScreenStack) container).getRootScreen() == getScreen()) {
|
||||
// this screen is the root of the container, if it is nested we can check parent container
|
||||
// if it is also a root or not
|
||||
Fragment parentFragment = getParentFragment();
|
||||
if (parentFragment instanceof ScreenStackFragment) {
|
||||
return ((ScreenStackFragment) parentFragment).canNavigateBack();
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
throw new IllegalStateException("ScreenStackFragment added into a non-stack container");
|
||||
}
|
||||
}
|
||||
|
||||
public void dismiss() {
|
||||
ScreenContainer container = mScreenView.getContainer();
|
||||
if (container instanceof ScreenStack) {
|
||||
((ScreenStack) container).dismiss(this);
|
||||
} else {
|
||||
throw new IllegalStateException("ScreenStackFragment added into a non-stack container");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package com.swmansion.rnscreens;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.PorterDuff;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.util.TypedValue;
|
||||
@@ -9,41 +8,49 @@ import android.view.Gravity;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.view.ViewParent;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.activity.OnBackPressedCallback;
|
||||
import androidx.appcompat.app.ActionBar;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.appcompat.widget.Toolbar;
|
||||
import androidx.fragment.app.Fragment;
|
||||
|
||||
import com.facebook.react.bridge.JSApplicationIllegalArgumentException;
|
||||
import com.facebook.react.views.text.ReactFontManager;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class ScreenStackHeaderConfig extends ViewGroup {
|
||||
|
||||
private final ScreenStackHeaderSubview mConfigSubviews[] = new ScreenStackHeaderSubview[3];
|
||||
private int mSubviewsCount = 0;
|
||||
private final ArrayList<ScreenStackHeaderSubview> mConfigSubviews = new ArrayList<>(3);
|
||||
private String mTitle;
|
||||
private int mTitleColor;
|
||||
private String mTitleFontFamily;
|
||||
private int mTitleFontSize;
|
||||
private float mTitleFontSize;
|
||||
private int mBackgroundColor;
|
||||
private boolean mIsHidden;
|
||||
private boolean mIsBackButtonHidden;
|
||||
private boolean mIsShadowHidden;
|
||||
private boolean mDestroyed;
|
||||
private int mTintColor;
|
||||
private final Toolbar mToolbar;
|
||||
|
||||
private OnBackPressedCallback mBackCallback = new OnBackPressedCallback(false) {
|
||||
@Override
|
||||
public void handleOnBackPressed() {
|
||||
getScreenStack().dismiss(getScreenFragment());
|
||||
}
|
||||
};
|
||||
private boolean mIsAttachedToWindow = false;
|
||||
|
||||
private OnClickListener mBackClickListener = new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
getScreenStack().dismiss(getScreenFragment());
|
||||
ScreenStack stack = getScreenStack();
|
||||
ScreenStackFragment fragment = getScreenFragment();
|
||||
if (stack.getRootScreen() == fragment.getScreen()) {
|
||||
Fragment parentFragment = fragment.getParentFragment();
|
||||
if (parentFragment instanceof ScreenStackFragment) {
|
||||
((ScreenStackFragment) parentFragment).dismiss();
|
||||
}
|
||||
} else {
|
||||
fragment.dismiss();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -52,6 +59,10 @@ public class ScreenStackHeaderConfig extends ViewGroup {
|
||||
setVisibility(View.GONE);
|
||||
|
||||
mToolbar = new Toolbar(context);
|
||||
// reset content insets to be 0 to allow react position custom navbar views. Note that this does
|
||||
// not affect platform native back button as toolbar does not apply left inset when navigation
|
||||
// button is specified
|
||||
mToolbar.setContentInsetsAbsolute(0, 0);
|
||||
|
||||
// set primary color as background by default
|
||||
TypedValue tv = new TypedValue();
|
||||
@@ -65,10 +76,21 @@ public class ScreenStackHeaderConfig extends ViewGroup {
|
||||
// no-op
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
mDestroyed = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onAttachedToWindow() {
|
||||
super.onAttachedToWindow();
|
||||
update();
|
||||
mIsAttachedToWindow = true;
|
||||
onUpdate();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDetachedFromWindow() {
|
||||
super.onDetachedFromWindow();
|
||||
mIsAttachedToWindow = false;
|
||||
}
|
||||
|
||||
private Screen getScreen() {
|
||||
@@ -101,14 +123,15 @@ public class ScreenStackHeaderConfig extends ViewGroup {
|
||||
return null;
|
||||
}
|
||||
|
||||
private void installBackCallback() {
|
||||
mBackCallback.remove();
|
||||
Fragment fragment = getScreenFragment();
|
||||
fragment.requireActivity().getOnBackPressedDispatcher().addCallback(fragment, mBackCallback);
|
||||
}
|
||||
|
||||
private void update() {
|
||||
public void onUpdate() {
|
||||
Screen parent = (Screen) getParent();
|
||||
final ScreenStack stack = getScreenStack();
|
||||
boolean isTop = stack == null ? true : stack.getTopScreen() == parent;
|
||||
|
||||
if (!mIsAttachedToWindow || !isTop || mDestroyed) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (mIsHidden) {
|
||||
if (mToolbar.getParent() != null) {
|
||||
getScreenFragment().removeToolbar();
|
||||
@@ -125,13 +148,7 @@ public class ScreenStackHeaderConfig extends ViewGroup {
|
||||
ActionBar actionBar = activity.getSupportActionBar();
|
||||
|
||||
// hide back button
|
||||
final ScreenStack stack = getScreenStack();
|
||||
boolean isRoot = stack == null ? true : stack.getRootScreen() == parent;
|
||||
actionBar.setDisplayHomeAsUpEnabled(isRoot ? false : !mIsBackButtonHidden);
|
||||
if (!isRoot) {
|
||||
installBackCallback();
|
||||
}
|
||||
mBackCallback.setEnabled(!isRoot);
|
||||
actionBar.setDisplayHomeAsUpEnabled(getScreenFragment().canNavigateBack() ? !mIsBackButtonHidden : false);
|
||||
|
||||
// when setSupportActionBar is called a toolbar wrapper gets initialized that overwrites
|
||||
// navigation click listener. The default behavior set in the wrapper is to call into
|
||||
@@ -172,10 +189,26 @@ public class ScreenStackHeaderConfig extends ViewGroup {
|
||||
}
|
||||
|
||||
// subviews
|
||||
for (int i = 0; i < mSubviewsCount; i++) {
|
||||
ScreenStackHeaderSubview view = mConfigSubviews[i];
|
||||
for (int i = mToolbar.getChildCount() - 1; i >= 0; i--) {
|
||||
if (mToolbar.getChildAt(i) instanceof ScreenStackHeaderSubview) {
|
||||
mToolbar.removeViewAt(i);
|
||||
}
|
||||
}
|
||||
for (int i = 0, size = mConfigSubviews.size(); i < size; i++) {
|
||||
ScreenStackHeaderSubview view = mConfigSubviews.get(i);
|
||||
ScreenStackHeaderSubview.Type type = view.getType();
|
||||
|
||||
if (type == ScreenStackHeaderSubview.Type.BACK) {
|
||||
// we special case BACK button header config type as we don't add it as a view into toolbar
|
||||
// but instead just copy the drawable from imageview that's added as a first child to it.
|
||||
View firstChild = view.getChildAt(0);
|
||||
if (!(firstChild instanceof ImageView)) {
|
||||
throw new JSApplicationIllegalArgumentException("Back button header config view should have Image as first child");
|
||||
}
|
||||
actionBar.setHomeAsUpIndicator(((ImageView) firstChild).getDrawable());
|
||||
continue;
|
||||
}
|
||||
|
||||
Toolbar.LayoutParams params =
|
||||
new Toolbar.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
|
||||
|
||||
@@ -190,41 +223,45 @@ public class ScreenStackHeaderConfig extends ViewGroup {
|
||||
case RIGHT:
|
||||
params.gravity = Gravity.RIGHT;
|
||||
break;
|
||||
case TITLE:
|
||||
params.width = LayoutParams.MATCH_PARENT;
|
||||
mToolbar.setTitle(null);
|
||||
case CENTER:
|
||||
params.width = LayoutParams.MATCH_PARENT;
|
||||
params.gravity = Gravity.CENTER_HORIZONTAL;
|
||||
mToolbar.setTitle(null);
|
||||
break;
|
||||
}
|
||||
|
||||
view.setLayoutParams(params);
|
||||
if (view.getParent() == null) {
|
||||
mToolbar.addView(view);
|
||||
}
|
||||
mToolbar.addView(view);
|
||||
}
|
||||
}
|
||||
|
||||
private void maybeUpdate() {
|
||||
if (getParent() != null && !mDestroyed) {
|
||||
onUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
public ScreenStackHeaderSubview getConfigSubview(int index) {
|
||||
return mConfigSubviews[index];
|
||||
return mConfigSubviews.get(index);
|
||||
}
|
||||
|
||||
public int getConfigSubviewsCount() {
|
||||
return mSubviewsCount;
|
||||
return mConfigSubviews.size();
|
||||
}
|
||||
|
||||
public void removeConfigSubview(int index) {
|
||||
if (mConfigSubviews[index] != null) {
|
||||
mSubviewsCount--;
|
||||
}
|
||||
mConfigSubviews[index] = null;
|
||||
mConfigSubviews.remove(index);
|
||||
maybeUpdate();
|
||||
}
|
||||
|
||||
public void removeAllConfigSubviews() {
|
||||
mConfigSubviews.clear();
|
||||
maybeUpdate();
|
||||
}
|
||||
|
||||
public void addConfigSubview(ScreenStackHeaderSubview child, int index) {
|
||||
if (mConfigSubviews[index] == null) {
|
||||
mSubviewsCount++;
|
||||
}
|
||||
mConfigSubviews[index] = child;
|
||||
mConfigSubviews.add(index, child);
|
||||
maybeUpdate();
|
||||
}
|
||||
|
||||
private TextView getTitleTextView() {
|
||||
@@ -248,7 +285,7 @@ public class ScreenStackHeaderConfig extends ViewGroup {
|
||||
mTitleFontFamily = titleFontFamily;
|
||||
}
|
||||
|
||||
public void setTitleFontSize(int titleFontSize) {
|
||||
public void setTitleFontSize(float titleFontSize) {
|
||||
mTitleFontSize = titleFontSize;
|
||||
}
|
||||
|
||||
|
||||
@@ -4,11 +4,12 @@ import android.view.View;
|
||||
|
||||
import com.facebook.react.bridge.JSApplicationCausedNativeException;
|
||||
import com.facebook.react.module.annotations.ReactModule;
|
||||
import com.facebook.react.uimanager.PixelUtil;
|
||||
import com.facebook.react.uimanager.ThemedReactContext;
|
||||
import com.facebook.react.uimanager.ViewGroupManager;
|
||||
import com.facebook.react.uimanager.annotations.ReactProp;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
@ReactModule(name = ScreenStackHeaderConfigViewManager.REACT_CLASS)
|
||||
public class ScreenStackHeaderConfigViewManager extends ViewGroupManager<ScreenStackHeaderConfig> {
|
||||
|
||||
@@ -32,6 +33,16 @@ public class ScreenStackHeaderConfigViewManager extends ViewGroupManager<ScreenS
|
||||
parent.addConfigSubview((ScreenStackHeaderSubview) child, index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDropViewInstance(@Nonnull ScreenStackHeaderConfig view) {
|
||||
view.destroy();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeAllViews(ScreenStackHeaderConfig parent) {
|
||||
parent.removeAllConfigSubviews();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeViewAt(ScreenStackHeaderConfig parent, int index) {
|
||||
parent.removeConfigSubview(index);
|
||||
@@ -52,6 +63,12 @@ public class ScreenStackHeaderConfigViewManager extends ViewGroupManager<ScreenS
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onAfterUpdateTransaction(ScreenStackHeaderConfig parent) {
|
||||
super.onAfterUpdateTransaction(parent);
|
||||
parent.onUpdate();
|
||||
}
|
||||
|
||||
@ReactProp(name = "title")
|
||||
public void setTitle(ScreenStackHeaderConfig config, String title) {
|
||||
config.setTitle(title);
|
||||
@@ -63,8 +80,8 @@ public class ScreenStackHeaderConfigViewManager extends ViewGroupManager<ScreenS
|
||||
}
|
||||
|
||||
@ReactProp(name = "titleFontSize")
|
||||
public void setTitleFontSize(ScreenStackHeaderConfig config, double titleFontSizeSP) {
|
||||
config.setTitleFontSize((int) PixelUtil.toPixelFromSP(titleFontSizeSP));
|
||||
public void setTitleFontSize(ScreenStackHeaderConfig config, float titleFontSize) {
|
||||
config.setTitleFontSize(titleFontSize);
|
||||
}
|
||||
|
||||
@ReactProp(name = "titleColor", customType = "Color")
|
||||
|
||||
@@ -9,20 +9,14 @@ import com.facebook.react.views.view.ReactViewGroup;
|
||||
|
||||
public class ScreenStackHeaderSubview extends ReactViewGroup {
|
||||
|
||||
public class Measurements {
|
||||
public int width;
|
||||
public int height;
|
||||
}
|
||||
|
||||
public enum Type {
|
||||
LEFT,
|
||||
CENTER,
|
||||
TITLE,
|
||||
RIGHT
|
||||
RIGHT,
|
||||
BACK
|
||||
}
|
||||
|
||||
private int mReactWidth, mReactHeight;
|
||||
private final UIManagerModule mUIManager;
|
||||
|
||||
@Override
|
||||
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
||||
@@ -42,29 +36,13 @@ public class ScreenStackHeaderSubview extends ReactViewGroup {
|
||||
|
||||
@Override
|
||||
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
|
||||
if (changed && (mType == Type.CENTER || mType == Type.TITLE)) {
|
||||
Measurements measurements = new Measurements();
|
||||
measurements.width = right - left;
|
||||
if (mType == Type.CENTER) {
|
||||
// if we want the view to be centered we need to account for the fact that right and left
|
||||
// paddings may not be equal.
|
||||
View parent = (View) getParent();
|
||||
int parentWidth = parent.getWidth();
|
||||
int rightPadding = parentWidth - right;
|
||||
int leftPadding = left;
|
||||
measurements.width = Math.max(0, parentWidth - 2 * Math.max(rightPadding, leftPadding));
|
||||
}
|
||||
measurements.height = bottom - top;
|
||||
mUIManager.setViewLocalData(getId(), measurements);
|
||||
}
|
||||
super.onLayout(changed, left, top, right, bottom);
|
||||
// no-op
|
||||
}
|
||||
|
||||
private Type mType = Type.RIGHT;
|
||||
|
||||
public ScreenStackHeaderSubview(ReactContext context) {
|
||||
super(context);
|
||||
mUIManager = context.getNativeModule(UIManagerModule.class);
|
||||
}
|
||||
|
||||
public void setType(Type type) {
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package com.swmansion.rnscreens;
|
||||
|
||||
import com.facebook.react.bridge.ReactApplicationContext;
|
||||
import com.facebook.react.module.annotations.ReactModule;
|
||||
import com.facebook.react.uimanager.LayoutShadowNode;
|
||||
import com.facebook.react.uimanager.ThemedReactContext;
|
||||
import com.facebook.react.uimanager.annotations.ReactProp;
|
||||
import com.facebook.react.views.view.ReactViewGroup;
|
||||
@@ -11,15 +9,6 @@ import com.facebook.react.views.view.ReactViewManager;
|
||||
@ReactModule(name = ScreenStackHeaderSubviewManager.REACT_CLASS)
|
||||
public class ScreenStackHeaderSubviewManager extends ReactViewManager {
|
||||
|
||||
private static class SubviewShadowNode extends LayoutShadowNode {
|
||||
@Override
|
||||
public void setLocalData(Object data) {
|
||||
ScreenStackHeaderSubview.Measurements measurements = (ScreenStackHeaderSubview.Measurements) data;
|
||||
setStyleWidth(measurements.width);
|
||||
setStyleHeight(measurements.height);
|
||||
}
|
||||
}
|
||||
|
||||
protected static final String REACT_CLASS = "RNSScreenStackHeaderSubview";
|
||||
|
||||
@Override
|
||||
@@ -32,21 +21,16 @@ public class ScreenStackHeaderSubviewManager extends ReactViewManager {
|
||||
return new ScreenStackHeaderSubview(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public LayoutShadowNode createShadowNodeInstance(ReactApplicationContext context) {
|
||||
return new SubviewShadowNode();
|
||||
}
|
||||
|
||||
@ReactProp(name = "type")
|
||||
public void setType(ScreenStackHeaderSubview view, String type) {
|
||||
if ("left".equals(type)) {
|
||||
view.setType(ScreenStackHeaderSubview.Type.LEFT);
|
||||
} else if ("center".equals(type)) {
|
||||
view.setType(ScreenStackHeaderSubview.Type.CENTER);
|
||||
} else if ("title".equals(type)) {
|
||||
view.setType(ScreenStackHeaderSubview.Type.TITLE);
|
||||
} else if ("right".equals(type)) {
|
||||
view.setType(ScreenStackHeaderSubview.Type.RIGHT);
|
||||
} else if ("back".equals(type)) {
|
||||
view.setType(ScreenStackHeaderSubview.Type.BACK);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,9 +35,11 @@ public class ScreenViewManager extends ViewGroupManager<Screen> {
|
||||
public void setStackPresentation(Screen view, String presentation) {
|
||||
if ("push".equals(presentation)) {
|
||||
view.setStackPresentation(Screen.StackPresentation.PUSH);
|
||||
} else if ("modal".equals(presentation)) {
|
||||
} else if ("modal".equals(presentation) || "containedModal".equals(presentation)) {
|
||||
// at the moment Android implementation does not handle contained vs regular modals
|
||||
view.setStackPresentation(Screen.StackPresentation.MODAL);
|
||||
} else if ("transparentModal".equals(presentation)) {
|
||||
} else if ("transparentModal".equals(presentation) || "containedTransparentModal".equals((presentation))) {
|
||||
// at the moment Android implementation does not handle contained vs regular modals
|
||||
view.setStackPresentation(Screen.StackPresentation.TRANSPARENT_MODAL);
|
||||
} else {
|
||||
throw new JSApplicationIllegalArgumentException("Unknown presentation type " + presentation);
|
||||
@@ -55,11 +57,18 @@ public class ScreenViewManager extends ViewGroupManager<Screen> {
|
||||
}
|
||||
}
|
||||
|
||||
@ReactProp(name = "gestureEnabled", defaultBoolean = true)
|
||||
public void setGestureEnabled(Screen view, boolean gestureEnabled) {
|
||||
view.setGestureEnabled(gestureEnabled);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public Map getExportedCustomDirectEventTypeConstants() {
|
||||
return MapBuilder.of(
|
||||
ScreenDismissedEvent.EVENT_NAME,
|
||||
MapBuilder.of("registrationName", "onDismissed"));
|
||||
MapBuilder.of("registrationName", "onDismissed"),
|
||||
ScreenAppearEvent.EVENT_NAME,
|
||||
MapBuilder.of("registrationName", "onAppear"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ import {
|
||||
StackRouter,
|
||||
SceneView,
|
||||
StackActions,
|
||||
NavigationActions,
|
||||
createNavigator,
|
||||
} from '@react-navigation/core';
|
||||
import { createKeyboardAwareNavigator } from '@react-navigation/native';
|
||||
@@ -13,21 +12,31 @@ import {
|
||||
ScreenStack,
|
||||
Screen,
|
||||
ScreenStackHeaderConfig,
|
||||
ScreenStackHeaderBackButtonImage,
|
||||
ScreenStackHeaderLeftView,
|
||||
ScreenStackHeaderRightView,
|
||||
ScreenStackHeaderTitleView,
|
||||
ScreenStackHeaderCenterView,
|
||||
} from 'react-native-screens';
|
||||
|
||||
function renderComponentOrThunk(componentOrThunk, props) {
|
||||
if (typeof componentOrThunk === 'function') {
|
||||
return componentOrThunk(props);
|
||||
}
|
||||
return componentOrThunk;
|
||||
}
|
||||
|
||||
class StackView extends React.Component {
|
||||
_removeScene = route => {
|
||||
const { navigation } = this.props;
|
||||
navigation.dispatch(
|
||||
NavigationActions.back({
|
||||
key: route.key,
|
||||
immediate: true,
|
||||
})
|
||||
this.props.navigation.dispatch(StackActions.pop({ key: route.key }));
|
||||
};
|
||||
|
||||
_onSceneFocus = (route, descriptor) => {
|
||||
descriptor.options &&
|
||||
descriptor.options.onAppear &&
|
||||
descriptor.options.onAppear();
|
||||
this.props.navigation.dispatch(
|
||||
StackActions.completeTransition({ toChildKey: route.key })
|
||||
);
|
||||
navigation.dispatch(StackActions.completeTransition());
|
||||
};
|
||||
|
||||
_renderHeaderConfig = (index, route, descriptor) => {
|
||||
@@ -41,11 +50,12 @@ class StackView extends React.Component {
|
||||
headerTitleStyle,
|
||||
headerBackTitleStyle,
|
||||
headerBackTitle,
|
||||
headerBackTitleVisible,
|
||||
headerTintColor,
|
||||
gestureEnabled,
|
||||
largeTitle,
|
||||
headerLargeTitleStyle,
|
||||
translucent,
|
||||
hideShadow,
|
||||
} = options;
|
||||
|
||||
const scene = {
|
||||
@@ -61,17 +71,17 @@ class StackView extends React.Component {
|
||||
titleFontFamily: headerTitleStyle && headerTitleStyle.fontFamily,
|
||||
titleColor: headerTintColor,
|
||||
titleFontSize: headerTitleStyle && headerTitleStyle.fontSize,
|
||||
backTitle: headerBackTitle,
|
||||
backTitle: headerBackTitleVisible === false ? '' : headerBackTitle,
|
||||
backTitleFontFamily:
|
||||
headerBackTitleStyle && headerBackTitleStyle.fontFamily,
|
||||
backTitleFontSize: headerBackTitleStyle && headerBackTitleStyle.fontSize,
|
||||
color: headerTintColor,
|
||||
gestureEnabled: gestureEnabled === undefined ? true : gestureEnabled,
|
||||
largeTitle,
|
||||
largeTitleFontFamily:
|
||||
headerLargeTitleStyle && headerLargeTitleStyle.fontFamily,
|
||||
largeTitleFontSize:
|
||||
headerLargeTitleStyle && headerLargeTitleStyle.fontSize,
|
||||
hideShadow,
|
||||
};
|
||||
|
||||
const hasHeader = headerMode !== 'none' && options.header !== null;
|
||||
@@ -85,10 +95,19 @@ class StackView extends React.Component {
|
||||
|
||||
const children = [];
|
||||
|
||||
if (options.backButtonImage) {
|
||||
children.push(
|
||||
<ScreenStackHeaderBackButtonImage
|
||||
key="backImage"
|
||||
source={options.backButtonImage}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
if (options.headerLeft !== undefined) {
|
||||
children.push(
|
||||
<ScreenStackHeaderLeftView key="left">
|
||||
{options.headerLeft({ scene })}
|
||||
{renderComponentOrThunk(options.headerLeft, { scene })}
|
||||
</ScreenStackHeaderLeftView>
|
||||
);
|
||||
} else if (options.headerBackImage !== undefined) {
|
||||
@@ -118,17 +137,21 @@ class StackView extends React.Component {
|
||||
}
|
||||
|
||||
if (options.headerTitle) {
|
||||
children.push(
|
||||
<ScreenStackHeaderTitleView key="title">
|
||||
{options.headerTitle({ scene })}
|
||||
</ScreenStackHeaderTitleView>
|
||||
);
|
||||
if (title === undefined && typeof options.headerTitle === 'string') {
|
||||
headerOptions.title = options.headerTitle;
|
||||
} else {
|
||||
children.push(
|
||||
<ScreenStackHeaderCenterView key="center">
|
||||
{renderComponentOrThunk(options.headerTitle, { scene })}
|
||||
</ScreenStackHeaderCenterView>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (options.headerRight) {
|
||||
children.push(
|
||||
<ScreenStackHeaderRightView key="right">
|
||||
{options.headerRight({ scene })}
|
||||
{renderComponentOrThunk(options.headerRight, { scene })}
|
||||
</ScreenStackHeaderRightView>
|
||||
);
|
||||
}
|
||||
@@ -141,21 +164,37 @@ class StackView extends React.Component {
|
||||
};
|
||||
|
||||
_renderScene = (index, route, descriptor) => {
|
||||
const { navigation, getComponent } = descriptor;
|
||||
const { navigation, getComponent, options } = descriptor;
|
||||
const { mode, transparentCard } = this.props.navigationConfig;
|
||||
const SceneComponent = getComponent();
|
||||
|
||||
let stackPresentation = 'push';
|
||||
if (mode === 'modal') {
|
||||
stackPresentation = transparentCard ? 'transparentModal' : 'modal';
|
||||
if (mode === 'modal' || mode === 'containedModal') {
|
||||
stackPresentation = mode;
|
||||
if (transparentCard || options.cardTransparent) {
|
||||
stackPresentation =
|
||||
mode === 'containedModal'
|
||||
? 'containedTransparentModal'
|
||||
: 'transparentModal';
|
||||
}
|
||||
}
|
||||
|
||||
let stackAnimation = options.stackAnimation;
|
||||
if (options.animationEnabled === false) {
|
||||
stackAnimation = 'none';
|
||||
}
|
||||
|
||||
const { screenProps } = this.props;
|
||||
return (
|
||||
<Screen
|
||||
key={`screen_${route.key}`}
|
||||
style={StyleSheet.absoluteFill}
|
||||
style={options.cardStyle}
|
||||
stackAnimation={stackAnimation}
|
||||
stackPresentation={stackPresentation}
|
||||
gestureEnabled={
|
||||
options.gestureEnabled === undefined ? true : options.gestureEnabled
|
||||
}
|
||||
onAppear={() => this._onSceneFocus(route, descriptor)}
|
||||
onDismissed={() => this._removeScene(route)}>
|
||||
{this._renderHeaderConfig(index, route, descriptor)}
|
||||
<SceneView
|
||||
@@ -186,7 +225,6 @@ const styles = StyleSheet.create({
|
||||
|
||||
function createStackNavigator(routeConfigMap, stackConfig = {}) {
|
||||
const router = StackRouter(routeConfigMap, stackConfig);
|
||||
|
||||
// Create a navigator with StackView as the view
|
||||
let Navigator = createNavigator(StackView, router, stackConfig);
|
||||
// if (!stackConfig.disableKeyboardHandling) {
|
||||
|
||||
@@ -17,6 +17,7 @@ typedef NS_ENUM(NSInteger, RNSScreenStackAnimation) {
|
||||
RNSScreenStackAnimationDefault,
|
||||
RNSScreenStackAnimationNone,
|
||||
RNSScreenStackAnimationFade,
|
||||
RNSScreenStackAnimationFlip,
|
||||
};
|
||||
|
||||
@interface RCTConvert (RNSScreen)
|
||||
@@ -26,15 +27,24 @@ typedef NS_ENUM(NSInteger, RNSScreenStackAnimation) {
|
||||
|
||||
@end
|
||||
|
||||
@interface RNSScreen : UIViewController
|
||||
|
||||
- (instancetype)initWithView:(UIView *)view;
|
||||
- (void)notifyFinishTransitioning;
|
||||
|
||||
@end
|
||||
|
||||
@interface RNSScreenManager : RCTViewManager
|
||||
@end
|
||||
|
||||
@interface RNSScreenView : RCTView
|
||||
|
||||
@property (nonatomic, copy) RCTDirectEventBlock onAppear;
|
||||
@property (nonatomic, copy) RCTDirectEventBlock onDismissed;
|
||||
@property (weak, nonatomic) UIView<RNSScreenContainerDelegate> *reactSuperview;
|
||||
@property (nonatomic, retain) UIViewController *controller;
|
||||
@property (nonatomic) BOOL active;
|
||||
@property (nonatomic) BOOL gestureEnabled;
|
||||
@property (nonatomic) RNSScreenStackAnimation stackAnimation;
|
||||
@property (nonatomic) RNSScreenStackPresentation stackPresentation;
|
||||
|
||||
|
||||
@@ -8,40 +8,6 @@
|
||||
#import <React/RCTShadowView.h>
|
||||
#import <React/RCTTouchHandler.h>
|
||||
|
||||
@interface RNSScreenFrameData : NSObject
|
||||
@property (nonatomic, readonly) CGFloat rightInset;
|
||||
@property (nonatomic, readonly) CGFloat topInset;
|
||||
@property (nonatomic, readonly) CGFloat bottomInset;
|
||||
@property (nonatomic, readonly) CGFloat leftInset;
|
||||
@property (nonatomic, readonly) CGFloat navbarOffset;
|
||||
|
||||
- (instancetype)initWithInsets:(UIEdgeInsets)insets;
|
||||
|
||||
@end
|
||||
|
||||
@implementation RNSScreenFrameData
|
||||
|
||||
- (instancetype)initWithInsets:(UIEdgeInsets)insets andNavbarOffset:(CGFloat)navbarOffset
|
||||
{
|
||||
if (self = [super init]) {
|
||||
_topInset = insets.top;
|
||||
_bottomInset = insets.bottom;
|
||||
_leftInset = insets.left;
|
||||
_rightInset = insets.right;
|
||||
_navbarOffset = navbarOffset;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@interface RNSScreen : UIViewController
|
||||
|
||||
- (instancetype)initWithView:(UIView *)view;
|
||||
- (void)notifyFinishTransitioning;
|
||||
|
||||
@end
|
||||
|
||||
@interface RNSScreenView () <UIAdaptivePresentationControllerDelegate>
|
||||
@end
|
||||
|
||||
@@ -60,6 +26,7 @@
|
||||
_controller = [[RNSScreen alloc] initWithView:self];
|
||||
_stackPresentation = RNSScreenStackPresentationPush;
|
||||
_stackAnimation = RNSScreenStackAnimationDefault;
|
||||
_gestureEnabled = YES;
|
||||
}
|
||||
|
||||
return self;
|
||||
@@ -67,6 +34,9 @@
|
||||
|
||||
- (void)reactSetFrame:(CGRect)frame
|
||||
{
|
||||
if (_active) {
|
||||
[super reactSetFrame:frame];
|
||||
}
|
||||
// ignore setFrame call from react, the frame of this view
|
||||
// is controlled by the UIViewController it is contained in
|
||||
}
|
||||
@@ -129,6 +99,9 @@
|
||||
case RNSScreenStackAnimationFade:
|
||||
_controller.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
|
||||
break;
|
||||
case RNSScreenStackAnimationFlip:
|
||||
_controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
|
||||
break;
|
||||
case RNSScreenStackAnimationNone:
|
||||
case RNSScreenStackAnimationDefault:
|
||||
// Default
|
||||
@@ -145,6 +118,8 @@
|
||||
{
|
||||
if (![view isKindOfClass:[RNSScreenStackHeaderConfig class]]) {
|
||||
[super addSubview:view];
|
||||
} else {
|
||||
((RNSScreenStackHeaderConfig*) view).screenView = self;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -164,6 +139,17 @@
|
||||
}
|
||||
}
|
||||
|
||||
- (void)notifyAppear
|
||||
{
|
||||
if (self.onAppear) {
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
if (self.onAppear) {
|
||||
self.onAppear(nil);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
- (BOOL)isMountedUnderScreenOrReactRoot
|
||||
{
|
||||
for (UIView *parent = self.superview; parent != nil; parent = parent.superview) {
|
||||
@@ -264,6 +250,12 @@
|
||||
}
|
||||
}
|
||||
|
||||
- (void)viewDidAppear:(BOOL)animated
|
||||
{
|
||||
[super viewDidAppear:animated];
|
||||
[((RNSScreenView *)self.view) notifyAppear];
|
||||
}
|
||||
|
||||
- (void)notifyFinishTransitioning
|
||||
{
|
||||
[_previousFirstResponder becomeFirstResponder];
|
||||
@@ -285,8 +277,10 @@
|
||||
RCT_EXPORT_MODULE()
|
||||
|
||||
RCT_EXPORT_VIEW_PROPERTY(active, BOOL)
|
||||
RCT_EXPORT_VIEW_PROPERTY(gestureEnabled, BOOL)
|
||||
RCT_EXPORT_VIEW_PROPERTY(stackPresentation, RNSScreenStackPresentation)
|
||||
RCT_EXPORT_VIEW_PROPERTY(stackAnimation, RNSScreenStackAnimation)
|
||||
RCT_EXPORT_VIEW_PROPERTY(onAppear, RCTDirectEventBlock);
|
||||
RCT_EXPORT_VIEW_PROPERTY(onDismissed, RCTDirectEventBlock);
|
||||
|
||||
- (UIView *)view
|
||||
@@ -309,8 +303,10 @@ RCT_ENUM_CONVERTER(RNSScreenStackPresentation, (@{
|
||||
RCT_ENUM_CONVERTER(RNSScreenStackAnimation, (@{
|
||||
@"default": @(RNSScreenStackAnimationDefault),
|
||||
@"none": @(RNSScreenStackAnimationNone),
|
||||
@"fade": @(RNSScreenStackAnimationFade)
|
||||
@"fade": @(RNSScreenStackAnimationFade),
|
||||
@"flip": @(RNSScreenStackAnimationFlip),
|
||||
}), RNSScreenStackAnimationDefault, integerValue)
|
||||
|
||||
|
||||
@end
|
||||
|
||||
|
||||
@@ -54,6 +54,7 @@
|
||||
{
|
||||
subview.reactSuperview = self;
|
||||
[_reactSubviews insertObject:subview atIndex:atIndex];
|
||||
subview.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
|
||||
}
|
||||
|
||||
- (void)removeReactSubview:(RNSScreenView *)subview
|
||||
@@ -159,6 +160,10 @@
|
||||
[super layoutSubviews];
|
||||
[self reactAddControllerToClosestParent:_controller];
|
||||
_controller.view.frame = self.bounds;
|
||||
for (RNSScreenView *subview in _reactSubviews) {
|
||||
subview.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
|
||||
[subview setNeedsLayout];
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#import <React/RCTUIManagerObserverCoordinator.h>
|
||||
#import "RNSScreenContainer.h"
|
||||
|
||||
@interface RNSScreenStackView : UIView <RNSScreenContainerDelegate>
|
||||
@interface RNSScreenStackView : UIView <RNSScreenContainerDelegate, RCTInvalidating>
|
||||
|
||||
- (void)markChildUpdated;
|
||||
- (void)didUpdateChildren;
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
#import <React/RCTUIManager.h>
|
||||
#import <React/RCTUIManagerUtils.h>
|
||||
#import <React/RCTShadowView.h>
|
||||
#import <React/RCTRootContentView.h>
|
||||
#import <React/RCTTouchHandler.h>
|
||||
|
||||
@interface RNSScreenStackView () <UINavigationControllerDelegate, UIGestureRecognizerDelegate>
|
||||
@end
|
||||
@@ -20,6 +22,7 @@
|
||||
NSMutableArray<RNSScreenView *> *_reactSubviews;
|
||||
NSMutableSet<RNSScreenView *> *_dismissedScreens;
|
||||
NSMutableArray<UIViewController *> *_presentedModals;
|
||||
__weak UIViewController* recentPopped;
|
||||
__weak RNSScreenStackManager *_manager;
|
||||
}
|
||||
|
||||
@@ -35,6 +38,12 @@
|
||||
_needUpdate = NO;
|
||||
[self addSubview:_controller.view];
|
||||
_controller.interactivePopGestureRecognizer.delegate = self;
|
||||
|
||||
// we have to initialize viewControllers with a non empty array for
|
||||
// largeTitle header to render in the opened state. If it is empty
|
||||
// the header will render in collapsed state which is perhaps a bug
|
||||
// in UIKit but ¯\_(ツ)_/¯
|
||||
[_controller setViewControllers:@[[UIViewController new]]];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
@@ -58,10 +67,13 @@
|
||||
if ([viewController isEqual:[_reactSubviews objectAtIndex:i - 1].controller]) {
|
||||
break;
|
||||
} else {
|
||||
// TODO: send dismiss event
|
||||
[_dismissedScreens addObject:[_reactSubviews objectAtIndex:i - 1]];
|
||||
}
|
||||
}
|
||||
if (recentPopped != nil) {
|
||||
recentPopped.view = nil;
|
||||
recentPopped = nil;
|
||||
}
|
||||
}
|
||||
|
||||
- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC
|
||||
@@ -70,9 +82,10 @@
|
||||
if (operation == UINavigationControllerOperationPush) {
|
||||
screen = (RNSScreenView *) toVC.view;
|
||||
} else if (operation == UINavigationControllerOperationPop) {
|
||||
screen = (RNSScreenView *) fromVC.view;
|
||||
screen = (RNSScreenView *) fromVC.view;
|
||||
recentPopped = fromVC;
|
||||
}
|
||||
if (screen != nil && screen.stackAnimation != RNSScreenStackAnimationDefault) {
|
||||
if (screen != nil && (screen.stackAnimation == RNSScreenStackAnimationFade || screen.stackAnimation == RNSScreenStackAnimationNone)) {
|
||||
return [[RNSScreenStackAnimator alloc] initWithOperation:operation];
|
||||
}
|
||||
return nil;
|
||||
@@ -85,28 +98,13 @@
|
||||
// Without the below code the Touchable will remain active (highlighted) for the duration of back
|
||||
// gesture and onPress may fire when we release the finger.
|
||||
UIView *parent = _controller.view;
|
||||
while (parent != nil && ![parent isKindOfClass:[RCTRootView class]]) parent = parent.superview;
|
||||
RCTRootView *rootView = (RCTRootView *)parent;
|
||||
[rootView cancelTouches];
|
||||
while (parent != nil && ![parent isKindOfClass:[RCTRootContentView class]]) parent = parent.superview;
|
||||
RCTRootContentView *rootView = (RCTRootContentView *)parent;
|
||||
[rootView.touchHandler cancel];
|
||||
|
||||
return _controller.viewControllers.count > 1;
|
||||
}
|
||||
RNSScreenView *topScreen = (RNSScreenView *)_controller.viewControllers.lastObject.view;
|
||||
|
||||
- (void)markUpdated
|
||||
{
|
||||
// We want 'updateContainer' to be executed on main thread after all enqueued operations in
|
||||
// uimanager are complete. In order to achieve that we enqueue call on UIManagerQueue from which
|
||||
// we enqueue call on the main queue. This seems to be working ok in all the cases I've tried but
|
||||
// there is a chance it is not the correct way to do that.
|
||||
if (!_needUpdate) {
|
||||
_needUpdate = YES;
|
||||
RCTExecuteOnUIManagerQueue(^{
|
||||
RCTExecuteOnMainQueue(^{
|
||||
_needUpdate = NO;
|
||||
[self updateContainer];
|
||||
});
|
||||
});
|
||||
}
|
||||
return _controller.viewControllers.count > 1 && topScreen.gestureEnabled;
|
||||
}
|
||||
|
||||
- (void)markChildUpdated
|
||||
@@ -126,14 +124,12 @@
|
||||
return;
|
||||
}
|
||||
[_reactSubviews insertObject:subview atIndex:atIndex];
|
||||
[self markUpdated];
|
||||
}
|
||||
|
||||
- (void)removeReactSubview:(RNSScreenView *)subview
|
||||
{
|
||||
[_reactSubviews removeObject:subview];
|
||||
[_dismissedScreens removeObject:subview];
|
||||
[self markUpdated];
|
||||
}
|
||||
|
||||
- (NSArray<UIView *> *)reactSubviews
|
||||
@@ -144,53 +140,84 @@
|
||||
- (void)didUpdateReactSubviews
|
||||
{
|
||||
// do nothing
|
||||
[self updateContainer];
|
||||
}
|
||||
|
||||
- (void)setModalViewControllers:(NSArray<UIViewController *> *)controllers
|
||||
{
|
||||
// when there is no change we return immediately. This check is important because sometime we may
|
||||
// accidently trigger modal dismiss if we don't verify to run the below code only when an actual
|
||||
// change in the list of presented modal was made.
|
||||
if ([_presentedModals isEqualToArray:controllers]) {
|
||||
return;
|
||||
}
|
||||
|
||||
NSMutableArray<UIViewController *> *newControllers = [NSMutableArray arrayWithArray:controllers];
|
||||
[newControllers removeObjectsInArray:_presentedModals];
|
||||
|
||||
NSMutableArray<UIViewController *> *controllersToRemove = [NSMutableArray arrayWithArray:_presentedModals];
|
||||
[controllersToRemove removeObjectsInArray:controllers];
|
||||
|
||||
// presenting new controllers
|
||||
for (UIViewController *newController in newControllers) {
|
||||
[_presentedModals addObject:newController];
|
||||
if (_controller.presentedViewController != nil) {
|
||||
[_controller.presentedViewController presentViewController:newController animated:YES completion:nil];
|
||||
// find bottom-most controller that should stay on the stack for the duration of transition
|
||||
NSUInteger changeRootIndex = 0;
|
||||
UIViewController *changeRootController = _controller;
|
||||
for (NSUInteger i = 0; i < MIN(_presentedModals.count, controllers.count); i++) {
|
||||
if (_presentedModals[i] == controllers[i]) {
|
||||
changeRootController = controllers[i];
|
||||
changeRootIndex = i + 1;
|
||||
} else {
|
||||
[_controller presentViewController:newController animated:YES completion:nil];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// hiding old controllers
|
||||
for (UIViewController *controller in [controllersToRemove reverseObjectEnumerator]) {
|
||||
[_presentedModals removeObject:controller];
|
||||
if (controller.presentedViewController != nil) {
|
||||
UIViewController *restore = controller.presentedViewController;
|
||||
UIViewController *parent = controller.presentingViewController;
|
||||
[controller dismissViewControllerAnimated:NO completion:^{
|
||||
[parent dismissViewControllerAnimated:NO completion:^{
|
||||
[parent presentViewController:restore animated:NO completion:nil];
|
||||
}];
|
||||
}];
|
||||
} else {
|
||||
[controller.presentingViewController dismissViewControllerAnimated:YES completion:nil];
|
||||
// we verify that controllers added on top of changeRootIndex are all new. Unfortunately modal
|
||||
// VCs cannot be reshuffled (there are some visual glitches when we try to dismiss then show as
|
||||
// even non-animated dismissal has delay and updates the screen several times)
|
||||
for (NSUInteger i = changeRootIndex; i < controllers.count; i++) {
|
||||
if ([_presentedModals containsObject:controllers[i]]) {
|
||||
RCTAssert(false, @"Modally presented controllers are being reshuffled, this is not allowed");
|
||||
}
|
||||
}
|
||||
|
||||
void (^finish)(void) = ^{
|
||||
UIViewController *previous = changeRootController;
|
||||
for (NSUInteger i = changeRootIndex; i < controllers.count; i++) {
|
||||
UIViewController *next = controllers[i];
|
||||
[previous presentViewController:next
|
||||
animated:(i == controllers.count - 1)
|
||||
completion:nil];
|
||||
previous = next;
|
||||
}
|
||||
};
|
||||
|
||||
if (changeRootController.presentedViewController) {
|
||||
[changeRootController
|
||||
dismissViewControllerAnimated:(changeRootIndex == controllers.count)
|
||||
completion:finish];
|
||||
} else {
|
||||
finish();
|
||||
}
|
||||
[_presentedModals setArray:controllers];
|
||||
}
|
||||
|
||||
- (void)setPushViewControllers:(NSArray<UIViewController *> *)controllers
|
||||
{
|
||||
// when there is no change we return immediately
|
||||
if ([_controller.viewControllers isEqualToArray:controllers]) {
|
||||
return;
|
||||
}
|
||||
|
||||
UIViewController *top = controllers.lastObject;
|
||||
UIViewController *lastTop = _controller.viewControllers.lastObject;
|
||||
|
||||
BOOL shouldAnimate = ((RNSScreenView *) lastTop.view).stackAnimation != RNSScreenStackAnimationNone;
|
||||
// at the start we set viewControllers to contain a single UIVIewController
|
||||
// instance. This is a workaround for header height adjustment bug (see comment
|
||||
// in the init function). Here, we need to detect if the initial empty
|
||||
// controller is still there
|
||||
BOOL firstTimePush = ![lastTop isKindOfClass:[RNSScreen class]];
|
||||
|
||||
if (_controller.viewControllers.count == 0) {
|
||||
BOOL shouldAnimate = !firstTimePush && ((RNSScreenView *) lastTop.view).stackAnimation != RNSScreenStackAnimationNone && !_controller.presentedViewController;
|
||||
|
||||
if (firstTimePush) {
|
||||
// nothing pushed yet
|
||||
[_controller setViewControllers:@[top] animated:NO];
|
||||
[_controller setViewControllers:controllers animated:NO];
|
||||
} else if (top != lastTop) {
|
||||
if (![controllers containsObject:lastTop]) {
|
||||
// last top controller is no longer on stack
|
||||
@@ -249,12 +276,18 @@
|
||||
_controller.view.frame = self.bounds;
|
||||
}
|
||||
|
||||
- (void)invalidate
|
||||
{
|
||||
for (UIViewController *controller in _presentedModals) {
|
||||
[controller dismissViewControllerAnimated:NO completion:nil];
|
||||
}
|
||||
[_presentedModals removeAllObjects];
|
||||
}
|
||||
|
||||
- (void)dismissOnReload
|
||||
{
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
for (UIViewController *controller in self->_presentedModals) {
|
||||
[controller dismissViewControllerAnimated:NO completion:nil];
|
||||
}
|
||||
[self invalidate];
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
#import <React/RCTViewManager.h>
|
||||
#import <React/RCTConvert.h>
|
||||
|
||||
#import "RNSScreen.h"
|
||||
|
||||
@interface RNSScreenStackHeaderConfig : UIView
|
||||
|
||||
@property (nonatomic, weak) RNSScreenView *screenView;
|
||||
|
||||
@property (nonatomic, retain) NSString *title;
|
||||
@property (nonatomic, retain) NSString *titleFontFamily;
|
||||
@property (nonatomic, retain) NSNumber *titleFontSize;
|
||||
@@ -19,7 +23,6 @@
|
||||
@property (nonatomic) BOOL hideBackButton;
|
||||
@property (nonatomic) BOOL hideShadow;
|
||||
@property (nonatomic) BOOL translucent;
|
||||
@property (nonatomic) BOOL gestureEnabled;
|
||||
|
||||
+ (void)willShowViewController:(UIViewController *)vc withConfig:(RNSScreenStackHeaderConfig*)config;
|
||||
|
||||
@@ -30,6 +33,7 @@
|
||||
@end
|
||||
|
||||
typedef NS_ENUM(NSInteger, RNSScreenStackHeaderSubviewType) {
|
||||
RNSScreenStackHeaderSubviewTypeBackButton,
|
||||
RNSScreenStackHeaderSubviewTypeLeft,
|
||||
RNSScreenStackHeaderSubviewTypeRight,
|
||||
RNSScreenStackHeaderSubviewTypeTitle,
|
||||
|
||||
@@ -5,34 +5,41 @@
|
||||
#import <React/RCTUIManager.h>
|
||||
#import <React/RCTUIManagerUtils.h>
|
||||
#import <React/RCTShadowView.h>
|
||||
#import <React/RCTImageLoader.h>
|
||||
#import <React/RCTImageView.h>
|
||||
#import <React/RCTImageSource.h>
|
||||
|
||||
@interface RNSScreenHeaderItemMeasurements : NSObject
|
||||
@property (nonatomic, readonly) CGSize headerSize;
|
||||
@property (nonatomic, readonly) CGFloat leftPadding;
|
||||
@property (nonatomic, readonly) CGFloat rightPadding;
|
||||
|
||||
- (instancetype)initWithHeaderSize:(CGSize)headerSize leftPadding:(CGFloat)leftPadding rightPadding:(CGFloat)rightPadding;
|
||||
// Some RN private method hacking below. Couldn't figure out better way to access image data
|
||||
// of a given RCTImageView. See more comments in the code section processing SubviewTypeBackButton
|
||||
@interface RCTImageView (Private)
|
||||
- (UIImage*)image;
|
||||
@end
|
||||
|
||||
@implementation RNSScreenHeaderItemMeasurements
|
||||
|
||||
- (instancetype)initWithHeaderSize:(CGSize)headerSize leftPadding:(CGFloat)leftPadding rightPadding:(CGFloat)rightPadding
|
||||
{
|
||||
if (self = [super init]) {
|
||||
_headerSize = headerSize;
|
||||
_leftPadding = leftPadding;
|
||||
_rightPadding = rightPadding;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
@interface RCTImageLoader (Private)
|
||||
- (id<RCTImageCache>)imageCache;
|
||||
@end
|
||||
|
||||
|
||||
@interface RNSScreenStackHeaderSubview : UIView
|
||||
|
||||
@property (nonatomic, weak) RCTBridge *bridge;
|
||||
@property (nonatomic, weak) UIView *reactSuperview;
|
||||
@property (nonatomic) RNSScreenStackHeaderSubviewType type;
|
||||
|
||||
- (instancetype)initWithBridge:(RCTBridge*)bridge;
|
||||
|
||||
@end
|
||||
|
||||
@implementation RNSScreenStackHeaderSubview
|
||||
|
||||
- (instancetype)initWithBridge:(RCTBridge *)bridge
|
||||
{
|
||||
if (self = [super init]) {
|
||||
_bridge = bridge;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@implementation RNSScreenStackHeaderConfig {
|
||||
@@ -45,7 +52,6 @@
|
||||
self.hidden = YES;
|
||||
_translucent = YES;
|
||||
_reactSubviews = [NSMutableArray new];
|
||||
_gestureEnabled = YES;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
@@ -66,60 +72,107 @@
|
||||
return _reactSubviews;
|
||||
}
|
||||
|
||||
- (UIViewController*)screen
|
||||
- (UIView *)reactSuperview
|
||||
{
|
||||
UIView *superview = self.superview;
|
||||
if ([superview isKindOfClass:[RNSScreenView class]]) {
|
||||
return ((RNSScreenView *)superview).controller;
|
||||
return _screenView;
|
||||
}
|
||||
|
||||
- (void)removeFromSuperview
|
||||
{
|
||||
[super removeFromSuperview];
|
||||
_screenView = nil;
|
||||
}
|
||||
|
||||
- (void)updateViewControllerIfNeeded
|
||||
{
|
||||
UIViewController *vc = _screenView.controller;
|
||||
UINavigationController *nav = (UINavigationController*) vc.parentViewController;
|
||||
UIViewController *nextVC = nav.visibleViewController;
|
||||
if (nav.transitionCoordinator != nil) {
|
||||
// if navigator is performing transition instead of allowing to update of `visibleConttroller`
|
||||
// we look at `topController`. This is because during transitiong the `visibleController` won't
|
||||
// point to the controller that is going to be revealed after transition. This check fixes the
|
||||
// problem when config gets updated while the transition is ongoing.
|
||||
nextVC = nav.topViewController;
|
||||
}
|
||||
return nil;
|
||||
|
||||
if (vc != nil && nextVC == vc) {
|
||||
[RNSScreenStackHeaderConfig updateViewController:self.screenView.controller withConfig:self];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)didSetProps:(NSArray<NSString *> *)changedProps
|
||||
{
|
||||
[super didSetProps:changedProps];
|
||||
[self updateViewControllerIfNeeded];
|
||||
}
|
||||
|
||||
- (void)didUpdateReactSubviews
|
||||
{
|
||||
[super didUpdateReactSubviews];
|
||||
[self updateViewControllerIfNeeded];
|
||||
}
|
||||
|
||||
+ (void)setAnimatedConfig:(UIViewController *)vc withConfig:(RNSScreenStackHeaderConfig *)config
|
||||
{
|
||||
UINavigationBar *navbar = ((UINavigationController *)vc.parentViewController).navigationBar;
|
||||
BOOL hideShadow = config.hideShadow;
|
||||
[navbar setTintColor:config.color];
|
||||
if (config.backgroundColor && CGColorGetAlpha(config.backgroundColor.CGColor) == 0.) {
|
||||
[navbar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
|
||||
[navbar setBarTintColor:[UIColor clearColor]];
|
||||
hideShadow = YES;
|
||||
|
||||
if (@available(iOS 13.0, *)) {
|
||||
// font customized on the navigation item level, so nothing to do here
|
||||
} else {
|
||||
[navbar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
|
||||
[navbar setBarTintColor:config.backgroundColor];
|
||||
}
|
||||
[navbar setTranslucent:config.translucent];
|
||||
[navbar setValue:@(hideShadow ? YES : NO) forKey:@"hidesShadow"];
|
||||
BOOL hideShadow = config.hideShadow;
|
||||
|
||||
if (config.titleFontFamily || config.titleFontSize || config.titleColor) {
|
||||
NSMutableDictionary *attrs = [NSMutableDictionary new];
|
||||
|
||||
if (config.titleColor) {
|
||||
attrs[NSForegroundColorAttributeName] = config.titleColor;
|
||||
}
|
||||
|
||||
CGFloat size = config.titleFontSize ? [config.titleFontSize floatValue] : 17;
|
||||
if (config.titleFontFamily) {
|
||||
attrs[NSFontAttributeName] = [UIFont fontWithName:config.titleFontFamily size:size];
|
||||
if (config.backgroundColor && CGColorGetAlpha(config.backgroundColor.CGColor) == 0.) {
|
||||
[navbar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
|
||||
[navbar setBarTintColor:[UIColor clearColor]];
|
||||
hideShadow = YES;
|
||||
} else {
|
||||
attrs[NSFontAttributeName] = [UIFont boldSystemFontOfSize:size];
|
||||
[navbar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
|
||||
[navbar setBarTintColor:config.backgroundColor];
|
||||
}
|
||||
[navbar setTitleTextAttributes:attrs];
|
||||
}
|
||||
|
||||
if (@available(iOS 11.0, *) && config.largeTitle) {
|
||||
if (config.largeTitleFontFamily || config.largeTitleFontSize || config.titleColor) {
|
||||
NSMutableDictionary *largeAttrs = [NSMutableDictionary new];
|
||||
[navbar setTranslucent:config.translucent];
|
||||
[navbar setValue:@(hideShadow ? YES : NO) forKey:@"hidesShadow"];
|
||||
|
||||
if (config.titleFontFamily || config.titleFontSize || config.titleColor) {
|
||||
NSMutableDictionary *attrs = [NSMutableDictionary new];
|
||||
|
||||
if (config.titleColor) {
|
||||
largeAttrs[NSForegroundColorAttributeName] = config.titleColor;
|
||||
attrs[NSForegroundColorAttributeName] = config.titleColor;
|
||||
}
|
||||
CGFloat largeSize = config.largeTitleFontSize ? [config.largeTitleFontSize floatValue] : 34;
|
||||
if (config.largeTitleFontFamily) {
|
||||
largeAttrs[NSFontAttributeName] = [UIFont fontWithName:config.largeTitleFontFamily size:largeSize];
|
||||
|
||||
CGFloat size = config.titleFontSize ? [config.titleFontSize floatValue] : 17;
|
||||
if (config.titleFontFamily) {
|
||||
attrs[NSFontAttributeName] = [UIFont fontWithName:config.titleFontFamily size:size];
|
||||
} else {
|
||||
largeAttrs[NSFontAttributeName] = [UIFont boldSystemFontOfSize:largeSize];
|
||||
attrs[NSFontAttributeName] = [UIFont boldSystemFontOfSize:size];
|
||||
}
|
||||
[navbar setLargeTitleTextAttributes:largeAttrs];
|
||||
[navbar setTitleTextAttributes:attrs];
|
||||
}
|
||||
|
||||
if (@available(iOS 11.0, *)) {
|
||||
if (config.largeTitle && (config.largeTitleFontFamily || config.largeTitleFontSize || config.titleColor)) {
|
||||
NSMutableDictionary *largeAttrs = [NSMutableDictionary new];
|
||||
if (config.titleColor) {
|
||||
largeAttrs[NSForegroundColorAttributeName] = config.titleColor;
|
||||
}
|
||||
CGFloat largeSize = config.largeTitleFontSize ? [config.largeTitleFontSize floatValue] : 34;
|
||||
if (config.largeTitleFontFamily) {
|
||||
largeAttrs[NSFontAttributeName] = [UIFont fontWithName:config.largeTitleFontFamily size:largeSize];
|
||||
} else {
|
||||
largeAttrs[NSFontAttributeName] = [UIFont boldSystemFontOfSize:largeSize];
|
||||
}
|
||||
[navbar setLargeTitleTextAttributes:largeAttrs];
|
||||
}
|
||||
}
|
||||
|
||||
UIImage *backButtonImage = [self loadBackButtonImageInViewController:vc withConfig:config];
|
||||
if (backButtonImage) {
|
||||
navbar.backIndicatorImage = backButtonImage;
|
||||
navbar.backIndicatorTransitionMaskImage = backButtonImage;
|
||||
} else if (navbar.backIndicatorImage) {
|
||||
navbar.backIndicatorImage = nil;
|
||||
navbar.backIndicatorTransitionMaskImage = nil;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -135,7 +188,65 @@
|
||||
}
|
||||
}
|
||||
|
||||
+ (UIImage*)loadBackButtonImageInViewController:(UIViewController *)vc
|
||||
withConfig:(RNSScreenStackHeaderConfig *)config
|
||||
{
|
||||
BOOL hasBackButtonImage = NO;
|
||||
for (RNSScreenStackHeaderSubview *subview in config.reactSubviews) {
|
||||
if (subview.type == RNSScreenStackHeaderSubviewTypeBackButton && subview.subviews.count > 0) {
|
||||
hasBackButtonImage = YES;
|
||||
RCTImageView *imageView = subview.subviews[0];
|
||||
UIImage *image = imageView.image;
|
||||
// IMPORTANT!!!
|
||||
// image can be nil in DEV MODE ONLY
|
||||
//
|
||||
// It is so, because in dev mode images are loaded over HTTP from the packager. In that case
|
||||
// we first check if image is already loaded in cache and if it is, we take it from cache and
|
||||
// display immediately. Otherwise we wait for the transition to finish and retry updating
|
||||
// header config.
|
||||
// Unfortunately due to some problems in UIKit we cannot update the image while the screen
|
||||
// transition is ongoing. This results in the settings being reset after the transition is done
|
||||
// to the state from before the transition.
|
||||
if (image == nil) {
|
||||
// in DEV MODE we try to load from cache (we use private API for that as it is not exposed
|
||||
// publically in headers).
|
||||
RCTImageSource *source = imageView.imageSources[0];
|
||||
image = [subview.bridge.imageLoader.imageCache
|
||||
imageForUrl:source.request.URL.absoluteString
|
||||
size:source.size
|
||||
scale:source.scale
|
||||
resizeMode:imageView.resizeMode];
|
||||
}
|
||||
if (image == nil) {
|
||||
// This will be triggered if the image is not in the cache yet. What we do is we wait until
|
||||
// the end of transition and run header config updates again. We could potentially wait for
|
||||
// image on load to trigger, but that would require even more private method hacking.
|
||||
if (vc.transitionCoordinator) {
|
||||
[vc.transitionCoordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
|
||||
// nothing, we just want completion
|
||||
} completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
|
||||
// in order for new back button image to be loaded we need to trigger another change
|
||||
// in back button props that'd make UIKit redraw the button. Otherwise the changes are
|
||||
// not reflected. Here we change back button visibility which is then immediately restored
|
||||
vc.navigationItem.hidesBackButton = YES;
|
||||
[config updateViewControllerIfNeeded];
|
||||
}];
|
||||
}
|
||||
return [UIImage new];
|
||||
} else {
|
||||
return image;
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
+ (void)willShowViewController:(UIViewController *)vc withConfig:(RNSScreenStackHeaderConfig *)config
|
||||
{
|
||||
[self updateViewController:vc withConfig:config];
|
||||
}
|
||||
|
||||
+ (void)updateViewController:(UIViewController *)vc withConfig:(RNSScreenStackHeaderConfig *)config
|
||||
{
|
||||
UINavigationItem *navitem = vc.navigationItem;
|
||||
UINavigationController *navctr = (UINavigationController *)vc.parentViewController;
|
||||
@@ -146,17 +257,26 @@
|
||||
BOOL wasHidden = navctr.navigationBarHidden;
|
||||
BOOL shouldHide = config == nil || config.hide;
|
||||
|
||||
if (!shouldHide && !config.translucent) {
|
||||
// when nav bar is not translucent we chage edgesForExtendedLayout to avoid system laying out
|
||||
// the screen underneath navigation controllers
|
||||
vc.edgesForExtendedLayout = UIRectEdgeNone;
|
||||
} else {
|
||||
// system default is UIRectEdgeAll
|
||||
vc.edgesForExtendedLayout = UIRectEdgeAll;
|
||||
}
|
||||
|
||||
[navctr setNavigationBarHidden:shouldHide animated:YES];
|
||||
navctr.interactivePopGestureRecognizer.enabled = config.gestureEnabled;
|
||||
#ifdef __IPHONE_13_0
|
||||
vc.modalInPresentation = !config.gestureEnabled;
|
||||
if (@available(iOS 13.0, *)) {
|
||||
vc.modalInPresentation = !config.screenView.gestureEnabled;
|
||||
}
|
||||
#endif
|
||||
if (shouldHide) {
|
||||
return;
|
||||
}
|
||||
|
||||
navitem.title = config.title;
|
||||
navitem.hidesBackButton = config.hideBackButton;
|
||||
if (config.backTitle != nil) {
|
||||
prevItem.backBarButtonItem = [[UIBarButtonItem alloc]
|
||||
initWithTitle:config.backTitle
|
||||
@@ -183,7 +303,84 @@
|
||||
}
|
||||
navitem.largeTitleDisplayMode = config.largeTitle ? UINavigationItemLargeTitleDisplayModeAlways : UINavigationItemLargeTitleDisplayModeNever;
|
||||
}
|
||||
#ifdef __IPHONE_13_0
|
||||
if (@available(iOS 13.0, *)) {
|
||||
UINavigationBarAppearance *appearance = [UINavigationBarAppearance new];
|
||||
|
||||
if (config.backgroundColor && CGColorGetAlpha(config.backgroundColor.CGColor) == 0.) {
|
||||
// transparent background color
|
||||
[appearance configureWithTransparentBackground];
|
||||
} else {
|
||||
// non-transparent background or default background
|
||||
if (config.translucent) {
|
||||
[appearance configureWithDefaultBackground];
|
||||
} else {
|
||||
[appearance configureWithOpaqueBackground];
|
||||
}
|
||||
|
||||
// set background color if specified
|
||||
if (config.backgroundColor) {
|
||||
appearance.backgroundColor = config.backgroundColor;
|
||||
}
|
||||
}
|
||||
|
||||
if (config.backgroundColor && CGColorGetAlpha(config.backgroundColor.CGColor) == 0.) {
|
||||
appearance.backgroundColor = config.backgroundColor;
|
||||
}
|
||||
|
||||
if (config.hideShadow) {
|
||||
appearance.shadowColor = nil;
|
||||
}
|
||||
|
||||
if (config.titleFontFamily || config.titleFontSize || config.titleColor) {
|
||||
NSMutableDictionary *attrs = [NSMutableDictionary new];
|
||||
|
||||
if (config.titleColor) {
|
||||
attrs[NSForegroundColorAttributeName] = config.titleColor;
|
||||
}
|
||||
|
||||
CGFloat size = config.titleFontSize ? [config.titleFontSize floatValue] : 17;
|
||||
if (config.titleFontFamily) {
|
||||
attrs[NSFontAttributeName] = [UIFont fontWithName:config.titleFontFamily size:size];
|
||||
} else {
|
||||
attrs[NSFontAttributeName] = [UIFont boldSystemFontOfSize:size];
|
||||
}
|
||||
appearance.titleTextAttributes = attrs;
|
||||
}
|
||||
|
||||
if (config.largeTitleFontFamily || config.largeTitleFontSize || config.titleColor) {
|
||||
NSMutableDictionary *largeAttrs = [NSMutableDictionary new];
|
||||
|
||||
if (config.titleColor) {
|
||||
largeAttrs[NSForegroundColorAttributeName] = config.titleColor;
|
||||
}
|
||||
|
||||
CGFloat largeSize = config.largeTitleFontSize ? [config.largeTitleFontSize floatValue] : 34;
|
||||
if (config.largeTitleFontFamily) {
|
||||
largeAttrs[NSFontAttributeName] = [UIFont fontWithName:config.largeTitleFontFamily size:largeSize];
|
||||
} else {
|
||||
largeAttrs[NSFontAttributeName] = [UIFont boldSystemFontOfSize:largeSize];
|
||||
}
|
||||
|
||||
appearance.largeTitleTextAttributes = largeAttrs;
|
||||
}
|
||||
|
||||
UIImage *backButtonImage = [self loadBackButtonImageInViewController:vc withConfig:config];
|
||||
if (backButtonImage) {
|
||||
[appearance setBackIndicatorImage:backButtonImage transitionMaskImage:backButtonImage];
|
||||
} else if (appearance.backIndicatorImage) {
|
||||
[appearance setBackIndicatorImage:nil transitionMaskImage:nil];
|
||||
}
|
||||
|
||||
navitem.standardAppearance = appearance;
|
||||
navitem.compactAppearance = appearance;
|
||||
navitem.scrollEdgeAppearance = appearance;
|
||||
}
|
||||
#endif
|
||||
navitem.hidesBackButton = config.hideBackButton;
|
||||
navitem.leftBarButtonItem = nil;
|
||||
navitem.rightBarButtonItem = nil;
|
||||
navitem.titleView = nil;
|
||||
for (RNSScreenStackHeaderSubview *subview in config.reactSubviews) {
|
||||
switch (subview.type) {
|
||||
case RNSScreenStackHeaderSubviewTypeLeft: {
|
||||
@@ -198,17 +395,22 @@
|
||||
}
|
||||
case RNSScreenStackHeaderSubviewTypeCenter:
|
||||
case RNSScreenStackHeaderSubviewTypeTitle: {
|
||||
subview.translatesAutoresizingMaskIntoConstraints = NO;
|
||||
navitem.titleView = subview;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (vc.transitionCoordinator != nil && !wasHidden) {
|
||||
[vc.transitionCoordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
|
||||
|
||||
} completion:nil];
|
||||
if (vc.transitionCoordinator != nil
|
||||
&& vc.transitionCoordinator.presentationStyle == UIModalPresentationNone
|
||||
&& !wasHidden) {
|
||||
// when there is an ongoing transition we may need to update navbar setting in animation block
|
||||
// using animateAlongsideTransition. However, we only do that given the transition is not a modal
|
||||
// transition (presentationStyle == UIModalPresentationNone) and that the bar was not previously
|
||||
// hidden. This is because both for modal transitions and transitions from screen with hidden bar
|
||||
// the transition animation block does not get triggered. This is ok, because with both of those
|
||||
// types of transitions there is no "shared" navigation bar that needs to be updated in an animated
|
||||
// way.
|
||||
[vc.transitionCoordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
|
||||
[self setAnimatedConfig:vc withConfig:config];
|
||||
} completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
|
||||
@@ -257,13 +459,13 @@ RCT_EXPORT_VIEW_PROPERTY(hideShadow, BOOL)
|
||||
// `hidden` is an UIView property, we need to use different name internally
|
||||
RCT_REMAP_VIEW_PROPERTY(hidden, hide, BOOL)
|
||||
RCT_EXPORT_VIEW_PROPERTY(translucent, BOOL)
|
||||
RCT_EXPORT_VIEW_PROPERTY(gestureEnabled, BOOL)
|
||||
|
||||
@end
|
||||
|
||||
@implementation RCTConvert (RNSScreenStackHeader)
|
||||
|
||||
RCT_ENUM_CONVERTER(RNSScreenStackHeaderSubviewType, (@{
|
||||
@"back": @(RNSScreenStackHeaderSubviewTypeBackButton),
|
||||
@"left": @(RNSScreenStackHeaderSubviewTypeLeft),
|
||||
@"right": @(RNSScreenStackHeaderSubviewTypeRight),
|
||||
@"title": @(RNSScreenStackHeaderSubviewTypeTitle),
|
||||
@@ -272,69 +474,6 @@ RCT_ENUM_CONVERTER(RNSScreenStackHeaderSubviewType, (@{
|
||||
|
||||
@end
|
||||
|
||||
@implementation RNSScreenStackHeaderSubview {
|
||||
__weak RCTBridge *_bridge;
|
||||
}
|
||||
|
||||
- (instancetype)initWithBridge:(RCTBridge *)bridge
|
||||
{
|
||||
if (self = [super init]) {
|
||||
_bridge = bridge;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)layoutSubviews
|
||||
{
|
||||
[super layoutSubviews];
|
||||
if (!self.translatesAutoresizingMaskIntoConstraints) {
|
||||
CGSize size = self.superview.frame.size;
|
||||
CGFloat right = size.width - self.frame.size.width - self.frame.origin.x;
|
||||
CGFloat left = self.frame.origin.x;
|
||||
[_bridge.uiManager
|
||||
setLocalData:[[RNSScreenHeaderItemMeasurements alloc]
|
||||
initWithHeaderSize:size
|
||||
leftPadding:left rightPadding:right]
|
||||
forView:self];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)reactSetFrame:(CGRect)frame
|
||||
{
|
||||
if (self.translatesAutoresizingMaskIntoConstraints) {
|
||||
[super reactSetFrame:frame];
|
||||
}
|
||||
}
|
||||
|
||||
- (CGSize)intrinsicContentSize
|
||||
{
|
||||
return UILayoutFittingExpandedSize;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@interface RNSScreenStackHeaderSubviewShadow : RCTShadowView
|
||||
@end
|
||||
|
||||
@implementation RNSScreenStackHeaderSubviewShadow
|
||||
|
||||
- (void)setLocalData:(RNSScreenHeaderItemMeasurements *)data
|
||||
{
|
||||
self.width = (YGValue){data.headerSize.width - data.leftPadding - data.rightPadding, YGUnitPoint};
|
||||
self.height = (YGValue){data.headerSize.height, YGUnitPoint};
|
||||
|
||||
if (data.leftPadding > data.rightPadding) {
|
||||
self.paddingLeft = (YGValue){0, YGUnitPoint};
|
||||
self.paddingRight = (YGValue){data.leftPadding - data.rightPadding, YGUnitPoint};
|
||||
} else {
|
||||
self.paddingLeft = (YGValue){data.rightPadding - data.leftPadding, YGUnitPoint};
|
||||
self.paddingRight = (YGValue){0, YGUnitPoint};
|
||||
}
|
||||
[self didSetProps:@[@"width", @"height", @"paddingLeft", @"paddingRight"]];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@implementation RNSScreenStackHeaderSubviewManager
|
||||
|
||||
RCT_EXPORT_MODULE()
|
||||
@@ -346,9 +485,4 @@ RCT_EXPORT_VIEW_PROPERTY(type, RNSScreenStackHeaderSubviewType)
|
||||
return [[RNSScreenStackHeaderSubview alloc] initWithBridge:self.bridge];
|
||||
}
|
||||
|
||||
- (RCTShadowView *)shadowView
|
||||
{
|
||||
return [RNSScreenStackHeaderSubviewShadow new];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "react-native-screens",
|
||||
"version": "2.0.0-alpha.5",
|
||||
"version": "2.0.0-beta.2",
|
||||
"description": "First incomplete navigation solution for your react-native app.",
|
||||
"scripts": {
|
||||
"start": "node node_modules/react-native/local-cli/cli.js start",
|
||||
|
||||
129
src/screens.d.ts
vendored
129
src/screens.d.ts
vendored
@@ -3,21 +3,144 @@
|
||||
|
||||
declare module 'react-native-screens' {
|
||||
import { ComponentClass } from 'react';
|
||||
import { ViewProps, Animated } from 'react-native';
|
||||
import {
|
||||
ViewProps,
|
||||
Animated,
|
||||
NativeSyntheticEvent,
|
||||
NativeTouchEvent,
|
||||
ImageProps,
|
||||
} from 'react-native';
|
||||
|
||||
export function useScreens(shouldUseScreens?: boolean): void;
|
||||
export function enableScreens(shouldEnableScreens?: boolean): void;
|
||||
export function screensEnabled(): boolean;
|
||||
|
||||
export type StackPresentationTypes = 'push' | 'modal' | 'transparentModal';
|
||||
export type StackAnimationTypes = 'default' | 'fade' | 'flip' | 'none';
|
||||
|
||||
export interface ScreenProps extends ViewProps {
|
||||
active?: 0 | 1 | Animated.AnimatedInterpolation;
|
||||
onComponentRef?: (view: any) => void;
|
||||
children?: React.ReactNode;
|
||||
onAppear?: (e: NativeSyntheticEvent<NativeTouchEvent>) => void;
|
||||
/**
|
||||
*@description A callback that gets called when the current screen is dismissed by hardware back (on Android) or dismiss gesture (swipe back or down). The callback takes no arguments.
|
||||
*/
|
||||
onDismissed?: (e: NativeSyntheticEvent<NativeTouchEvent>) => void;
|
||||
/**
|
||||
* @type "push" – the new screen will be pushed onto a stack which on iOS means that the default animation will be slide from the side, the animation on Android may vary depending on the OS version and theme.
|
||||
* @type "modal" – the new screen will be presented modally. In addition this allow for a nested stack to be rendered inside such screens
|
||||
* @type "transparentModal" – the new screen will be presented modally but in addition the second to last screen will remain attached to the stack container such that if the top screen is non opaque the content below can still be seen. If "modal" is used instead the below screen will get unmounted as soon as the transition ends.
|
||||
*/
|
||||
stackPresentation: StackPresentationTypes;
|
||||
/**
|
||||
*@description Allows for the customization of how the given screen should appear/dissapear when pushed or popped at the top of the stack. The followin values are currently supported:
|
||||
* @type "default" – uses a platform default animation
|
||||
* @type "fade" – fades screen in or out
|
||||
* @type "flip" – flips the screen, requires stackPresentation: "modal" (iOS only)
|
||||
* @type "none" – the screen appears/dissapears without an animation
|
||||
*/
|
||||
stackAnimation?: StackAnimationTypes;
|
||||
/**
|
||||
* @description When set to false the back swipe gesture will be disabled when the parent Screen is on top of the stack. The default value is true.
|
||||
*/
|
||||
gestureEnabled?: boolean;
|
||||
}
|
||||
export const Screen: ComponentClass<ScreenProps>;
|
||||
|
||||
export type ScreenContainerProps = ViewProps;
|
||||
export const ScreenContainer: ComponentClass<ScreenContainerProps>;
|
||||
|
||||
export interface ScreenStackProps extends ViewProps {
|
||||
transitioning?: number;
|
||||
progress?: number;
|
||||
}
|
||||
|
||||
export interface ScreenStackHeaderConfigProps extends ViewProps {
|
||||
/**
|
||||
*@description String that representing screen title that will get rendered in the middle section of the header. On iOS the title is centered on the header while on Android it is aligned to the left and placed next to back button (if one is present).
|
||||
*/
|
||||
title?: string;
|
||||
/**
|
||||
*@description When set to true the header will be hidden while the parent Screen is on the top of the stack. The default value is false.
|
||||
*/
|
||||
hidden?: boolean;
|
||||
/**
|
||||
*@description Controls the color of items rendered on the header. This includes back icon, back text (iOS only) and title text. If you want the title to have different color use titleColor property.
|
||||
*/
|
||||
color?: string;
|
||||
/**
|
||||
*@description Customize font family to be used for the title.
|
||||
*/
|
||||
titleFontFamily?: string;
|
||||
/**
|
||||
*@description Customize the size of the font to be used for the title.
|
||||
*/
|
||||
titleFontSize?: number;
|
||||
/**
|
||||
*@description Allows for setting text color of the title.
|
||||
*/
|
||||
titleColor?: string;
|
||||
/**
|
||||
*@description Controlls the color of the navigation header.
|
||||
*/
|
||||
backgroundColor?: string;
|
||||
/**
|
||||
* @description Boolean that allows for disabling drop shadow under navigation header. The default value is true.
|
||||
*/
|
||||
hideShadow?: boolean;
|
||||
/**
|
||||
* @description If set to true the back button will not be rendered as a part of navigation header.
|
||||
*/
|
||||
hideBackButton?: boolean;
|
||||
/**
|
||||
* @host (iOS only)
|
||||
* @description When set to true, it makes native navigation bar on iOS semi transparent with blur effect. It is a common way of presenting navigation bar introduced in iOS 11. The default value is false
|
||||
*/
|
||||
translucent?: boolean;
|
||||
/**
|
||||
* @host (iOS only)
|
||||
* @description Allows for controlling the string to be rendered next to back button. By default iOS uses the title of the previous screen.
|
||||
*/
|
||||
backTitle?: string;
|
||||
/**
|
||||
* @host (iOS only)
|
||||
* @description Allows for customizing font family to be used for back button title on iOS.
|
||||
*/
|
||||
backTitleFontFamily?: string;
|
||||
/**
|
||||
* @host (iOS only)
|
||||
* @description Allows for customizing font size to be used for back button title on iOS.
|
||||
*/
|
||||
backTitleFontSize?: number;
|
||||
/**
|
||||
* @host (iOS only)
|
||||
* @description When set to true it makes the title display using the large title effect.
|
||||
*/
|
||||
largeTitle?: boolean;
|
||||
/**
|
||||
* @host (iOS only)
|
||||
* @description Customize font family to be used for the large title.
|
||||
*/
|
||||
largeTitleFontFamily?: string;
|
||||
/**
|
||||
* @host (iOS only)
|
||||
* @description Customize the size of the font to be used for the large title.
|
||||
*/
|
||||
largeTitleFontSize?: number;
|
||||
/**
|
||||
* Pass HeaderLeft, HeaderRight and HeaderTitle
|
||||
*/
|
||||
children?: React.ReactNode;
|
||||
}
|
||||
|
||||
export const Screen: ComponentClass<ScreenProps>;
|
||||
export const ScreenContainer: ComponentClass<ScreenContainerProps>;
|
||||
export const NativeScreen: ComponentClass<ScreenProps>;
|
||||
export const NativeScreenContainer: ComponentClass<ScreenContainerProps>;
|
||||
export const ScreenStack: ComponentClass<ScreenStackProps>;
|
||||
export const ScreenStackHeaderBackButtonImage: ComponentClass<ImageProps>;
|
||||
export const ScreenStackHeaderLeftView: ComponentClass<ViewProps>;
|
||||
export const ScreenStackHeaderRightView: ComponentClass<ViewProps>;
|
||||
export const ScreenStackHeaderConfig: ComponentClass<
|
||||
ScreenStackHeaderConfigProps
|
||||
>;
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import {
|
||||
requireNativeComponent,
|
||||
View,
|
||||
UIManager,
|
||||
Image,
|
||||
StyleSheet,
|
||||
} from 'react-native';
|
||||
import { version } from 'react-native/Libraries/Core/ReactNativeVersion';
|
||||
@@ -17,7 +18,7 @@ const getViewManagerConfigCompat = name =>
|
||||
? UIManager.getViewManagerConfig(name)
|
||||
: UIManager[name];
|
||||
|
||||
export function enableScreens(shouldEnableScreens = true) {
|
||||
function enableScreens(shouldEnableScreens = true) {
|
||||
ENABLE_SCREENS = shouldEnableScreens;
|
||||
if (ENABLE_SCREENS && !getViewManagerConfigCompat('RNSScreen')) {
|
||||
console.error(
|
||||
@@ -27,11 +28,12 @@ export function enableScreens(shouldEnableScreens = true) {
|
||||
}
|
||||
|
||||
// we should remove this at some point
|
||||
export function useScreens(shouldUseScreens = true) {
|
||||
function useScreens(shouldUseScreens = true) {
|
||||
console.warn('Method `useScreens` is deprecated, please use `enableScreens`');
|
||||
enableScreens(shouldUseScreens);
|
||||
}
|
||||
|
||||
export function screensEnabled() {
|
||||
function screensEnabled() {
|
||||
return ENABLE_SCREENS;
|
||||
}
|
||||
|
||||
@@ -145,6 +147,14 @@ const styles = StyleSheet.create({
|
||||
},
|
||||
});
|
||||
|
||||
const ScreenStackHeaderBackButtonImage = props => (
|
||||
<ScreensNativeModules.NativeScreenStackHeaderSubview
|
||||
type="back"
|
||||
style={styles.headerSubview}>
|
||||
<Image resizeMode="center" fadeDuration={0} {...props} />
|
||||
</ScreensNativeModules.NativeScreenStackHeaderSubview>
|
||||
);
|
||||
|
||||
const ScreenStackHeaderRightView = props => (
|
||||
<ScreensNativeModules.NativeScreenStackHeaderSubview
|
||||
{...props}
|
||||
@@ -161,14 +171,6 @@ const ScreenStackHeaderLeftView = props => (
|
||||
/>
|
||||
);
|
||||
|
||||
const ScreenStackHeaderTitleView = props => (
|
||||
<ScreensNativeModules.NativeScreenStackHeaderSubview
|
||||
{...props}
|
||||
type="title"
|
||||
style={styles.headerSubview}
|
||||
/>
|
||||
);
|
||||
|
||||
const ScreenStackHeaderCenterView = props => (
|
||||
<ScreensNativeModules.NativeScreenStackHeaderSubview
|
||||
{...props}
|
||||
@@ -197,11 +199,12 @@ module.exports = {
|
||||
get ScreenStackHeaderSubview() {
|
||||
return ScreensNativeModules.NativeScreenStackHeaderSubview;
|
||||
},
|
||||
ScreenStackHeaderBackButtonImage,
|
||||
ScreenStackHeaderRightView,
|
||||
ScreenStackHeaderLeftView,
|
||||
ScreenStackHeaderTitleView,
|
||||
ScreenStackHeaderCenterView,
|
||||
|
||||
enableScreens,
|
||||
useScreens,
|
||||
screensEnabled,
|
||||
};
|
||||
|
||||
22
yarn.lock
22
yarn.lock
@@ -1741,10 +1741,10 @@ commander@~2.13.0:
|
||||
version "2.13.0"
|
||||
resolved "https://registry.yarnpkg.com/commander/-/commander-2.13.0.tgz#6964bca67685df7c1f1430c584f07d7597885b9c"
|
||||
|
||||
commander@~2.20.0:
|
||||
version "2.20.0"
|
||||
resolved "https://registry.yarnpkg.com/commander/-/commander-2.20.0.tgz#d58bb2b5c1ee8f87b0d340027e9e94e222c5a422"
|
||||
integrity sha512-7j2y+40w61zy6YC2iRNpUe/NwhNyoXrYpHMrSunaMG64nRnaf96zO/KMQR4OyN/UnE5KLyEBnKHd4aG3rskjpQ==
|
||||
commander@~2.20.3:
|
||||
version "2.20.3"
|
||||
resolved "https://registry.yarnpkg.com/commander/-/commander-2.20.3.tgz#fd485e84c03eb4881c20722ba48035e8531aeb33"
|
||||
integrity sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==
|
||||
|
||||
commondir@^1.0.1:
|
||||
version "1.0.1"
|
||||
@@ -2765,9 +2765,9 @@ growly@^1.3.0:
|
||||
resolved "https://registry.yarnpkg.com/growly/-/growly-1.3.0.tgz#f10748cbe76af964b7c96c93c6bcc28af120c081"
|
||||
|
||||
handlebars@^4.0.3:
|
||||
version "4.1.2"
|
||||
resolved "https://registry.yarnpkg.com/handlebars/-/handlebars-4.1.2.tgz#b6b37c1ced0306b221e094fc7aca3ec23b131b67"
|
||||
integrity sha512-nvfrjqvt9xQ8Z/w0ijewdD/vvWDTOweBUm96NTr66Wfvo1mJenBLwcYmPs3TIBP5ruzYGD7Hx/DaM9RmhroGPw==
|
||||
version "4.5.3"
|
||||
resolved "https://registry.yarnpkg.com/handlebars/-/handlebars-4.5.3.tgz#5cf75bd8714f7605713511a56be7c349becb0482"
|
||||
integrity sha512-3yPecJoJHK/4c6aZhSvxOyG4vJKDshV36VHp0iVCDVh7o9w2vwi3NSnL2MMPj3YdduqaBcu7cGbggJQM0br9xA==
|
||||
dependencies:
|
||||
neo-async "^2.6.0"
|
||||
optimist "^0.6.1"
|
||||
@@ -5855,11 +5855,11 @@ uglify-es@^3.1.9:
|
||||
source-map "~0.6.1"
|
||||
|
||||
uglify-js@^3.1.4:
|
||||
version "3.6.0"
|
||||
resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-3.6.0.tgz#704681345c53a8b2079fb6cec294b05ead242ff5"
|
||||
integrity sha512-W+jrUHJr3DXKhrsS7NUVxn3zqMOFn0hL/Ei6v0anCIMoKC93TjcflTagwIHLW7SfMFfiQuktQyFVCFHGUE0+yg==
|
||||
version "3.7.3"
|
||||
resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-3.7.3.tgz#f918fce9182f466d5140f24bb0ff35c2d32dcc6a"
|
||||
integrity sha512-7tINm46/3puUA4hCkKYo4Xdts+JDaVC9ZPRcG8Xw9R4nhO/gZgUM3TENq8IF4Vatk8qCig4MzP/c8G4u2BkVQg==
|
||||
dependencies:
|
||||
commander "~2.20.0"
|
||||
commander "~2.20.3"
|
||||
source-map "~0.6.1"
|
||||
|
||||
ultron@1.0.x:
|
||||
|
||||
Reference in New Issue
Block a user