Spaces:
Sleeping
Sleeping
File size: 5,107 Bytes
3c56493 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | import { browser } from '$app/environment';
import { writable } from 'svelte/store';
import type { AudioQuality } from '$lib/types';
import { type PerformanceLevel } from '$lib/utils/performance';
export type PerformanceMode = 'medium' | 'low';
export interface UserPreferencesState {
playbackQuality: AudioQuality;
convertAacToMp3: boolean;
downloadCoversSeperately: boolean;
performanceMode: PerformanceMode;
}
const STORAGE_KEY = 'tidal-ui.userPreferences';
const DEFAULT_STATE: UserPreferencesState = {
playbackQuality: 'HI_RES_LOSSLESS',
convertAacToMp3: false,
downloadCoversSeperately: false,
performanceMode: 'medium'
};
/**
* Detect if the user is on a mobile/touch device for initial performance setting
*/
function detectIsMobileDevice(): boolean {
if (!browser) return false;
// Check for touch capability
const hasTouch = 'ontouchstart' in window || navigator.maxTouchPoints > 0;
// Check screen size (typical mobile breakpoint)
const isSmallScreen = window.innerWidth <= 768;
// Check for mobile user agent patterns
const mobileUA = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
navigator.userAgent
);
return hasTouch && (isSmallScreen || mobileUA);
}
function parseStoredPreferences(raw: string | null): UserPreferencesState {
if (!raw) {
return DEFAULT_STATE;
}
try {
const parsed = JSON.parse(raw) as Partial<UserPreferencesState>;
const quality = parsed?.playbackQuality;
const convertFlag = parsed?.convertAacToMp3;
const downloadCoversFlag = parsed?.downloadCoversSeperately;
const perfMode = parsed?.performanceMode;
return {
playbackQuality:
quality === 'HI_RES_LOSSLESS' ||
quality === 'LOSSLESS' ||
quality === 'HIGH' ||
quality === 'LOW'
? quality
: DEFAULT_STATE.playbackQuality,
convertAacToMp3:
typeof convertFlag === 'boolean' ? convertFlag : DEFAULT_STATE.convertAacToMp3,
downloadCoversSeperately:
typeof downloadCoversFlag === 'boolean'
? downloadCoversFlag
: DEFAULT_STATE.downloadCoversSeperately,
performanceMode:
perfMode === 'medium' || perfMode === 'low' ? perfMode : DEFAULT_STATE.performanceMode
};
} catch (error) {
console.warn('Failed to parse stored user preferences', error);
return DEFAULT_STATE;
}
}
const readInitialPreferences = (): UserPreferencesState => {
if (!browser) {
return DEFAULT_STATE;
}
try {
const storedRaw = localStorage.getItem(STORAGE_KEY);
// If no stored preferences, detect mobile and set appropriate defaults
if (!storedRaw) {
const isMobile = detectIsMobileDevice();
const initialState: UserPreferencesState = {
...DEFAULT_STATE,
// Mobile devices default to 'medium' performance (already default)
// Could set to 'low' for very resource-constrained devices if needed
performanceMode: isMobile ? 'medium' : 'medium'
};
// Persist the initial preferences so this detection only runs once
try {
localStorage.setItem(STORAGE_KEY, JSON.stringify(initialState));
} catch {
// Ignore storage errors
}
return initialState;
}
return parseStoredPreferences(storedRaw);
} catch (error) {
console.warn('Unable to read user preferences from storage', error);
return DEFAULT_STATE;
}
};
const createUserPreferencesStore = () => {
const { subscribe, set, update } = writable<UserPreferencesState>(readInitialPreferences());
if (browser) {
subscribe((state) => {
try {
localStorage.setItem(STORAGE_KEY, JSON.stringify(state));
} catch (error) {
console.warn('Failed to persist user preferences', error);
}
});
window.addEventListener('storage', (event) => {
if (event.key !== STORAGE_KEY) return;
set(parseStoredPreferences(event.newValue));
});
}
return {
subscribe,
setPlaybackQuality(quality: AudioQuality) {
update((state) => {
if (state.playbackQuality === quality) {
return state;
}
return { ...state, playbackQuality: quality };
});
},
setConvertAacToMp3(value: boolean) {
update((state) => {
if (state.convertAacToMp3 === value) {
return state;
}
return { ...state, convertAacToMp3: value };
});
},
toggleConvertAacToMp3() {
update((state) => ({ ...state, convertAacToMp3: !state.convertAacToMp3 }));
},
setDownloadCoversSeperately(value: boolean) {
update((state) => {
if (state.downloadCoversSeperately === value) {
return state;
}
return { ...state, downloadCoversSeperately: value };
});
},
toggleDownloadCoversSeperately() {
update((state) => ({ ...state, downloadCoversSeperately: !state.downloadCoversSeperately }));
},
setPerformanceMode(mode: PerformanceMode) {
update((state) => {
if (state.performanceMode === mode) {
return state;
}
return { ...state, performanceMode: mode };
});
},
getEffectivePerformanceLevel(): PerformanceLevel {
const state = readInitialPreferences();
return state.performanceMode as PerformanceLevel;
},
reset() {
set(DEFAULT_STATE);
}
};
};
export const userPreferencesStore = createUserPreferencesStore();
|