删除了完整的目标管理功能,包括目标创建、编辑、任务管理等相关页面和组件。同时移除了相关的API服务、Redux状态管理、类型定义和通知功能。应用版本从1.0.20升级到1.0.21。
146 lines
4.6 KiB
TypeScript
146 lines
4.6 KiB
TypeScript
import * as Notifications from 'expo-notifications';
|
|
import { useCallback, useEffect, useState } from 'react';
|
|
import { NotificationData, notificationService, NotificationTypes } from '../services/notifications';
|
|
|
|
export interface UseNotificationsReturn {
|
|
isInitialized: boolean;
|
|
permissionStatus: Notifications.PermissionStatus | null;
|
|
initialize: () => Promise<void>;
|
|
requestPermission: () => Promise<Notifications.PermissionStatus>;
|
|
sendNotification: (notification: NotificationData) => Promise<string>;
|
|
scheduleNotification: (notification: NotificationData, date: Date) => Promise<string>;
|
|
scheduleRepeatingNotification: (
|
|
notification: NotificationData,
|
|
interval: {
|
|
seconds?: number;
|
|
minutes?: number;
|
|
hours?: number;
|
|
days?: number;
|
|
weeks?: number;
|
|
months?: number;
|
|
years?: number;
|
|
}
|
|
) => Promise<string>;
|
|
cancelNotification: (notificationId: string) => Promise<void>;
|
|
cancelAllNotifications: () => Promise<void>;
|
|
getAllScheduledNotifications: () => Promise<Notifications.NotificationRequest[]>;
|
|
sendWorkoutReminder: (title: string, body: string, date?: Date) => Promise<string>;
|
|
sendMoodCheckinReminder: (title: string, body: string, date?: Date) => Promise<string>;
|
|
}
|
|
|
|
export const useNotifications = (): UseNotificationsReturn => {
|
|
const [isInitialized, setIsInitialized] = useState(false);
|
|
const [permissionStatus, setPermissionStatus] = useState<Notifications.PermissionStatus | null>(null);
|
|
|
|
const initialize = useCallback(async () => {
|
|
try {
|
|
await notificationService.initialize();
|
|
const status = await notificationService.getPermissionStatus();
|
|
setPermissionStatus(status);
|
|
setIsInitialized(true);
|
|
} catch (error) {
|
|
console.error('初始化通知服务失败:', error);
|
|
}
|
|
}, []);
|
|
|
|
const requestPermission = useCallback(async () => {
|
|
try {
|
|
const status = await notificationService.requestPermission();
|
|
setPermissionStatus(status);
|
|
return status;
|
|
} catch (error) {
|
|
console.error('请求通知权限失败:', error);
|
|
throw error;
|
|
}
|
|
}, []);
|
|
|
|
const sendNotification = useCallback(async (notification: NotificationData) => {
|
|
return notificationService.sendImmediateNotification(notification);
|
|
}, []);
|
|
|
|
const scheduleNotification = useCallback(async (notification: NotificationData, date: Date) => {
|
|
return notificationService.scheduleNotificationAtDate(notification, date);
|
|
}, []);
|
|
|
|
const scheduleRepeatingNotification = useCallback(async (
|
|
notification: NotificationData,
|
|
interval: {
|
|
seconds?: number;
|
|
minutes?: number;
|
|
hours?: number;
|
|
days?: number;
|
|
weeks?: number;
|
|
months?: number;
|
|
years?: number;
|
|
}
|
|
) => {
|
|
return notificationService.scheduleRepeatingNotification(notification, interval);
|
|
}, []);
|
|
|
|
const cancelNotification = useCallback(async (notificationId: string) => {
|
|
return notificationService.cancelNotification(notificationId);
|
|
}, []);
|
|
|
|
const cancelAllNotifications = useCallback(async () => {
|
|
return notificationService.cancelAllNotifications();
|
|
}, []);
|
|
|
|
const getAllScheduledNotifications = useCallback(async () => {
|
|
return notificationService.getAllScheduledNotifications();
|
|
}, []);
|
|
|
|
const sendWorkoutReminder = useCallback(async (title: string, body: string, date?: Date) => {
|
|
const notification: NotificationData = {
|
|
title,
|
|
body,
|
|
data: { type: NotificationTypes.WORKOUT_REMINDER },
|
|
sound: true,
|
|
priority: 'high',
|
|
};
|
|
|
|
if (date) {
|
|
return notificationService.scheduleNotificationAtDate(notification, date);
|
|
} else {
|
|
return notificationService.sendImmediateNotification(notification);
|
|
}
|
|
}, []);
|
|
|
|
// sendGoalAchievement 函数已删除,因为目标功能已移除
|
|
|
|
const sendMoodCheckinReminder = useCallback(async (title: string, body: string, date?: Date) => {
|
|
const notification: NotificationData = {
|
|
title,
|
|
body,
|
|
data: { type: NotificationTypes.MOOD_CHECKIN },
|
|
sound: true,
|
|
priority: 'normal',
|
|
};
|
|
|
|
if (date) {
|
|
return notificationService.scheduleNotificationAtDate(notification, date);
|
|
} else {
|
|
return notificationService.sendImmediateNotification(notification);
|
|
}
|
|
}, []);
|
|
|
|
// 组件挂载时自动初始化
|
|
useEffect(() => {
|
|
initialize();
|
|
}, [initialize]);
|
|
|
|
return {
|
|
isInitialized,
|
|
permissionStatus,
|
|
initialize,
|
|
requestPermission,
|
|
sendNotification,
|
|
scheduleNotification,
|
|
scheduleRepeatingNotification,
|
|
cancelNotification,
|
|
cancelAllNotifications,
|
|
getAllScheduledNotifications,
|
|
sendWorkoutReminder,
|
|
sendMoodCheckinReminder,
|
|
};
|
|
};
|