@@ -5,13 +5,7 @@ import AsyncStorage from "@react-native-async-storage/async-storage";
55import { useMutation , useQuery } from "@tanstack/react-query" ;
66import { Stack , useLocalSearchParams } from "expo-router" ;
77import React , { useCallback , useEffect , useState } from "react" ;
8- import {
9- Platform ,
10- PermissionsAndroid ,
11- ScrollView ,
12- StyleSheet ,
13- View ,
14- } from "react-native" ;
8+ import { ScrollView , StyleSheet , View } from "react-native" ;
159import BackgroundService from "react-native-background-actions" ;
1610import RNFS from "react-native-fs" ;
1711import {
@@ -21,28 +15,10 @@ import {
2115 Text ,
2216} from "react-native-paper" ;
2317import Toast from "react-native-toast-message" ;
24-
25- const requestStoragePermission = async ( ) => {
26- try {
27- if ( Number ( Platform . Version ) >= 33 ) {
28- return true ;
29- }
30- const granted = await PermissionsAndroid . request (
31- PermissionsAndroid . PERMISSIONS . WRITE_EXTERNAL_STORAGE ,
32- {
33- title : "Storage Permission" ,
34- message : "App needs access to storage to download files" ,
35- buttonNeutral : "Ask Me Later" ,
36- buttonNegative : "Cancel" ,
37- buttonPositive : "OK" ,
38- } ,
39- ) ;
40- return granted === PermissionsAndroid . RESULTS . GRANTED ;
41- } catch ( err ) {
42- console . warn ( err ) ;
43- return false ;
44- }
45- } ;
18+ import {
19+ requestNotificationPermission ,
20+ requestStoragePermission ,
21+ } from "@/utils/deviceMethods" ;
4622
4723interface DownloadProgress {
4824 [ key : string ] : {
@@ -109,6 +85,7 @@ export default function TorrentDetailsScreen() {
10985 } ;
11086
11187 loadCachedLinks ( ) ;
88+
11289 return ( ) => {
11390 BackgroundService . stop ( ) ;
11491 } ;
@@ -200,6 +177,20 @@ export default function TorrentDetailsScreen() {
200177 try {
201178 const filename = item . filename ;
202179 const downloadUrl = item . download ;
180+ const notificationTaskName = `Download-${ item . filename } ` ;
181+
182+ let downloadNotificationOptions = {
183+ taskName : notificationTaskName ,
184+ taskTitle : `Downloading ${ item . filename } ` ,
185+ taskDesc : "Progress: 0%" ,
186+ color : "#ff00ff" ,
187+ progressBar : {
188+ max : 100 ,
189+ value : 0 ,
190+ indeterminate : false ,
191+ } ,
192+ } ;
193+ await BackgroundService . updateNotification ( downloadNotificationOptions ) ;
203194
204195 // Get the downloads directory path
205196 const downloadPath = RNFS . DownloadDirectoryPath + "/Kaizoku/" + filename ;
@@ -212,10 +203,16 @@ export default function TorrentDetailsScreen() {
212203 const { promise } = RNFS . downloadFile ( {
213204 fromUrl : downloadUrl ,
214205 toFile : downloadPath ,
215- progress : ( response ) => {
206+ progress : async ( response ) => {
216207 const progress =
217208 ( response . bytesWritten / response . contentLength ) * 100 ;
218209
210+ downloadNotificationOptions . progressBar . value = progress ;
211+ downloadNotificationOptions . taskDesc = `Progress: ${ progress } %` ;
212+ await BackgroundService . updateNotification (
213+ downloadNotificationOptions ,
214+ ) ;
215+
219216 // Update the downloads state
220217 setDownloads ( ( prev ) => ( {
221218 ...prev ,
@@ -276,8 +273,9 @@ export default function TorrentDetailsScreen() {
276273
277274 const handleDownload = async ( item : UnrestrictModel . UnrestrictedItem ) => {
278275 const hasStoragePermission = await requestStoragePermission ( ) ;
276+ const hasNotificationPermission = await requestNotificationPermission ( ) ;
279277
280- if ( ! hasStoragePermission ) {
278+ if ( ! hasStoragePermission && ! hasNotificationPermission ) {
281279 Toast . show ( {
282280 type : "error" ,
283281 text1 : "Permission Required" ,
0 commit comments