Offline Support
This commit is contained in:
@@ -34,6 +34,7 @@ import '../../widgets/mono_text.dart';
|
||||
import '../../widgets/responsive_body.dart';
|
||||
import '../../widgets/status_pill.dart';
|
||||
import '../../theme/app_surfaces.dart';
|
||||
import '../../widgets/sync_pending_badge.dart';
|
||||
import '../../widgets/task_assignment_section.dart';
|
||||
import '../../widgets/typing_dots.dart';
|
||||
import '../../widgets/gemini_button.dart';
|
||||
@@ -229,6 +230,25 @@ class _TaskDetailScreenState extends ConsumerState<TaskDetailScreen>
|
||||
child: Center(child: Text('Task not found.')),
|
||||
);
|
||||
}
|
||||
|
||||
// When the live stream delivers this task (i.e. it just synced from offline),
|
||||
// remove it from the pending list. This is a safety-net duplicate of the same
|
||||
// listener in tasks_list_screen; it fires even when the list screen is not
|
||||
// in the widget tree (e.g. deep-linked directly to this route).
|
||||
ref.listen<AsyncValue<List<Task>>>(tasksProvider, (_, next) {
|
||||
final live = next.valueOrNull;
|
||||
if (live == null) return;
|
||||
final pending = ref.read(offlinePendingTasksProvider);
|
||||
if (pending.isEmpty) return;
|
||||
final liveIds = live.map((t) => t.id).toSet();
|
||||
final stillPending =
|
||||
pending.where((t) => !liveIds.contains(t.id)).toList();
|
||||
if (stillPending.length != pending.length) {
|
||||
ref.read(offlinePendingTasksProvider.notifier).state = stillPending;
|
||||
}
|
||||
});
|
||||
|
||||
final isPending = ref.watch(isTaskPendingProvider(task.id));
|
||||
final ticketId = task.ticketId;
|
||||
final typingChannelId = task.id;
|
||||
final ticket = ticketId == null
|
||||
@@ -248,7 +268,21 @@ class _TaskDetailScreenState extends ConsumerState<TaskDetailScreen>
|
||||
orElse: () => false,
|
||||
);
|
||||
final showAssign = canAssign && task.status != 'completed';
|
||||
final assignments = assignmentsAsync.valueOrNull ?? <TaskAssignment>[];
|
||||
final liveAssignments = assignmentsAsync.valueOrNull ?? <TaskAssignment>[];
|
||||
final pendingAssignUserIds =
|
||||
ref.watch(offlinePendingAssignmentsProvider)[task.id];
|
||||
final assignments = pendingAssignUserIds != null
|
||||
? [
|
||||
...liveAssignments.where((a) => a.taskId != task.id),
|
||||
...pendingAssignUserIds.map(
|
||||
(uid) => TaskAssignment(
|
||||
taskId: task.id,
|
||||
userId: uid,
|
||||
createdAt: DateTime.now(),
|
||||
),
|
||||
),
|
||||
]
|
||||
: liveAssignments;
|
||||
final profileById = {
|
||||
for (final profile in profilesAsync.valueOrNull ?? <Profile>[])
|
||||
profile.id: profile,
|
||||
@@ -276,9 +310,25 @@ class _TaskDetailScreenState extends ConsumerState<TaskDetailScreen>
|
||||
final typingState = ref.watch(typingIndicatorProvider(typingChannelId));
|
||||
final canSendMessages = task.status != 'completed';
|
||||
|
||||
final rawPendingMsgs =
|
||||
ref.watch(offlinePendingMessagesProvider)[widget.taskId] ?? [];
|
||||
final pendingOfflineMessages = List.generate(rawPendingMsgs.length, (i) {
|
||||
final payload = rawPendingMsgs[i];
|
||||
return TicketMessage(
|
||||
id: -(i + 1),
|
||||
ticketId: payload['ticket_id'] as String?,
|
||||
taskId: payload['task_id'] as String?,
|
||||
senderId: payload['sender_id'] as String?,
|
||||
content: payload['content'] as String? ?? '',
|
||||
createdAt: payload['created_at'] != null
|
||||
? DateTime.tryParse(payload['created_at'] as String) ?? DateTime.now()
|
||||
: DateTime.now(),
|
||||
);
|
||||
});
|
||||
final messagesAsync = _mergeMessages(
|
||||
taskMessagesAsync,
|
||||
ticketId == null ? null : ref.watch(ticketMessagesProvider(ticketId)),
|
||||
pendingOffline: pendingOfflineMessages,
|
||||
);
|
||||
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
@@ -404,6 +454,10 @@ class _TaskDetailScreenState extends ConsumerState<TaskDetailScreen>
|
||||
final detailsContent = Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
if (isPending) ...[
|
||||
const _TaskPendingBanner(),
|
||||
const SizedBox(height: 12),
|
||||
],
|
||||
Center(
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
@@ -418,6 +472,7 @@ class _TaskDetailScreenState extends ConsumerState<TaskDetailScreen>
|
||||
?.copyWith(fontWeight: FontWeight.w700),
|
||||
),
|
||||
),
|
||||
SyncPendingBadge(isPending: isPending, compact: true),
|
||||
const SizedBox(width: 8),
|
||||
Builder(
|
||||
builder: (ctx) {
|
||||
@@ -470,7 +525,9 @@ class _TaskDetailScreenState extends ConsumerState<TaskDetailScreen>
|
||||
_MetaBadge(label: 'Office', value: officeName),
|
||||
_MetaBadge(
|
||||
label: 'Task #',
|
||||
value: task.taskNumber ?? task.id,
|
||||
value: isPending && task.taskNumber == null
|
||||
? 'Pending'
|
||||
: (task.taskNumber ?? task.id),
|
||||
isMono: true,
|
||||
),
|
||||
],
|
||||
@@ -3495,10 +3552,23 @@ class _TaskDetailScreenState extends ConsumerState<TaskDetailScreen>
|
||||
|
||||
AsyncValue<List<TicketMessage>> _mergeMessages(
|
||||
AsyncValue<List<TicketMessage>> taskMessages,
|
||||
AsyncValue<List<TicketMessage>>? ticketMessages,
|
||||
) {
|
||||
AsyncValue<List<TicketMessage>>? ticketMessages, {
|
||||
List<TicketMessage> pendingOffline = const [],
|
||||
}) {
|
||||
void addPending(Map<int, TicketMessage> byId) {
|
||||
for (final m in pendingOffline) {
|
||||
byId[m.id] = m;
|
||||
}
|
||||
}
|
||||
|
||||
if (ticketMessages == null) {
|
||||
return taskMessages;
|
||||
return taskMessages.whenData((msgs) {
|
||||
if (pendingOffline.isEmpty) return msgs;
|
||||
final byId = <int, TicketMessage>{for (final m in msgs) m.id: m};
|
||||
addPending(byId);
|
||||
return byId.values.toList()
|
||||
..sort((a, b) => b.createdAt.compareTo(a.createdAt));
|
||||
});
|
||||
}
|
||||
return taskMessages.when(
|
||||
data: (taskData) => ticketMessages.when(
|
||||
@@ -3507,6 +3577,7 @@ class _TaskDetailScreenState extends ConsumerState<TaskDetailScreen>
|
||||
for (final message in taskData) message.id: message,
|
||||
for (final message in ticketData) message.id: message,
|
||||
};
|
||||
addPending(byId);
|
||||
final merged = byId.values.toList()
|
||||
..sort((a, b) => b.createdAt.compareTo(a.createdAt));
|
||||
return AsyncValue.data(merged);
|
||||
@@ -4030,10 +4101,20 @@ class _TaskDetailScreenState extends ConsumerState<TaskDetailScreen>
|
||||
showSuccessSnackBar(context, 'Task updated');
|
||||
} catch (e) {
|
||||
if (!mounted) return;
|
||||
showErrorSnackBar(
|
||||
context,
|
||||
'Failed to update task: $e',
|
||||
);
|
||||
if (isOfflineSaveError(e)) {
|
||||
if (dialogContext.mounted) {
|
||||
Navigator.of(dialogContext).pop();
|
||||
}
|
||||
showSuccessSnackBar(
|
||||
context,
|
||||
'Task update saved offline — will sync when connected.',
|
||||
);
|
||||
} else {
|
||||
showErrorSnackBar(
|
||||
context,
|
||||
'Failed to update task: $e',
|
||||
);
|
||||
}
|
||||
} finally {
|
||||
if (dialogContext.mounted) {
|
||||
setDialogState(() => saving = false);
|
||||
@@ -4274,7 +4355,17 @@ class _TaskDetailScreenState extends ConsumerState<TaskDetailScreen>
|
||||
}
|
||||
} catch (e) {
|
||||
if (context.mounted) {
|
||||
showErrorSnackBar(context, e.toString());
|
||||
if (isOfflineSaveError(e)) {
|
||||
if (dialogContext.mounted) {
|
||||
Navigator.of(dialogContext).pop();
|
||||
}
|
||||
showSuccessSnackBar(
|
||||
context,
|
||||
'Task cancellation saved offline — will sync when connected.',
|
||||
);
|
||||
} else {
|
||||
showErrorSnackBar(context, e.toString());
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
if (context.mounted) {
|
||||
@@ -4321,9 +4412,14 @@ class _TaskDetailScreenState extends ConsumerState<TaskDetailScreen>
|
||||
.read(tasksControllerProvider)
|
||||
.updateTaskStatus(taskId: task.id, status: value);
|
||||
} catch (e) {
|
||||
// surface validation or other errors to user
|
||||
if (mounted) {
|
||||
showErrorSnackBarGlobal(e.toString());
|
||||
if (isOfflineSaveError(e)) {
|
||||
showSuccessSnackBarGlobal(
|
||||
'Status update saved offline — will sync when connected.',
|
||||
);
|
||||
} else {
|
||||
showErrorSnackBarGlobal(e.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -4622,6 +4718,66 @@ class _MetaBadge extends StatelessWidget {
|
||||
}
|
||||
}
|
||||
|
||||
class _TaskPendingBanner extends StatefulWidget {
|
||||
const _TaskPendingBanner();
|
||||
|
||||
@override
|
||||
State<_TaskPendingBanner> createState() => _TaskPendingBannerState();
|
||||
}
|
||||
|
||||
class _TaskPendingBannerState extends State<_TaskPendingBanner>
|
||||
with SingleTickerProviderStateMixin {
|
||||
late final AnimationController _ctrl;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_ctrl = AnimationController(
|
||||
vsync: this,
|
||||
duration: const Duration(seconds: 2),
|
||||
)..repeat();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_ctrl.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final scheme = Theme.of(context).colorScheme;
|
||||
return Material(
|
||||
color: scheme.errorContainer,
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
|
||||
child: Row(
|
||||
children: [
|
||||
RotationTransition(
|
||||
turns: _ctrl,
|
||||
child: Icon(
|
||||
Icons.sync_rounded,
|
||||
color: scheme.onErrorContainer,
|
||||
size: 16,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: Text(
|
||||
'Pending sync — created offline. Edits will queue automatically.',
|
||||
style: Theme.of(context).textTheme.labelMedium?.copyWith(
|
||||
color: scheme.onErrorContainer,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
extension _FirstOrNull<T> on Iterable<T> {
|
||||
T? get firstOrNull => isEmpty ? null : first;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user