refactor: 重构提醒通知系统

This commit is contained in:
zl-q
2026-04-01 00:42:34 +08:00
parent 9a231dae9e
commit 6722f3d74b
21 changed files with 375 additions and 171 deletions
@@ -50,14 +50,21 @@ class CalendarService {
return events;
}
Future<ScheduleItemModel> getEventById(String id) async {
Future<ScheduleItemModel> getEventById(
String id, {
bool reconcileReminder = true,
}) async {
final response = await _apiClient.get<Map<String, dynamic>>('$_prefix/$id');
final data = response.data;
if (data == null) {
throw StateError('Invalid getEventById response: empty payload');
}
final event = ScheduleItemModel.fromJson(data);
await _reminderReconcileService?.reconcileEvent(_toReminderSnapshot(event));
if (reconcileReminder) {
await _reminderReconcileService?.reconcileEvent(
_toReminderSnapshot(event),
);
}
return event;
}
@@ -106,12 +106,20 @@ class DayEventLayoutEngine {
final clusterColumnCount =
cluster.map((item) => item.column).reduce((a, b) => a > b ? a : b) +
1;
final totalGap = (clusterColumnCount - 1) * columnGap;
final columnWidth = clusterColumnCount > 0
? ((eventAreaWidth - totalGap) / clusterColumnCount).toDouble()
final maxVisibleColumns =
((eventAreaWidth + columnGap) /
(DayTimelineMetrics.minEventCardWidth + columnGap))
.floor()
.clamp(1, clusterColumnCount);
final totalGap = (maxVisibleColumns - 1) * columnGap;
final columnWidth = maxVisibleColumns > 0
? (eventAreaWidth - totalGap) / maxVisibleColumns
: eventAreaWidth;
for (final item in cluster) {
if (item.column >= maxVisibleColumns) {
continue;
}
final top = scale.pixelsForMinutes(item.startMinutes);
final geometryHeight = scale.pixelsForMinutes(
item.endMinutes - item.startMinutes,
@@ -125,7 +133,7 @@ class DayEventLayoutEngine {
startMinutes: item.startMinutes,
endMinutes: item.endMinutes,
column: item.column,
columnCount: clusterColumnCount,
columnCount: maxVisibleColumns,
top: top,
geometryHeight: geometryHeight,
visualHeight: visualHeight,
@@ -9,6 +9,7 @@ class DayTimelineMetrics {
static const double timeLabelGap = 8;
static const double eventRightInset = 4;
static const double eventColumnGap = 4;
static const double minEventCardWidth = 30;
static double timelineHeight(DayViewScale scale) {
return scale.pixelsForMinutes(minutesInDay);
@@ -20,10 +21,10 @@ class DayTimelineMetrics {
static double eventAreaWidth(double boardWidth) {
final width = boardWidth - eventAreaLeft() - eventRightInset;
return width > 0 ? width : 0;
return width < 0 ? 0 : width;
}
static int clampMinuteOfDay(int minute) {
return minute.clamp(0, minutesInDay).toInt();
return minute.clamp(0, minutesInDay);
}
}
@@ -718,32 +718,57 @@ class _CalendarDayWeekScreenState extends State<CalendarDayWeekScreen>
borderRadius: BorderRadius.circular(4),
border: Border.all(color: eventColor, width: 1),
),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
width: 6,
height: 6,
decoration: BoxDecoration(
color: eventColor,
shape: BoxShape.circle,
),
),
if (!isCompact) const SizedBox(width: 4),
if (!isCompact)
Expanded(
child: Text(
layout.event.title,
style: TextStyle(
fontSize: 11,
fontWeight: FontWeight.w500,
color: eventColor,
child: LayoutBuilder(
builder: (context, constraints) {
const markerSize = 6.0;
const markerTitleGap = 4.0;
final canShowMarker = constraints.maxWidth >= markerSize;
final canShowTitle =
!isCompact &&
constraints.maxWidth >= markerSize + markerTitleGap + 8;
if (!canShowMarker) {
return const SizedBox.shrink();
}
return Stack(
children: [
Align(
alignment: Alignment.centerLeft,
child: SizedBox(
width: markerSize,
height: markerSize,
child: DecoratedBox(
decoration: BoxDecoration(
color: eventColor,
shape: BoxShape.circle,
),
),
),
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
),
],
if (canShowTitle)
Positioned(
left: markerSize + markerTitleGap,
right: 0,
top: 0,
bottom: 0,
child: Align(
alignment: Alignment.centerLeft,
child: Text(
layout.event.title,
style: TextStyle(
fontSize: 11,
fontWeight: FontWeight.w500,
color: eventColor,
),
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
),
),
],
);
},
),
),
),
@@ -58,7 +58,9 @@ class _CalendarMonthScreenState extends State<CalendarMonthScreen>
return;
}
_eventsByDay.clear();
for (final event in events) {
for (final event in events.where(
(e) => e.status != ScheduleStatus.archived,
)) {
final key = formatYmd(event.startAt);
_eventsByDay[key] = [...(_eventsByDay[key] ?? const []), event];
}
@@ -28,12 +28,18 @@ class CalendarReminderAlarmScreen extends StatefulWidget {
class _CalendarReminderAlarmScreenState
extends State<CalendarReminderAlarmScreen> {
late final Future<ScheduleItemModel> _eventFuture;
bool _isSubmitting = false;
bool _isArchiving = false;
bool _isSnoozing = false;
bool get _isProcessing => _isArchiving || _isSnoozing;
@override
void initState() {
super.initState();
_eventFuture = sl<CalendarService>().getEventById(widget.eventId);
_eventFuture = sl<CalendarService>().getEventById(
widget.eventId,
reconcileReminder: false,
);
}
@override
@@ -78,8 +84,8 @@ class _CalendarReminderAlarmScreenState
child: AppButton(
text: context.l10n.notificationSnoozeLater,
isOutlined: true,
isLoading: _isSubmitting,
onPressed: _isSubmitting
isLoading: _isSnoozing,
onPressed: _isProcessing
? null
: () => _snoozeEvent(event),
),
@@ -88,8 +94,8 @@ class _CalendarReminderAlarmScreenState
Expanded(
child: AppButton(
text: context.l10n.calendarDetailArchiveConfirm,
isLoading: _isSubmitting,
onPressed: _isSubmitting
isLoading: _isArchiving,
onPressed: _isProcessing
? null
: () => _archiveEvent(event),
),
@@ -107,15 +113,16 @@ class _CalendarReminderAlarmScreenState
Future<void> _archiveEvent(ScheduleItemModel event) async {
setState(() {
_isSubmitting = true;
_isArchiving = true;
});
try {
await sl<CalendarService>().archiveEvent(event.id);
if (!mounted) {
return;
}
context.go(AppRoutes.calendarEventDetail(event.id));
} catch (_) {
context.go(AppRoutes.homeMain);
} catch (e, st) {
debugPrint('[_archiveEvent] error: $e\n$st');
if (mounted) {
Toast.show(
context,
@@ -126,7 +133,7 @@ class _CalendarReminderAlarmScreenState
} finally {
if (mounted) {
setState(() {
_isSubmitting = false;
_isArchiving = false;
});
}
}
@@ -134,7 +141,7 @@ class _CalendarReminderAlarmScreenState
Future<void> _snoozeEvent(ScheduleItemModel event) async {
setState(() {
_isSubmitting = true;
_isSnoozing = true;
});
try {
await sl<ReminderReconcileService>().snooze10m(_snapshotFromEvent(event));
@@ -142,19 +149,20 @@ class _CalendarReminderAlarmScreenState
return;
}
Toast.show(context, context.l10n.notificationSnoozeMinutes(10));
context.go(AppRoutes.calendarEventDetail(event.id));
} catch (_) {
context.go(AppRoutes.homeMain);
} catch (e, st) {
debugPrint('[_snoozeEvent] error: $e\n$st');
if (mounted) {
Toast.show(
context,
context.l10n.todoSaveFailed('snooze failed'),
context.l10n.todoSaveFailed(e.toString()),
type: ToastType.error,
);
}
} finally {
if (mounted) {
setState(() {
_isSubmitting = false;
_isSnoozing = false;
});
}
}
@@ -378,24 +378,11 @@ class _CreateEventSheetState extends State<CreateEventSheet>
_startDate = date;
_startTime = time;
if (_endDate != null && _endTime != null) {
final endDateTime = DateTime(
_endDate!.year,
_endDate!.month,
_endDate!.day,
_endTime!.hour,
_endTime!.minute,
);
final startDateTime = DateTime(
date.year,
date.month,
date.day,
time.hour,
time.minute,
);
final endDateTime = _composeDateTime(_endDate!, _endTime!);
final startDateTime = _composeDateTime(date, time);
if (endDateTime.isBefore(startDateTime) ||
endDateTime.isAtSameMomentAs(startDateTime)) {
_endDate = date;
_endTime = time.add(const Duration(hours: 1));
_setEndDateTime(_defaultEndDateTime(startDateTime));
}
}
});
@@ -408,44 +395,47 @@ class _CreateEventSheetState extends State<CreateEventSheet>
_endTime ?? _startTime,
(date, time) {
setState(() {
final startDateTime = DateTime(
_startDate.year,
_startDate.month,
_startDate.day,
_startTime.hour,
_startTime.minute,
);
final endDateTime = DateTime(
date.year,
date.month,
date.day,
time.hour,
time.minute,
);
final startDateTime = _composeDateTime(_startDate, _startTime);
final endDateTime = _composeDateTime(date, time);
if (endDateTime.isBefore(startDateTime) ||
endDateTime.isAtSameMomentAs(startDateTime)) {
_endDate = _startDate;
_endTime = _startTime.add(const Duration(hours: 1));
Toast.show(
context,
context.l10n.calendarCreateInvalidTimeRange,
type: ToastType.error,
);
_setEndDateTime(_defaultEndDateTime(startDateTime));
} else {
_endDate = date;
_endTime = time;
_setEndDateTime(endDateTime);
}
});
},
isOptional: true,
minTime: DateTime(
_startDate.year,
_startDate.month,
_startDate.day,
_startTime.hour,
_startTime.minute,
),
),
],
),
);
}
DateTime _composeDateTime(DateTime date, DateTime time) {
return DateTime(date.year, date.month, date.day, time.hour, time.minute);
}
DateTime _defaultEndDateTime(DateTime startDateTime) {
return startDateTime.add(const Duration(hours: 1));
}
void _setEndDateTime(DateTime value) {
_endDate = DateTime(value.year, value.month, value.day);
_endTime = DateTime(
value.year,
value.month,
value.day,
value.hour,
value.minute,
);
}
Widget _buildAdvancedTab() {
return SingleChildScrollView(
keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,