From 2b96942841faf7144b724f780a7b3cb538bb6788 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?th=E1=BB=8Bnh?= Date: Sat, 28 Sep 2024 18:26:45 +0700 Subject: [PATCH] Minor fixes on event new processing memory created, duplicated new memory, performance issue on memory provider --- .../memories/widgets/memory_list_item.dart | 1 + .../memories/widgets/processing_capture.dart | 1 - app/lib/providers/capture_provider.dart | 2 +- app/lib/providers/memory_provider.dart | 48 ++++++++++++++++--- backend/models/message_event.py | 1 + backend/routers/transcribe.py | 4 +- 6 files changed, 47 insertions(+), 10 deletions(-) diff --git a/app/lib/pages/memories/widgets/memory_list_item.dart b/app/lib/pages/memories/widgets/memory_list_item.dart index 128fc7a21..13c66c567 100644 --- a/app/lib/pages/memories/widgets/memory_list_item.dart +++ b/app/lib/pages/memories/widgets/memory_list_item.dart @@ -34,6 +34,7 @@ class _MemoryListItemState extends State { onTap: () async { MixpanelManager().memoryListItemClicked(widget.memory, widget.memoryIdx); context.read().updateMemory(widget.memoryIdx, widget.date); + Provider.of(context, listen: false).onMemoryTap(widget.memoryIdx); routeToPage( context, MemoryDetailPage(memory: widget.memory, isFromOnboarding: widget.isFromOnboarding), diff --git a/app/lib/pages/memories/widgets/processing_capture.dart b/app/lib/pages/memories/widgets/processing_capture.dart index 1ad6a74fe..712200e45 100644 --- a/app/lib/pages/memories/widgets/processing_capture.dart +++ b/app/lib/pages/memories/widgets/processing_capture.dart @@ -145,7 +145,6 @@ class _MemoryCaptureWidgetState extends State { mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ deviceProvider.connectedDevice == null && - !deviceProvider.isConnecting && !isUsingPhoneMic && havingCapturingMemory ? Row( diff --git a/app/lib/providers/capture_provider.dart b/app/lib/providers/capture_provider.dart index e46e328b5..28c1de9a0 100644 --- a/app/lib/providers/capture_provider.dart +++ b/app/lib/providers/capture_provider.dart @@ -291,7 +291,7 @@ class CaptureProvider extends ChangeNotifier // use memory provider to add memory MixpanelManager().memoryCreated(memory); - memoryProvider?.addMemory(memory); + memoryProvider?.upsertMemory(memory); if (memoryProvider?.memories.isEmpty ?? false) { memoryProvider?.getMoreMemoriesFromServer(); } diff --git a/app/lib/providers/memory_provider.dart b/app/lib/providers/memory_provider.dart index 2b70887c2..787d6eae5 100644 --- a/app/lib/providers/memory_provider.dart +++ b/app/lib/providers/memory_provider.dart @@ -21,6 +21,20 @@ class MemoryProvider extends ChangeNotifier { List processingMemories = []; Timer? _processingMemoryWatchTimer; + void onMemoryTap(int idx) { + if (idx < 0 || idx > memories.length - 1) { + return; + } + var changed = false; + if (memories[idx].isNew) { + memories[idx].isNew = false; + changed = true; + } + if (changed) { + filterGroupedMemories(''); + } + } + void toggleDiscardMemories() { MixpanelManager().showDiscardedMemoriesToggled(!SharedPreferencesUtil().showDiscardedMemories); SharedPreferencesUtil().showDiscardedMemories = !SharedPreferencesUtil().showDiscardedMemories; @@ -40,7 +54,7 @@ class MemoryProvider extends ChangeNotifier { } else { SharedPreferencesUtil().cachedMemories = memories; } - groupMemoriesByDate(); + _groupMemoriesByDateWithoutNotify(); // Processing memories var pms = await getProcessingMemories(); @@ -49,7 +63,7 @@ class MemoryProvider extends ChangeNotifier { notifyListeners(); } - void groupMemoriesByDate() { + void _groupMemoriesByDateWithoutNotify() { groupedMemories = {}; for (var memory in memories) { if (SharedPreferencesUtil().showDiscardedMemories && memory.discarded && !memory.isNew) continue; @@ -57,16 +71,23 @@ class MemoryProvider extends ChangeNotifier { if (!groupedMemories.containsKey(date)) { groupedMemories[date] = []; } - groupedMemories[date]!.add(memory); - groupedMemories[date]!.sort((a, b) => b.createdAt.compareTo(a.createdAt)); + groupedMemories[date]?.add(memory); + } + // Sort + for (final date in groupedMemories.keys) { + groupedMemories[date]?.sort((a, b) => b.createdAt.compareTo(a.createdAt)); } + } + + void groupMemoriesByDate() { + _groupMemoriesByDateWithoutNotify(); notifyListeners(); } - void filterGroupedMemories(String query) { + void _filterGroupedMemoriesWithoutNotify(String query) { if (query.isEmpty) { groupedMemories = {}; - groupMemoriesByDate(); + _groupMemoriesByDateWithoutNotify(); } else { groupedMemories = {}; for (var memory in memories) { @@ -77,10 +98,14 @@ class MemoryProvider extends ChangeNotifier { if ((memory.getTranscript() + memory.structured.title + memory.structured.overview) .toLowerCase() .contains(query.toLowerCase())) { - groupedMemories[date]!.add(memory); + groupedMemories[date]?.add(memory); } } } + } + + void filterGroupedMemories(String query) { + _filterGroupedMemoriesWithoutNotify(query); notifyListeners(); } @@ -224,6 +249,15 @@ class MemoryProvider extends ChangeNotifier { notifyListeners(); } + void upsertMemory(ServerMemory memory) { + int idx = memories.indexWhere((m) => m.id == memory.id); + if (idx < 0) { + addMemory(memory); + } else { + updateMemory(memory, idx); + } + } + void addMemoryToGroupedMemories(ServerMemory memory) { var date = DateTime(memory.createdAt.year, memory.createdAt.month, memory.createdAt.day); if (groupedMemories.containsKey(date)) { diff --git a/backend/models/message_event.py b/backend/models/message_event.py index 31af0de14..eb1a032c7 100644 --- a/backend/models/message_event.py +++ b/backend/models/message_event.py @@ -29,6 +29,7 @@ def to_json(self): class NewProcessingMemoryCreated(MessageEvent): processing_memory_id: Optional[str] = None + memory_id: Optional[str] = None def to_json(self): j = self.model_dump(mode="json") diff --git a/backend/routers/transcribe.py b/backend/routers/transcribe.py index c92e0cdf6..4f340177d 100644 --- a/backend/routers/transcribe.py +++ b/backend/routers/transcribe.py @@ -408,7 +408,9 @@ async def _create_processing_memory(): # Message: New processing memory created ok = await _send_message_event(NewProcessingMemoryCreated( event_type="new_processing_memory_created", - processing_memory_id=processing_memory.id), + processing_memory_id=processing_memory.id, + memory_id=processing_memory.memory_id, + ), ) if not ok: print("Can not send message event new_processing_memory_created")