This commit is contained in:
Fredrik Burmester
2025-07-16 19:51:17 +02:00
parent 2d434a0125
commit 83a264d5a1
7 changed files with 8448 additions and 497 deletions

View File

@@ -138,7 +138,8 @@
{
"useDefaultExpandedMediaControls": true
}
]
],
"expo-background-task"
],
"experiments": {
"typedRoutes": true

View File

@@ -36,10 +36,6 @@ const BackGroundDownloader = !Platform.isTV
import { DarkTheme, ThemeProvider } from "@react-navigation/native";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
const BackgroundFetch = !Platform.isTV
? require("expo-background-fetch")
: null;
import * as Device from "expo-device";
import * as FileSystem from "expo-file-system";
@@ -49,7 +45,7 @@ import { router, Stack, useSegments } from "expo-router";
import * as SplashScreen from "expo-splash-screen";
import * as ScreenOrientation from "@/packages/expo-screen-orientation";
const TaskManager = !Platform.isTV ? require("expo-task-manager") : null;
import * as TaskManager from "expo-task-manager";
import { getLocales } from "expo-localization";
import { Provider as JotaiProvider } from "jotai";
@@ -130,7 +126,9 @@ if (!Platform.isTV) {
console.log("TaskManager ~ sessions trigger");
const api = store.get(apiAtom);
if (api === null || api === undefined) return;
if (api === null || api === undefined) {
return { value: null };
}
const response = await getSessionApi(api).getSessions({
activeWithinSeconds: 360,
@@ -139,7 +137,7 @@ if (!Platform.isTV) {
const result = response.data.filter((s) => s.NowPlayingItem);
Notifications.setBadgeCountAsync(result.length);
return BackgroundFetch.BackgroundFetchResult.NewData;
return { value: "success" };
});
TaskManager.defineTask(BACKGROUND_FETCH_TASK, async () => {
@@ -149,20 +147,18 @@ if (!Platform.isTV) {
const settingsData = storage.getString("settings");
if (!settingsData) return BackgroundFetch.BackgroundFetchResult.NoData;
if (!settingsData) return { value: null };
const settings: Partial<Settings> = JSON.parse(settingsData);
const url = settings?.optimizedVersionsServerUrl;
if (!settings?.autoDownload || !url)
return BackgroundFetch.BackgroundFetchResult.NoData;
if (!settings?.autoDownload || !url) return { value: null };
const token = getTokenFromStorage();
const deviceId = getOrSetDeviceId();
const baseDirectory = FileSystem.documentDirectory;
if (!token || !deviceId || !baseDirectory)
return BackgroundFetch.BackgroundFetchResult.NoData;
if (!token || !deviceId || !baseDirectory) return { value: null };
const jobs = await getAllJobsByDeviceId({
deviceId,
@@ -195,7 +191,7 @@ if (!Platform.isTV) {
})
.done(() => {
console.log("TaskManager ~ Download completed: ", job.id);
saveDownloadedItemInfo(job.item);
_saveDownloadedItemInfo(job.item);
BackGroundDownloader.completeHandler(job.id);
cancelJobById({
authHeader: token,
@@ -233,7 +229,7 @@ if (!Platform.isTV) {
console.log(`Auto download started: ${new Date(now).toISOString()}`);
// Be sure to return the successful result type!
return BackgroundFetch.BackgroundFetchResult.NewData;
return { value: "success" };
});
}

742
bun.lock

File diff suppressed because it is too large Load Diff

View File

@@ -20,8 +20,7 @@ import { Text } from "../common/Text";
import { ListGroup } from "../list/ListGroup";
import { ListItem } from "../list/ListItem";
const BackgroundFetch = require("expo-background-fetch");
const TaskManager = require("expo-task-manager");
import * as TaskManager from "expo-task-manager";
export const OtherSettings: React.FC = () => {
const router = useRouter();
@@ -33,7 +32,8 @@ export const OtherSettings: React.FC = () => {
* Background task
*******************/
const checkStatusAsync = async () => {
await BackgroundFetch.getStatusAsync();
// expo-background-task doesn't have a direct status check
// Just check if the task is registered
return await TaskManager.isTaskRegisteredAsync(BACKGROUND_FETCH_TASK);
};

View File

@@ -34,7 +34,7 @@
"axios": "^1.7.9",
"expo": "~53.0.17",
"expo-asset": "~11.1.7",
"expo-background-fetch": "~13.1.6",
"expo-background-task": "~0.2.8",
"expo-blur": "~14.1.5",
"expo-brightness": "~13.1.4",
"expo-build-properties": "~0.14.8",

View File

@@ -1,46 +1,41 @@
const BackgroundFetch = require("expo-background-fetch");
import * as BackgroundTask from "expo-background-task";
export const BACKGROUND_FETCH_TASK = "background-fetch";
export const BACKGROUND_FETCH_TASK_SESSIONS = "background-fetch-sessions";
export async function registerBackgroundFetchAsync() {
try {
BackgroundFetch.registerTaskAsync(BACKGROUND_FETCH_TASK, {
minimumInterval: 60 * 1, // 1 minutes
stopOnTerminate: false, // android only,
startOnBoot: false, // android only
await BackgroundTask.registerTaskAsync(BACKGROUND_FETCH_TASK, {
minimumInterval: 60, // 1 minute (in seconds, not milliseconds)
});
} catch (error) {
console.log("Error registering background fetch task", error);
console.log("Error registering background task", error);
}
}
export async function unregisterBackgroundFetchAsync() {
try {
BackgroundFetch.unregisterTaskAsync(BACKGROUND_FETCH_TASK);
await BackgroundTask.unregisterTaskAsync(BACKGROUND_FETCH_TASK);
} catch (error) {
console.log("Error unregistering background fetch task", error);
console.log("Error unregistering background task", error);
}
}
export const BACKGROUND_FETCH_TASK_SESSIONS = "background-fetch-sessions";
export async function registerBackgroundFetchAsyncSessions() {
try {
console.log("Registering background fetch sessions");
BackgroundFetch.registerTaskAsync(BACKGROUND_FETCH_TASK_SESSIONS, {
minimumInterval: 1 * 60, // 1 minutes
stopOnTerminate: false, // android only,
startOnBoot: true, // android only
console.log("Registering background task sessions");
await BackgroundTask.registerTaskAsync(BACKGROUND_FETCH_TASK_SESSIONS, {
minimumInterval: 60, // 1 minute
});
} catch (error) {
console.log("Error registering background fetch task", error);
console.log("Error registering background task sessions", error);
}
}
export async function unregisterBackgroundFetchAsyncSessions() {
try {
BackgroundFetch.unregisterTaskAsync(BACKGROUND_FETCH_TASK_SESSIONS);
await BackgroundTask.unregisterTaskAsync(BACKGROUND_FETCH_TASK_SESSIONS);
} catch (error) {
console.log("Error unregistering background fetch task", error);
console.log("Error unregistering background task sessions", error);
}
}

8137
yarn.lock Normal file

File diff suppressed because it is too large Load Diff