Skip to content

Commit fbf3681

Browse files
committed
reactNativeUtils [nfc]: Factor out androidSdkVersion helper
1 parent d815680 commit fbf3681

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

src/lightbox/download.js

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import { Platform, PermissionsAndroid } from 'react-native';
33
import type { Rationale } from 'react-native/Libraries/PermissionsAndroid/PermissionsAndroid';
44
import { CameraRoll } from '@react-native-camera-roll/camera-roll';
55
import RNFetchBlob from 'rn-fetch-blob';
6-
import invariant from 'invariant';
76

87
import type { Auth } from '../api/transportTypes';
98
import { getMimeTypeFromFileExtension } from '../utils/url';
9+
import { androidSdkVersion } from '../reactNativeUtils';
1010

1111
/**
1212
* Request permission WRITE_EXTERNAL_STORAGE if needed or throw if can't get it.
@@ -18,17 +18,7 @@ import { getMimeTypeFromFileExtension } from '../utils/url';
1818
* as a toast.
1919
*/
2020
export const androidEnsureStoragePermission = async (rationale: Rationale): Promise<void> => {
21-
invariant(
22-
Platform.OS === 'android',
23-
'androidEnsureStoragePermission should only be called on Android',
24-
);
25-
// Flow isn't refining `Platform` to a type that corresponds to values
26-
// we'll see on Android. We do expect `Platform.Version` to be a number on
27-
// Android; see https://reactnative.dev/docs/platform#version. Empirically
28-
// (and this isn't in the doc yet), it's the SDK version, so for Android
29-
// 10 it won't be 10, it'll be 29.
30-
const androidSdkVersion = (Platform.Version: number);
31-
if (androidSdkVersion > 28) {
21+
if (androidSdkVersion() > 28) {
3222
return;
3323
}
3424

src/reactNativeUtils.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
/* @flow strict-local */
22

33
import React from 'react';
4-
import { AppState } from 'react-native';
4+
import { AppState, Platform } from 'react-native';
55
import type { AppStateValues } from 'react-native/Libraries/AppState/AppState';
66
// eslint-disable-next-line id-match
77
import type { ____ViewStyle_Internal } from 'react-native/Libraries/StyleSheet/StyleSheetTypes';
8+
import invariant from 'invariant';
89

910
import * as logging from './utils/logging';
1011
import type { BoundedDiff } from './generics';
@@ -62,3 +63,19 @@ export function useAppState(): null | AppStateValues {
6263
}, []);
6364
return value;
6465
}
66+
67+
/**
68+
* The Android SDK version (e.g., 33 for Android 13 a.k.a. Tiramisu).
69+
*
70+
* Throws if called on iOS.
71+
*/
72+
export function androidSdkVersion(): number {
73+
invariant(Platform.OS === 'android', 'androidSdkVersion called on iOS');
74+
75+
// Flow isn't refining `Platform` to a type that corresponds to values
76+
// we'll see on Android. We do expect `Platform.Version` to be a number on
77+
// Android; see https://reactnative.dev/docs/platform#version. Empirically
78+
// (and this isn't in the doc yet), it's the SDK version, so for Android
79+
// 10 it won't be 10, it'll be 29.
80+
return (Platform.Version: number);
81+
}

0 commit comments

Comments
 (0)