Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"expo": {
"name": "Security Application",
"slug": "security-app",
"version": "1.2.0",
"version": "1.2.1",
"orientation": "portrait",
"icon": "./assets/images/icon.png",
"scheme": "securityapp",
Expand Down
10 changes: 10 additions & 0 deletions app/(tabs)/gallery.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import ImageGrid from '@/components/image-grid'
import MainView from '@/components/main-view'
import Pagination from '@/components/pagination'
import ThemedText from '@/components/themed-text'
import TimelapsePlayer from '@/components/timelapse-player'
import { ITEMS_PER_PAGE } from '@/constants/api'
import { ContextError } from '@/context/ContextError'
import { ContextImageRotation } from '@/context/ContextImageRotation'
Expand Down Expand Up @@ -247,6 +248,15 @@ const GalleryScreen = () => {
</View>
) : selectedDay && images ? (
<>
<TimelapsePlayer
key={selectedDay}
day={selectedDay}
thumbnail={
days?.find((day) => day.date === selectedDay)
?.thumbnail
}
userToken={userToken}
/>
{actions}
<ImageGrid
images={images}
Expand Down
2 changes: 2 additions & 0 deletions components/icon-symbol.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ type MAPPING = {
'expand-less': 'expand-less'
image: 'image'
login: 'login'
pause: 'pause'
person: 'person'
'photo-camera': 'photo-camera'
'play-arrow': 'play-arrow'
}

export type IconMapping = keyof MAPPING
Expand Down
303 changes: 303 additions & 0 deletions components/timelapse-player.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,303 @@
import { Image } from 'expo-image'
import { useFocusEffect } from 'expo-router'
import {
useCallback,
useContext,
useEffect,
useMemo,
useRef,
useState,
} from 'react'
import { ActivityIndicator, Pressable, StyleSheet, View } from 'react-native'

import IconSymbol from '@/components/icon-symbol'
import ThemedText from '@/components/themed-text'
import { API_TIMEOUT_MS, URL } from '@/constants/api'
import { Colors } from '@/constants/theme'
import { ContextImageRotation } from '@/context/ContextImageRotation'
import { useThemeColor } from '@/hooks/use-theme-color'
import { formatDay } from '@/utils/formatDay'

const errorImageSource = require('./../assets/images/error.webp')

const FRAMES_PER_SECOND = 12
const FRAMES_BEFORE_START = 36
const PREFETCH_CONCURRENCY = 4

type Status = 'idle' | 'loading' | 'playing' | 'paused' | 'error'

type TimelapsePlayerProps = {
day: string
thumbnail?: string
userToken: string
}

const frameTime = (uri: string) => {
const match = /_(\d{2})-(\d{2})-\d{2}\.jpg$/.exec(uri)
return match ? `${match[1]}:${match[2]}` : ''
}

const fetchDayImages = async (day: string, headers: Record<string, string>) => {
const paths: string[] = []
let page = 1
let total = Infinity

while (paths.length < total) {
const controller = new AbortController()
const timeoutId = setTimeout(() => controller.abort(), API_TIMEOUT_MS)
try {
const response = await fetch(
`${URL}/images?day=${day}&page=${page}`,
{ headers, signal: controller.signal }
)
if (!response.ok) {
throw new Error(`Error ${response.status}`)
}
const data: { images: string[]; total_images: number } =
await response.json()
total = data.total_images
if (!data.images.length) {
break
}
paths.push(...data.images)
page++
} finally {
clearTimeout(timeoutId)
}
}

return paths.map((path) => `${URL}${encodeURI(path)}`)
}

const TimelapsePlayer = ({
day,
thumbnail,
userToken,
}: TimelapsePlayerProps) => {
const contextImageRotation = useContext(ContextImageRotation)
if (!contextImageRotation) {
throw new Error(
'TimelapsePlayer must be used within a ContextImageRotation.Provider'
)
}
const { imageRotation } = contextImageRotation

const barColor = useThemeColor(
{ light: Colors.dark.highlight },
'highlight'
)
const barTextColor = useThemeColor({}, 'textLight')
const borderColor = useThemeColor({}, 'border')
const highlightColor = useThemeColor({}, 'highlight')

const headers = useMemo(
() => ({ Authorization: `Bearer ${userToken}` }),
[userToken]
)

const [frames, setFrames] = useState<string[]>([])
const [index, setIndex] = useState<number>(0)
const [loaded, setLoaded] = useState<number>(0)
const [status, setStatus] = useState<Status>('idle')
const [thumbnailFailed, setThumbnailFailed] = useState<boolean>(false)

const isMounted = useRef<boolean>(true)
const loadedRef = useRef<number>(0)

useEffect(() => {
isMounted.current = true
return () => {
isMounted.current = false
}
}, [])

const loadFrames = async () => {
setStatus('loading')
setFrames([])
setIndex(0)
setLoaded(0)
loadedRef.current = 0

try {
const uris = await fetchDayImages(day, headers)
if (!isMounted.current) {
return
}
if (!uris.length) {
throw new Error('No images')
}
setFrames(uris)

const done = new Array<boolean>(uris.length).fill(false)
let next = 0
let contiguous = 0

const worker = async () => {
while (isMounted.current && next < uris.length) {
const current = next++
await Image.prefetch(uris[current], {
cachePolicy: 'memory-disk',
headers,
}).catch(() => false)
done[current] = true
while (contiguous < uris.length && done[contiguous]) {
contiguous++
}
if (isMounted.current) {
loadedRef.current = contiguous
setLoaded(contiguous)
if (
contiguous >=
Math.min(FRAMES_BEFORE_START, uris.length)
) {
setStatus((status) =>
status === 'loading' ? 'playing' : status
)
}
}
}
}

await Promise.all(
Array.from({ length: PREFETCH_CONCURRENCY }, worker)
)
} catch {
if (isMounted.current) {
setStatus('error')
}
}
}

useEffect(() => {
if (status !== 'playing' || !frames.length) {
return
}
const interval = setInterval(() => {
setIndex((current) => {
const next = (current + 1) % frames.length
return next !== 0 && next >= loadedRef.current ? current : next
})
}, 1000 / FRAMES_PER_SECOND)
return () => clearInterval(interval)
}, [frames.length, status])

useFocusEffect(
useCallback(() => {
return () => {
setStatus((current) =>
current === 'playing' ? 'paused' : current
)
}
}, [])
)

const handlePress = () => {
if (status === 'idle' || status === 'error') {
loadFrames()
} else if (status === 'playing') {
setStatus('paused')
} else if (status === 'paused') {
setStatus('playing')
}
}

const showFrames = frames.length > 0 && status !== 'error'
const imageSource = showFrames
? { uri: frames[index], headers }
: thumbnail && !thumbnailFailed
? { uri: `${URL}${encodeURI(thumbnail)}`, headers }
: errorImageSource

const isBuffering =
status === 'playing' && loaded < frames.length && index + 1 >= loaded

const label =
status === 'loading'
? `Loading ${loaded} / ${frames.length || '…'}`
: status === 'error'
? 'Timelapse unavailable, tap to retry'
: status === 'idle'
? 'Timelapse'
: frameTime(frames[index])

const accessibilityLabel =
status === 'playing'
? 'Pause Timelapse'
: status === 'paused'
? `Resume Timelapse, paused at ${frameTime(frames[index])}`
: status === 'loading'
? 'Loading Timelapse'
: `Play Timelapse of ${formatDay(day)}`

const styles = StyleSheet.create({
bar: {
alignItems: 'center',
backgroundColor: barColor,
bottom: 0,
flexDirection: 'row',
gap: 8,
left: 0,
paddingHorizontal: 12,
paddingVertical: 6,
position: 'absolute',
right: 0,
},
image: {
aspectRatio: 16 / 9,
backgroundColor: highlightColor,
transform: [{ rotate: `${imageRotation}deg` }],
width: '100%',
},
label: {
color: barTextColor,
flex: 1,
},
wrapper: {
borderColor,
borderRadius: 12,
borderWidth: 2,
overflow: 'hidden',
},
})

return (
<Pressable
accessible={true}
accessibilityRole="button"
accessibilityLabel={accessibilityLabel}
accessibilityState={{ busy: status === 'loading' }}
onPress={handlePress}
style={({ pressed }) => [
styles.wrapper,
{ opacity: pressed ? 0.75 : 1 },
]}
>
<Image
alt={`Timelapse of ${formatDay(day)}`}
cachePolicy="memory-disk"
contentFit="cover"
onError={() => {
if (!showFrames) {
setThumbnailFailed(true)
}
}}
source={imageSource}
style={styles.image}
/>
<View style={styles.bar}>
{status === 'loading' || isBuffering ? (
<ActivityIndicator color={barTextColor} />
) : (
<IconSymbol
color={barTextColor}
name={status === 'playing' ? 'pause' : 'play-arrow'}
size={28}
/>
)}
<ThemedText style={styles.label}>{label}</ThemedText>
</View>
</Pressable>
)
}

export default TimelapsePlayer
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "security-app",
"main": "expo-router/entry",
"version": "1.2.0",
"version": "1.2.1",
"scripts": {
"start": "expo start --go",
"android": "expo start --android",
Expand Down