-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Group all memories and other sync improvements #1021
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| import 'package:flutter/material.dart'; | ||
| import 'package:friend_private/backend/http/api/memories.dart'; | ||
| import 'package:friend_private/backend/schema/memory.dart'; | ||
| import 'package:friend_private/pages/memory_detail/memory_detail_provider.dart'; | ||
| import 'package:friend_private/pages/memory_detail/page.dart'; | ||
| import 'package:friend_private/providers/memory_provider.dart'; | ||
| import 'package:friend_private/utils/other/temp.dart'; | ||
| import 'package:provider/provider.dart'; | ||
|
|
@@ -54,7 +56,14 @@ class _SyncedMemoryListItemState extends State<SyncedMemoryListItem> { | |
| } | ||
|
|
||
| return GestureDetector( | ||
| onTap: () async {}, | ||
| onTap: () async { | ||
| context.read<MemoryDetailProvider>().updateMemory(widget.memoryIdx, widget.date); | ||
| Provider.of<MemoryProvider>(context, listen: false).onMemoryTap(widget.memoryIdx); | ||
| routeToPage( | ||
| context, | ||
| MemoryDetailPage(memory: widget.memory, isFromOnboarding: false), | ||
| ); | ||
| }, | ||
|
Comment on lines
+59
to
+66
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Use consistent methods to access providers In line 60, you're using |
||
| child: Padding( | ||
| padding: const EdgeInsets.only(top: 12, left: 16, right: 16), | ||
| child: Container( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ class MemoryProvider extends ChangeNotifier implements IWalServiceListener, IWal | |
|
|
||
| bool isLoadingMemories = false; | ||
| bool hasNonDiscardedMemories = true; | ||
| bool showDiscardedMemories = false; | ||
|
|
||
| String previousQuery = ''; | ||
|
|
||
|
|
@@ -36,8 +37,6 @@ class MemoryProvider extends ChangeNotifier implements IWalServiceListener, IWal | |
|
|
||
| bool isSyncing = false; | ||
| bool syncCompleted = false; | ||
| Map<String, dynamic>? syncResult; | ||
| Map<String, List<ServerMemory?>>? syncedMemories; | ||
| Map<String, List<Record>>? syncedMemoriesPointers; | ||
|
|
||
| MemoryProvider() { | ||
|
|
@@ -77,7 +76,8 @@ class MemoryProvider extends ChangeNotifier implements IWalServiceListener, IWal | |
| void toggleDiscardMemories() { | ||
| MixpanelManager().showDiscardedMemoriesToggled(!SharedPreferencesUtil().showDiscardedMemories); | ||
| SharedPreferencesUtil().showDiscardedMemories = !SharedPreferencesUtil().showDiscardedMemories; | ||
| filterGroupedMemories(''); | ||
| showDiscardedMemories = SharedPreferencesUtil().showDiscardedMemories; | ||
| // filterGroupedMemories(''); | ||
| notifyListeners(); | ||
| } | ||
|
|
||
|
|
@@ -92,7 +92,6 @@ class MemoryProvider extends ChangeNotifier implements IWalServiceListener, IWal | |
| processingMemories = memories.where((m) => m.status == MemoryStatus.processing).toList(); | ||
|
|
||
| memories = memories.where((m) => m.status == MemoryStatus.completed).toList(); | ||
|
|
||
| if (memories.isEmpty) { | ||
| memories = SharedPreferencesUtil().cachedMemories; | ||
| } else { | ||
|
|
@@ -105,7 +104,7 @@ class MemoryProvider extends ChangeNotifier implements IWalServiceListener, IWal | |
| void _groupMemoriesByDateWithoutNotify() { | ||
| groupedMemories = {}; | ||
| for (var memory in memories) { | ||
| if (SharedPreferencesUtil().showDiscardedMemories && memory.discarded && !memory.isNew) continue; | ||
| // 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] = []; | ||
|
|
@@ -144,7 +143,8 @@ class MemoryProvider extends ChangeNotifier implements IWalServiceListener, IWal | |
| } | ||
|
|
||
| void filterGroupedMemories(String query) { | ||
| _filterGroupedMemoriesWithoutNotify(query); | ||
| // _filterGroupedMemoriesWithoutNotify(query); | ||
| _groupMemoriesByDateWithoutNotify(); | ||
| notifyListeners(); | ||
| } | ||
|
|
||
|
|
@@ -345,13 +345,8 @@ class MemoryProvider extends ChangeNotifier implements IWalServiceListener, IWal | |
| setIsSyncing(true); | ||
| var res = await _walService.syncAll(progress: this); | ||
| if (res != null) { | ||
| syncResult = {"new_memories": res.newMemoryIds, 'updated_memories': res.updatedMemoryIds}; | ||
| } | ||
| if (syncResult != null) { | ||
| if (syncResult!['new_memories'] != [] || syncResult!['updated_memories']) { | ||
| syncedMemories = {}; | ||
| await getSyncedMemoriesData(); | ||
| addSyncedMemoriesToGroupedMemories(); | ||
| if (res.newMemoryIds.isNotEmpty || res.updatedMemoryIds.isNotEmpty) { | ||
| await getSyncedMemoriesData(res); | ||
| } | ||
| } | ||
| syncCompleted = true; | ||
|
|
@@ -360,39 +355,51 @@ class MemoryProvider extends ChangeNotifier implements IWalServiceListener, IWal | |
| return; | ||
| } | ||
|
|
||
| Future getSyncedMemoriesData() async { | ||
| List<dynamic> newMemories = syncResult!['new_memories'] ?? []; | ||
| List<dynamic> updatedMemories = syncResult!['updated_memories'] ?? []; | ||
| Future getSyncedMemoriesData(SyncLocalFilesResponse syncResult) async { | ||
| List<dynamic> newMemories = syncResult.newMemoryIds; | ||
| List<dynamic> updatedMemories = syncResult.updatedMemoryIds; | ||
|
|
||
| List<Future<ServerMemory?>> newMemoriesFutures = newMemories.map((item) => getMemoryDetails(item)).toList(); | ||
|
|
||
| List<Future<ServerMemory?>> updatedMemoriesFutures = updatedMemories.map((item) => getMemoryDetails(item)).toList(); | ||
| var syncedMemories = {'new_memories': [], 'updated_memories': []}; | ||
| try { | ||
| final newMemoriesResponses = await Future.wait(newMemoriesFutures); | ||
| syncedMemories!['new_memories'] = newMemoriesResponses; | ||
| syncedMemories['new_memories'] = newMemoriesResponses; | ||
|
|
||
| final updatedMemoriesResponses = await Future.wait(updatedMemoriesFutures); | ||
| syncedMemories!['updated_memories'] = updatedMemoriesResponses; | ||
| syncedMemories['updated_memories'] = updatedMemoriesResponses; | ||
| addSyncedMemoriesToGroupedMemories(syncedMemories); | ||
| } catch (e) { | ||
| print('Error during API calls: $e'); | ||
| } | ||
| } | ||
|
|
||
| void addSyncedMemoriesToGroupedMemories() { | ||
| void addSyncedMemoriesToGroupedMemories(Map syncedMemories) { | ||
| syncedMemoriesPointers = {'new_memories': [], 'updated_memories': []}; | ||
| if (syncedMemories == null) return; | ||
| if (syncedMemories!['new_memories'] != null) { | ||
| for (var memory in syncedMemories!['new_memories']!) { | ||
| if (memory != null) { | ||
| if (syncedMemories['new_memories'] != []) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix list emptiness checks by using In Dart, comparing a list directly to an empty list using Apply this diff to correct the checks: - if (syncedMemories['new_memories'] != []) {
+ if (syncedMemories['new_memories'].isNotEmpty) {
- if (syncedMemories['updated_memories'] != []) {
+ if (syncedMemories['updated_memories'].isNotEmpty) {Also applies to: 387-387 |
||
| for (var memory in syncedMemories['new_memories']!) { | ||
| if (memory != null && memory.status == MemoryStatus.completed) { | ||
| addMemory(memory); | ||
| syncedMemoriesPointers!['new_memories']!.add(getMemoryDateAndIndex(memory)); | ||
| } | ||
| } | ||
| } | ||
| if (syncedMemories!['updated_memories'] != null) { | ||
| for (var memory in syncedMemories!['updated_memories']!) { | ||
| if (syncedMemories['updated_memories'] != []) { | ||
| for (var memory in syncedMemories['updated_memories']!) { | ||
| if (memory != null && memory.status == MemoryStatus.completed) { | ||
| upsertMemory(memory); | ||
| syncedMemoriesPointers!['updated_memories']!.add(getMemoryDateAndIndex(memory)); | ||
| } | ||
| } | ||
| } | ||
| for (var memory in syncedMemories['new_memories']!) { | ||
| if (memory != null && memory.status == MemoryStatus.completed) { | ||
| syncedMemoriesPointers!['new_memories']!.add(getMemoryDateAndIndex(memory)); | ||
| } | ||
| } | ||
| if (syncedMemories['updated_memories'] != []) { | ||
| for (var memory in syncedMemories['updated_memories']!) { | ||
| if (memory != null && memory.status == MemoryStatus.completed) { | ||
| updateMemoryInSortedList(memory); | ||
| syncedMemoriesPointers!['updated_memories']!.add(getMemoryDateAndIndex(memory)); | ||
| } | ||
| } | ||
|
|
@@ -410,6 +417,16 @@ class MemoryProvider extends ChangeNotifier implements IWalServiceListener, IWal | |
| syncedMemoriesPointers!['updated_memories']![idx] = getMemoryDateAndIndex(memory); | ||
| } | ||
| } | ||
| // if new_memories also include the same memory, update it coz we currently have duplicates | ||
| if (syncedMemoriesPointers!['new_memories'] != null) { | ||
| var idx = syncedMemoriesPointers!['new_memories']!.indexWhere((element) { | ||
| dynamic e = element; | ||
| return e.$3.id == memory.id; | ||
| }); | ||
|
Comment on lines
+424
to
+425
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Use named fields in records for better readability Accessing record fields using positional notation like Modify the return type of ({DateTime date, int index, ServerMemory memory}) getMemoryDateAndIndex(ServerMemory memory) {
// implementation
}Then access the fields using: return e.memory.id == memory.id; |
||
| if (idx != -1) { | ||
| syncedMemoriesPointers!['new_memories']![idx] = getMemoryDateAndIndex(memory); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| (DateTime, int, ServerMemory) getMemoryDateAndIndex(ServerMemory memory) { | ||
|
|
@@ -427,7 +444,6 @@ class MemoryProvider extends ChangeNotifier implements IWalServiceListener, IWal | |
| } | ||
|
|
||
| void clearSyncResult() { | ||
| syncResult = null; | ||
| syncCompleted = false; | ||
| notifyListeners(); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Codebase verification
Issue:
showDiscardedMemorieschanges do not trigger UI updates.The
showDiscardedMemoriesproperty changes do not callnotifyListeners(), preventing the UI from rebuilding as expected.showDiscardedMemoriesincludes a call tonotifyListeners(). For example:🔗 Analysis chain
LGTM! Consider adding a comment and verifying UI updates.
The implementation of the filtering mechanism for discarded memories looks good and aligns with the PR objectives. Great job on improving the memory management!
A few suggestions to enhance the code:
Consider adding a comment explaining the filtering logic for better code readability. For example:
// Show all memories if showDiscardedMemories is true, otherwise show only non-discarded memoriesEnsure that changing the
showDiscardedMemoriesproperty triggers a rebuild of the widget. You may want to verify this behavior:🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
Length of output: 555