Skip to content

Commit

Permalink
initial changes to migrate lib to fabric
Browse files Browse the repository at this point in the history
  • Loading branch information
Hans De Smedt committed Oct 10, 2022
1 parent d010827 commit f041473
Show file tree
Hide file tree
Showing 12 changed files with 113 additions and 60 deletions.
19 changes: 18 additions & 1 deletion RNDeviceInfo.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ require 'json'

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

folly_compiler_flags = '-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1 -Wno-comma -Wno-shorten-64-to-32'

This comment has been minimized.

Copy link
@mikehardy

mikehardy Oct 13, 2022

A comment with a link to some docs explaining why this is needed would be great, seems odd to dive into folly compiler flags - that's a really low-level implementation detail, similar to the pod_target_xcconfig stuff.

Might be best to have this (which is only used in one spot?) closer to the place it is used and one doc link explaining why we are twiddling with build settings in unrelated projects

Please note that I am more than happy to twiddle with build settings (the xcode_12... workaround thing in everyone's Podfile was mine 😆 🙈 ) I just want an explanation and a link upstream so we know why + when to sunset it


Pod::Spec.new do |s|
s.name = "RNDeviceInfo"
s.version = package['version']
Expand All @@ -15,7 +17,22 @@ Pod::Spec.new do |s|
s.tvos.deployment_target = '10.0'

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

s.dependency 'React-Core'

# This guard prevent to install the dependencies when we run `pod install` in the old architecture.
if ENV['RCT_NEW_ARCH_ENABLED'] == '1' then
s.compiler_flags = folly_compiler_flags + " -DRCT_NEW_ARCH_ENABLED=1"
s.pod_target_xcconfig = {
"HEADER_SEARCH_PATHS" => "\"$(PODS_ROOT)/boost\"",
"CLANG_CXX_LANGUAGE_STANDARD" => "c++17"
}

s.dependency "React-Codegen"
s.dependency "RCT-Folly"
s.dependency "RCTRequired"
s.dependency "RCTTypeSafety"
s.dependency "ReactCommon/turbomodule/core"
end
end
8 changes: 8 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,16 @@ buildscript {
}
}

def isNewArchitectureEnabled() {
return project.hasProperty("newArchEnabled") && project.newArchEnabled == "true"
}

apply plugin: 'com.android.library'

if (isNewArchitectureEnabled()) {
apply plugin: 'com.facebook.react'
}

def safeExtGet(prop, fallback) {
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
}
Expand Down
34 changes: 18 additions & 16 deletions ios/RNDeviceInfo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
objects = {

/* Begin PBXBuildFile section */
663F51B9262200CA0023385E /* EnvironmentUtil.m in Sources */ = {isa = PBXBuildFile; fileRef = 663F51B8262200CA0023385E /* EnvironmentUtil.m */; };
76E65CA41D4CA143009B7AF1 /* DeviceUID.m in Sources */ = {isa = PBXBuildFile; fileRef = 76E65CA31D4CA143009B7AF1 /* DeviceUID.m */; };
5530B85E28F454BA00C78B4C /* RNDeviceInfo.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5530B85B28F454BA00C78B4C /* RNDeviceInfo.mm */; };
5530B85F28F454BA00C78B4C /* DeviceUID.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5530B85C28F454BA00C78B4C /* DeviceUID.mm */; };
5530B86028F454BA00C78B4C /* EnvironmentUtil.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5530B85D28F454BA00C78B4C /* EnvironmentUtil.mm */; };
559373D428F4623300D4B7EF /* DeviceUID.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5530B85C28F454BA00C78B4C /* DeviceUID.mm */; };
559373D528F4623800D4B7EF /* RNDeviceInfo.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5530B85B28F454BA00C78B4C /* RNDeviceInfo.mm */; };
559373D628F4623C00D4B7EF /* EnvironmentUtil.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5530B85D28F454BA00C78B4C /* EnvironmentUtil.mm */; };
BF770A3D1F6A3EEE007E5F09 /* DeviceUID.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 76E65CA21D4CA143009B7AF1 /* DeviceUID.h */; };
DA5891DC1BA9A9FC002B4DB2 /* RNDeviceInfo.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = DA5891DB1BA9A9FC002B4DB2 /* RNDeviceInfo.h */; };
DA5891DE1BA9A9FC002B4DB2 /* RNDeviceInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = DA5891DD1BA9A9FC002B4DB2 /* RNDeviceInfo.m */; };
E72EC1491F7ABC0C0001BC90 /* DeviceUID.m in Sources */ = {isa = PBXBuildFile; fileRef = 76E65CA31D4CA143009B7AF1 /* DeviceUID.m */; };
E72EC14A1F7ABC0E0001BC90 /* RNDeviceInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = DA5891DD1BA9A9FC002B4DB2 /* RNDeviceInfo.m */; };
E72EC14B1F7ABC1A0001BC90 /* DeviceUID.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 76E65CA21D4CA143009B7AF1 /* DeviceUID.h */; };
E72EC14C1F7ABC1D0001BC90 /* RNDeviceInfo.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = DA5891DB1BA9A9FC002B4DB2 /* RNDeviceInfo.h */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -44,13 +45,13 @@
/* End PBXCopyFilesBuildPhase section */

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

Expand Down Expand Up @@ -93,11 +94,11 @@
isa = PBXGroup;
children = (
76E65CA21D4CA143009B7AF1 /* DeviceUID.h */,
76E65CA31D4CA143009B7AF1 /* DeviceUID.m */,
5530B85C28F454BA00C78B4C /* DeviceUID.mm */,
DA5891DB1BA9A9FC002B4DB2 /* RNDeviceInfo.h */,
DA5891DD1BA9A9FC002B4DB2 /* RNDeviceInfo.m */,
5530B85B28F454BA00C78B4C /* RNDeviceInfo.mm */,
663F51B7262200520023385E /* EnvironmentUtil.h */,
663F51B8262200CA0023385E /* EnvironmentUtil.m */,
5530B85D28F454BA00C78B4C /* EnvironmentUtil.mm */,
);
path = RNDeviceInfo;
sourceTree = "<group>";
Expand Down Expand Up @@ -181,18 +182,19 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
76E65CA41D4CA143009B7AF1 /* DeviceUID.m in Sources */,
663F51B9262200CA0023385E /* EnvironmentUtil.m in Sources */,
DA5891DE1BA9A9FC002B4DB2 /* RNDeviceInfo.m in Sources */,
5530B85F28F454BA00C78B4C /* DeviceUID.mm in Sources */,
5530B85E28F454BA00C78B4C /* RNDeviceInfo.mm in Sources */,
5530B86028F454BA00C78B4C /* EnvironmentUtil.mm in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
E72EC13C1F7ABB5A0001BC90 /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
E72EC1491F7ABC0C0001BC90 /* DeviceUID.m in Sources */,
E72EC14A1F7ABC0E0001BC90 /* RNDeviceInfo.m in Sources */,
559373D428F4623300D4B7EF /* DeviceUID.mm in Sources */,
559373D528F4623800D4B7EF /* RNDeviceInfo.mm in Sources */,
559373D628F4623C00D4B7EF /* EnvironmentUtil.mm in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#import "DeviceUID.h"

@import UIKit;
#import <UIKit/UIKit.h>

@interface DeviceUID ()

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
// Copyright © 2015 Learnium Limited. All rights reserved.
//

// Thanks to this guard, we won't import this header when we build for the old architecture.
#ifdef RCT_NEW_ARCH_ENABLED
#import "RNDeviceInfoSpec.h"
#endif

#include <ifaddrs.h>
#include <arpa/inet.h>
#import <mach/mach.h>
Expand Down Expand Up @@ -33,8 +38,9 @@ typedef NS_ENUM(NSInteger, DeviceType) {
#define DeviceTypeValues [NSArray arrayWithObjects: @"Handset", @"Tablet", @"Tv", @"Desktop", @"unknown", nil]

#if !(TARGET_OS_TV)
@import CoreTelephony;
@import Darwin.sys.sysctl;
#import<CoreTelephony/CTCarrier.h>
#import<CoreTelephony/CTTelephonyNetworkInfo.h>
#include <sys/sysctl.h>
#endif

@implementation RNDeviceInfo
Expand Down Expand Up @@ -132,7 +138,7 @@ - (DeviceType) getDeviceType
}

- (NSDictionary *) getStorageDictionary {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject] error: nil];
}

Expand Down Expand Up @@ -354,7 +360,7 @@ - (NSString *) getBuildId {
if (status != 0) {
return @"unknown";
}
NSString* buildId = [[NSString alloc] initWithCString:buffer.mutableBytes encoding:NSUTF8StringEncoding];
NSString* buildId = [[NSString alloc] initWithData:buffer encoding:NSUTF8StringEncoding];
return buildId;
#endif
}
Expand Down Expand Up @@ -879,4 +885,13 @@ - (void)dealloc
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

// Thanks to this guard, we won't compile this code when we build for the old architecture.
#ifdef RCT_NEW_ARCH_ENABLED
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
(const facebook::react::ObjCTurboModule::InitParams &)params
{
return std::make_shared<facebook::react::NativeRNDeviceInfoSpecJSI>(params);
}
#endif

@end
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,13 @@
"module",
"typescript"
]
}
},
"codegenConfig": {
"name": "RNDeviceInfoSpec",
"type": "modules",
"jsSrcsDir": "src",
"android": {
"javaPackageName": "com.learnium.RNDeviceInfo;"
}
}
}
5 changes: 5 additions & 0 deletions src/NativeRNDeviceInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { TurboModule } from 'react-native/Libraries/TurboModule/RCTExport';
import { TurboModuleRegistry } from 'react-native';

export interface Spec extends TurboModule {}
export default TurboModuleRegistry.get<Spec>('RNDeviceInfo');
57 changes: 26 additions & 31 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useCallback, useEffect, useState } from 'react';
import { Dimensions, NativeEventEmitter, NativeModules, Platform } from 'react-native';
import { Dimensions, NativeEventEmitter, Platform } from 'react-native';
import { useOnEvent, useOnMount } from './internal/asyncHookWrappers';
import devicesWithDynamicIsland from "./internal/devicesWithDynamicIsland";
import devicesWithDynamicIsland from './internal/devicesWithDynamicIsland';
import devicesWithNotch from './internal/devicesWithNotch';
import RNDeviceInfo from './internal/nativeInterface';
import {
Expand All @@ -16,6 +16,7 @@ import type {
LocationProviderInfo,
PowerState,
} from './internal/types';
import NativeRNDeviceInfo from './NativeRNDeviceInfo';

export const [getUniqueId, getUniqueIdSync] = getSupportedPlatformInfoFunctions({
memoKey: 'uniqueId',
Expand Down Expand Up @@ -170,16 +171,14 @@ export const getBundleId = () =>
getter: () => RNDeviceInfo.bundleId,
});

export const [
getInstallerPackageName,
getInstallerPackageNameSync,
] = getSupportedPlatformInfoFunctions({
memoKey: 'installerPackageName',
supportedPlatforms: ['android', 'windows', 'ios'],
getter: () => RNDeviceInfo.getInstallerPackageName(),
syncGetter: () => RNDeviceInfo.getInstallerPackageNameSync(),
defaultValue: 'unknown',
});
export const [getInstallerPackageName, getInstallerPackageNameSync] =
getSupportedPlatformInfoFunctions({
memoKey: 'installerPackageName',
supportedPlatforms: ['android', 'windows', 'ios'],
getter: () => RNDeviceInfo.getInstallerPackageName(),
syncGetter: () => RNDeviceInfo.getInstallerPackageNameSync(),
defaultValue: 'unknown',
});

export const getApplicationName = () =>
getSupportedPlatformInfoSync({
Expand Down Expand Up @@ -641,15 +640,13 @@ export function isLowBatteryLevel(level: number): boolean {
return level < 0.2;
}

export const [
getSystemAvailableFeatures,
getSystemAvailableFeaturesSync,
] = getSupportedPlatformInfoFunctions({
supportedPlatforms: ['android'],
getter: () => RNDeviceInfo.getSystemAvailableFeatures(),
syncGetter: () => RNDeviceInfo.getSystemAvailableFeaturesSync(),
defaultValue: [] as string[],
});
export const [getSystemAvailableFeatures, getSystemAvailableFeaturesSync] =
getSupportedPlatformInfoFunctions({
supportedPlatforms: ['android'],
getter: () => RNDeviceInfo.getSystemAvailableFeatures(),
syncGetter: () => RNDeviceInfo.getSystemAvailableFeaturesSync(),
defaultValue: [] as string[],
});

export const [isLocationEnabled, isLocationEnabledSync] = getSupportedPlatformInfoFunctions({
supportedPlatforms: ['android', 'ios', 'web'],
Expand Down Expand Up @@ -688,15 +685,13 @@ export const isTabletMode = () =>
defaultValue: false,
});

export const [
getAvailableLocationProviders,
getAvailableLocationProvidersSync,
] = getSupportedPlatformInfoFunctions({
supportedPlatforms: ['android', 'ios'],
getter: () => RNDeviceInfo.getAvailableLocationProviders(),
syncGetter: () => RNDeviceInfo.getAvailableLocationProvidersSync(),
defaultValue: {},
});
export const [getAvailableLocationProviders, getAvailableLocationProvidersSync] =
getSupportedPlatformInfoFunctions({
supportedPlatforms: ['android', 'ios'],
getter: () => RNDeviceInfo.getAvailableLocationProviders(),
syncGetter: () => RNDeviceInfo.getAvailableLocationProvidersSync(),
defaultValue: {},
});

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

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

Expand Down
5 changes: 3 additions & 2 deletions src/internal/asyncHookWrappers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useState, useEffect } from 'react';
import { NativeEventEmitter, NativeModules } from 'react-native';
import { NativeEventEmitter } from 'react-native';
import type { AsyncHookResult } from './types';
import NativeRNDeviceInfo from '../NativeRNDeviceInfo';

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

export const deviceInfoEmitter = new NativeEventEmitter(NativeModules.RNDeviceInfo);
export const deviceInfoEmitter = new NativeEventEmitter(NativeRNDeviceInfo);

/**
* simple hook wrapper for handling events
Expand Down
5 changes: 3 additions & 2 deletions src/internal/nativeInterface.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Platform, NativeModules } from 'react-native';
import { Platform } from 'react-native';
import { DeviceInfoNativeModule } from './privateTypes';
import NativeRNDeviceInfo from '../NativeRNDeviceInfo';

let RNDeviceInfo: DeviceInfoNativeModule | undefined = NativeModules.RNDeviceInfo;
let RNDeviceInfo: DeviceInfoNativeModule | undefined = NativeRNDeviceInfo;

// @ts-ignore
if (Platform.OS === 'web' || Platform.OS === 'dom') {
Expand Down
5 changes: 3 additions & 2 deletions src/web/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NativeEventEmitter, NativeModules } from 'react-native';
import { NativeEventEmitter } from 'react-native';
import NativeRNDeviceInfo from '../NativeRNDeviceInfo';

const deviceInfoEmitter = new NativeEventEmitter(NativeModules.RNDeviceInfo);
const deviceInfoEmitter = new NativeEventEmitter(NativeRNDeviceInfo);

let batteryCharging = false,
batteryLevel = -1,
Expand Down

0 comments on commit f041473

Please sign in to comment.