created basic realtime implementation

This commit is contained in:
Myzel394 2022-08-16 10:36:25 +02:00
parent 50191a305e
commit 0519c5caab
3 changed files with 70 additions and 16 deletions

View File

@ -15,6 +15,8 @@ class TimelineModel extends PropertyChangeNotifier<String> {
Map<String, MemoryPack>? timeline, Map<String, MemoryPack>? timeline,
}) : _timeline = timeline ?? {}; }) : _timeline = timeline ?? {};
late RealtimeSubscription _serverSubscription;
int _currentIndex = 0; int _currentIndex = 0;
int _memoryIndex = 0; int _memoryIndex = 0;
bool _paused = false; bool _paused = false;
@ -51,6 +53,12 @@ class TimelineModel extends PropertyChangeNotifier<String> {
); );
} }
@override
void dispose() {
_serverSubscription.unsubscribe(timeout: Duration.zero);
super.dispose();
}
DateTime dateAtIndex(final int index) => DateTime dateAtIndex(final int index) =>
DateTime.parse(_timeline.keys.elementAt(index)); DateTime.parse(_timeline.keys.elementAt(index));
@ -139,12 +147,67 @@ class TimelineModel extends PropertyChangeNotifier<String> {
Future<void> initialize() async { Future<void> initialize() async {
setIsInitializing(true); setIsInitializing(true);
await refreshFromServer(); await _listenToServer();
setIsInitializing(false); setIsInitializing(false);
} }
Future<void> refreshFromServer() async { void _insertMemory(final Memory memory) {
final date = DateFormat('yyyy-MM-dd').format(memory.creationDate);
if (_timeline.containsKey(date)) {
_timeline[date]!.memories.add(memory);
// Sort descending based on creation date
_timeline[date]!.memories.sort(
(a, b) => b.creationDate.compareTo(a.creationDate),
);
} else {
_timeline[date] = MemoryPack([memory]);
}
}
void _updateMemory(final String id, final Memory memory) {
final date = DateFormat('yyyy-MM-dd').format(memory.creationDate);
if (_timeline.containsKey(date)) {
_timeline[date]!.memories.removeWhere((m) => m.id == id);
_timeline[date]!.memories.add(memory);
// Sort descending based on creation date
_timeline[date]!.memories.sort(
(a, b) => b.creationDate.compareTo(a.creationDate),
);
} else {
_timeline[date] = MemoryPack([memory]);
}
}
void _deleteMemory(final String id) {
for (final date in _timeline.keys) {
_timeline[date]!.memories.removeWhere((m) => m.id == id);
}
}
Future<void> _onMemoriesUpdate(
final SupabaseRealtimePayload response,
) async {
if (response == null) {
return;
}
switch (response.eventType) {
case 'INSERT':
final memory = Memory.parse(response.newRecord!);
_insertMemory(memory);
break;
case 'UPDATE':
final memory = Memory.parse(response.newRecord!);
_updateMemory(response.oldRecord!['id'], memory);
break;
case 'DELETE':
_deleteMemory(response.oldRecord!['id']);
break;
}
}
Future<void> _listenToServer() async {
final response = await supabase final response = await supabase
.from('memories') .from('memories')
.select() .select()
@ -158,6 +221,11 @@ class TimelineModel extends PropertyChangeNotifier<String> {
..clear() ..clear()
..addAll(mapFromMemoriesList(memories)); ..addAll(mapFromMemoriesList(memories));
_serverSubscription = supabase
.from('memories')
.on(SupabaseEventTypes.all, _onMemoriesUpdate)
.subscribe();
notifyListeners(); notifyListeners();
} }
} }

View File

@ -12,15 +12,11 @@ import 'package:supabase_flutter/supabase_flutter.dart';
class MemorySheet extends StatefulWidget { class MemorySheet extends StatefulWidget {
final Memory memory; final Memory memory;
final BuildContext sheetContext; final BuildContext sheetContext;
final VoidCallback onDelete;
final VoidCallback onVisibilityChanged;
const MemorySheet({ const MemorySheet({
Key? key, Key? key,
required this.memory, required this.memory,
required this.sheetContext, required this.sheetContext,
required this.onDelete,
required this.onVisibilityChanged,
}) : super(key: key); }) : super(key: key);
@override @override
@ -36,8 +32,6 @@ class _MemorySheetState extends State<MemorySheet> with Loadable {
if (mounted) { if (mounted) {
Navigator.pop(context); Navigator.pop(context);
} }
widget.onDelete();
} }
Future<void> downloadFile() async { Future<void> downloadFile() async {
@ -83,8 +77,6 @@ class _MemorySheetState extends State<MemorySheet> with Loadable {
} else { } else {
context.showSuccessSnackBar(message: 'Your Memory is private now.'); context.showSuccessSnackBar(message: 'Your Memory is private now.');
} }
widget.onVisibilityChanged();
} catch (error) { } catch (error) {
context.showErrorSnackBar(message: 'There was an error.'); context.showErrorSnackBar(message: 'There was an error.');
} }

View File

@ -121,12 +121,6 @@ class _TimelinePageState extends State<TimelinePage> {
builder: (sheetContext) => MemorySheet( builder: (sheetContext) => MemorySheet(
memory: timeline.currentMemory, memory: timeline.currentMemory,
sheetContext: sheetContext, sheetContext: sheetContext,
onDelete: () async {
timeline.removeCurrentMemory();
},
onVisibilityChanged: () async {
timeline.refreshFromServer();
},
), ),
); );