Since _deleteBooking() is an action callback called from Command._execute method, which in turn has it's own try...finally block with notifyListeners(), theres no need to duplicate this code in action, right?
|
Future<Result<void>> _deleteBooking(int id) async { |
|
try { |
|
final resultDelete = await _bookingRepository.delete(id); |
|
switch (resultDelete) { |
|
case Ok<void>(): |
|
_log.fine('Deleted booking $id'); |
|
case Error<void>(): |
|
_log.warning('Failed to delete booking $id', resultDelete.error); |
|
return resultDelete; |
|
} |
|
|
|
// After deleting the booking, we need to reload the bookings list. |
|
// BookingRepository is the source of truth for bookings. |
|
final resultLoadBookings = await _bookingRepository.getBookingsList(); |
|
switch (resultLoadBookings) { |
|
case Ok<List<BookingSummary>>(): |
|
_bookings = resultLoadBookings.value; |
|
_log.fine('Loaded bookings'); |
|
case Error<List<BookingSummary>>(): |
|
_log.warning('Failed to load bookings', resultLoadBookings.error); |
|
return resultLoadBookings; |
|
} |
|
|
|
return resultLoadBookings; |
|
} finally { |
|
notifyListeners(); |
|
} |
|
} |
|
Future<void> _execute(CommandAction0<T> action) async { |
|
// Ensure the action can't launch multiple times. |
|
// e.g. avoid multiple taps on button |
|
if (_running) return; |
|
|
|
// Notify listeners. |
|
// e.g. button shows loading state |
|
_running = true; |
|
_result = null; |
|
notifyListeners(); |
|
|
|
try { |
|
_result = await action(); |
|
} finally { |
|
_running = false; |
|
notifyListeners(); |
|
} |
|
} |
Since _deleteBooking() is an action callback called from Command._execute method, which in turn has it's own try...finally block with notifyListeners(), theres no need to duplicate this code in action, right?
samples/compass_app/app/lib/ui/home/view_models/home_viewmodel.dart
Lines 67 to 94 in 460d6f9
samples/compass_app/app/lib/utils/command.dart
Lines 53 to 70 in 460d6f9