Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 25 additions & 30 deletions app/lib/pages/memories/page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -171,51 +171,46 @@ class MemoriesPageState extends State<MemoriesPage> with AutomaticKeepAliveClien
(() async {
final provider = context.read<MemoriesProvider>();
await provider.init();
if (!mounted) return;

// Apply the date-based default filter
_applyFilter(_currentFilter);

// Mark initial load as complete
if (mounted) {
setState(() {
_isInitialLoad = false;
});
}
setState(() {
_isInitialLoad = false;
});
Comment on lines 169 to +174
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

There are two separate setState calls here, one inside _applyFilter and another one here to set _isInitialLoad. This is inefficient as it causes two rebuilds. After applying the suggested change to _applyFilter to accept a callback, you can combine these into a single state update.

      _applyFilter(_currentFilter, andThen: () {
        _isInitialLoad = false;
      });


if (!mounted) return;
final unreviewedMemories = provider.unreviewed;
final unreviewed = provider.unreviewed;
final home = context.read<HomeProvider>();
if (unreviewedMemories.isNotEmpty && home.selectedIndex == 2) {
_showReviewSheet(context, unreviewedMemories, provider);

if (unreviewed.isNotEmpty && home.selectedIndex == 2) {
_showReviewSheet(context, unreviewed, provider);
}
}).withPostFrameCallback();
}

void _applyFilter(FilterOption option) {
setState(() {
_currentFilter = option;
if (!mounted) return;

switch (option) {
case FilterOption.interesting:
_filterByCategory(MemoryCategory.interesting);
MixpanelManager().memoriesFiltered('interesting');
break;
case FilterOption.system:
_filterByCategory(MemoryCategory.system);
MixpanelManager().memoriesFiltered('system');
break;
case FilterOption.all:
_filterByCategory(null); // null means no category filter
MixpanelManager().memoriesFiltered('all');
break;
}
});
}
MemoryCategory? category;
switch (option) {
case FilterOption.interesting:
category = MemoryCategory.interesting;
MixpanelManager().memoriesFiltered('interesting');
break;
case FilterOption.system:
category = MemoryCategory.system;
MixpanelManager().memoriesFiltered('system');
break;
case FilterOption.all:
category = null; // null means no category filter
MixpanelManager().memoriesFiltered('all');
break;
}

void _filterByCategory(MemoryCategory? category) {
if (!mounted) return;

setState(() {
_currentFilter = option;
_selectedCategory = category;
});

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

To allow combining setState calls from different places (like in initState), you can modify this method to accept an optional callback. This callback can be executed inside the setState block, consolidating multiple state updates into one. This improves performance by reducing unnecessary rebuilds.

Additionally, the mounted check on line 211 is redundant since there's already a check on line 193 and no await operations are performed in between. The suggested change removes it.

  void _applyFilter(FilterOption option, {void Function()? andThen}) {
    if (!mounted) return;

    MemoryCategory? category;
    switch (option) {
      case FilterOption.interesting:
        category = MemoryCategory.interesting;
        MixpanelManager().memoriesFiltered('interesting');
        break;
      case FilterOption.system:
        category = MemoryCategory.system;
        MixpanelManager().memoriesFiltered('system');
        break;
      case FilterOption.all:
        category = null; // null means no category filter
        MixpanelManager().memoriesFiltered('all');
        break;
    }

    setState(() {
      _currentFilter = option;
      _selectedCategory = category;
      andThen?.call();
    });
  }

Expand Down