Skip to content

Instantly share code, notes, and snippets.

@escamoteur
Last active May 2, 2019 06:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save escamoteur/7096002923fb3953f019bfc1776fd313 to your computer and use it in GitHub Desktop.
Save escamoteur/7096002923fb3953f019bfc1776fd313 to your computer and use it in GitHub Desktop.
RxCommandListener<ImageStorageRequest, ImageLocation> selectImageListener;
RxCommandListener<ChatEntry, void> deleteChatEntryListener;
@override
void initState() {
super.initState();
selectImageListener = RxCommandListener(
command: sl.get<ImageManager>().selectAndUploadImageCommand,
onValue: (imageLocation) async {
if (imageLocation == null) return;
sl.get<EventManager>().createChatEntryCommand(new ChatEntry(
event: widget.event,
isImage: true,
content: imageLocation.downloadUrl,
));
},
onIsBusy: () => JoCLoader.show(context),
onNotBusy: JoCLoader.hide,
onError: (ex) => showMessageDialog(context, 'Upload problem',
"We cannot upload your selected image at the moment. Please check your internet connection"));
deleteChatEntryListener = RxCommandListener(
command: sl.get<EventManager>().deleteChatEntryCommand,
onValue: (_) => setState(() => {}),
);
void dispose() {
selectImageListener?.dispose();
deleteChatEntryListener?.dispose();
}
////////////////////////////////////////////////////////
//compared to:
///////////////////////////////////////////////////////
StreamSubscription deleteChatEntrySubscription;
StreamSubscription _selectImageIsExecutingSubscription;
StreamSubscription _selectImageCommandSubscription;
StreamSubscription _selectImageErrorSubscription;
@override
void initState() {
super.initState();
_selectImageCommandSubscription = sl
.get<ImageManager>()
.selectAndUploadImageCommand
.listen((imageLocation) async {
if (imageLocation == null) return;
sl.get<EventManager>().createChatEntryCommand(new ChatEntry(
event: widget.event,
isImage: true,
content: imageLocation.downloadUrl,
));
});
_selectImageIsExecutingSubscription = sl
.get<ImageManager>()
.selectAndUploadImageCommand
.isExecuting
.listen((busy) {
if (busy) {
JoCLoader.show(context);
} else {
JoCLoader.hide();
}
});
_selectImageErrorSubscription = sl
.get<ImageManager>()
.selectAndUploadImageCommand
.thrownExceptions
.listen((ex) => showMessageDialog(context, 'Upload problem',
"We cannot upload your selected image at the moment. Please check your internet connection"));
deleteChatEntrySubscription = sl
.get<EventManager>()
.deleteChatEntryCommand
.listen((_) => setState(() => {}));
}
void dispose() {
_selectImageIsExecutingSubscription?.cancel();
_selectImageCommandSubscription?.cancel();
_selectImageErrorSubscription?.cancel();
deleteChatEntrySubscription?.cancel();
}
@escamoteur
Copy link
Author

This is the definition of an RxCommandListener

class RxCommandListener<TParam, TResult> {
  StreamSubscription valueSubscription;
  StreamSubscription resultsSubscription;
  StreamSubscription busyChangeSubscription;
  StreamSubscription busySubscription;
  StreamSubscription errorSubscription;
  StreamSubscription canExecuteStateSubscription;

  final RxCommand<TParam, TResult> command;

  final void Function(TResult value) onValue;
  final void Function(bool isBusy) onIsBusyChange;
  final void Function(Exception ex) onError;
  final void Function(bool state) onCanExecuteChange;
  final void Function(CommandResult<TResult> result) onResult;
  final void Function() onIsBusy;
  final void Function() onNotBusy;

  final Duration debounceDuration;

  RxCommandListener({
    @required
    this.command,
    this.onValue,
    this.onIsBusyChange,
    this.onIsBusy,
    this.onNotBusy,
    this.onError,
    this.onCanExecuteChange,
    this.onResult,
    this.debounceDuration,}
  ) 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment