Repository navigation
[feature/alarm-audio] Implement custom alarm sound feature #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e88fbd1
feat: add custom alarm audio selection
HoweiAY 8d487b8
test: add unit tests for alarm audio selection
HoweiAY 1d4bfe3
perf: release uncommitted audio files for alarm sound
HoweiAY c670831
fix: preserve staged alarm sound until replacement/persistence succee…
HoweiAY 07201df
fix: guard Alert.alert calls with disposedRef to prevent stale alerts
HoweiAY 96dd3bd
fix: prevent failed save from overwriting a newer sound selection
HoweiAY 4ac6fe7
feat: block UI while alarm is being persisted
HoweiAY 5791c69
fix: prevent late audio picker results from replacing newer sound state
HoweiAY 4bdfaf0
fix: invalidate pending alarm sound picks before cancellation
HoweiAY File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| import { File } from "expo-file-system"; | ||
| import { useAlarmStore } from "@/store/alarmStore"; | ||
|
|
||
| export const DEFAULT_SOUND_LABEL = "Default"; | ||
|
|
||
| export interface SoundSelection { | ||
| alarmSoundSelected: string; | ||
| alarmSoundUri: string | null; | ||
| } | ||
|
|
||
| export function isDefaultSound(soundUri: string | null): boolean { | ||
| return soundUri == null || soundUri === ""; | ||
| } | ||
|
|
||
| export function defaultSoundSelection(): SoundSelection { | ||
| return { | ||
| alarmSoundSelected: DEFAULT_SOUND_LABEL, | ||
| alarmSoundUri: null, | ||
| }; | ||
| } | ||
|
|
||
| const UNIQUE_PREFIX_PATTERN = /^\d+_/; | ||
|
|
||
| function decodeFileName(basename: string): string { | ||
| try { | ||
| return decodeURIComponent(basename); | ||
| } catch { | ||
| return basename; | ||
| } | ||
| } | ||
|
|
||
| export function soundLabelFor(soundUri: string | null): string { | ||
| if (isDefaultSound(soundUri)) { | ||
| return DEFAULT_SOUND_LABEL; | ||
| } | ||
| const uri = soundUri as string; | ||
| const slashIndex = uri.lastIndexOf("/"); | ||
| const basename = slashIndex >= 0 ? uri.slice(slashIndex + 1) : uri; | ||
| if (basename.length === 0) { | ||
| return uri; | ||
| } | ||
| return decodeFileName(basename).replace(UNIQUE_PREFIX_PATTERN, ""); | ||
| } | ||
|
|
||
| let fileNameCounter = 0; | ||
|
|
||
| export function sanitizeAudioFileName(name: string | null | undefined): string { | ||
| const base = (name ?? "") | ||
| .replace(/[\\/]+/g, "_") | ||
| .replace(/\.\./g, "_") | ||
| .trim(); | ||
| const extMatch = base.match(/(\.[A-Za-z0-9]+)$/); | ||
| const stem = extMatch ? base.slice(0, -extMatch[1].length) : base; | ||
| const ext = extMatch ? extMatch[1] : ""; | ||
| const safeStem = stem.length > 0 ? stem : "alarm_sound"; | ||
| const unique = `${Date.now()}${fileNameCounter++}`; | ||
| return `${unique}_${safeStem}${ext}`; | ||
| } | ||
|
|
||
| export function buildSoundSelection( | ||
| name: string | undefined, | ||
| uri: string, | ||
| ): SoundSelection { | ||
| const trimmed = name?.trim(); | ||
| return { | ||
| alarmSoundSelected: | ||
| trimmed && trimmed.length > 0 ? trimmed : soundLabelFor(uri), | ||
| alarmSoundUri: uri, | ||
| }; | ||
| } | ||
|
|
||
| export function deleteCustomSoundFile(uri: string | null): void { | ||
| if (isDefaultSound(uri)) return; | ||
| try { | ||
| const file = new File(uri as string); | ||
| if (file.exists) { | ||
| file.delete(); | ||
| } | ||
| } catch (e) { | ||
| console.warn("Failed to delete custom sound file", uri, e); | ||
| } | ||
| } | ||
|
|
||
| export function isSoundFileReferenced(uri: string | null): boolean { | ||
| if (isDefaultSound(uri)) return false; | ||
| return useAlarmStore.getState().alarms.some((a) => a.sound === uri); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import * as DocumentPicker from "expo-document-picker"; | ||
| import { Directory, File, Paths } from "expo-file-system"; | ||
| import { | ||
| buildSoundSelection, | ||
| sanitizeAudioFileName, | ||
| type SoundSelection, | ||
| } from "./audioSelection"; | ||
|
|
||
| const ALARM_SOUNDS_DIR = "alarm_sounds"; | ||
|
|
||
| export async function pickAlarmSoundFromDevice(): Promise<SoundSelection | null> { | ||
| const result = await DocumentPicker.getDocumentAsync({ | ||
| type: "audio/*", | ||
| copyToCacheDirectory: true, | ||
| }); | ||
| if (result.canceled) { | ||
| return null; | ||
| } | ||
| const asset = result.assets[0]; | ||
| if (!asset) { | ||
| return null; | ||
| } | ||
| const source = new File(asset.uri); | ||
| const directory = new Directory(Paths.document, ALARM_SOUNDS_DIR); | ||
| directory.create({ idempotent: true, intermediates: true }); | ||
| const destination = new File(directory, sanitizeAudioFileName(asset.name)); | ||
| await source.copy(destination); | ||
| return buildSoundSelection(asset.name, destination.uri); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.