diff options
author | uvok | 2025-08-02 10:25:25 +0200 |
---|---|---|
committer | uvok | 2025-08-02 10:25:25 +0200 |
commit | c74b7197d7e120a63733dbcf3326097aacbad795 (patch) | |
tree | b812f49bc419edb46139c4d4408b7ea6af1a7eaf /lib/view_model/badge_motive_view_model.dart | |
parent | 4d9f24b89c30eb7829797d514aeaa25cfee1024f (diff) |
bmvm: Add error text, "safe action"
Diffstat (limited to 'lib/view_model/badge_motive_view_model.dart')
-rw-r--r-- | lib/view_model/badge_motive_view_model.dart | 33 |
1 files changed, 14 insertions, 19 deletions
diff --git a/lib/view_model/badge_motive_view_model.dart b/lib/view_model/badge_motive_view_model.dart index 28acf42..64ba387 100644 --- a/lib/view_model/badge_motive_view_model.dart +++ b/lib/view_model/badge_motive_view_model.dart @@ -24,6 +24,7 @@ class BadgeMotiveViewModel extends ChangeNotifier { final BadgeMotiveSelection _motivSelect; List<BadgeMotive> _motives = []; bool _busy = false; + String? errorMessage; BadgeMotiveViewModel({required BadgeMotiveSelection motivSelect}) : _motivSelect = motivSelect; @@ -33,41 +34,35 @@ class BadgeMotiveViewModel extends ChangeNotifier { BadgeMotive? currentMotive; Future<void> updateMotives() async { - if (_busy) return; - _busy = true; - notifyListeners(); - - try { + await safeAction(() async { _motives = await _motivSelect.getMotives(); - } finally { - _busy = false; - notifyListeners(); - } + }); } Future<void> setMotive(BadgeMotive motive) async { - if (_busy) return; - _busy = true; - notifyListeners(); - - try { + await safeAction(() async { logger.t(">Set motive to ${motive.id}"); await _motivSelect.setCurrentMotive(motive); logger.t("<Set motive to ${motive.id}"); currentMotive = motive; - } finally { - _busy = false; - notifyListeners(); - } + }); } Future<void> getCurrentMotive() async { + await safeAction(() async { + currentMotive = await _motivSelect.getCurrentMotive(); + }); + } + + Future<void> safeAction(Future<void> Function() action) async { if (_busy) return; _busy = true; notifyListeners(); try { - currentMotive = await _motivSelect.getCurrentMotive(); + await action(); + } on Exception catch (e) { + errorMessage = "${e.runtimeType}: ${e.toString()}"; } finally { _busy = false; notifyListeners(); |