-
Notifications
You must be signed in to change notification settings - Fork 14
Add strategy and folder pinning #75
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
njiedev
wants to merge
8
commits into
SunkenInTime:main
Choose a base branch
from
njiedev: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
8 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 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,57 @@ | ||
| 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); | ||
|
|
||
| /// Tracks which strategies/folders are pinned to the home screen. | ||
| /// | ||
| /// Stored as a Hive box keyed by the item's id, with the pin timestamp | ||
| /// (milliseconds since epoch) as the value. The timestamp gives us ordering. | ||
| /// State is a `Map<String, int>` of id -> pinnedAt. | ||
| 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); | ||
|
|
||
| /// Pinned ids, most recently pinned first. | ||
| List<String> pinnedIdsByRecency() { | ||
| final entries = state.entries.toList() | ||
| ..sort((a, b) => b.value.compareTo(a.value)); | ||
| return entries.map((e) => e.key).toList(); | ||
| } | ||
|
|
||
| Future<void> togglePin(String id) async { | ||
| if (isPinned(id)) { | ||
| await removePin(id); | ||
| return; | ||
| } | ||
| final now = DateTime.now().millisecondsSinceEpoch; | ||
| await _box.put(id, now); | ||
| state = {...state, id: now}; | ||
| } | ||
|
|
||
| Future<void> removePin(String id) async { | ||
| if (!isPinned(id)) return; | ||
| await _box.delete(id); | ||
| state = {...state}..remove(id); | ||
| } | ||
|
|
||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import 'dart:io'; | ||
|
|
||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:flutter_riverpod/flutter_riverpod.dart'; | ||
| import 'package:hive_ce/hive.dart'; | ||
| import 'package:icarus/const/hive_boxes.dart'; | ||
| import 'package:icarus/providers/pinned_items_provider.dart'; | ||
|
|
||
| void main() { | ||
| TestWidgetsFlutterBinding.ensureInitialized(); | ||
|
|
||
| late Directory tempDir; | ||
| late ProviderContainer container; | ||
|
|
||
| setUp(() async { | ||
| tempDir = await Directory.systemTemp.createTemp('icarus-pins-'); | ||
| Hive.init(tempDir.path); | ||
| await Hive.openBox<int>(HiveBoxNames.pinnedItemsBox); | ||
| container = ProviderContainer(); | ||
| }); | ||
|
|
||
| tearDown(() async { | ||
| container.dispose(); | ||
| await Hive.close(); | ||
| if (await tempDir.exists()) { | ||
| await tempDir.delete(recursive: true); | ||
| } | ||
| }); | ||
|
|
||
| test('pinning an id makes it pinned, toggling again unpins', () async { | ||
| final notifier = container.read(pinnedItemsProvider.notifier); | ||
|
|
||
| expect(notifier.isPinned('a'), false); | ||
|
|
||
| await notifier.togglePin('a'); | ||
| expect(notifier.isPinned('a'), true); | ||
|
|
||
| await notifier.togglePin('a'); | ||
| expect(notifier.isPinned('a'), false); | ||
| }); | ||
|
|
||
| test('pinnedIdsByRecency returns most-recently-pinned first', () async { | ||
| final notifier = container.read(pinnedItemsProvider.notifier); | ||
|
|
||
| await notifier.togglePin('first'); | ||
| await Future<void>.delayed(const Duration(milliseconds: 5)); | ||
| await notifier.togglePin('second'); | ||
|
|
||
| expect(notifier.pinnedIdsByRecency(), ['second', 'first']); | ||
| }); | ||
|
|
||
| test('removePin is a no-op when the id is not pinned', () async { | ||
| final notifier = container.read(pinnedItemsProvider.notifier); | ||
|
|
||
| await notifier.removePin('missing'); // should not throw | ||
| expect(notifier.isPinned('missing'), false); | ||
| }); | ||
| } |
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.
ref.readinsidebuild()for state accesspinnedIdsByRecency()is called viaref.read(pinnedItemsProvider.notifier)on line 183 insidebuild(). Riverpod'sref.readtakes a snapshot at call time and does not subscribe, so in the narrow window between theref.watchon line 180 and thisref.read, the notifier state could theoretically change (e.g. a concurrent async pin toggle completing). In practice this meanspinned(line 180) and the snapshot used bypinnedIdsByRecency()could differ. Sincepinnedis already the fullMap<String, int>state, you can derive the sorted order directly from it — noref.readneeded.