-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
05ac25a
commit 83e9bcd
Showing
5 changed files
with
114 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import 'package:async/async.dart'; | ||
import 'package:flutter/material.dart'; | ||
|
||
abstract class Command<T> extends ChangeNotifier { | ||
Result<T>? _result; | ||
|
||
bool _running = false; | ||
|
||
Command(); | ||
|
||
bool get isError => result?.asError != null; | ||
|
||
bool get isOk => result?.asValue != null; | ||
|
||
Result<T>? get result => _result; | ||
|
||
bool get running => _running; | ||
|
||
Future<void> _execute(action) async { | ||
if (running) return; | ||
|
||
_result = null; | ||
_running = true; | ||
notifyListeners(); | ||
|
||
try { | ||
_result = await action(); | ||
} catch (e) { | ||
_result = Result.error(e); | ||
} finally { | ||
_running = false; | ||
notifyListeners(); | ||
} | ||
} | ||
} | ||
|
||
class Command0<T> extends Command<T> { | ||
final Future<Result<T>> Function() action; | ||
|
||
Command0(this.action); | ||
|
||
Future<Result<T>> execute() async { | ||
await _execute(action); | ||
|
||
return _result!; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters