From 83b77c05efef6cfcd5e45fc1de38fc39217cb2f3 Mon Sep 17 00:00:00 2001 From: AmirReza Jamali Date: Sat, 30 May 2026 18:46:13 +0330 Subject: [PATCH] Refactor card movement logic in BoardViewModel to improve error handling and success validation - Updated the handling of card movement results to differentiate between successful and unsuccessful API responses. - Implemented rollback of board state and error messaging for failed moves, ensuring a more robust user experience. - Enhanced the notification system to surface relevant error messages when card movements fail. --- lib/view_models/board_view_model.dart | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/view_models/board_view_model.dart b/lib/view_models/board_view_model.dart index c568cf1..ad6bcb0 100644 --- a/lib/view_models/board_view_model.dart +++ b/lib/view_models/board_view_model.dart @@ -104,10 +104,24 @@ class BoardViewModel with ChangeNotifier { newOrder: newOrder, ); - if (result is Error) { - _board = _board!.copyWith(columns: previousColumns); - notifyListeners(); + switch (result) { + case Ok(): + // HTTP 200 alone isn't success: the API can return + // {"success": false, "message": ...} (e.g. column over-limit, + // permission denied). Only keep the optimistic move when the body + // confirms success; otherwise roll back and surface the message. + if (result.value.success == true) { + return result; + } + _board = _board!.copyWith(columns: previousColumns); + notifyListeners(); + return Result.error( + Exception(result.value.message ?? 'Failed to move card'), + ); + case Error(): + _board = _board!.copyWith(columns: previousColumns); + notifyListeners(); + return result; } - return result; } }