Skip to content

Commit 96abdd6

Browse files
committed
background fetch
1 parent 139d544 commit 96abdd6

6 files changed

Lines changed: 84 additions & 0 deletions

File tree

apps/mobile/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"expo-apple-authentication": "7.1.3",
5555
"expo-application": "6.0.2",
5656
"expo-av": "15.0.2",
57+
"expo-background-fetch": "13.0.6",
5758
"expo-blur": "14.0.3",
5859
"expo-build-properties": "0.13.2",
5960
"expo-clipboard": "7.0.1",
@@ -78,6 +79,7 @@
7879
"expo-status-bar": "2.0.1",
7980
"expo-symbols": "0.2.2",
8081
"expo-system-ui": "4.0.9",
82+
"expo-task-manager": "12.0.6",
8183
"expo-updates": "0.27.4",
8284
"expo-web-browser": "14.0.2",
8385
"hono": "4.7.5",
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import * as BackgroundFetch from "expo-background-fetch"
2+
import * as TaskManager from "expo-task-manager"
3+
4+
import { unreadSyncService } from "../store/unread/store"
5+
6+
const BACKGROUND_FETCH_TASK = "background-fetch"
7+
8+
export async function initBackgroundFetch() {
9+
TaskManager.defineTask(BACKGROUND_FETCH_TASK, async () => {
10+
// const now = Date.now()
11+
// console.log(`Got background fetch call at date: ${new Date(now).toISOString()}`)
12+
13+
try {
14+
const res = await unreadSyncService.updateBadgeAtBackground()
15+
return res
16+
? BackgroundFetch.BackgroundFetchResult.NewData
17+
: BackgroundFetch.BackgroundFetchResult.NoData
18+
} catch (err) {
19+
console.error(err)
20+
return BackgroundFetch.BackgroundFetchResult.Failed
21+
}
22+
})
23+
24+
return BackgroundFetch.registerTaskAsync(BACKGROUND_FETCH_TASK, {
25+
minimumInterval: 60 * 15, // 15 minutes
26+
})
27+
}

apps/mobile/src/initialize/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { nativeApplicationVersion } from "expo-application"
44
import { initializeDb } from "../database"
55
import { initAnalytics } from "./analytics"
66
import { initializeAppCheck } from "./app-check"
7+
import { initBackgroundFetch } from "./background"
78
import { initCrashlytics } from "./crashlytics"
89
import { initializeDayjs } from "./dayjs"
910
import { hydrateDatabaseToStore, hydrateQueryClient, hydrateSettings } from "./hydrate"
@@ -40,6 +41,7 @@ export const initializeApp = async () => {
4041
using_indexed_db: true,
4142
})
4243
initCrashlytics()
44+
initBackgroundFetch()
4345
console.log(`Initialize done,`, `${loadingTime}ms`)
4446
}
4547

apps/mobile/src/store/unread/getter.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,8 @@ export const getUnreadCount = (id: string) => {
44
const state = useUnreadStore.getState()
55
return state.data[id] ?? 0
66
}
7+
8+
export const getAllUnreadCount = () => {
9+
const state = useUnreadStore.getState()
10+
return Object.values(state.data).reduce((acc, unread) => acc + unread, 0)
11+
}

apps/mobile/src/store/unread/store.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { FeedViewType } from "@follow/constants"
2+
import * as Notifications from "expo-notifications"
23

34
import type { UnreadSchema } from "@/src/database/schemas/types"
45
import { apiClient } from "@/src/lib/api-fetch"
@@ -9,6 +10,7 @@ import { getEntry } from "../entry/getter"
910
import { entryActions } from "../entry/store"
1011
import { createTransaction, createZustandStore } from "../internal/helper"
1112
import { getSubscriptionByView } from "../subscription/getter"
13+
import { getAllUnreadCount } from "./getter"
1214

1315
type SubscriptionId = string
1416
interface UnreadStore {
@@ -30,6 +32,17 @@ class UnreadSyncService {
3032
return res.data
3133
}
3234

35+
async updateBadgeAtBackground() {
36+
await this.fetch()
37+
const allUnreadCount = getAllUnreadCount()
38+
const currentBadgeCount = await Notifications.getBadgeCountAsync()
39+
if (allUnreadCount === currentBadgeCount) {
40+
return false
41+
}
42+
await Notifications.setBadgeCountAsync(allUnreadCount)
43+
return true
44+
}
45+
3346
private async updateUnreadStatus(feedIds: string[]) {
3447
await unreadActions.upsertMany(feedIds.map((id) => ({ subscriptionId: id, count: 0 })))
3548
entryActions.markEntryReadStatusInSession({ feedIds, read: true })

pnpm-lock.yaml

Lines changed: 35 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)