Skip to content

Commit

Permalink
Request storage permission to support older android versions
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Ray committed Jan 28, 2024
1 parent 3a17c28 commit 4f2592c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
25 changes: 19 additions & 6 deletions apps/app/lib/lifecycle.native.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AppState, BackHandler } from 'react-native'
import { Alert, AppState, BackHandler, PermissionsAndroid } from 'react-native'
import { EventType } from '@notifee/react-native'

import * as transmission from './transmission'
Expand All @@ -7,6 +7,8 @@ import {
destroyPersistentNotification,
registerEvents,
} from './persistentNotification'
import { requestStoragePermission } from './permissionsAndroid'
import i18n from '../i18n'

const handleEvents = async ({ type, detail }) => {
// Start all
Expand Down Expand Up @@ -34,16 +36,29 @@ const handleEvents = async ({ type, detail }) => {

let isInitialized = false

const init = () => {
const createPermissionDeniedAlert = () =>
Alert.alert(
i18n.t('alerts.storagePermissionDenied.title'),
i18n.t('alerts.storagePermissionDenied.description'),
[{ text: 'Quit', onPress: quitApp }]
)

const init = async () => {
isInitialized = true

// Init transmission & persistent notification
transmission.init()
createPersistentNotification()
isInitialized = true

const granted = await requestStoragePermission()
if (granted !== PermissionsAndroid.RESULTS.GRANTED) {
createPermissionDeniedAlert()
}
}

export const initApp = async () => {
// Next time the app become active, re-init if quitApp has been called
AppState.addEventListener('change', (nextAppState) => {
AppState.addEventListener('change', async (nextAppState) => {
if (nextAppState === 'active' && !isInitialized) {
init()
}
Expand All @@ -52,8 +67,6 @@ export const initApp = async () => {
// background events handler should be registered only once
registerEvents(handleEvents)
init()

isInitialized = true
}

export const quitApp = async () => {
Expand Down
15 changes: 15 additions & 0 deletions apps/app/lib/permissionsAndroid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { PermissionsAndroid } from 'react-native'
import i18n from '../i18n'

export const requestStoragePermission = async () => {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
{
title: i18n.t('alerts.storagePermissionRequest.title'),
message: i18n.t('alerts.storagePermissionRequest.description'),
buttonPositive: 'OK',
}
)

return granted
}
10 changes: 10 additions & 0 deletions apps/app/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -179,5 +179,15 @@
"library": {
"download": "Download",
"moreToCome": "More to come..."
},
"alerts": {
"storagePermissionRequest": {
"title": "Storage permission",
"description": "PikaTorrent needs access to storage to save downloaded data."
},
"storagePermissionDenied": {
"title": "Storage permission denied",
"description": "'You need to allow storage permission for PikaTorrent.'"
}
}
}

0 comments on commit 4f2592c

Please sign in to comment.