Skip to content

Commit

Permalink
chore
Browse files Browse the repository at this point in the history
  • Loading branch information
lovetingyuan committed Jan 2, 2024
1 parent 52fa07d commit 03b2668
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 26 deletions.
2 changes: 2 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import ImagesView from './components/ImagesView'
import type { ProviderConfiguration, SWRConfiguration } from 'swr/_internal'
import ErrorFallback from './components/ErrorFallback'
import useIsDark from './hooks/useIsDark'
// import { useCheckLivingUps } from './api/living-info'

let online = true
let focus = true
Expand Down Expand Up @@ -88,6 +89,7 @@ export default function App() {
mode: dark ? 'dark' : 'light',
})
}, [dark])
// useCheckLivingUps()
return (
<RootSiblingParent>
<StatusBar style="auto" />
Expand Down
24 changes: 21 additions & 3 deletions src/api/dynamic-items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -492,9 +492,12 @@ export function useDynamicItems(mid?: string | number) {
)

const dynamicItems: DynamicListResponse['items'] =
data?.reduce((a, b) => {
return a.concat(b.items)
}, [] as DynamicListResponse['items']) || []
data?.reduce(
(a, b) => {
return a.concat(b.items)
},
[] as DynamicListResponse['items'],
) || []

const isEmpty = data?.[0]?.items.length === 0
const isReachingEnd = isEmpty || (data && !data[data.length - 1]?.has_more)
Expand Down Expand Up @@ -580,3 +583,18 @@ export async function checkUpdateUps(first: boolean) {
}
})
}

let checkUpdateUpsTimer: number | null = null

export function startCheckUpdateUps() {
if (typeof checkUpdateUpsTimer === 'number') {
window.clearInterval(checkUpdateUpsTimer)
}
checkUpdateUps(false)
checkUpdateUpsTimer = window.setInterval(
() => {
checkUpdateUps(false)
},
10 * 60 * 1000,
)
}
2 changes: 1 addition & 1 deletion src/api/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default function request<D extends any>(url: string) {
? url
: 'https://api.bilibili.com' + url

// __DEV__ && console.log('request url: ', url.slice(0, 150))
__DEV__ && console.log('request url: ', url.slice(0, 150))
// console.log(444, requestUrl)
return fetch(requestUrl, {
headers: {
Expand Down
48 changes: 44 additions & 4 deletions src/api/living-info.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { z } from 'zod'
import request from './fetcher'
import { LiveInfoBatchItemSchema } from './living-info.schema'
import store from '../store'
import store, { useStore } from '../store'
import { Vibration } from 'react-native'
import useSWR from 'swr'

Expand Down Expand Up @@ -67,14 +67,54 @@ export const checkLivingUps = () => {
})
}

export const useCheckLivingUps = () => {
const { $followedUps } = useStore()
const url =
$followedUps.length > 0
? `https://api.live.bilibili.com/room/v1/Room/get_status_info_by_uids?${$followedUps
.map(user => `uids[]=${user.mid}`)
.sort()
.join('&')}`
: null
const { data } = useSWR<
Record<string, z.infer<typeof LiveInfoBatchItemSchema>>
>(url, request, {
refreshInterval: 5 * 60 * 1000,
})
const livingMap: Record<string, string> = {}
if (!data) {
return
}
Object.keys(data).forEach(mid => {
// https://live.bilibili.com/h5/24446464
const { live_status, room_id } = data[mid]
if (live_status === 1) {
livingMap[mid] = 'https://live.bilibili.com/h5/' + room_id
}
})
let notVibrate = true
for (const id in livingMap) {
if (!(id in prevLivingMap) && notVibrate) {
Vibration.vibrate(900)
notVibrate = false
break
}
}
prevLivingMap = livingMap
store.livingUps = livingMap
}

let checkLivingTimer: number | null = null

export function startCheckLivingUps() {
if (typeof checkLivingTimer === 'number') {
window.clearInterval(checkLivingTimer)
}
checkLivingUps()
checkLivingTimer = window.setInterval(() => {
checkLivingUps()
}, 5 * 60 * 1000)
checkLivingTimer = window.setInterval(
() => {
checkLivingUps()
},
5 * 60 * 1000,
)
}
3 changes: 3 additions & 0 deletions src/routes/About/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ import BlackUps from './BlackUps'
import BlackTags from './BlackTags'
import SortCate from './SortCate'
import Backup from './Backup'
import { useStore } from '../../store'

export default React.memo(function About() {
// const { testid } = useStore()
const content = (
<ScrollView style={styles.container}>
{/* <Text>testid: {testid}</Text> */}
<Header />
<Divider style={{ marginBottom: 18, marginTop: 15 }} />
<Version />
Expand Down
1 change: 1 addition & 0 deletions src/routes/Follow/AddFollow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ function SearchedItem(props: { up: SearchedUpType }) {
}
const handler = () => {
store.$followedUps.unshift(user)
// store.$followedUps = [user, ...store.$followedUps]
}
const goToDynamic = () => {
Linking.openURL(`https://m.bilibili.com/space/${up.mid}`)
Expand Down
22 changes: 8 additions & 14 deletions src/routes/Follow/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import commonStyles from '../../styles'
import AddFollow from './AddFollow'
import useMounted from '../../hooks/useMounted'
import useIsDark from '../../hooks/useIsDark'
import { checkLivingUps } from '../../api/living-info'

const renderItem = ({
item,
Expand All @@ -30,8 +31,6 @@ const renderItem = ({
return <View style={commonStyles.flex1} />
}

let firstRender = true

const tvL = require('../../../assets/tv-l.png')
const tvR = require('../../../assets/tv-r.png')

Expand Down Expand Up @@ -60,25 +59,18 @@ const TvImg: React.FC = () => {
/>
)
}

export default function Follow() {
let checkLiveTime = 0
function Follow() {
// eslint-disable-next-line no-console
__DEV__ && console.log('Follow page')
const { $followedUps, $upUpdateMap, livingUps } = useStore()
const followListRef = React.useRef<FlatList | null>(null)
const dark = useIsDark()

// 检查用户更新,由于组件会随路由重新创建,这里保证只运行一次
useMounted(() => {
if (firstRender) {
checkUpdateUps(true)
firstRender = false
window.setInterval(
() => {
checkUpdateUps(false)
},
10 * 60 * 1000,
)
if (Date.now() - checkLiveTime > 60 * 1000) {
checkLivingUps()
checkLiveTime = Date.now()
}
})

Expand Down Expand Up @@ -168,6 +160,8 @@ export default function Follow() {
)
}

export default React.memo(Follow)

const styles = StyleSheet.create({
container: {
flex: 1,
Expand Down
8 changes: 6 additions & 2 deletions src/routes/VideoList/VideoItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default React.memo(function HotItem({ video }: { video: VideoItem }) {
const { theme } = useTheme()
const { width } = useWindowDimensions()
const itemWidth = (width - 24) / 2
const { isWiFi } = useStore()
const { isWiFi, followedUpsMap } = useStore()
return (
<View style={[styles.itemContainer, { width: itemWidth }]}>
<View style={commonStyles.flex1}>
Expand Down Expand Up @@ -65,7 +65,11 @@ export default React.memo(function HotItem({ video }: { video: VideoItem }) {
style={[
styles.upNameText,
{
color: theme.colors.primary,
color:
video.mid in followedUpsMap
? theme.colors.secondary
: theme.colors.primary,
fontWeight: video.mid in followedUpsMap ? 'bold' : 'normal',
},
]}>
{video.name}
Expand Down
28 changes: 26 additions & 2 deletions src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ import AsyncStorage from '@react-native-async-storage/async-storage'
import { proxy, subscribe, useSnapshot } from 'valtio'
import { RanksConfig } from '../constants'
import { checkUpdate } from '../api/check-update'
import type { VideoItem } from '../api/hot-videos'
import { startCheckLivingUps } from '../api/living-info'
import { UpInfo } from '../types'
// import { registerCheckLivingUps } from '../utils/check-live'
// import { showToast } from '../utils'
// import { subscribeKey } from 'valtio/utils'
import { startCheckLivingUps } from '../api/living-info'
import { startCheckUpdateUps } from '../api/dynamic-items'

interface UpdateUpInfo {
latestId: string
Expand All @@ -22,6 +25,7 @@ const store = proxy<{
// $cachedHotVideos: VideoItem[]
// $pinUps: Record<string, number>
// ----------------------------
followedUpsMap: Record<string, UpInfo>
initialed: boolean
isWiFi: boolean
webViewMode: 'PC' | 'MOBILE'
Expand All @@ -40,9 +44,18 @@ const store = proxy<{
showCaptcha: boolean
updatedCount: number
moreRepliesUrl: string
testid: number
}>({
$blackUps: {},
$followedUps: [],
get followedUpsMap() {
// console.log('followedUpsMapfollowedUpsMapfollowedUpsMap')
const ups: Record<string, UpInfo> = {}
for (const up of this.$followedUps) {
ups[up.mid] = up
}
return ups
},
$blackTags: {},
$upUpdateMap: {},
$ignoredVersions: [],
Expand All @@ -62,6 +75,7 @@ const store = proxy<{
currentImageIndex: 0,
overlayButtons: [],
showCaptcha: false,
testid: 0,
get updatedCount() {
const aa = Object.values<UpdateUpInfo>(this.$upUpdateMap)
return aa.filter(item => {
Expand Down Expand Up @@ -90,6 +104,9 @@ Promise.all(
)
.then(() => {
store.initialed = true
// subscribeKey(store, '$followedUps', v =>
// console.log('$followedUps has changed to', v),
// )
subscribe(store, changes => {
const changedKeys = new Set<StoredKeys>()
for (const op of changes) {
Expand All @@ -98,13 +115,20 @@ Promise.all(
changedKeys.add(ck as StoredKeys)
}
}
// console.log('chencged', [...changedKeys])
for (const ck of changedKeys) {
AsyncStorage.setItem(StoragePrefix + ck, JSON.stringify(store[ck]))
}
})
})
.then(() => {
startCheckLivingUps()
startCheckUpdateUps()
// return registerCheckLivingUps().then(good => {
// if (!good) {
// showToast('检查直播失败')
// }
// })
})

export function useStore() {
Expand Down
46 changes: 46 additions & 0 deletions src/utils/check-live.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import * as BackgroundFetch from 'expo-background-fetch'
import * as TaskManager from 'expo-task-manager'
import { checkLivingUps } from '../api/living-info'
import store from '../store'

const CHECK_LIVING_UPS = 'check-living-ups'

// 1. Define the task by providing a name and the function that should be executed
// Note: This needs to be called in the global scope (e.g outside of your React components)
TaskManager.defineTask(CHECK_LIVING_UPS, async () => {
// const now = Date.now();
console.log(4353453453)
store.testid++
// console.log(`Got background fetch call at date: ${new Date(now).toISOString()}`);
// await checkLivingUps()
// Be sure to return the successful result type!
return BackgroundFetch.BackgroundFetchResult.NewData
})

// 2. Register the task at some point in your app by providing the same name,
// and some configuration options for how the background fetch should behave
// Note: This does NOT need to be in the global scope and CAN be used in your React components!
export async function registerCheckLivingUps() {
return BackgroundFetch.registerTaskAsync(CHECK_LIVING_UPS, {
minimumInterval: 60 * 10, // 10 minutes
stopOnTerminate: false, // android only,
startOnBoot: true, // android only
}).then(() => {
return isCheckLivingUpsTaskGood()
})
}

// 3. (Optional) Unregister tasks by specifying the task name
// This will cancel any future background fetch calls that match the given name
// Note: This does NOT need to be in the global scope and CAN be used in your React components!
export async function unregisterCheckLivingUps() {
return BackgroundFetch.unregisterTaskAsync(CHECK_LIVING_UPS)
}

async function isCheckLivingUpsTaskGood() {
const status = await BackgroundFetch.getStatusAsync()
const isRegistered = await TaskManager.isTaskRegisteredAsync(CHECK_LIVING_UPS)
return (
status === BackgroundFetch.BackgroundFetchStatus.Available && isRegistered
)
}

0 comments on commit 03b2668

Please sign in to comment.