Skip to content

Commit f041473

Browse files
author
Hans De Smedt
committed
initial changes to migrate lib to fabric
1 parent d010827 commit f041473

File tree

12 files changed

+113
-60
lines changed

12 files changed

+113
-60
lines changed

RNDeviceInfo.podspec

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ require 'json'
22

33
package = JSON.parse(File.read(File.join(__dir__, 'package.json')))
44

5+
folly_compiler_flags = '-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1 -Wno-comma -Wno-shorten-64-to-32'
6+
57
Pod::Spec.new do |s|
68
s.name = "RNDeviceInfo"
79
s.version = package['version']
@@ -15,7 +17,22 @@ Pod::Spec.new do |s|
1517
s.tvos.deployment_target = '10.0'
1618

1719
s.source = { :git => "https://github.com/react-native-device-info/react-native-device-info.git", :tag => "v#{s.version}" }
18-
s.source_files = "ios/**/*.{h,m}"
20+
s.source_files = "ios/**/*.{h,m,mm,swift}"
1921

2022
s.dependency 'React-Core'
23+
24+
# This guard prevent to install the dependencies when we run `pod install` in the old architecture.
25+
if ENV['RCT_NEW_ARCH_ENABLED'] == '1' then
26+
s.compiler_flags = folly_compiler_flags + " -DRCT_NEW_ARCH_ENABLED=1"
27+
s.pod_target_xcconfig = {
28+
"HEADER_SEARCH_PATHS" => "\"$(PODS_ROOT)/boost\"",
29+
"CLANG_CXX_LANGUAGE_STANDARD" => "c++17"
30+
}
31+
32+
s.dependency "React-Codegen"
33+
s.dependency "RCT-Folly"
34+
s.dependency "RCTRequired"
35+
s.dependency "RCTTypeSafety"
36+
s.dependency "ReactCommon/turbomodule/core"
37+
end
2138
end

android/build.gradle

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,16 @@ buildscript {
1515
}
1616
}
1717

18+
def isNewArchitectureEnabled() {
19+
return project.hasProperty("newArchEnabled") && project.newArchEnabled == "true"
20+
}
21+
1822
apply plugin: 'com.android.library'
1923

24+
if (isNewArchitectureEnabled()) {
25+
apply plugin: 'com.facebook.react'
26+
}
27+
2028
def safeExtGet(prop, fallback) {
2129
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
2230
}

ios/RNDeviceInfo.xcodeproj/project.pbxproj

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10-
663F51B9262200CA0023385E /* EnvironmentUtil.m in Sources */ = {isa = PBXBuildFile; fileRef = 663F51B8262200CA0023385E /* EnvironmentUtil.m */; };
11-
76E65CA41D4CA143009B7AF1 /* DeviceUID.m in Sources */ = {isa = PBXBuildFile; fileRef = 76E65CA31D4CA143009B7AF1 /* DeviceUID.m */; };
10+
5530B85E28F454BA00C78B4C /* RNDeviceInfo.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5530B85B28F454BA00C78B4C /* RNDeviceInfo.mm */; };
11+
5530B85F28F454BA00C78B4C /* DeviceUID.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5530B85C28F454BA00C78B4C /* DeviceUID.mm */; };
12+
5530B86028F454BA00C78B4C /* EnvironmentUtil.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5530B85D28F454BA00C78B4C /* EnvironmentUtil.mm */; };
13+
559373D428F4623300D4B7EF /* DeviceUID.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5530B85C28F454BA00C78B4C /* DeviceUID.mm */; };
14+
559373D528F4623800D4B7EF /* RNDeviceInfo.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5530B85B28F454BA00C78B4C /* RNDeviceInfo.mm */; };
15+
559373D628F4623C00D4B7EF /* EnvironmentUtil.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5530B85D28F454BA00C78B4C /* EnvironmentUtil.mm */; };
1216
BF770A3D1F6A3EEE007E5F09 /* DeviceUID.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 76E65CA21D4CA143009B7AF1 /* DeviceUID.h */; };
1317
DA5891DC1BA9A9FC002B4DB2 /* RNDeviceInfo.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = DA5891DB1BA9A9FC002B4DB2 /* RNDeviceInfo.h */; };
14-
DA5891DE1BA9A9FC002B4DB2 /* RNDeviceInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = DA5891DD1BA9A9FC002B4DB2 /* RNDeviceInfo.m */; };
15-
E72EC1491F7ABC0C0001BC90 /* DeviceUID.m in Sources */ = {isa = PBXBuildFile; fileRef = 76E65CA31D4CA143009B7AF1 /* DeviceUID.m */; };
16-
E72EC14A1F7ABC0E0001BC90 /* RNDeviceInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = DA5891DD1BA9A9FC002B4DB2 /* RNDeviceInfo.m */; };
1718
E72EC14B1F7ABC1A0001BC90 /* DeviceUID.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 76E65CA21D4CA143009B7AF1 /* DeviceUID.h */; };
1819
E72EC14C1F7ABC1D0001BC90 /* RNDeviceInfo.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = DA5891DB1BA9A9FC002B4DB2 /* RNDeviceInfo.h */; };
1920
/* End PBXBuildFile section */
@@ -44,13 +45,13 @@
4445
/* End PBXCopyFilesBuildPhase section */
4546

4647
/* Begin PBXFileReference section */
48+
5530B85B28F454BA00C78B4C /* RNDeviceInfo.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = RNDeviceInfo.mm; sourceTree = "<group>"; };
49+
5530B85C28F454BA00C78B4C /* DeviceUID.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = DeviceUID.mm; sourceTree = "<group>"; };
50+
5530B85D28F454BA00C78B4C /* EnvironmentUtil.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = EnvironmentUtil.mm; sourceTree = "<group>"; };
4751
663F51B7262200520023385E /* EnvironmentUtil.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = EnvironmentUtil.h; sourceTree = "<group>"; };
48-
663F51B8262200CA0023385E /* EnvironmentUtil.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = EnvironmentUtil.m; sourceTree = "<group>"; };
4952
76E65CA21D4CA143009B7AF1 /* DeviceUID.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DeviceUID.h; sourceTree = "<group>"; };
50-
76E65CA31D4CA143009B7AF1 /* DeviceUID.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DeviceUID.m; sourceTree = "<group>"; };
5153
DA5891D81BA9A9FC002B4DB2 /* libRNDeviceInfo.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libRNDeviceInfo.a; sourceTree = BUILT_PRODUCTS_DIR; };
5254
DA5891DB1BA9A9FC002B4DB2 /* RNDeviceInfo.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNDeviceInfo.h; sourceTree = "<group>"; };
53-
DA5891DD1BA9A9FC002B4DB2 /* RNDeviceInfo.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RNDeviceInfo.m; sourceTree = "<group>"; };
5455
E72EC1401F7ABB5A0001BC90 /* libRNDeviceInfo-tvOS.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libRNDeviceInfo-tvOS.a"; sourceTree = BUILT_PRODUCTS_DIR; };
5556
/* End PBXFileReference section */
5657

@@ -93,11 +94,11 @@
9394
isa = PBXGroup;
9495
children = (
9596
76E65CA21D4CA143009B7AF1 /* DeviceUID.h */,
96-
76E65CA31D4CA143009B7AF1 /* DeviceUID.m */,
97+
5530B85C28F454BA00C78B4C /* DeviceUID.mm */,
9798
DA5891DB1BA9A9FC002B4DB2 /* RNDeviceInfo.h */,
98-
DA5891DD1BA9A9FC002B4DB2 /* RNDeviceInfo.m */,
99+
5530B85B28F454BA00C78B4C /* RNDeviceInfo.mm */,
99100
663F51B7262200520023385E /* EnvironmentUtil.h */,
100-
663F51B8262200CA0023385E /* EnvironmentUtil.m */,
101+
5530B85D28F454BA00C78B4C /* EnvironmentUtil.mm */,
101102
);
102103
path = RNDeviceInfo;
103104
sourceTree = "<group>";
@@ -181,18 +182,19 @@
181182
isa = PBXSourcesBuildPhase;
182183
buildActionMask = 2147483647;
183184
files = (
184-
76E65CA41D4CA143009B7AF1 /* DeviceUID.m in Sources */,
185-
663F51B9262200CA0023385E /* EnvironmentUtil.m in Sources */,
186-
DA5891DE1BA9A9FC002B4DB2 /* RNDeviceInfo.m in Sources */,
185+
5530B85F28F454BA00C78B4C /* DeviceUID.mm in Sources */,
186+
5530B85E28F454BA00C78B4C /* RNDeviceInfo.mm in Sources */,
187+
5530B86028F454BA00C78B4C /* EnvironmentUtil.mm in Sources */,
187188
);
188189
runOnlyForDeploymentPostprocessing = 0;
189190
};
190191
E72EC13C1F7ABB5A0001BC90 /* Sources */ = {
191192
isa = PBXSourcesBuildPhase;
192193
buildActionMask = 2147483647;
193194
files = (
194-
E72EC1491F7ABC0C0001BC90 /* DeviceUID.m in Sources */,
195-
E72EC14A1F7ABC0E0001BC90 /* RNDeviceInfo.m in Sources */,
195+
559373D428F4623300D4B7EF /* DeviceUID.mm in Sources */,
196+
559373D528F4623800D4B7EF /* RNDeviceInfo.mm in Sources */,
197+
559373D628F4623C00D4B7EF /* EnvironmentUtil.mm in Sources */,
196198
);
197199
runOnlyForDeploymentPostprocessing = 0;
198200
};

ios/RNDeviceInfo/DeviceUID.m renamed to ios/RNDeviceInfo/DeviceUID.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#import "DeviceUID.h"
66

7-
@import UIKit;
7+
#import <UIKit/UIKit.h>
88

99
@interface DeviceUID ()
1010

ios/RNDeviceInfo/RNDeviceInfo.m renamed to ios/RNDeviceInfo/RNDeviceInfo.mm

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
// Copyright © 2015 Learnium Limited. All rights reserved.
77
//
88

9+
// Thanks to this guard, we won't import this header when we build for the old architecture.
10+
#ifdef RCT_NEW_ARCH_ENABLED
11+
#import "RNDeviceInfoSpec.h"
12+
#endif
13+
914
#include <ifaddrs.h>
1015
#include <arpa/inet.h>
1116
#import <mach/mach.h>
@@ -33,8 +38,9 @@ typedef NS_ENUM(NSInteger, DeviceType) {
3338
#define DeviceTypeValues [NSArray arrayWithObjects: @"Handset", @"Tablet", @"Tv", @"Desktop", @"unknown", nil]
3439

3540
#if !(TARGET_OS_TV)
36-
@import CoreTelephony;
37-
@import Darwin.sys.sysctl;
41+
#import<CoreTelephony/CTCarrier.h>
42+
#import<CoreTelephony/CTTelephonyNetworkInfo.h>
43+
#include <sys/sysctl.h>
3844
#endif
3945

4046
@implementation RNDeviceInfo
@@ -132,7 +138,7 @@ - (DeviceType) getDeviceType
132138
}
133139

134140
- (NSDictionary *) getStorageDictionary {
135-
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
141+
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
136142
return [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject] error: nil];
137143
}
138144

@@ -354,7 +360,7 @@ - (NSString *) getBuildId {
354360
if (status != 0) {
355361
return @"unknown";
356362
}
357-
NSString* buildId = [[NSString alloc] initWithCString:buffer.mutableBytes encoding:NSUTF8StringEncoding];
363+
NSString* buildId = [[NSString alloc] initWithData:buffer encoding:NSUTF8StringEncoding];
358364
return buildId;
359365
#endif
360366
}
@@ -879,4 +885,13 @@ - (void)dealloc
879885
[[NSNotificationCenter defaultCenter] removeObserver:self];
880886
}
881887

888+
// Thanks to this guard, we won't compile this code when we build for the old architecture.
889+
#ifdef RCT_NEW_ARCH_ENABLED
890+
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
891+
(const facebook::react::ObjCTurboModule::InitParams &)params
892+
{
893+
return std::make_shared<facebook::react::NativeRNDeviceInfoSpecJSI>(params);
894+
}
895+
#endif
896+
882897
@end

package.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,5 +134,13 @@
134134
"module",
135135
"typescript"
136136
]
137-
}
137+
},
138+
"codegenConfig": {
139+
"name": "RNDeviceInfoSpec",
140+
"type": "modules",
141+
"jsSrcsDir": "src",
142+
"android": {
143+
"javaPackageName": "com.learnium.RNDeviceInfo;"
144+
}
145+
}
138146
}

src/NativeRNDeviceInfo.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import type { TurboModule } from 'react-native/Libraries/TurboModule/RCTExport';
2+
import { TurboModuleRegistry } from 'react-native';
3+
4+
export interface Spec extends TurboModule {}
5+
export default TurboModuleRegistry.get<Spec>('RNDeviceInfo');

src/index.ts

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useCallback, useEffect, useState } from 'react';
2-
import { Dimensions, NativeEventEmitter, NativeModules, Platform } from 'react-native';
2+
import { Dimensions, NativeEventEmitter, Platform } from 'react-native';
33
import { useOnEvent, useOnMount } from './internal/asyncHookWrappers';
4-
import devicesWithDynamicIsland from "./internal/devicesWithDynamicIsland";
4+
import devicesWithDynamicIsland from './internal/devicesWithDynamicIsland';
55
import devicesWithNotch from './internal/devicesWithNotch';
66
import RNDeviceInfo from './internal/nativeInterface';
77
import {
@@ -16,6 +16,7 @@ import type {
1616
LocationProviderInfo,
1717
PowerState,
1818
} from './internal/types';
19+
import NativeRNDeviceInfo from './NativeRNDeviceInfo';
1920

2021
export const [getUniqueId, getUniqueIdSync] = getSupportedPlatformInfoFunctions({
2122
memoKey: 'uniqueId',
@@ -170,16 +171,14 @@ export const getBundleId = () =>
170171
getter: () => RNDeviceInfo.bundleId,
171172
});
172173

173-
export const [
174-
getInstallerPackageName,
175-
getInstallerPackageNameSync,
176-
] = getSupportedPlatformInfoFunctions({
177-
memoKey: 'installerPackageName',
178-
supportedPlatforms: ['android', 'windows', 'ios'],
179-
getter: () => RNDeviceInfo.getInstallerPackageName(),
180-
syncGetter: () => RNDeviceInfo.getInstallerPackageNameSync(),
181-
defaultValue: 'unknown',
182-
});
174+
export const [getInstallerPackageName, getInstallerPackageNameSync] =
175+
getSupportedPlatformInfoFunctions({
176+
memoKey: 'installerPackageName',
177+
supportedPlatforms: ['android', 'windows', 'ios'],
178+
getter: () => RNDeviceInfo.getInstallerPackageName(),
179+
syncGetter: () => RNDeviceInfo.getInstallerPackageNameSync(),
180+
defaultValue: 'unknown',
181+
});
183182

184183
export const getApplicationName = () =>
185184
getSupportedPlatformInfoSync({
@@ -641,15 +640,13 @@ export function isLowBatteryLevel(level: number): boolean {
641640
return level < 0.2;
642641
}
643642

644-
export const [
645-
getSystemAvailableFeatures,
646-
getSystemAvailableFeaturesSync,
647-
] = getSupportedPlatformInfoFunctions({
648-
supportedPlatforms: ['android'],
649-
getter: () => RNDeviceInfo.getSystemAvailableFeatures(),
650-
syncGetter: () => RNDeviceInfo.getSystemAvailableFeaturesSync(),
651-
defaultValue: [] as string[],
652-
});
643+
export const [getSystemAvailableFeatures, getSystemAvailableFeaturesSync] =
644+
getSupportedPlatformInfoFunctions({
645+
supportedPlatforms: ['android'],
646+
getter: () => RNDeviceInfo.getSystemAvailableFeatures(),
647+
syncGetter: () => RNDeviceInfo.getSystemAvailableFeaturesSync(),
648+
defaultValue: [] as string[],
649+
});
653650

654651
export const [isLocationEnabled, isLocationEnabledSync] = getSupportedPlatformInfoFunctions({
655652
supportedPlatforms: ['android', 'ios', 'web'],
@@ -688,15 +685,13 @@ export const isTabletMode = () =>
688685
defaultValue: false,
689686
});
690687

691-
export const [
692-
getAvailableLocationProviders,
693-
getAvailableLocationProvidersSync,
694-
] = getSupportedPlatformInfoFunctions({
695-
supportedPlatforms: ['android', 'ios'],
696-
getter: () => RNDeviceInfo.getAvailableLocationProviders(),
697-
syncGetter: () => RNDeviceInfo.getAvailableLocationProvidersSync(),
698-
defaultValue: {},
699-
});
688+
export const [getAvailableLocationProviders, getAvailableLocationProvidersSync] =
689+
getSupportedPlatformInfoFunctions({
690+
supportedPlatforms: ['android', 'ios'],
691+
getter: () => RNDeviceInfo.getAvailableLocationProviders(),
692+
syncGetter: () => RNDeviceInfo.getAvailableLocationProvidersSync(),
693+
defaultValue: {},
694+
});
700695

701696
export const [getBrightness, getBrightnessSync] = getSupportedPlatformInfoFunctions({
702697
supportedPlatforms: ['ios'],
@@ -712,7 +707,7 @@ export async function getDeviceToken() {
712707
return 'unknown';
713708
}
714709

715-
const deviceInfoEmitter = new NativeEventEmitter(NativeModules.RNDeviceInfo);
710+
const deviceInfoEmitter = new NativeEventEmitter(NativeRNDeviceInfo);
716711
export function useBatteryLevel(): number | null {
717712
const [batteryLevel, setBatteryLevel] = useState<number | null>(null);
718713

src/internal/asyncHookWrappers.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useState, useEffect } from 'react';
2-
import { NativeEventEmitter, NativeModules } from 'react-native';
2+
import { NativeEventEmitter } from 'react-native';
33
import type { AsyncHookResult } from './types';
4+
import NativeRNDeviceInfo from '../NativeRNDeviceInfo';
45

56
/**
67
* simple hook wrapper for async functions for 'on-mount / componentDidMount' that only need to fired once
@@ -26,7 +27,7 @@ export function useOnMount<T>(asyncGetter: () => Promise<T>, initialResult: T):
2627
return response;
2728
}
2829

29-
export const deviceInfoEmitter = new NativeEventEmitter(NativeModules.RNDeviceInfo);
30+
export const deviceInfoEmitter = new NativeEventEmitter(NativeRNDeviceInfo);
3031

3132
/**
3233
* simple hook wrapper for handling events

src/internal/nativeInterface.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { Platform, NativeModules } from 'react-native';
1+
import { Platform } from 'react-native';
22
import { DeviceInfoNativeModule } from './privateTypes';
3+
import NativeRNDeviceInfo from '../NativeRNDeviceInfo';
34

4-
let RNDeviceInfo: DeviceInfoNativeModule | undefined = NativeModules.RNDeviceInfo;
5+
let RNDeviceInfo: DeviceInfoNativeModule | undefined = NativeRNDeviceInfo;
56

67
// @ts-ignore
78
if (Platform.OS === 'web' || Platform.OS === 'dom') {

src/web/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { NativeEventEmitter, NativeModules } from 'react-native';
1+
import { NativeEventEmitter } from 'react-native';
2+
import NativeRNDeviceInfo from '../NativeRNDeviceInfo';
23

3-
const deviceInfoEmitter = new NativeEventEmitter(NativeModules.RNDeviceInfo);
4+
const deviceInfoEmitter = new NativeEventEmitter(NativeRNDeviceInfo);
45

56
let batteryCharging = false,
67
batteryLevel = -1,

0 commit comments

Comments
 (0)