-
Notifications
You must be signed in to change notification settings - Fork 245
feat: added a custom slot provider option for user to select the slot no and rearrange the order #1494
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
base: development
Are you sure you want to change the base?
feat: added a custom slot provider option for user to select the slot no and rearrange the order #1494
Changes from 10 commits
7f41ce6
360374d
6abc751
88d1bee
d340a76
f692295
07ed548
ac6bf53
9013add
dfeab6f
ebe65ef
9221ee8
d920947
b3a4ceb
e6d31ad
087f73a
c885e5e
a2e9891
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,27 +1,95 @@ | ||
| import 'package:flutter/material.dart'; | ||
|
|
||
| extension FirstOrNullExtension<E> on Iterable<E> { | ||
| E? get firstOrNull => isEmpty ? null : first; | ||
| } | ||
|
|
||
| class BadgeSlotProvider with ChangeNotifier { | ||
| final Set<String> _selectedBadges = {}; | ||
| // Maps selected badge key -> assigned slot number (1..8) | ||
| final Map<String, int> _badgeKeyToSlot = {}; | ||
|
|
||
| // Available slot numbers pool | ||
| final Set<int> _availableSlots = {1, 2, 3, 4, 5, 6, 7, 8}; | ||
|
|
||
| static const int maxSelectedBadges = 8; | ||
|
|
||
| Set<String> get selectedBadges => _selectedBadges; | ||
| Set<String> get selectedBadges => _badgeKeyToSlot.keys.toSet(); | ||
|
|
||
| bool isSelected(String badgeKey) => _selectedBadges.contains(badgeKey); | ||
| bool isSelected(String badgeKey) => _badgeKeyToSlot.containsKey(badgeKey); | ||
|
|
||
| bool get canSelectMore => _selectedBadges.length < maxSelectedBadges; | ||
| bool get canSelectMore => | ||
| _badgeKeyToSlot.length < maxSelectedBadges && _availableSlots.isNotEmpty; | ||
|
|
||
| int? getSlotForBadge(String badgeKey) => _badgeKeyToSlot[badgeKey]; | ||
|
|
||
| List<String> getSelectionsOrderedBySlot() { | ||
| final entries = _badgeKeyToSlot.entries.toList() | ||
| ..sort((a, b) => a.value.compareTo(b.value)); | ||
| return entries.map((e) => e.key).toList(); | ||
| } | ||
|
|
||
| /// ✅ Updated reorderSlots to perform push-down insertion | ||
nope3472 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| void reorderSlots(String fromBadgeKey, String toBadgeKey) { | ||
| if (!_badgeKeyToSlot.containsKey(fromBadgeKey) || | ||
| !_badgeKeyToSlot.containsKey(toBadgeKey)) return; | ||
|
|
||
| final orderedKeys = getSelectionsOrderedBySlot(); | ||
| orderedKeys.remove(fromBadgeKey); | ||
| final toIndex = orderedKeys.indexOf(toBadgeKey); | ||
|
|
||
| orderedKeys.insert(toIndex, fromBadgeKey); | ||
|
|
||
| _badgeKeyToSlot.clear(); | ||
| _availableSlots | ||
| ..clear() | ||
| ..addAll({1, 2, 3, 4, 5, 6, 7, 8}); | ||
|
|
||
| for (int i = 0; i < orderedKeys.length; i++) { | ||
| final slot = i + 1; | ||
| _badgeKeyToSlot[orderedKeys[i]] = slot; | ||
| _availableSlots.remove(slot); | ||
| } | ||
|
|
||
| notifyListeners(); | ||
| } | ||
|
|
||
| void toggleSelection(String badgeKey) { | ||
| if (_selectedBadges.contains(badgeKey)) { | ||
| _selectedBadges.remove(badgeKey); | ||
| notifyListeners(); | ||
| } else if (_selectedBadges.length < maxSelectedBadges) { | ||
| _selectedBadges.add(badgeKey); | ||
| if (_badgeKeyToSlot.containsKey(badgeKey)) { | ||
| // Unselect: free its slot | ||
| final freedSlot = _badgeKeyToSlot.remove(badgeKey); | ||
| if (freedSlot != null) { | ||
| _availableSlots.add(freedSlot); | ||
| } | ||
| notifyListeners(); | ||
| return; | ||
| } | ||
|
|
||
| if (_badgeKeyToSlot.length >= maxSelectedBadges || | ||
| _availableSlots.isEmpty) { | ||
| return; // Cannot select more | ||
| } | ||
|
|
||
| // Assign smallest available slot | ||
| final smallest = _availableSlots.reduce((a, b) => a < b ? a : b); | ||
|
||
| _availableSlots.remove(smallest); | ||
| _badgeKeyToSlot[badgeKey] = smallest; | ||
| notifyListeners(); | ||
| } | ||
|
|
||
| void clearSelections() { | ||
| _selectedBadges.clear(); | ||
| _badgeKeyToSlot.clear(); | ||
| _availableSlots | ||
| ..clear() | ||
| ..addAll({1, 2, 3, 4, 5, 6, 7, 8}); | ||
| notifyListeners(); | ||
| } | ||
|
|
||
| bool canTransfer(String badgeKey) { | ||
| final slot = _badgeKeyToSlot[badgeKey]; | ||
| return slot != null && slot <= 8; | ||
sourcery-ai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| List<String> getTransferableBadges() { | ||
| return getSelectionsOrderedBySlot().take(8).toList(); | ||
| } | ||
| } | ||
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.
The
FirstOrNullExtensionis defined but never used in the codebase. Consider removing it to reduce code clutter, or use it if it was intended for future functionality.