feat:增加刷新日程提醒功能

This commit is contained in:
2025-11-20 14:01:43 +08:00
parent b2260d139b
commit 6ed9efe06d
5 changed files with 65 additions and 4 deletions

View File

@@ -2,7 +2,13 @@ import 'package:flisp_app/layout/app_body.dart';
import 'package:flisp_app/layout/app_drawer.dart';
import 'package:flisp_app/layout/app_floating_button.dart';
import 'package:flisp_app/layout/app_navbar.dart';
import 'package:flisp_app/models/calendar.dart';
import 'package:flisp_app/models/todo.dart';
import 'package:flisp_app/provider/app_provider.dart';
import 'package:flisp_app/service/calendar_service.dart';
import 'package:flisp_app/service/todo_service.dart';
import 'package:flisp_app/utils/date_utils.dart';
import 'package:flisp_app/utils/notify_utils.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
@@ -15,11 +21,55 @@ class MainScreen extends StatefulWidget {
class _MainScreenState extends State<MainScreen> {
final GlobalKey<AppBodyState> _appBodyKey = GlobalKey();
final NotifyService notifyService = NotifyService();
void _onPressedFloatingButton(int index) {
_appBodyKey.currentState?.showPageAddDialog(index);
}
@override
void initState() {
super.initState();
refreshScheduled();
}
/// 重新刷新提醒 防止每次重启设备后提醒丢失的问题
void refreshScheduled() async {
await notifyService.cancelAllNotifications();
TodoService todoService = TodoService();
final List<Todo> todoResult = todoService.getAllTodos(
TodoSortMode.priority,
);
final List<Todo> todayTodos =
todoResult.where((todo) => isToday(todo.scheduledTime)).toList();
for (Todo todo in todayTodos) {
await notifyService.scheduleNotification(
id: todo.id,
title: '待办提醒',
body: todo.title,
scheduledTime: todo.scheduledTime!,
);
}
CalendarService calendarService = CalendarService();
final List<Calendar> calendarResult = calendarService.getAllCalendars();
final List<Calendar> todayCalendars =
calendarResult
.where((calendar) => isToday(calendar.scheduledTime))
.toList();
for (Calendar calendar in todayCalendars) {
await notifyService.scheduleNotification(
id: calendar.id,
title: '日程提醒',
body: calendar.title,
scheduledTime: calendar.scheduledTime!,
);
}
}
@override
Widget build(BuildContext context) {
final provider = context.watch<AppProvider>();