diff --git a/README.md b/README.md index 6e3f763..2f1588c 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,39 @@ The state updates automatically as the command executes: --- +## Getters for State Checks + +To simplify state management and improve code readability, the following getters are available: + +- **`isIdle`**: Checks if the command is in the idle state. + ```dart + bool get isIdle => value is IdleCommand; + ``` + +- **`isRunning`**: Checks if the command is currently running. + ```dart + bool get isRunning => value is RunningCommand; + ``` + +- **`isCancelled`**: Checks if the command has been cancelled. + ```dart + bool get isCancelled => value is CancelledCommand; + ``` + +- **`isSuccess`**: Checks if the command execution was successful. + ```dart + bool get isSuccess => value is SuccessCommand; + ``` + +- **`isFailure`**: Checks if the command execution failed. + ```dart + bool get isFailure => value is FailureCommand; + ``` + +These getters allow you to write cleaner and more intuitive code when interacting with commands in your views or controllers. + +--- + ## Examples ### Example 1: Simple Command with No Arguments diff --git a/lib/src/command.dart b/lib/src/command.dart index d897b3a..7a29c5c 100644 --- a/lib/src/command.dart +++ b/lib/src/command.dart @@ -45,6 +45,16 @@ abstract class Command extends ChangeNotifier // /// The current state of the command. CommandState _value = IdleCommand(); + bool get isIdle => value is IdleCommand; + + bool get isRunning => value is RuningCommand; + + bool get isCancelled => value is CancelledCommand; + + bool get isSuccess => value is SuccessCommand; + + bool get isFailure => value is FailureCommand; + @override CommandState get value => _value; @@ -53,7 +63,7 @@ abstract class Command extends ChangeNotifier // /// If the command is in the [RuningCommand] state, the [onCancel] callback is invoked, /// and the state transitions to [CancelledCommand]. void cancel() { - if (value is RuningCommand) { + if (isRunning) { onCancel?.call(); _setValue(CancelledCommand()); } @@ -80,7 +90,7 @@ abstract class Command extends ChangeNotifier // /// /// If the command is cancelled during execution, the result is ignored. Future _execute(CommandAction0 action) async { - if (value is RuningCommand) return; // Prevent multiple concurrent executions. + if (isRunning) return; // Prevent multiple concurrent executions. _setValue(RuningCommand()); Result? result; @@ -94,7 +104,7 @@ abstract class Command extends ChangeNotifier // .map(SuccessCommand.new) .mapError(FailureCommand.new) .fold(identity, identity); - if (value is RuningCommand) { + if (isRunning) { _setValue(newValue); } }