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
9 changes: 8 additions & 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.3.3",
"version": "1.3.4",
"orientation": "portrait",
"icon": "./assets/images/icon.png",
"scheme": "securityapp",
Expand Down Expand Up @@ -38,6 +38,13 @@
"usesCleartextTraffic": true
}
}
],
[
"expo-media-library",
{
"granularPermissions": ["photo"],
"isAccessMediaLocationEnabled": false
}
]
],
"experiments": {
Expand Down
28 changes: 21 additions & 7 deletions app/(tabs)/live.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { ContextError } from '@/context/ContextError'
import { ContextImageRotation } from '@/context/ContextImageRotation'
import { ContextUser } from '@/context/ContextUser'
import { useThemeColor } from '@/hooks/use-theme-color'
import { downloadImage } from '@/utils/downloadImage'
import { saveImage, shareImage } from '@/utils/downloadImage'

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

Expand Down Expand Up @@ -91,9 +91,12 @@ const LiveScreen = () => {
}

const handlePress = () => {
downloadImage({ imageName, imageSource, imageRotation })
shareImage({ imageName, imageSource, imageRotation })
}

const handleSave = () =>
saveImage({ imageName, imageSource, imageRotation })

const handleFetchLive = useCallback(
(setIsLoading: React.Dispatch<React.SetStateAction<boolean>>) => {
handleApiFetchLive({
Expand Down Expand Up @@ -152,6 +155,10 @@ const LiveScreen = () => {
)

const styles = StyleSheet.create({
actions: {
flexDirection: 'row',
justifyContent: 'space-between',
},
activityLoader: {
alignItems: 'center',
aspectRatio: 16 / 9,
Expand Down Expand Up @@ -269,11 +276,18 @@ const LiveScreen = () => {
</View>
)}
</Pressable>
<Button
accessibilityLabel="Rotate Live Image"
handlePress={handleRotateImage}
label="Rotate"
/>
<View style={styles.actions}>
<Button
accessibilityLabel="Rotate Live Image"
handlePress={handleRotateImage}
label="Rotate"
/>
<Button
accessibilityLabel="Save Live Image to Gallery"
handlePress={handleSave}
label="Save"
/>
</View>
</>
) : (
<View style={styles.noImagesContainer}>
Expand Down
40 changes: 31 additions & 9 deletions components/highlight-image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import {
View,
} from 'react-native'

import Button from '@/components/button'
import ThemedText from '@/components/themed-text'
import { ContextImageRotation } from '@/context/ContextImageRotation'
import { ContextUser } from '@/context/ContextUser'
import { useHighlightImageSize } from '@/hooks/use-highlight-image-size'
import { useThemeColor } from '@/hooks/use-theme-color'
import { downloadImage } from '@/utils/downloadImage'
import { saveImage, shareImage } from '@/utils/downloadImage'

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

Expand Down Expand Up @@ -90,12 +91,17 @@ const HighlightImage = ({
setImageIsLoadedSuccess(false)
}

const handlePress = () => {
const imageName = decodeURI(imageSource.uri)
const imageProps = () => ({
imageName: decodeURI(imageSource.uri)
.replace(/.*(?=security_image_)/, '')
.replace(/\.jpg.*$/, '.jpg')
downloadImage({ imageName, imageSource, imageRotation })
}
.replace(/\.jpg.*$/, '.jpg'),
imageRotation,
imageSource,
})

const handlePress = () => shareImage(imageProps())

const handleSave = () => saveImage(imageProps())

useHighlightImageSize({
height,
Expand All @@ -109,6 +115,10 @@ const HighlightImage = ({

const styles = StyleSheet.create({
activity: { backgroundColor, height: imageHeight },
buttons: {
flexDirection: 'row',
justifyContent: 'space-between',
},
footer: {
backgroundColor,
paddingHorizontal: 16,
Expand Down Expand Up @@ -153,13 +163,25 @@ const HighlightImage = ({
contentFit="contain"
onError={handleErrorImage}
/>
{formattedDate().length ? (
<View style={styles.footer}>
<View style={styles.footer}>
{formattedDate().length ? (
<ThemedText center>
{formattedDate()}
</ThemedText>
) : undefined}
<View style={styles.buttons}>
<Button
accessibilityLabel="Save Image to Gallery"
handlePress={handleSave}
label="Save"
/>
<Button
accessibilityLabel="Share Image"
handlePress={handlePress}
label="Share"
/>
</View>
) : undefined}
</View>
</>
)}
{imageIsLoaded && !imageIsLoadedSuccess && (
Expand Down
15 changes: 13 additions & 2 deletions package-lock.json

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

3 changes: 2 additions & 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.3.3",
"version": "1.3.4",
"scripts": {
"start": "expo start --go",
"android": "expo start --android",
Expand All @@ -21,6 +21,7 @@
"expo-image": "~58.0.18",
"expo-image-manipulator": "~58.0.16",
"expo-linking": "~58.0.17",
"expo-media-library": "~58.0.14",
"expo-router": "~58.0.32",
"expo-sharing": "~58.0.23",
"expo-splash-screen": "~58.0.14",
Expand Down
75 changes: 67 additions & 8 deletions utils/downloadImage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as FileSystem from 'expo-file-system/legacy'
import { ImageManipulator, SaveFormat } from 'expo-image-manipulator'
import { Asset, requestPermissionsAsync } from 'expo-media-library'
import { shareAsync } from 'expo-sharing'
import { Alert } from 'react-native'

Expand All @@ -9,6 +10,11 @@ type downloadImageProps = {
imageRotation: number
}

type PreparedImage = {
name: string
uri: string
}

const IMAGE_NAME_PATTERN =
/^security_image_\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}\.jpg$/

Expand All @@ -20,11 +26,13 @@ const getServedImageName = (headers: Record<string, string>) => {
return name && IMAGE_NAME_PATTERN.test(name) ? name : undefined
}

export const downloadImage = async ({
// downloads the image into the cache and applies the rotation,
// returns null after showing an alert if that fails
const prepareImage = async ({
imageName,
imageSource,
imageRotation,
}: downloadImageProps) => {
}: downloadImageProps): Promise<PreparedImage | null> => {
const tempDownloadUri =
FileSystem.cacheDirectory + 'raw_download_' + imageName

Expand All @@ -43,7 +51,7 @@ export const downloadImage = async ({
? 'This image no longer exists. It has probably been replaced by a newer one.'
: `The image could not be downloaded (HTTP ${download.status}).`
)
return
return null
}

const finalImageName = getServedImageName(download.headers) ?? imageName
Expand Down Expand Up @@ -80,13 +88,64 @@ export const downloadImage = async ({

await FileSystem.deleteAsync(tempDownloadUri, { idempotent: true })

await shareAsync(finalTargetUri, {
mimeType: 'image/jpeg',
dialogTitle: finalImageName,
})
return { name: finalImageName, uri: finalTargetUri }
} catch (error: any) {
console.error('Download/Processing failed:', error)
await FileSystem.deleteAsync(tempDownloadUri, { idempotent: true })
Alert.alert('Download failed', 'The image could not be shared.')
Alert.alert('Download failed', 'The image could not be downloaded.')
return null
}
}

export const shareImage = async (props: downloadImageProps) => {
const image = await prepareImage(props)
if (!image) {
return
}

try {
await shareAsync(image.uri, {
mimeType: 'image/jpeg',
dialogTitle: image.name,
})
} catch (error: any) {
console.error('Sharing failed:', error)
Alert.alert('Sharing failed', 'The image could not be shared.')
}
}

export const saveImage = async (props: downloadImageProps) => {
let permission
try {
permission = await requestPermissionsAsync(true, ['photo'])
} catch (error: any) {
console.error('Requesting media library permission failed:', error)
Alert.alert(
'Saving not available',
'This app version cannot access the gallery. Use a development or release build.'
)
return
}
if (!permission.granted) {
Alert.alert(
'Permission required',
'Allow access to your photos in the system settings to save images.'
)
return
}

const image = await prepareImage(props)
if (!image) {
return
}

try {
await Asset.create(image.uri)
Alert.alert('Image saved', `${image.name} is now in your gallery.`)
} catch (error: any) {
console.error('Saving failed:', error)
Alert.alert('Saving failed', 'The image could not be saved.')
} finally {
await FileSystem.deleteAsync(image.uri, { idempotent: true })
}
}