Files
digital-pilates/utils/userPreferences.ts

104 lines
3.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import AsyncStorage from '@react-native-async-storage/async-storage';
// 用户偏好设置的存储键
const PREFERENCES_KEYS = {
QUICK_WATER_AMOUNT: 'user_preference_quick_water_amount',
NOTIFICATION_ENABLED: 'user_preference_notification_enabled',
} as const;
// 用户偏好设置接口
export interface UserPreferences {
quickWaterAmount: number;
notificationEnabled: boolean;
}
// 默认的用户偏好设置
const DEFAULT_PREFERENCES: UserPreferences = {
quickWaterAmount: 150, // 默认快速添加饮水量为 150ml
notificationEnabled: true, // 默认开启消息推送
};
/**
* 获取用户偏好设置
*/
export const getUserPreferences = async (): Promise<UserPreferences> => {
try {
const quickWaterAmount = await AsyncStorage.getItem(PREFERENCES_KEYS.QUICK_WATER_AMOUNT);
const notificationEnabled = await AsyncStorage.getItem(PREFERENCES_KEYS.NOTIFICATION_ENABLED);
return {
quickWaterAmount: quickWaterAmount ? parseInt(quickWaterAmount, 10) : DEFAULT_PREFERENCES.quickWaterAmount,
notificationEnabled: notificationEnabled !== null ? notificationEnabled === 'true' : DEFAULT_PREFERENCES.notificationEnabled,
};
} catch (error) {
console.error('获取用户偏好设置失败:', error);
return DEFAULT_PREFERENCES;
}
};
/**
* 设置快速添加饮水的默认值
* @param amount 饮水量(毫升)
*/
export const setQuickWaterAmount = async (amount: number): Promise<void> => {
try {
// 确保值在合理范围内50ml - 1000ml
const validAmount = Math.max(50, Math.min(1000, amount));
await AsyncStorage.setItem(PREFERENCES_KEYS.QUICK_WATER_AMOUNT, validAmount.toString());
} catch (error) {
console.error('设置快速添加饮水默认值失败:', error);
throw error;
}
};
/**
* 获取快速添加饮水的默认值
*/
export const getQuickWaterAmount = async (): Promise<number> => {
try {
const amount = await AsyncStorage.getItem(PREFERENCES_KEYS.QUICK_WATER_AMOUNT);
return amount ? parseInt(amount, 10) : DEFAULT_PREFERENCES.quickWaterAmount;
} catch (error) {
console.error('获取快速添加饮水默认值失败:', error);
return DEFAULT_PREFERENCES.quickWaterAmount;
}
};
/**
* 设置消息推送开关
* @param enabled 是否开启消息推送
*/
export const setNotificationEnabled = async (enabled: boolean): Promise<void> => {
try {
await AsyncStorage.setItem(PREFERENCES_KEYS.NOTIFICATION_ENABLED, enabled.toString());
} catch (error) {
console.error('设置消息推送开关失败:', error);
throw error;
}
};
/**
* 获取消息推送开关状态
*/
export const getNotificationEnabled = async (): Promise<boolean> => {
try {
const enabled = await AsyncStorage.getItem(PREFERENCES_KEYS.NOTIFICATION_ENABLED);
return enabled !== null ? enabled === 'true' : DEFAULT_PREFERENCES.notificationEnabled;
} catch (error) {
console.error('获取消息推送开关状态失败:', error);
return DEFAULT_PREFERENCES.notificationEnabled;
}
};
/**
* 重置所有用户偏好设置为默认值
*/
export const resetUserPreferences = async (): Promise<void> => {
try {
await AsyncStorage.removeItem(PREFERENCES_KEYS.QUICK_WATER_AMOUNT);
await AsyncStorage.removeItem(PREFERENCES_KEYS.NOTIFICATION_ENABLED);
} catch (error) {
console.error('重置用户偏好设置失败:', error);
throw error;
}
};