diff options
author | uvok | 2025-08-01 15:07:26 +0200 |
---|---|---|
committer | uvok | 2025-08-01 15:07:26 +0200 |
commit | 220eae3eb135eac5b03122d95d6fbc8c3bde2513 (patch) | |
tree | a67cc7ef330f7f4ea18442ac2e15e3bde8ad2d75 | |
parent | 5c1ffb1caa83681b9bb128841c6919ccfc1823e7 (diff) |
Add proper selection/setting motive
-rw-r--r-- | lib/model/motive_selection/mock_badge_motive_selection.dart | 3 | ||||
-rw-r--r-- | lib/view_model/badge_motive_view_model.dart | 22 | ||||
-rw-r--r-- | lib/widgets/badge_motive_list.dart | 15 |
3 files changed, 32 insertions, 8 deletions
diff --git a/lib/model/motive_selection/mock_badge_motive_selection.dart b/lib/model/motive_selection/mock_badge_motive_selection.dart index 6a4cff0..572da2a 100644 --- a/lib/model/motive_selection/mock_badge_motive_selection.dart +++ b/lib/model/motive_selection/mock_badge_motive_selection.dart @@ -27,7 +27,6 @@ class MockBadgeMotiveSelection implements BadgeMotiveSelection { @override Future<BadgeMotive> getCurrentMotive() async { - templates.add(BadgeMotive(templates.length, "foo")); return _currentMotive; } @@ -38,7 +37,7 @@ class MockBadgeMotiveSelection implements BadgeMotiveSelection { @override Future<void> setCurrentMotive(BadgeMotive motive) async { - return await Future.delayed(Duration(milliseconds: 100), () { + return await Future.delayed(Duration(milliseconds: 300), () { _currentMotive = motive; }); } diff --git a/lib/view_model/badge_motive_view_model.dart b/lib/view_model/badge_motive_view_model.dart index bf2a910..6746827 100644 --- a/lib/view_model/badge_motive_view_model.dart +++ b/lib/view_model/badge_motive_view_model.dart @@ -14,18 +14,40 @@ // along with this program. If not, see <https://www.gnu.org/licenses/>. import 'package:flutter/foundation.dart'; +import 'package:logger/logger.dart'; import 'package:uvok_epaper_badge/model/badge_motive.dart'; import 'package:uvok_epaper_badge/model/motive_selection/badge_motive_selection.dart'; import 'package:uvok_epaper_badge/model/motive_selection/mock_badge_motive_selection.dart'; +Logger logger = Logger(); + class BadgeMotiveViewModel extends ChangeNotifier { final BadgeMotiveSelection _motivSelect = MockBadgeMotiveSelection(); List<BadgeMotive> _motives = []; + bool _busy = false; + bool get allowSelection => !_busy; List<BadgeMotive> get motives => _motives; Future<void> updateMotives() async { + if (_busy) return; + _busy = true; + notifyListeners(); + _motives = await _motivSelect.getMotives(); + _busy = false; + notifyListeners(); + } + + Future<void> setMotive(BadgeMotive motive) async { + if (_busy) return; + + _busy = true; + notifyListeners(); + logger.t(">Set motive to ${motive.id}"); + await _motivSelect.setCurrentMotive(motive); + logger.t("<Set motive to ${motive.id}"); + _busy = false; notifyListeners(); } } diff --git a/lib/widgets/badge_motive_list.dart b/lib/widgets/badge_motive_list.dart index 1da85fa..e59eeeb 100644 --- a/lib/widgets/badge_motive_list.dart +++ b/lib/widgets/badge_motive_list.dart @@ -55,12 +55,15 @@ class _BadgeMotiveListState extends State<BadgeMotiveList> { title: Text(item.description), selectedTileColor: mytheme.primaryColorLight, selectedColor: Colors.black, - onTap: () { - setState(() { - _selectedIndex = index; - }); - widget.onItemSelected(item); - }, + onTap: !widget._motiveVM.allowSelection + ? null + : () async { + setState(() { + _selectedIndex = index; + }); + widget.onItemSelected(item); + await widget._motiveVM.setMotive(item); + }, selected: _selectedIndex == index, ); }, |