Skip to content

Commit c8cc208

Browse files
authored
feat(iOS): Add a useLegacy flag to switch between the old/new iOS implementation (#783)
* wip: unsuspicious changes * wip: unsuspicious changes v2 * FABRIC NEW IMPL -> OLD IMPL * wip: bring back useLegacy on the RN side * wip: bring back duplicate types to fix codegen issues * wip: remove #705 related code for now * wip: old/new impl division first draft * wip: old/new impl division continued * wip: old/new impl v3 * wip: add a `LEGACY_` prefix to all legacy implementation-related symbols * wip: fix styles for new implementation on Fabric * wip: move old/new impl into separate folders * wip: fix old impl fabric symbol names * wip: xcode changes * wip: clean up & unify the naming convention * wip: fix linter issues * wip: fix styles for new implementation on Paper * wip: make Fabric example run on another port by default to make it possible to run both examples in parallel * wip: implement an abstraction over native commands invocations to reduce branching * refactor: remove the unnecessary value for boolean props * fix: bump react-native-safe-area-context to a Fabric-enabled version * feat: bring back & adjust the `bootstrap-fabric` script * feat: adjust the home screen title depending on the used architecture * chore: update example/Podfile.lock * chore: update an Xcode project file after building * ci: make next branch events trigger ios/android build workflows * chore: remove commented-out code related to #705 for now * chore: add legacy implementation explanation comment * wip: Android fixes * fix: unnecessary comma in MainActivity.java * feat: readme makeover * chore: bump react-native-screens & react-native-gesture-handler in example * refactor(android): extract module name to shared variable, add comment for context * chore: remove unnecessary yarn.lock deps * chore(ios): bring back removed build flags * chore(ios): remove unnecessary concurrentRootEnabled method According to React Native Upgrade Helper, this method is to be removed when updating to RN 0.72: https://react-native-community.github.io/upgrade-helper/?from=0.71.14&to=0.72.0#RnDiffApp-ios-RnDiffApp-AppDelegate.mm * fix(android): adjust incorrect param type on Fabric * chore: remove unnecessary tsconfig.json comment * chore(ios): bring back (currently unused) code related to #712 and #705
1 parent 08486ba commit c8cc208

33 files changed

+1589
-185
lines changed

.github/workflows/android.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ on:
44
pull_request:
55
branches:
66
- master
7+
- next
78
paths:
89
- '.github/workflows/android.yml'
910
- 'android/**'
1011
- 'example/android/**'
1112
push:
1213
branches:
1314
- master
15+
- next
1416

1517
concurrency:
1618
group: ${{ github.ref }}-android

.github/workflows/ios.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ on:
44
pull_request:
55
branches:
66
- master
7+
- next
78
paths:
89
- '.github/workflows/ios.yml'
910
- 'ios/**'
1011
- 'example/ios/**'
1112
push:
1213
branches:
1314
- master
15+
- next
1416

1517
concurrency:
1618
group: ${{ github.ref }}-ios

README.md

Lines changed: 74 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
[![iOS Build](https://github.com/callstack/react-native-pager-view/actions/workflows/ios.yml/badge.svg)](https://github.com/callstack/react-native-pager-view/actions/workflows/ios.yml)
99
[![Android Build](https://github.com/callstack/react-native-pager-view/actions/workflows/android.yml/badge.svg)](https://github.com/callstack/react-native-pager-view/actions/workflows/android.yml)
1010

11-
This component allows the user to swipe left and right through pages of data. Under the hood it is using the native [Android ViewPager](https://developer.android.com/reference/android/support/v4/view/ViewPager) and the [iOS UIPageViewController](https://developer.apple.com/documentation/uikit/uipageviewcontroller) implementations. [See it in action!](https://github.com/callstack/react-native-pager-view#preview)
11+
PagerView allows the user to swipe left and right through pages of data.
12+
It leverages the native [Android ViewPager2](https://developer.android.com/jetpack/androidx/releases/viewpager2)
13+
and [iOS UIScrollView](https://developer.apple.com/documentation/uikit/uiscrollview) under the hood.
14+
[See it in action!](https://github.com/callstack/react-native-pager-view#preview)
1215

1316
<br/>
1417
<p align="center">
@@ -17,22 +20,80 @@ This component allows the user to swipe left and right through pages of data. Un
1720

1821
<br/>
1922

20-
## Versions
23+
## Getting started
2124

22-
| 4.x | 5.x |
23-
| ---------- | ----------- |
24-
| iOS | iOS support |
25-
| ViewPager1 | ViewPager2 |
25+
Install the library with:
2626

27-
## Migration
27+
```sh
28+
yarn add react-native-pager-view
29+
```
2830

29-
In version **6.x** support for `transitionStyle` property has been dropped. More information [here](https://github.com/callstack/react-native-pager-view/blob/master/MIGRATION.md).
31+
or:
3032

31-
`"@react-native-community/viewpager"` library has been changed to `react-native-pager-view`. Here you can find more information, how to migrate pager view to the latest [version](https://github.com/callstack/react-native-pager-view/blob/master/MIGRATION.md)
33+
```sh
34+
npm install react-native-pager-view
35+
```
3236

33-
## Getting started
37+
## Migration from `@react-native-community/viewpager`
38+
39+
The `@react-native-community/viewpager` library has been moved and now lives here, in the `react-native-pager-view` repo.
40+
[Here](https://github.com/callstack/react-native-pager-view/blob/master/MIGRATION.md) you can find
41+
more information on how to migrate PagerView to the latest version.
42+
43+
## Versions & compatibility
44+
45+
The underlying iOS/Android native implementations of PagerView have changed over the years.
46+
Here's an overview of the implementation details throughout the library's lifespan:
47+
48+
| | `1.x.x` | `2.x.x` | `5.0.8` | `7.x.x` |
49+
| ------- | ----------- | ---------------------- | ---------------------- | -------------- |
50+
| iOS || `UIPageViewController` | `UIPageViewController` | `UIScrollView` |
51+
| Android | `ViewPager` | `ViewPager` | `ViewPager2` | `ViewPager2` |
52+
53+
### Legacy iOS implementation support (`UIPageViewController`)
54+
55+
As per the table above, the iOS implementation of this library has been rewritten to use
56+
[`UIScrollView`](https://developer.apple.com/documentation/uikit/uiscrollview) over
57+
[`UIPageViewController`](https://developer.apple.com/documentation/uikit/uipageviewcontroller).
58+
59+
For backwards-compatibility purposes, the old iOS implementation is still supported, however — simply pass `true` to
60+
the `useLegacy` boolean prop to switch the used implementation back to `UIPageViewController`.
61+
62+
### Other notes
63+
64+
In version **6.x.x** the support for the iOS `transitionStyle` property has been dropped.
65+
More information [here](https://github.com/callstack/react-native-pager-view/blob/master/MIGRATION.md).
66+
67+
## New architecture support (Fabric)
68+
69+
This library supports both architectures — Paper and Fabric!
3470

35-
`yarn add react-native-pager-view`
71+
To properly migrate to the new architecture and enable it in your app consult the newest
72+
[docs](https://reactnative.dev/docs/new-architecture-app-intro).
73+
74+
From the perspective of PagerView, the steps required to make the library work with Fabric are as follows:
75+
76+
### iOS
77+
78+
Install pods the usual way, with the `RCT_NEW_ARCH_ENABLED` flag set, e.g.:
79+
80+
```sh
81+
cd ios && RCT_NEW_ARCH_ENABLED=1 bundle exec pod install
82+
```
83+
84+
### Android
85+
86+
In the `android/gradle.properties` file, set `newArchEnabled` to `true` and build the app, e.g.:
87+
88+
```sh
89+
yarn android
90+
```
91+
92+
If you have issues running the Android build, you can try generating the Codegen files before the build using:
93+
94+
```sh
95+
cd android && ./gradlew generateCodegenArtifactsFromSchema
96+
```
3697

3798
## Linking
3899

@@ -133,7 +194,7 @@ const styles = StyleSheet.create({
133194

134195
## Advanced usage
135196

136-
For advanced usage please take a look into our [example project](https://github.com/callstack/react-native-pager-view/blob/master/example/src/BasicPagerViewExample.tsx)
197+
For advanced usage please take a look into our [example project](https://github.com/callstack/react-native-pager-view/blob/master/example/src/BasicPagerViewExample.tsx).
137198

138199
## API
139200

@@ -188,7 +249,7 @@ requestAnimationFrame(() => refPagerView.current?.setPage(index));
188249

189250
## Reanimated onPageScroll handler
190251

191-
An example can be found [here](https://github.com/callstack/react-native-pager-view/blob/master/example/src/ReanimatedOnPageScrollExample.tsx)
252+
An example can be found [here](https://github.com/callstack/react-native-pager-view/blob/master/example/src/ReanimatedOnPageScrollExample.tsx).
192253

193254
#### Instructions
194255

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.reactnativepagerview
2+
3+
import com.facebook.react.uimanager.ViewGroupManager
4+
import android.widget.FrameLayout
5+
import com.facebook.react.module.annotations.ReactModule
6+
import com.facebook.react.uimanager.ThemedReactContext
7+
8+
// Note: The LEGACY_ variant is an iOS-only feature and the related Android files
9+
// are only included because of and relevant to auxiliary processes, like Codegen.
10+
@ReactModule(name = LEGACY_PagerViewViewManagerImpl.NAME)
11+
class LEGACY_PagerViewViewManager : ViewGroupManager<FrameLayout>() {
12+
override fun getName() = LEGACY_PagerViewViewManagerImpl.NAME
13+
14+
override fun createViewInstance(context: ThemedReactContext): FrameLayout {
15+
throw Error("LEGACY_RNCViewPager is an iOS-only feature")
16+
}
17+
}

android/src/fabric/java/com/reactnativepagerview/PagerViewViewManager.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,11 @@ class PagerViewViewManager : ViewGroupManager<NestedScrollableHost>(), RNCViewPa
164164
return
165165
}
166166

167+
@ReactProp(name = "useLegacy")
168+
override fun setUseLegacy(view: NestedScrollableHost?, value: Boolean) {
169+
return
170+
}
171+
167172
fun goTo(root: NestedScrollableHost?, selectedPage: Int, scrollWithAnimation: Boolean) {
168173
if (root == null) {
169174
return
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.reactnativepagerview
2+
3+
// Note: The LEGACY_ variant is an iOS-only feature and the related Android files
4+
// are only included because of and relevant to auxiliary processes, like Codegen.
5+
object LEGACY_PagerViewViewManagerImpl {
6+
const val NAME = "LEGACY_RNCViewPager"
7+
}

android/src/main/java/com/reactnativepagerview/PagerViewViewPackage.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ class PagerViewPackage : ReactPackage {
1212
}
1313

1414
override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> {
15-
return listOf(PagerViewViewManager())
15+
return listOf(PagerViewViewManager(), LEGACY_PagerViewViewManager())
1616
}
1717
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.reactnativepagerview
2+
3+
import com.facebook.react.uimanager.ViewGroupManager
4+
import android.widget.FrameLayout
5+
import com.facebook.react.module.annotations.ReactModule
6+
import com.facebook.react.uimanager.ThemedReactContext
7+
8+
// Note: The LEGACY_ variant is an iOS-only feature and the related Android files
9+
// are only included because of and relevant to auxiliary processes, like Codegen.
10+
@ReactModule(name = LEGACY_PagerViewViewManagerImpl.NAME)
11+
class LEGACY_PagerViewViewManager : ViewGroupManager<FrameLayout>() {
12+
override fun getName() = LEGACY_PagerViewViewManagerImpl.NAME
13+
14+
override fun createViewInstance(context: ThemedReactContext): FrameLayout {
15+
throw Error("LEGACY_RNCViewPager is an iOS-only feature")
16+
}
17+
}

example/android/app/src/main/java/com/pagerviewexample/MainActivity.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ protected ReactActivityDelegate createReactActivityDelegate() {
3232
this,
3333
getMainComponentName(),
3434
// If you opted-in for the New Architecture, we enable the Fabric Renderer.
35-
DefaultNewArchitectureEntryPoint.getFabricEnabled());
35+
DefaultNewArchitectureEntryPoint.getFabricEnabled() // fabricEnabled
36+
// If you opted-in for the New Architecture, we enable Concurrent React (i.e. React 18).
37+
// DefaultNewArchitectureEntryPoint.getConcurrentReactEnabled() // concurrentRootEnabled // TODO: why has this been deleted?
38+
);
3639
}
3740
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>

0 commit comments

Comments
 (0)