Skip to content

Commit

Permalink
Minor fixes on event new processing memory created, duplicated new me…
Browse files Browse the repository at this point in the history
…mory, performance issue on memory provider
  • Loading branch information
beastoin committed Sep 28, 2024
1 parent 8f761c8 commit 2b96942
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 10 deletions.
1 change: 1 addition & 0 deletions app/lib/pages/memories/widgets/memory_list_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class _MemoryListItemState extends State<MemoryListItem> {
onTap: () async {
MixpanelManager().memoryListItemClicked(widget.memory, widget.memoryIdx);
context.read<MemoryDetailProvider>().updateMemory(widget.memoryIdx, widget.date);
Provider.of<MemoryProvider>(context, listen: false).onMemoryTap(widget.memoryIdx);
routeToPage(
context,
MemoryDetailPage(memory: widget.memory, isFromOnboarding: widget.isFromOnboarding),
Expand Down
1 change: 0 additions & 1 deletion app/lib/pages/memories/widgets/processing_capture.dart
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ class _MemoryCaptureWidgetState extends State<MemoryCaptureWidget> {
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
deviceProvider.connectedDevice == null &&
!deviceProvider.isConnecting &&
!isUsingPhoneMic &&
havingCapturingMemory
? Row(
Expand Down
2 changes: 1 addition & 1 deletion app/lib/providers/capture_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
48 changes: 41 additions & 7 deletions app/lib/providers/memory_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ class MemoryProvider extends ChangeNotifier {
List<ServerProcessingMemory> 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;
Expand All @@ -40,7 +54,7 @@ class MemoryProvider extends ChangeNotifier {
} else {
SharedPreferencesUtil().cachedMemories = memories;
}
groupMemoriesByDate();
_groupMemoriesByDateWithoutNotify();

// Processing memories
var pms = await getProcessingMemories();
Expand All @@ -49,24 +63,31 @@ class MemoryProvider extends ChangeNotifier {
notifyListeners();
}

void groupMemoriesByDate() {
void _groupMemoriesByDateWithoutNotify() {
groupedMemories = {};
for (var memory in memories) {
if (SharedPreferencesUtil().showDiscardedMemories && memory.discarded && !memory.isNew) continue;
var date = DateTime(memory.createdAt.year, memory.createdAt.month, memory.createdAt.day);
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) {
Expand All @@ -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();
}

Expand Down Expand Up @@ -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)) {
Expand Down
1 change: 1 addition & 0 deletions backend/models/message_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 3 additions & 1 deletion backend/routers/transcribe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 2b96942

Please sign in to comment.