Skip to content

Commit 513aa28

Browse files
committed
feat: Add notifications to downloads
1 parent a3c840c commit 513aa28

File tree

2 files changed

+71
-31
lines changed

2 files changed

+71
-31
lines changed

app/(details)/[id].tsx

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,7 @@ import AsyncStorage from "@react-native-async-storage/async-storage";
55
import { useMutation, useQuery } from "@tanstack/react-query";
66
import { Stack, useLocalSearchParams } from "expo-router";
77
import 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";
159
import BackgroundService from "react-native-background-actions";
1610
import RNFS from "react-native-fs";
1711
import {
@@ -21,28 +15,10 @@ import {
2115
Text,
2216
} from "react-native-paper";
2317
import 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

4723
interface 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",

utils/deviceMethods.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { Platform, PermissionsAndroid } from "react-native";
2+
3+
export const requestStoragePermission = async () => {
4+
try {
5+
if (Number(Platform.Version) >= 33) {
6+
return true;
7+
}
8+
const granted = await PermissionsAndroid.request(
9+
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
10+
{
11+
title: "Storage Permission",
12+
message: "App needs access to storage to download files",
13+
buttonNeutral: "Ask Me Later",
14+
buttonNegative: "Cancel",
15+
buttonPositive: "OK",
16+
},
17+
);
18+
return granted === PermissionsAndroid.RESULTS.GRANTED;
19+
} catch (err) {
20+
console.warn(err);
21+
return false;
22+
}
23+
};
24+
25+
export const requestNotificationPermission = async () => {
26+
try {
27+
const granted = await PermissionsAndroid.request(
28+
PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS,
29+
{
30+
title: "Notification Permission",
31+
message: "App needs permission to send notifications",
32+
buttonNeutral: "Ask Me Later",
33+
buttonNegative: "Cancel",
34+
buttonPositive: "Allow",
35+
},
36+
);
37+
return granted === PermissionsAndroid.RESULTS.GRANTED;
38+
} catch (err) {
39+
console.warn(err);
40+
return;
41+
}
42+
};

0 commit comments

Comments
 (0)