6ef658558a
First slice of the tier 2 offline mode work tracked in Fable #147. Notes are the only domain wired so far; this PR establishes the shape and proves the round-trip end-to-end before extending to tasks / projects / events / etc. in phase 2. Local store - Adds drift ^2.20.0 + sqlite3_flutter_libs ^0.5.24 + path ^1.9.0 runtime deps and drift_dev / build_runner dev deps. - New lib/data/local/database.dart: FabledDatabase with CachedNotes (mirroring the Note model 1:1; tags stored as JSON-encoded text) and SyncMetadata (per-domain last_synced_at). Database file lives at $appDocs/fabled_cache.sqlite; opened lazily in a background isolate. - fabledDatabaseProvider on the existing api_client_provider; closes the DB when the ProviderScope disposes. Repository pattern - NotesRepository now takes (NotesApi, FabledDatabase). Reads attempt the network first; on success the response is written to the cache. On NetworkException reads fall back to the cache (rethrowing if it is empty so fresh-install offline still surfaces the network error). - Writes (create/update/delete) hit the server then sync the cache; offline write queueing is phase 3 and explicitly not wired here. - AuthStatus.offline stays owned by AuthNotifier.verify()'s heartbeat; this repo deliberately doesn't poke that state. OfflineBanner - Reads notesRepository.lastSyncedAt(); when present, swaps "Offline — showing cached data." for "Offline — last sync X min ago." A 30s timer refreshes the relative-time string while the banner is mounted. - Falls back to the generic message if no cache exists yet (fresh install offline). Verification - flutter analyze: No issues found Out of scope (later phases per the task body) - Tasks / projects / milestones / events / conversation list / journal day caching (phase 2 — same wrapper pattern, repeated) - Generic write queue with conflict resolution on updated_at (phase 3) - Read-only UI indicators on screens that depend on cached data (phase 4) - Full offline chat (out of scope; needs a local model) - RAG / search over cached content (out of scope) Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
127 lines
3.7 KiB
Dart
127 lines
3.7 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:lucide_icons/lucide_icons.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
import '../providers/api_client_provider.dart';
|
|
import '../providers/auth_provider.dart';
|
|
|
|
/// Sticky banner shown when the backend is unreachable. Surfaces a retry
|
|
/// action plus a "last sync X ago" hint when there is cached content to
|
|
/// fall back on (Tier 2 offline mode).
|
|
class OfflineBanner extends ConsumerStatefulWidget {
|
|
const OfflineBanner({super.key});
|
|
|
|
@override
|
|
ConsumerState<OfflineBanner> createState() => _OfflineBannerState();
|
|
}
|
|
|
|
class _OfflineBannerState extends ConsumerState<OfflineBanner> {
|
|
bool _retrying = false;
|
|
DateTime? _lastSync;
|
|
Timer? _refreshTimer;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_loadLastSync();
|
|
// Refresh the relative-time string every 30s so "5 min ago" rolls
|
|
// forward without the user having to interact with the banner.
|
|
_refreshTimer = Timer.periodic(
|
|
const Duration(seconds: 30),
|
|
(_) => _loadLastSync(),
|
|
);
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_refreshTimer?.cancel();
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> _loadLastSync() async {
|
|
if (!mounted) return;
|
|
try {
|
|
final ts =
|
|
await ref.read(notesRepositoryProvider).lastSyncedAt();
|
|
if (!mounted) return;
|
|
setState(() => _lastSync = ts);
|
|
} catch (_) {
|
|
// Non-critical — banner just won't show the sync hint.
|
|
}
|
|
}
|
|
|
|
Future<void> _retry() async {
|
|
if (_retrying) return;
|
|
setState(() => _retrying = true);
|
|
try {
|
|
await ref.read(authProvider.notifier).verify();
|
|
} finally {
|
|
if (mounted) setState(() => _retrying = false);
|
|
}
|
|
}
|
|
|
|
String? _relativeAgo(DateTime ts) {
|
|
final diff = DateTime.now().difference(ts);
|
|
if (diff.isNegative) return null;
|
|
if (diff.inMinutes < 1) return 'just now';
|
|
if (diff.inMinutes < 60) return '${diff.inMinutes} min ago';
|
|
if (diff.inHours < 24) {
|
|
final h = diff.inHours;
|
|
return '$h ${h == 1 ? 'hour' : 'hours'} ago';
|
|
}
|
|
final d = diff.inDays;
|
|
return '$d ${d == 1 ? 'day' : 'days'} ago';
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final status = ref.watch(authProvider);
|
|
if (status != AuthStatus.offline) return const SizedBox.shrink();
|
|
|
|
final scheme = Theme.of(context).colorScheme;
|
|
final ago = _lastSync == null ? null : _relativeAgo(_lastSync!);
|
|
final message = ago == null
|
|
? 'Offline — showing cached data.'
|
|
: 'Offline — last sync $ago.';
|
|
|
|
return Material(
|
|
color: scheme.errorContainer,
|
|
child: SafeArea(
|
|
bottom: false,
|
|
child: Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
|
|
child: Row(
|
|
children: [
|
|
Icon(LucideIcons.cloudOff,
|
|
size: 18, color: scheme.onErrorContainer),
|
|
const SizedBox(width: 8),
|
|
Expanded(
|
|
child: Text(
|
|
message,
|
|
style: TextStyle(color: scheme.onErrorContainer),
|
|
),
|
|
),
|
|
TextButton(
|
|
onPressed: _retrying ? null : _retry,
|
|
style: TextButton.styleFrom(
|
|
foregroundColor: scheme.onErrorContainer,
|
|
padding: const EdgeInsets.symmetric(horizontal: 12),
|
|
),
|
|
child: _retrying
|
|
? const SizedBox(
|
|
width: 14,
|
|
height: 14,
|
|
child: CircularProgressIndicator(strokeWidth: 2),
|
|
)
|
|
: const Text('Retry'),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|