From c74b7197d7e120a63733dbcf3326097aacbad795 Mon Sep 17 00:00:00 2001 From: uvok Date: Sat, 2 Aug 2025 10:25:25 +0200 Subject: bmvm: Add error text, "safe action" --- lib/view_model/badge_motive_view_model.dart | 33 ++++++++++++----------------- 1 file changed, 14 insertions(+), 19 deletions(-) (limited to 'lib/view_model/badge_motive_view_model.dart') 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 _motives = []; bool _busy = false; + String? errorMessage; BadgeMotiveViewModel({required BadgeMotiveSelection motivSelect}) : _motivSelect = motivSelect; @@ -33,41 +34,35 @@ class BadgeMotiveViewModel extends ChangeNotifier { BadgeMotive? currentMotive; Future updateMotives() async { - if (_busy) return; - _busy = true; - notifyListeners(); - - try { + await safeAction(() async { _motives = await _motivSelect.getMotives(); - } finally { - _busy = false; - notifyListeners(); - } + }); } Future 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(" getCurrentMotive() async { + await safeAction(() async { + currentMotive = await _motivSelect.getCurrentMotive(); + }); + } + + Future safeAction(Future 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(); -- cgit v1.2.3