feat: 添加用户推送通知偏好设置功能,支持开启/关闭推送通知
This commit is contained in:
@@ -3,16 +3,19 @@ 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, // 默认快速添加饮水量为 250ml
|
||||
quickWaterAmount: 150, // 默认快速添加饮水量为 150ml
|
||||
notificationEnabled: true, // 默认开启消息推送
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -21,9 +24,11 @@ const DEFAULT_PREFERENCES: UserPreferences = {
|
||||
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);
|
||||
@@ -59,12 +64,39 @@ export const getQuickWaterAmount = async (): Promise<number> => {
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 设置消息推送开关
|
||||
* @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;
|
||||
|
||||
Reference in New Issue
Block a user