A self-hostable bookmark-everything app (links, notes and images) with AI-based automatic tagging and full text search
feat(mobile): run offline downloads while the app is open (#3177)
Mohamed Bassem364f709dfbd9
diff --git a/apps/mobile/components/OfflineDownloadSync.tsx b/apps/mobile/components/OfflineDownloadSync.tsxnew file mode 100644index 00000000..64f1ab13--- /dev/null+++ b/apps/mobile/components/OfflineDownloadSync.tsx@@ -0,0 +1,104 @@+import type { AppStateStatus } from "react-native";+import { useEffect } from "react";+import { AppState } from "react-native";+import * as Network from "expo-network";+import { subscribeToOfflineDownloadPrefs } from "@/lib/offlineDownloadPrefs";+import {+ getOfflineLibraryScope,+ reconcileOfflineLibrary,+} from "@/lib/offlineLibrary";+import {+ cancelOfflineDownloads,+ refreshOfflineDownloads,+} from "@/lib/offlineSync";+import useAppSettings from "@/lib/settings";++// Settle time after a burst of changes (toggling several lists, flaky+// connectivity) before starting a pass.+const CHANGE_DEBOUNCE = 1_500;+// Much shorter than the refresh throttle, which skips the ticks inside it, so+// a pass starts soon after one is due rather than up to a throttle late.+const TICK_INTERVAL = 60_000;++function isActive(state: AppStateStatus = AppState.currentState) {+ return state === "active";+}++/**+ * Keeps automatic offline downloads going while the app is open: on launch,+ * on return to the foreground, periodically, and shortly after a download+ * settings or connectivity change.+ */+export function OfflineDownloadSync() {+ const { settings } = useAppSettings();+ const isLoggedIn = !!settings.apiKey;+ const scope = getOfflineLibraryScope(settings);++ useEffect(() => {+ if (!isLoggedIn) {+ return;+ }++ reconcileOfflineLibrary(scope);++ let pending: ReturnType<typeof setTimeout> | undefined;+ // A change that came due in the background, to act on once back.+ let changedInBackground = false;+ const refreshSoon = () => {+ clearTimeout(pending);+ pending = setTimeout(() => {+ if (isActive()) {+ void refreshOfflineDownloads({ force: true });+ } else {+ changedInBackground = true;+ }+ }, CHANGE_DEBOUNCE);+ };+ const refreshIfDue = () => {+ if (isActive()) {+ void refreshOfflineDownloads({ force: changedInBackground });+ changedInBackground = false;+ }+ };+ refreshIfDue();++ const interval = setInterval(refreshIfDue, TICK_INTERVAL);+ const appStateSubscription = AppState.addEventListener(+ "change",+ (state) => {+ if (isActive(state)) {+ refreshIfDue();+ } else if (state === "background") {+ cancelOfflineDownloads();+ }+ },+ );+ // Restart rather than continue: a pass that started on Wi-Fi must not+ // carry on over cellular. Only for changes that matter to a pass; Android+ // also reports capability changes that leave all of these as they were.+ let network: string | undefined;+ const networkSubscription = Network.addNetworkStateListener((state) => {+ const next = `${state.isConnected}|${state.type}`;+ if (next === network) {+ return;+ }+ network = next;+ cancelOfflineDownloads();+ refreshSoon();+ });+ const unsubscribePrefs = subscribeToOfflineDownloadPrefs(scope, () => {+ cancelOfflineDownloads();+ refreshSoon();+ });++ return () => {+ clearTimeout(pending);+ clearInterval(interval);+ appStateSubscription.remove();+ networkSubscription.remove();+ unsubscribePrefs();+ };+ }, [isLoggedIn, scope]);++ return null;+}diff --git a/apps/mobile/lib/providers.tsx b/apps/mobile/lib/providers.tsxindex 7d79c225..5d1a8b37 100644--- a/apps/mobile/lib/providers.tsx+++ b/apps/mobile/lib/providers.tsx@@ -1,4 +1,5 @@import { useEffect, useMemo, useState } from "react";+import { OfflineDownloadSync } from "@/components/OfflineDownloadSync";import FullPageSpinner from "@/components/ui/FullPageSpinner";import { PersistQueryClientProvider } from "@tanstack/react-query-persist-client";import { Toaster } from "sonner-native";@@ -52,6 +53,7 @@ export function Providers({ children }: { children: React.ReactNode }) {><TRPCSettingsProvider settings={settings} queryClient={queryClient}><ClientConfigProvider>+ <OfflineDownloadSync /><ConnectionStatusProvider enabled={!!settings.apiKey}><ReaderSettingsProvider>{children}