From 17737e7c75bf234fefdf09782f61e0d07de65a19 Mon Sep 17 00:00:00 2001 From: Rodrigo Rafael Date: Thu, 12 Dec 2024 14:25:34 -0300 Subject: [PATCH 1/4] added getters in command --- lib/src/command.dart | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/src/command.dart b/lib/src/command.dart index d897b3a..f508100 100644 --- a/lib/src/command.dart +++ b/lib/src/command.dart @@ -45,6 +45,14 @@ 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; + @override CommandState get value => _value; @@ -53,7 +61,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 +88,8 @@ 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 +103,7 @@ abstract class Command extends ChangeNotifier // .map(SuccessCommand.new) .mapError(FailureCommand.new) .fold(identity, identity); - if (value is RuningCommand) { + if (isRunning) { _setValue(newValue); } } From ac6ea4bdf2d6c2932cece7d6ff5881f5fe45e2db Mon Sep 17 00:00:00 2001 From: Rodrigo Rafael Date: Thu, 12 Dec 2024 15:12:04 -0300 Subject: [PATCH 2/4] added isFailure and updated README.me --- README.md | 14 +++++++------- lib/src/command.dart | 5 +++-- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 6e3f763..78a59cd 100644 --- a/README.md +++ b/README.md @@ -68,10 +68,10 @@ final fetchGreetingCommand = Command0( ); fetchGreetingCommand.addListener(() { - if (fetchGreetingCommand.value is SuccessCommand) { + if (fetchGreetingCommand.value.isSuccess) { final result = (fetchGreetingCommand.value as SuccessCommand).value; print('Success: $result'); - } else if (fetchGreetingCommand.value is FailureCommand) { + } else if (fetchGreetingCommand.value.isFailure) { final error = (fetchGreetingCommand.value as FailureCommand).error; print('Failure: $error'); } @@ -97,10 +97,10 @@ final calculateSquareCommand = Command1( ); calculateSquareCommand.addListener(() { - if (calculateSquareCommand.value is SuccessCommand) { + if (calculateSquareCommand.value.isSuccess) { final result = (calculateSquareCommand.value as SuccessCommand).value; print('Square: $result'); - } else if (calculateSquareCommand.value is FailureCommand) { + } else if (calculateSquareCommand.value.isFailure) { final error = (calculateSquareCommand.value as FailureCommand).error; print('Error: $error'); } @@ -131,11 +131,11 @@ Widget build(BuildContext context) { ValueListenableBuilder>( valueListenable: loginCommand, builder: (context, state, child) { - if (state is RunningCommand) { + if (state.isRunning) { return CircularProgressIndicator(); - } else if (state is SuccessCommand) { + } else if (state.isSuccess) { return Text('Login Successful!'); - } else if (state is FailureCommand) { + } else if (state.isFailure) { return Text('Login Failed: ${(state as FailureCommand).error}'); } return ElevatedButton( diff --git a/lib/src/command.dart b/lib/src/command.dart index f508100..7a29c5c 100644 --- a/lib/src/command.dart +++ b/lib/src/command.dart @@ -53,6 +53,8 @@ abstract class Command extends ChangeNotifier // bool get isSuccess => value is SuccessCommand; + bool get isFailure => value is FailureCommand; + @override CommandState get value => _value; @@ -88,8 +90,7 @@ abstract class Command extends ChangeNotifier // /// /// If the command is cancelled during execution, the result is ignored. Future _execute(CommandAction0 action) async { - if (isRunning) - return; // Prevent multiple concurrent executions. + if (isRunning) return; // Prevent multiple concurrent executions. _setValue(RuningCommand()); Result? result; From e7b5d6e0e613e8d9e8f9534056b9626f8df1ed77 Mon Sep 17 00:00:00 2001 From: Rodrigo Rafael Date: Thu, 12 Dec 2024 16:40:56 -0300 Subject: [PATCH 3/4] updated readme with getters --- README.md | 47 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 78a59cd..e859650 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 was 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 @@ -68,10 +101,10 @@ final fetchGreetingCommand = Command0( ); fetchGreetingCommand.addListener(() { - if (fetchGreetingCommand.value.isSuccess) { + if (fetchGreetingCommand.value is SuccessCommand) { final result = (fetchGreetingCommand.value as SuccessCommand).value; print('Success: $result'); - } else if (fetchGreetingCommand.value.isFailure) { + } else if (fetchGreetingCommand.value is FailureCommand) { final error = (fetchGreetingCommand.value as FailureCommand).error; print('Failure: $error'); } @@ -97,10 +130,10 @@ final calculateSquareCommand = Command1( ); calculateSquareCommand.addListener(() { - if (calculateSquareCommand.value.isSuccess) { + if (calculateSquareCommand.value is SuccessCommand) { final result = (calculateSquareCommand.value as SuccessCommand).value; print('Square: $result'); - } else if (calculateSquareCommand.value.isFailure) { + } else if (calculateSquareCommand.value is FailureCommand) { final error = (calculateSquareCommand.value as FailureCommand).error; print('Error: $error'); } @@ -131,11 +164,11 @@ Widget build(BuildContext context) { ValueListenableBuilder>( valueListenable: loginCommand, builder: (context, state, child) { - if (state.isRunning) { + if (state is RunningCommand) { return CircularProgressIndicator(); - } else if (state.isSuccess) { + } else if (state is SuccessCommand) { return Text('Login Successful!'); - } else if (state.isFailure) { + } else if (state is FailureCommand) { return Text('Login Failed: ${(state as FailureCommand).error}'); } return ElevatedButton( From abfa01b58442a6b4d523ecb181267cba4075fbb7 Mon Sep 17 00:00:00 2001 From: Rodrigo Rafael Date: Thu, 12 Dec 2024 16:43:20 -0300 Subject: [PATCH 4/4] fix README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e859650..2f1588c 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,7 @@ To simplify state management and improve code readability, the following getters bool get isSuccess => value is SuccessCommand; ``` -- **`isFailure`**: Checks if the command execution was failed. +- **`isFailure`**: Checks if the command execution failed. ```dart bool get isFailure => value is FailureCommand; ```