Skip to content

Commit

Permalink
feat: Add 5g cellular type (#436)
Browse files Browse the repository at this point in the history
* [build] Update compileSdkVersion and buildToolsVersion to 29
* [feat] add 5g enum android
* [feat] add ios enum
* [feat] add ts integration
  • Loading branch information
luisnaranjo733 committed Nov 13, 2021
1 parent c7b95be commit 6ba68e9
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 3 deletions.
4 changes: 2 additions & 2 deletions android/gradle.properties
@@ -1,4 +1,4 @@
ReactNativeNetInfo_compileSdkVersion=28
ReactNativeNetInfo_buildToolsVersion=28.0.3
ReactNativeNetInfo_compileSdkVersion=29
ReactNativeNetInfo_buildToolsVersion=29.0.3
ReactNativeNetInfo_targetSdkVersion=27
ReactNativeNetInfo_minSdkVersion=16
Expand Up @@ -16,7 +16,8 @@ public enum CellularGeneration {
// We need to prefix these with "CG_" because they cannot start with numbers
CG_2G("2g"),
CG_3G("3g"),
CG_4G("4g");
CG_4G("4g"),
CG_5G("5g");

public final String label;

Expand Down Expand Up @@ -49,6 +50,8 @@ public static CellularGeneration fromNetworkInfo(@Nullable NetworkInfo networkIn
case TelephonyManager.NETWORK_TYPE_HSPAP:
case TelephonyManager.NETWORK_TYPE_LTE:
return CellularGeneration.CG_4G;
case TelephonyManager.NETWORK_TYPE_NR:
return CellularGeneration.CG_5G;
case TelephonyManager.NETWORK_TYPE_UNKNOWN:
default:
return null;
Expand Down
1 change: 1 addition & 0 deletions ios/RNCConnectionState.h
Expand Up @@ -23,6 +23,7 @@ static NSString *const RNCConnectionTypeEthernet = @"ethernet";
static NSString *const RNCCellularGeneration2g = @"2g";
static NSString *const RNCCellularGeneration3g = @"3g";
static NSString *const RNCCellularGeneration4g = @"4g";
static NSString *const RNCCellularGeneration5g = @"5g";

@interface RNCConnectionState : NSObject

Expand Down
3 changes: 3 additions & 0 deletions ios/RNCConnectionState.m
Expand Up @@ -62,6 +62,9 @@ - (instancetype)initWithReachabilityFlags:(SCNetworkReachabilityFlags)flags
_cellularGeneration = RNCCellularGeneration3g;
} else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyLTE]) {
_cellularGeneration = RNCCellularGeneration4g;
} else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyNRNSA] ||
[netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyNR]) {
_cellularGeneration = RNCCellularGeneration5g;
}
}
}
Expand Down
62 changes: 62 additions & 0 deletions src/__tests__/fetch.spec.ts
Expand Up @@ -24,6 +24,68 @@ beforeEach(() => {

describe('@react-native-community/netinfo fetch', () => {
describe('with cellular data types', () => {
describe('5g cellular generation', () => {
function dataProvider() {
return [
{
description:
'should resolve the promise correctly with expected 5g settings',
expectedConnectionInfo: {
type: NetInfoStateType.cellular,
isConnected: true,
isInternetReachable: true,
details: {
isConnectionExpensive: true,
cellularGeneration: NetInfoCellularGeneration['5g'],
},
},
},
{
description:
'should resolve the promise correctly when 5g returns not connected',
expectedConnectionInfo: {
type: NetInfoStateType.cellular,
isConnected: false,
isInternetReachable: true,
details: {
isConnectionExpensive: true,
cellularGeneration: NetInfoCellularGeneration['5g'],
},
},
},
{
description:
'should resolve the promise correctly when 5g returns internet not reachable',
expectedConnectionInfo: {
type: NetInfoStateType.cellular,
isConnected: true,
isInternetReachable: false,
details: {
isConnectionExpensive: true,
cellularGeneration: NetInfoCellularGeneration['5g'],
},
},
},
];
}

dataProvider().forEach(testCase => {
it(testCase.description, () => {
mockNativeModule.getCurrentState.mockResolvedValue(
testCase.expectedConnectionInfo,
);

NativeInterface.eventEmitter.emit(
DEVICE_CONNECTIVITY_EVENT,
testCase.expectedConnectionInfo,
);
expect(NetInfo.fetch()).resolves.toEqual(
testCase.expectedConnectionInfo,
);
});
});
});

describe('4g cellular generation', () => {
function dataProvider() {
return [
Expand Down
1 change: 1 addition & 0 deletions src/internal/types.ts
Expand Up @@ -23,6 +23,7 @@ export enum NetInfoCellularGeneration {
'2g' = '2g',
'3g' = '3g',
'4g' = '4g',
'5g' = '5g',
}

export interface NetInfoConnectedDetails {
Expand Down

0 comments on commit 6ba68e9

Please sign in to comment.