Skip to content

Commit

Permalink
reactNativeUtils [nfc]: Factor out androidSdkVersion helper
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbobbe committed Jan 12, 2023
1 parent d815680 commit fbf3681
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
14 changes: 2 additions & 12 deletions src/lightbox/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { Platform, PermissionsAndroid } from 'react-native';
import type { Rationale } from 'react-native/Libraries/PermissionsAndroid/PermissionsAndroid';
import { CameraRoll } from '@react-native-camera-roll/camera-roll';
import RNFetchBlob from 'rn-fetch-blob';
import invariant from 'invariant';

import type { Auth } from '../api/transportTypes';
import { getMimeTypeFromFileExtension } from '../utils/url';
import { androidSdkVersion } from '../reactNativeUtils';

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

Expand Down
19 changes: 18 additions & 1 deletion src/reactNativeUtils.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/* @flow strict-local */

import React from 'react';
import { AppState } from 'react-native';
import { AppState, Platform } from 'react-native';
import type { AppStateValues } from 'react-native/Libraries/AppState/AppState';
// eslint-disable-next-line id-match
import type { ____ViewStyle_Internal } from 'react-native/Libraries/StyleSheet/StyleSheetTypes';
import invariant from 'invariant';

import * as logging from './utils/logging';
import type { BoundedDiff } from './generics';
Expand Down Expand Up @@ -62,3 +63,19 @@ export function useAppState(): null | AppStateValues {
}, []);
return value;
}

/**
* The Android SDK version (e.g., 33 for Android 13 a.k.a. Tiramisu).
*
* Throws if called on iOS.
*/
export function androidSdkVersion(): number {
invariant(Platform.OS === 'android', 'androidSdkVersion called on iOS');

// Flow isn't refining `Platform` to a type that corresponds to values
// we'll see on Android. We do expect `Platform.Version` to be a number on
// Android; see https://reactnative.dev/docs/platform#version. Empirically
// (and this isn't in the doc yet), it's the SDK version, so for Android
// 10 it won't be 10, it'll be 29.
return (Platform.Version: number);
}

0 comments on commit fbf3681

Please sign in to comment.