diff options
| author | mirror <mirror@localhost> | 2026-11-22 08:41:00 +0900 |
|---|---|---|
| committer | mirror <mirror@localhost> | 2026-11-22 08:41:00 +0900 |
| commit | 449d8b74bd6d9dae98a07254cdfad677b920e7b0 (patch) | |
| tree | c131c3528c4d517f56e19b854a7453fedb097d40 /lib/model/selection.dart | |
| download | aves-src-master.tar.gz aves-src-master.zip | |
Diffstat (limited to 'lib/model/selection.dart')
| -rw-r--r-- | lib/model/selection.dart | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/lib/model/selection.dart b/lib/model/selection.dart new file mode 100644 index 0000000..fbb6014 --- /dev/null +++ b/lib/model/selection.dart @@ -0,0 +1,58 @@ +import 'package:flutter/foundation.dart'; + +class Selection<T> extends ChangeNotifier { + bool _isSelecting = false; + + bool get isSelecting => _isSelecting; + + final Set<T> _selectedItems = {}; + + Set<T> get selectedItems => _selectedItems; + + Selection() { + if (kFlutterMemoryAllocationsEnabled) ChangeNotifier.maybeDispatchObjectCreation(this); + } + + void browse() { + if (!_isSelecting) return; + _isSelecting = false; + notifyListeners(); + } + + void select() { + if (_isSelecting) return; + // clear selection on `select`, not on `browse`, so that + // the selection count is stable when transitioning to browse + clearSelection(); + _isSelecting = true; + notifyListeners(); + } + + bool isSelected(Iterable<T> items) => items.every(selectedItems.contains); + + void addToSelection(Iterable<T> items) { + if (items.isEmpty) return; + + select(); + _selectedItems.addAll(items); + notifyListeners(); + } + + void removeFromSelection(Iterable<T> items) { + if (items.isEmpty) return; + + _selectedItems.removeAll(items); + notifyListeners(); + } + + void clearSelection() { + _selectedItems.clear(); + notifyListeners(); + } + + void toggleSelection(T item) { + if (!_isSelecting) select(); + if (!_selectedItems.remove(item)) _selectedItems.add(item); + notifyListeners(); + } +} |
