Skip to content

Commit

Permalink
feat: Better example app (#331)
Browse files Browse the repository at this point in the history
* feat: Add proper LaunchScreen to iOS app

* Add bottom tabs

* Bottom view

* Add Benchmarks screen

* feat: Improve benchmarks screen

* feat: Create ExampleTurboModule for `addNumbers(..)` benchmark

* fix: Make benchmark work

* Add text to explain start

* feat: Implement ExampleTurboModule on Android

* Fix elevation
  • Loading branch information
mrousavy authored Nov 13, 2024
1 parent 372fdc8 commit 3bf5161
Show file tree
Hide file tree
Showing 23 changed files with 666 additions and 40 deletions.
Binary file modified bun.lockb
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.margelo.nitroexample

import android.os.Bundle
import com.facebook.react.ReactActivity
import com.facebook.react.ReactActivityDelegate
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.fabricEnabled
Expand All @@ -19,4 +20,8 @@ class MainActivity : ReactActivity() {
*/
override fun createReactActivityDelegate(): ReactActivityDelegate =
DefaultReactActivityDelegate(this, mainComponentName, fabricEnabled)

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(null)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.load
import com.facebook.react.defaults.DefaultReactHost.getDefaultReactHost
import com.facebook.react.defaults.DefaultReactNativeHost
import com.facebook.soloader.SoLoader
import com.nitroexample.exampleturbomodule.ExampleTurboModulePackage

class MainApplication : Application(), ReactApplication {

Expand All @@ -19,7 +20,7 @@ class MainApplication : Application(), ReactApplication {
override fun getPackages(): List<ReactPackage> =
PackageList(this).packages.apply {
// Packages that cannot be autolinked yet can be added manually here, for example:
// add(MyReactNativePackage())
add(ExampleTurboModulePackage())
}

override fun getJSMainModuleName(): String = "index"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.nitroexample.exampleturbomodule

import com.facebook.react.bridge.ReactApplicationContext

class ExampleTurboModuleModule(reactContext: ReactApplicationContext) : NativeExampleTurboModuleSpec(reactContext) {
override fun getName() = NAME

override fun addNumbers(a: Double, b: Double): Double {
return a + b
}

companion object {
const val NAME = "ExampleTurboModule"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.nitroexample.exampleturbomodule

import com.facebook.react.TurboReactPackage
import com.facebook.react.bridge.NativeModule
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.module.model.ReactModuleInfo
import com.facebook.react.module.model.ReactModuleInfoProvider

class ExampleTurboModulePackage: TurboReactPackage() {
override fun getModule(name: String, context: ReactApplicationContext): NativeModule? {
if (name == ExampleTurboModuleModule.NAME) {
return ExampleTurboModuleModule(context)
} else {
return null
}
}
override fun getReactModuleInfoProvider() = ReactModuleInfoProvider {
mapOf(
ExampleTurboModuleModule.NAME to ReactModuleInfo(
ExampleTurboModuleModule.NAME,
ExampleTurboModuleModule.NAME,
false, // canOverrideExistingModule
false, // needsEagerInit
true, // hasConstants
false, // isCxxModule
true // isTurboModule
)
)
}
}
17 changes: 17 additions & 0 deletions example/ios/Example Turbo Module/MGLExampleTurboModule.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// MGLExampleTurboModule.h
// NitroExample
//
// Created by Marc Rousavy on 13.11.24.
//

#import <Foundation/Foundation.h>
#import <ExampleTurboModule/ExampleTurboModule.h>

NS_ASSUME_NONNULL_BEGIN

@interface ExampleTurboModule : NSObject <NativeExampleTurboModuleSpec>

@end

NS_ASSUME_NONNULL_END
25 changes: 25 additions & 0 deletions example/ios/Example Turbo Module/MGLExampleTurboModule.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// MGLExampleTurboModule.mm
// NitroExample
//
// Created by Marc Rousavy on 13.11.24.
//

#import "MGLExampleTurboModule.h"

@implementation ExampleTurboModule

RCT_EXPORT_MODULE()

- (NSNumber*)addNumbers:(double)a b:(double)b {
NSNumber* result = [[NSNumber alloc] initWithDouble:a + b];
return result;
}

- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
(const facebook::react::ObjCTurboModule::InitParams &)params
{
return std::make_shared<facebook::react::NativeExampleTurboModuleSpecJSI>(params);
}

@end
14 changes: 14 additions & 0 deletions example/ios/NitroExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
7699B88040F8A987B510C191 /* libPods-NitroExample-NitroExampleTests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 19F6CBCC0A4E27FBF8BF4A61 /* libPods-NitroExample-NitroExampleTests.a */; };
81AB9BB82411601600AC10FF /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 81AB9BB72411601600AC10FF /* LaunchScreen.storyboard */; };
B5425A16F500085048F53261 /* PrivacyInfo.xcprivacy in Resources */ = {isa = PBXBuildFile; fileRef = A98DCE138B16911E2CF8DB85 /* PrivacyInfo.xcprivacy */; };
B88F64252CE4CF530079E862 /* MGLExampleTurboModule.mm in Sources */ = {isa = PBXBuildFile; fileRef = B88F64242CE4CF4F0079E862 /* MGLExampleTurboModule.mm */; };
B8D0C54B2C2357CE0037DD32 /* DummyFile.swift in Sources */ = {isa = PBXBuildFile; fileRef = B8D0C54A2C2357CE0037DD32 /* DummyFile.swift */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -47,6 +48,8 @@
81AB9BB72411601600AC10FF /* LaunchScreen.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; name = LaunchScreen.storyboard; path = NitroExample/LaunchScreen.storyboard; sourceTree = "<group>"; };
89C6BE57DB24E9ADA2F236DE /* Pods-NitroExample-NitroExampleTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-NitroExample-NitroExampleTests.release.xcconfig"; path = "Target Support Files/Pods-NitroExample-NitroExampleTests/Pods-NitroExample-NitroExampleTests.release.xcconfig"; sourceTree = "<group>"; };
A98DCE138B16911E2CF8DB85 /* PrivacyInfo.xcprivacy */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xml; name = PrivacyInfo.xcprivacy; path = NitroExample/PrivacyInfo.xcprivacy; sourceTree = "<group>"; };
B88F64232CE4CF480079E862 /* MGLExampleTurboModule.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MGLExampleTurboModule.h; sourceTree = "<group>"; };
B88F64242CE4CF4F0079E862 /* MGLExampleTurboModule.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = MGLExampleTurboModule.mm; sourceTree = "<group>"; };
B8D0C5492C2357CE0037DD32 /* NitroExample-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "NitroExample-Bridging-Header.h"; sourceTree = "<group>"; };
B8D0C54A2C2357CE0037DD32 /* DummyFile.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DummyFile.swift; sourceTree = "<group>"; };
ED297162215061F000B7C4FE /* JavaScriptCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = JavaScriptCore.framework; path = System/Library/Frameworks/JavaScriptCore.framework; sourceTree = SDKROOT; };
Expand Down Expand Up @@ -92,6 +95,7 @@
13B07FAE1A68108700A75B9A /* NitroExample */ = {
isa = PBXGroup;
children = (
B88F64222CE4CF3F0079E862 /* Example Turbo Module */,
13B07FAF1A68108700A75B9A /* AppDelegate.h */,
13B07FB01A68108700A75B9A /* AppDelegate.mm */,
13B07FB51A68108700A75B9A /* Images.xcassets */,
Expand Down Expand Up @@ -147,6 +151,15 @@
name = Products;
sourceTree = "<group>";
};
B88F64222CE4CF3F0079E862 /* Example Turbo Module */ = {
isa = PBXGroup;
children = (
B88F64242CE4CF4F0079E862 /* MGLExampleTurboModule.mm */,
B88F64232CE4CF480079E862 /* MGLExampleTurboModule.h */,
);
path = "Example Turbo Module";
sourceTree = "<group>";
};
BBD78D7AC51CEA395F1C20DB /* Pods */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -406,6 +419,7 @@
13B07FBC1A68108700A75B9A /* AppDelegate.mm in Sources */,
B8D0C54B2C2357CE0037DD32 /* DummyFile.swift in Sources */,
13B07FC11A68108700A75B9A /* main.m in Sources */,
B88F64252CE4CF530079E862 /* MGLExampleTurboModule.mm in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"images" : [
{
"filename" : "android-chrome-192x192 2.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "android-chrome-192x192 1.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "android-chrome-192x192.png",
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 15 additions & 21 deletions example/ios/NitroExample/LaunchScreen.storyboard
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="15702" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="01J-lp-oVM">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="23504" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="01J-lp-oVM">
<device id="retina4_7" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="15704"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="23506"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
Expand All @@ -16,32 +16,26 @@
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="NitroExample" textAlignment="center" lineBreakMode="middleTruncation" baselineAdjustment="alignBaselines" minimumFontSize="18" translatesAutoresizingMaskIntoConstraints="NO" id="GJd-Yh-RWb">
<rect key="frame" x="0.0" y="202" width="375" height="43"/>
<fontDescription key="fontDescription" type="boldSystem" pointSize="36"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Powered by React Native" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" minimumFontSize="9" translatesAutoresizingMaskIntoConstraints="NO" id="MN2-I3-ftu">
<rect key="frame" x="0.0" y="626" width="375" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="highlightedColor"/>
</label>
<imageView clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="nos" translatesAutoresizingMaskIntoConstraints="NO" id="fWi-ja-ggx">
<rect key="frame" x="139.5" y="295.5" width="96" height="96"/>
</imageView>
</subviews>
<color key="backgroundColor" systemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
<viewLayoutGuide key="safeArea" id="Bcu-3y-fUS"/>
<color key="backgroundColor" red="0.57190001010000002" green="0.77295333150000001" blue="0.90689653160000006" alpha="1" colorSpace="custom" customColorSpace="displayP3"/>
<constraints>
<constraint firstItem="Bcu-3y-fUS" firstAttribute="bottom" secondItem="MN2-I3-ftu" secondAttribute="bottom" constant="20" id="OZV-Vh-mqD"/>
<constraint firstItem="Bcu-3y-fUS" firstAttribute="centerX" secondItem="GJd-Yh-RWb" secondAttribute="centerX" id="Q3B-4B-g5h"/>
<constraint firstItem="MN2-I3-ftu" firstAttribute="centerX" secondItem="Bcu-3y-fUS" secondAttribute="centerX" id="akx-eg-2ui"/>
<constraint firstItem="MN2-I3-ftu" firstAttribute="leading" secondItem="Bcu-3y-fUS" secondAttribute="leading" id="i1E-0Y-4RG"/>
<constraint firstItem="GJd-Yh-RWb" firstAttribute="centerY" secondItem="Ze5-6b-2t3" secondAttribute="bottom" multiplier="1/3" constant="1" id="moa-c2-u7t"/>
<constraint firstItem="GJd-Yh-RWb" firstAttribute="leading" secondItem="Bcu-3y-fUS" secondAttribute="leading" symbolic="YES" id="x7j-FC-K8j"/>
<constraint firstItem="fWi-ja-ggx" firstAttribute="height" secondItem="Ze5-6b-2t3" secondAttribute="height" multiplier="0.143928" id="1fI-Qy-CAw"/>
<constraint firstItem="fWi-ja-ggx" firstAttribute="centerX" secondItem="Bcu-3y-fUS" secondAttribute="centerX" id="F5X-wD-51b"/>
<constraint firstItem="fWi-ja-ggx" firstAttribute="centerY" secondItem="Bcu-3y-fUS" secondAttribute="centerY" id="RYh-KH-o66"/>
<constraint firstItem="fWi-ja-ggx" firstAttribute="width" secondItem="Ze5-6b-2t3" secondAttribute="width" multiplier="0.256" id="qVc-g6-8wG"/>
</constraints>
<viewLayoutGuide key="safeArea" id="Bcu-3y-fUS"/>
</view>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="iYj-Kq-Ea1" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="52.173913043478265" y="375"/>
<point key="canvasLocation" x="52" y="374.66266866566718"/>
</scene>
</scenes>
<resources>
<image name="nos" width="96" height="96"/>
</resources>
</document>
51 changes: 50 additions & 1 deletion example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1620,6 +1620,51 @@ PODS:
- React-logger (= 0.76.1)
- React-perflogger (= 0.76.1)
- React-utils (= 0.76.1)
- RNScreens (4.0.0):
- DoubleConversion
- glog
- hermes-engine
- RCT-Folly (= 2024.01.01.00)
- RCTRequired
- RCTTypeSafety
- React-Core
- React-debug
- React-Fabric
- React-featureflags
- React-graphics
- React-ImageManager
- React-NativeModulesApple
- React-RCTFabric
- React-RCTImage
- React-rendererdebug
- React-utils
- ReactCodegen
- ReactCommon/turbomodule/bridging
- ReactCommon/turbomodule/core
- RNScreens/common (= 4.0.0)
- Yoga
- RNScreens/common (4.0.0):
- DoubleConversion
- glog
- hermes-engine
- RCT-Folly (= 2024.01.01.00)
- RCTRequired
- RCTTypeSafety
- React-Core
- React-debug
- React-Fabric
- React-featureflags
- React-graphics
- React-ImageManager
- React-NativeModulesApple
- React-RCTFabric
- React-RCTImage
- React-rendererdebug
- React-utils
- ReactCodegen
- ReactCommon/turbomodule/bridging
- ReactCommon/turbomodule/core
- Yoga
- SocketRocket (0.7.1)
- Yoga (0.0.0)

Expand Down Expand Up @@ -1692,6 +1737,7 @@ DEPENDENCIES:
- React-utils (from `../../node_modules/react-native/ReactCommon/react/utils`)
- ReactCodegen (from `build/generated/ios`)
- ReactCommon/turbomodule/core (from `../../node_modules/react-native/ReactCommon`)
- RNScreens (from `../../node_modules/react-native-screens`)
- Yoga (from `../../node_modules/react-native/ReactCommon/yoga`)

SPEC REPOS:
Expand Down Expand Up @@ -1832,6 +1878,8 @@ EXTERNAL SOURCES:
:path: build/generated/ios
ReactCommon:
:path: "../../node_modules/react-native/ReactCommon"
RNScreens:
:path: "../../node_modules/react-native-screens"
Yoga:
:path: "../../node_modules/react-native/ReactCommon/yoga"

Expand Down Expand Up @@ -1900,8 +1948,9 @@ SPEC CHECKSUMS:
React-runtimescheduler: e7df538274de0c65736068e40efc0d2228f42d0d
React-timing: b3b233fe819d9e5b6ca32b605aa732621bdfa5aa
React-utils: 5362bd16a9563f9916e7a56c011ddc533507650f
ReactCodegen: 4e26d365313307cc7c95e693529e539acfb5c64c
ReactCodegen: 0102cbeaaa6a147c48738529da06d9b5b8f69128
ReactCommon: 422e364463f33e336fc4db196aeb50fd801d90d6
RNScreens: 2fe13c8d610ef2d9d5ace2e7d85b716ec0f5217c
SocketRocket: d4aabe649be1e368d1318fdf28a022d714d65748
Yoga: 157bed1c62656587df4639d4dc29714898f8fb10

Expand Down
13 changes: 12 additions & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
},
"dependencies": {
"@react-native-segmented-control/segmented-control": "^2.5.2",
"@react-navigation/bottom-tabs": "^7.0.1",
"@react-navigation/native": "^7.0.0",
"deep-equal": "^2.2.3",
"react": "*",
"react-native": "^0.76.1",
"react-native-safe-area-context": "^4.14.0"
"react-native-safe-area-context": "^4.14.0",
"react-native-screens": "^4.0.0"
},
"devDependencies": {
"@babel/core": "^7.25.2",
Expand All @@ -42,5 +45,13 @@
},
"engines": {
"node": ">=18"
},
"codegenConfig": {
"name": "ExampleTurboModule",
"type": "modules",
"jsSrcsDir": "src",
"android": {
"javaPackageName": "com.nitroexample.exampleturbomodule"
}
}
}
Loading

0 comments on commit 3bf5161

Please sign in to comment.