fix: resolve navigation-cache regressions and todo UX
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
import '../data/services/calendar_service.dart';
|
||||
import '../../../core/notifications/local_notification_service.dart';
|
||||
import 'models/reminder_action.dart';
|
||||
@@ -13,23 +11,16 @@ class ReminderActionExecutor {
|
||||
final LocalNotificationService _notificationService;
|
||||
final ReminderOutboxStore _outboxStore;
|
||||
final Random _random;
|
||||
final bool Function() _isAppActive;
|
||||
|
||||
ReminderActionExecutor({
|
||||
required CalendarService calendarService,
|
||||
required LocalNotificationService notificationService,
|
||||
required ReminderOutboxStore outboxStore,
|
||||
Random? random,
|
||||
bool Function()? isAppActive,
|
||||
}) : _calendarService = calendarService,
|
||||
_notificationService = notificationService,
|
||||
_outboxStore = outboxStore,
|
||||
_random = random ?? Random(),
|
||||
_isAppActive =
|
||||
isAppActive ??
|
||||
(() =>
|
||||
WidgetsBinding.instance.lifecycleState ==
|
||||
AppLifecycleState.resumed);
|
||||
_random = random ?? Random();
|
||||
|
||||
Future<void> handleAction({
|
||||
required ReminderAction action,
|
||||
@@ -95,9 +86,11 @@ class ReminderActionExecutor {
|
||||
}
|
||||
|
||||
Future<void> _archiveEvent(String eventId, ReminderAction action) async {
|
||||
if (_isAppActive()) {
|
||||
try {
|
||||
await _calendarService.archiveEvent(eventId);
|
||||
return;
|
||||
} catch (_) {
|
||||
// fall through to enqueue local outbox for retry
|
||||
}
|
||||
|
||||
final opId =
|
||||
|
||||
@@ -304,9 +304,14 @@ class _CalendarDayWeekScreenState extends State<CalendarDayWeekScreen>
|
||||
if (isNotToday) const SizedBox(width: 8),
|
||||
AppPressable(
|
||||
borderRadius: BorderRadius.circular(AppRadius.full),
|
||||
onTap: () => context.push(
|
||||
'${AppRoutes.calendarEventCreate}?date=${formatYmd(_selectedDate)}',
|
||||
),
|
||||
onTap: () async {
|
||||
final changed = await context.push<bool>(
|
||||
'${AppRoutes.calendarEventCreate}?date=${formatYmd(_selectedDate)}',
|
||||
);
|
||||
if (changed == true) {
|
||||
await _loadEvents(forceRefresh: true);
|
||||
}
|
||||
},
|
||||
child: Container(
|
||||
width: 36,
|
||||
height: 36,
|
||||
@@ -625,8 +630,14 @@ class _CalendarDayWeekScreenState extends State<CalendarDayWeekScreen>
|
||||
height: tapHeight,
|
||||
child: GestureDetector(
|
||||
behavior: HitTestBehavior.translucent,
|
||||
onTap: () =>
|
||||
context.push(AppRoutes.calendarEventDetail(layout.event.id)),
|
||||
onTap: () async {
|
||||
final changed = await context.push<bool>(
|
||||
AppRoutes.calendarEventDetail(layout.event.id),
|
||||
);
|
||||
if (changed == true) {
|
||||
await _loadEvents(forceRefresh: true);
|
||||
}
|
||||
},
|
||||
child: Stack(
|
||||
children: [
|
||||
Positioned(
|
||||
|
||||
@@ -481,7 +481,7 @@ class _CalendarEventDetailScreenState extends State<CalendarEventDetailScreen> {
|
||||
if (!mounted) {
|
||||
return;
|
||||
}
|
||||
context.pop();
|
||||
context.pop(true);
|
||||
}
|
||||
|
||||
Future<void> _archiveEvent() async {
|
||||
@@ -496,9 +496,8 @@ class _CalendarEventDetailScreenState extends State<CalendarEventDetailScreen> {
|
||||
}
|
||||
try {
|
||||
await sl<CalendarService>().archiveEvent(widget.eventId);
|
||||
await _loadEvent();
|
||||
if (mounted) {
|
||||
Toast.show(context, '已归档', type: ToastType.success);
|
||||
context.pop(true);
|
||||
}
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
|
||||
@@ -150,7 +150,14 @@ class _CalendarMonthScreenState extends State<CalendarMonthScreen>
|
||||
const Spacer(),
|
||||
AppPressable(
|
||||
borderRadius: BorderRadius.circular(AppRadius.full),
|
||||
onTap: () => context.push(AppRoutes.calendarEventCreate),
|
||||
onTap: () async {
|
||||
final changed = await context.push<bool>(
|
||||
AppRoutes.calendarEventCreate,
|
||||
);
|
||||
if (changed == true) {
|
||||
await _loadMonthEvents(forceRefresh: true);
|
||||
}
|
||||
},
|
||||
child: Container(
|
||||
width: 36,
|
||||
height: 36,
|
||||
@@ -345,9 +352,14 @@ class _CalendarMonthScreenState extends State<CalendarMonthScreen>
|
||||
);
|
||||
return AppPressable(
|
||||
borderRadius: BorderRadius.circular(AppRadius.sm),
|
||||
onTap: () {
|
||||
onTap: () async {
|
||||
_calendarManager.setSelectedDate(date);
|
||||
context.push('/calendar/events/${event.id}');
|
||||
final changed = await context.push<bool>(
|
||||
'/calendar/events/${event.id}',
|
||||
);
|
||||
if (changed == true) {
|
||||
await _loadMonthEvents(forceRefresh: true);
|
||||
}
|
||||
},
|
||||
child: Container(
|
||||
margin: const EdgeInsets.only(bottom: 2),
|
||||
|
||||
@@ -751,7 +751,7 @@ class _CreateEventSheetState extends State<CreateEventSheet>
|
||||
|
||||
widget.onSaved?.call();
|
||||
if (mounted) {
|
||||
Navigator.pop(context);
|
||||
Navigator.pop(context, true);
|
||||
}
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
|
||||
@@ -37,7 +37,13 @@ void returnToHomePreserveState(
|
||||
context.pop();
|
||||
return;
|
||||
case HomeReturnAction.goHome:
|
||||
context.go(AppRoutes.homeMain);
|
||||
return;
|
||||
case HomeReturnAction.goHomeForDock:
|
||||
if (context.canPop()) {
|
||||
context.pop();
|
||||
return;
|
||||
}
|
||||
context.go(AppRoutes.homeMain);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -46,11 +46,7 @@ class TodoRepository {
|
||||
);
|
||||
if (cached != null) {
|
||||
final next = cached.value
|
||||
.map(
|
||||
(todo) => todo.id == id
|
||||
? todo.copyWith(status: 'completed', completedAt: now())
|
||||
: todo,
|
||||
)
|
||||
.where((todo) => todo.id != id)
|
||||
.toList(growable: false);
|
||||
await store.write<CacheEntry<List<TodoResponse>>>(
|
||||
pendingListKey,
|
||||
@@ -58,9 +54,9 @@ class TodoRepository {
|
||||
);
|
||||
}
|
||||
|
||||
invalidator.invalidate(pendingListKey);
|
||||
try {
|
||||
await api.completeTodo(id);
|
||||
invalidator.invalidate(pendingListKey);
|
||||
} catch (error) {
|
||||
if (cached != null) {
|
||||
await store.write<CacheEntry<List<TodoResponse>>>(
|
||||
|
||||
@@ -29,6 +29,7 @@ class _TodoDetailScreenState extends State<TodoDetailScreen> {
|
||||
|
||||
TodoResponse? _todo;
|
||||
bool _isLoading = true;
|
||||
bool _didMutate = false;
|
||||
String? _error;
|
||||
|
||||
@override
|
||||
@@ -122,7 +123,7 @@ class _TodoDetailScreenState extends State<TodoDetailScreen> {
|
||||
Widget _buildHeader() {
|
||||
return BackTitlePageHeader(
|
||||
title: '待办详情',
|
||||
onBack: () => context.pop(),
|
||||
onBack: () => context.pop(_didMutate),
|
||||
trailing: _buildHeaderMenu(),
|
||||
);
|
||||
}
|
||||
@@ -379,10 +380,11 @@ class _TodoDetailScreenState extends State<TodoDetailScreen> {
|
||||
}
|
||||
final changed = await context.push<bool>(AppRoutes.todoEdit(_todo!.id));
|
||||
if (changed == true) {
|
||||
await _loadTodo();
|
||||
if (mounted && _error != null) {
|
||||
Toast.show(context, '刷新失败: $_error', type: ToastType.error);
|
||||
_didMutate = true;
|
||||
if (!mounted) {
|
||||
return;
|
||||
}
|
||||
context.pop(true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -398,7 +400,7 @@ class _TodoDetailScreenState extends State<TodoDetailScreen> {
|
||||
try {
|
||||
await _todoApi.deleteTodo(_todo!.id);
|
||||
if (mounted) {
|
||||
context.pop();
|
||||
context.pop(true);
|
||||
}
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
|
||||
@@ -268,11 +268,8 @@ class _TodoQuadrantsScreenState extends State<TodoQuadrantsScreen> {
|
||||
Future<void> _completeTodo(TodoResponse todo) async {
|
||||
try {
|
||||
await _todoRepository.completeTodo(todo.id);
|
||||
if (mounted) {
|
||||
Toast.show(context, '已完成', type: ToastType.success);
|
||||
}
|
||||
try {
|
||||
await _loadTodos();
|
||||
await _loadTodos(showPageLoader: false);
|
||||
} catch (_) {
|
||||
// ignore reload error
|
||||
}
|
||||
@@ -283,14 +280,17 @@ class _TodoQuadrantsScreenState extends State<TodoQuadrantsScreen> {
|
||||
}
|
||||
}
|
||||
|
||||
void _navigateToDetail(TodoResponse todo) {
|
||||
context.push(AppRoutes.todoDetail(todo.id));
|
||||
Future<void> _navigateToDetail(TodoResponse todo) async {
|
||||
final changed = await context.push<bool>(AppRoutes.todoDetail(todo.id));
|
||||
if (changed == true) {
|
||||
await _loadTodos(showPageLoader: false);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _addTodo() async {
|
||||
final created = await context.push<bool>(AppRoutes.todoCreate);
|
||||
if (created == true) {
|
||||
await _loadTodos();
|
||||
await _loadTodos(showPageLoader: false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -326,25 +326,6 @@ class _TodoQuadrantsScreenState extends State<TodoQuadrantsScreen> {
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
children: [
|
||||
AppPressable(
|
||||
borderRadius: BorderRadius.circular(AppRadius.full),
|
||||
onTap: _loadTodos,
|
||||
child: Container(
|
||||
width: 36,
|
||||
height: 36,
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.messageBtnWrap,
|
||||
borderRadius: BorderRadius.circular(AppRadius.full),
|
||||
border: Border.all(color: AppColors.messageBtnBorder),
|
||||
),
|
||||
child: const Icon(
|
||||
LucideIcons.refreshCcw,
|
||||
size: 18,
|
||||
color: AppColors.slate600,
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: AppSpacing.sm),
|
||||
AppPressable(
|
||||
borderRadius: BorderRadius.circular(AppRadius.full),
|
||||
onTap: _addTodo,
|
||||
@@ -448,6 +429,7 @@ class _TodoQuadrantsScreenState extends State<TodoQuadrantsScreen> {
|
||||
horizontal: AppSpacing.sm,
|
||||
),
|
||||
child: _TodoItemWidget(
|
||||
key: ValueKey(item.id),
|
||||
item: item,
|
||||
onComplete: () => _completeTodo(item),
|
||||
onTap: () => _navigateToDetail(item),
|
||||
@@ -603,6 +585,7 @@ class _TodoItemWidget extends StatefulWidget {
|
||||
final VoidCallback onTap;
|
||||
|
||||
const _TodoItemWidget({
|
||||
super.key,
|
||||
required this.item,
|
||||
required this.onComplete,
|
||||
required this.onTap,
|
||||
|
||||
Reference in New Issue
Block a user