-
Notifications
You must be signed in to change notification settings - Fork 14
Add strategy pinning with manual ordering #77
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
Open
devin-ai-integration
wants to merge
14
commits into
main
Choose a base branch
from
feature/strategy-pinning
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
7a386c9
Add PinnedItemsProvider and pins Hive box
njiedev efc1508
Add Pin/Unpin action to strategy tile menu
njiedev 9747dc1
Add Pin/Unpin action to folder pill menu
njiedev 2ff17af
Remove pins when strategies or folders are deleted
njiedev 515e371
Render pinned strategies and folders at home screen
njiedev 1e515d3
Make Pinned header visible and label the All section
njiedev 869c658
Float pinned items to top of grid instead of a separate section
njiedev 6a635f7
Update lib/widgets/folder_content.dart
njiedev f86eab5
Add manual pinned item ordering
devin-ai-integration[bot] e98001f
Address manual pin ordering review feedback
devin-ai-integration[bot] 6fbd42a
Fix preview tests without Hive-backed preferences
devin-ai-integration[bot] cfb8f60
Reset drag state before agent tap selection
devin-ai-integration[bot] c845c3a
Handle agent taps after stale drag state
devin-ai-integration[bot] 7b29520
Use drag and drop for pinned reorder
devin-ai-integration[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| import 'package:flutter_riverpod/flutter_riverpod.dart'; | ||
| import 'package:hive_ce_flutter/hive_flutter.dart'; | ||
| import 'package:icarus/const/hive_boxes.dart'; | ||
|
|
||
| final pinnedItemsProvider = | ||
| NotifierProvider<PinnedItemsProvider, Map<String, int>>( | ||
| PinnedItemsProvider.new); | ||
|
|
||
| const _legacyTimestampThreshold = 1000000000000; | ||
|
|
||
| List<String> pinnedIdsInManualOrder(Map<String, int> pinned) { | ||
| final entries = pinned.entries.toList()..sort(_comparePinnedEntries); | ||
| return entries.map((e) => e.key).toList(); | ||
| } | ||
|
|
||
| int _comparePinnedEntries(MapEntry<String, int> a, MapEntry<String, int> b) { | ||
| final hasLegacyTimestamp = a.value > _legacyTimestampThreshold || | ||
| b.value > _legacyTimestampThreshold; | ||
| if (hasLegacyTimestamp) return b.value.compareTo(a.value); | ||
| return a.value.compareTo(b.value); | ||
| } | ||
|
|
||
| /// Tracks which strategies/folders are pinned to the home screen. | ||
| /// | ||
| /// Stored as a Hive box keyed by the item's id, with a zero-based manual sort | ||
| /// index as the value. State is a `Map<String, int>` of id -> order. | ||
| class PinnedItemsProvider extends Notifier<Map<String, int>> { | ||
| Box<int> get _box => Hive.box<int>(HiveBoxNames.pinnedItemsBox); | ||
|
|
||
| @override | ||
| Map<String, int> build() { | ||
| return _readFromBox(); | ||
| } | ||
|
|
||
| bool isPinned(String id) => state.containsKey(id); | ||
|
|
||
| List<String> pinnedIdsByManualOrder() => pinnedIdsInManualOrder(state); | ||
|
|
||
| @Deprecated('Use pinnedIdsByManualOrder') | ||
| List<String> pinnedIdsByRecency() => pinnedIdsByManualOrder(); | ||
|
|
||
| Future<void> togglePin(String id) async { | ||
| if (isPinned(id)) { | ||
| await removePin(id); | ||
| return; | ||
| } | ||
| await _saveOrder([id, ...pinnedIdsByManualOrder()]); | ||
| } | ||
|
|
||
| Future<void> removePin(String id) async { | ||
| if (!isPinned(id)) return; | ||
| final orderedIds = pinnedIdsByManualOrder()..remove(id); | ||
| await _saveOrder(orderedIds); | ||
| } | ||
|
|
||
| Future<void> movePinUp(String id) async { | ||
| final orderedIds = pinnedIdsByManualOrder(); | ||
| final index = orderedIds.indexOf(id); | ||
| if (index <= 0) return; | ||
| orderedIds | ||
| ..removeAt(index) | ||
| ..insert(index - 1, id); | ||
| await _saveOrder(orderedIds); | ||
| } | ||
|
|
||
| Future<void> movePinDown(String id) async { | ||
| final orderedIds = pinnedIdsByManualOrder(); | ||
| final index = orderedIds.indexOf(id); | ||
| if (index == -1 || index == orderedIds.length - 1) return; | ||
| orderedIds | ||
| ..removeAt(index) | ||
| ..insert(index + 1, id); | ||
| await _saveOrder(orderedIds); | ||
| } | ||
|
|
||
| Future<void> movePinToTop(String id) async { | ||
| final orderedIds = pinnedIdsByManualOrder(); | ||
| if (!orderedIds.remove(id)) return; | ||
| await _saveOrder([id, ...orderedIds]); | ||
| } | ||
|
|
||
| Future<void> movePin({ | ||
| required String id, | ||
| required String targetId, | ||
| required bool insertAfterTarget, | ||
| }) async { | ||
| if (id == targetId || !isPinned(id) || !isPinned(targetId)) return; | ||
|
|
||
| final orderedIds = pinnedIdsByManualOrder()..remove(id); | ||
| final targetIndex = orderedIds.indexOf(targetId); | ||
| if (targetIndex == -1) return; | ||
|
|
||
| orderedIds.insert( | ||
| targetIndex + (insertAfterTarget ? 1 : 0), | ||
| id, | ||
| ); | ||
| await _saveOrder(orderedIds); | ||
| } | ||
|
|
||
| Future<void> _saveOrder(List<String> orderedIds) async { | ||
| final nextState = <String, int>{ | ||
| for (final entry in orderedIds.asMap().entries) entry.value: entry.key, | ||
| }; | ||
| await _box.putAll(nextState); | ||
| final staleKeys = _box.keys | ||
| .where((key) => key is! String || !nextState.containsKey(key)) | ||
| .toList(); | ||
| await _box.deleteAll(staleKeys); | ||
| state = nextState; | ||
| } | ||
|
|
||
| Map<String, int> _readFromBox() { | ||
| final result = <String, int>{}; | ||
| for (final key in _box.keys) { | ||
| if (key is! String) continue; // resilient to stale/invalid keys | ||
| final value = _box.get(key); | ||
| if (value == null) continue; | ||
| result[key] = value; | ||
| } | ||
| return result; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
_box.clear()and_box.putAll()are two separate async operations. If the app is terminated (crash, force-close, OS kill) between the two calls, the box is left empty and all pin data is permanently lost. A safer pattern is to write the new state first and then remove stale keys, so there is never a window where valid data has been erased but the replacement has not yet landed.