LCOV - code coverage report
Current view: top level - components/button - foss_button_controller.dart Coverage Total Hit
Test: fossui coverage Lines: 100.0 % 16 16
Test Date: 2026-11-29 09:15:47 Functions: - 0 0

            Line data    Source code
       1              : part of 'foss_button.dart';
       2              : 
       3              : /// The interactive status a [FossButtonController] holds. The three states are
       4              : /// mutually exclusive: [idle] is the normal, enabled state.
       5              : enum FossButtonStatus {
       6              :   /// Enabled and ready. The normal resting state.
       7              :   idle,
       8              : 
       9              :   /// Running work: shows a spinner and does not respond to taps.
      10              :   loading,
      11              : 
      12              :   /// Switched off: the button does not respond to taps.
      13              :   disabled,
      14              : }
      15              : 
      16              : /// Drives a [FossButton] imperatively, so loading and disabled can be toggled
      17              : /// without rebuilding the button from new parameters. Pass one to a button's
      18              : /// `controller`; you own it and must [dispose] it.
      19              : ///
      20              : /// The button stays interactive only while the status is
      21              : /// [FossButtonStatus.idle] (and its `onPressed` is non-null). [run] is the
      22              : /// common path: it wraps an async action in a loading state that clears when
      23              : /// the action settles.
      24              : ///
      25              : /// ```dart
      26              : /// final controller = FossButtonController();
      27              : ///
      28              : /// FossButton(
      29              : ///   controller: controller,
      30              : ///   onPressed: () => controller.run(() => save()),
      31              : ///   child: const Text('Save'),
      32              : /// );
      33              : /// ```
      34              : class FossButtonController extends ChangeNotifier {
      35              :   /// Creates a controller, [idle] by default.
      36            2 :   FossButtonController([FossButtonStatus status = FossButtonStatus.idle])
      37              :     : _status = status;
      38              : 
      39              :   FossButtonStatus _status;
      40              : 
      41              :   /// The current status.
      42            4 :   FossButtonStatus get status => _status;
      43              : 
      44              :   /// Whether the status is [FossButtonStatus.loading].
      45            6 :   bool get isLoading => _status == FossButtonStatus.loading;
      46              : 
      47              :   /// Whether the button is interactive: the status is [FossButtonStatus.idle].
      48            3 :   bool get isEnabled => _status == FossButtonStatus.idle;
      49              : 
      50              :   /// Returns to the normal, enabled state.
      51            2 :   void idle() => _set(FossButtonStatus.idle);
      52              : 
      53              :   /// Shows a spinner and stops responding to taps.
      54            4 :   void loading() => _set(FossButtonStatus.loading);
      55              : 
      56              :   /// Switches the button off without removing its `onPressed`.
      57            2 :   void disable() => _set(FossButtonStatus.disabled);
      58              : 
      59              :   /// Runs [action] with the button in [FossButtonStatus.loading], returning to
      60              :   /// [FossButtonStatus.idle] when it settles, including on error. A no-op
      61              :   /// unless the status is [FossButtonStatus.idle], so overlapping runs and runs
      62              :   /// on a disabled button cannot race the status back to idle.
      63            1 :   Future<void> run(Future<void> Function() action) async {
      64            2 :     if (_status != FossButtonStatus.idle) return;
      65            1 :     loading();
      66              :     try {
      67            1 :       await action();
      68              :     } finally {
      69            1 :       idle();
      70              :     }
      71              :   }
      72              : 
      73            2 :   void _set(FossButtonStatus status) {
      74            4 :     if (_status == status) return;
      75            2 :     _status = status;
      76            2 :     notifyListeners();
      77              :   }
      78              : }
        

Generated by: LCOV version 2.0-1