From e942459f64b2a864f2e16a413fe069e080898378 Mon Sep 17 00:00:00 2001 From: Niels Rijnberg Date: Fri, 25 Apr 2025 10:42:45 +0200 Subject: [PATCH] Add refresh stream mixin to the toolkit --- example/pubspec.lock | 2 +- lib/common/mixins/refresh_stream_mixin.dart | 71 +++++++++++++++++++++ lib/dcc_toolkit.dart | 1 + 3 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 lib/common/mixins/refresh_stream_mixin.dart diff --git a/example/pubspec.lock b/example/pubspec.lock index 758d8c6..c54fc5f 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -212,7 +212,7 @@ packages: path: ".." relative: true source: path - version: "0.0.11" + version: "0.0.13" equatable: dependency: transitive description: diff --git a/lib/common/mixins/refresh_stream_mixin.dart b/lib/common/mixins/refresh_stream_mixin.dart new file mode 100644 index 0000000..eed4427 --- /dev/null +++ b/lib/common/mixins/refresh_stream_mixin.dart @@ -0,0 +1,71 @@ +import 'dart:async'; + +import 'package:flutter/foundation.dart'; + +/// A mixin that provides a stream of update events. +/// +/// This mixin is used to notify listeners when an update occurs. +/// It is used in the [RefreshStreamMixin] class. +/// +/// Example: +/// ```dart +/// class MyRepository with RefreshNotifierMixin { +/// void create() { +/// // Execute create logic +/// refreshListeners(RefreshType.create); +/// } +/// +/// void update() { +/// // Execute update logic +/// refreshListeners(RefreshType.update); +/// } +/// } +/// ``` +/// +/// Then you can listen to the refresh stream like this: +/// ```dart +/// myRepository.listen((event) { +/// switch (event) { +/// case RefreshType.create: +/// // Execute create logic +/// break; +/// case RefreshType.update: +/// // Execute update logic +/// break; +/// } +/// }); +/// ``` +mixin RefreshStreamMixin { + final _refreshStream = StreamController.broadcast(); + + /// Adds a new refresh event to the stream. + @protected + void refreshListeners(RefreshType event) => _refreshStream.add(event); + + /// Listener function for the refresh stream. + StreamSubscription listen(void Function(RefreshType event) events) { + return _refreshStream.stream.listen(events); + } + + /// Closes the refresh stream. + Future close() async { + await _refreshStream.close(); + } +} + +/// The type of refresh event. +enum RefreshType { + /// The [RefreshType.create] event should be used when a new object is created. + create, + + /// The [RefreshType.update] event should be used when an object is updated. + update, + + /// The [RefreshType.delete] event should be used when an object is deleted. + delete, + + /// The [RefreshType.custom] event should be used when a custom event occurs, + /// if the refresh should trigger different logic than a normal create/update/delete event. + /// This way you can separate behaviour in the place where you are listening to the events + custom, +} diff --git a/lib/dcc_toolkit.dart b/lib/dcc_toolkit.dart index 18e62ac..53a744c 100644 --- a/lib/dcc_toolkit.dart +++ b/lib/dcc_toolkit.dart @@ -4,6 +4,7 @@ export 'common/extensions/build_context.dart'; export 'common/extensions/color.dart'; export 'common/extensions/iterable.dart'; export 'common/extensions/text_theme.dart'; +export 'common/mixins/refresh_stream_mixin.dart'; export 'common/result/result.dart'; export 'common/type_defs.dart'; export 'logger/bolt_logger.dart';