refactor: extract BaseNotifier and HierarchicalEventMixin

This commit is contained in:
edde746
2026-02-06 17:01:56 +01:00
parent 8eca2051e5
commit 44eae4ef84
6 changed files with 121 additions and 139 deletions
+4 -24
View File
@@ -1,4 +1,4 @@
import 'dart:async';
import 'base_notifier.dart';
/// Types of library refresh events
enum LibraryRefreshType { collections, playlists }
@@ -7,27 +7,13 @@ enum LibraryRefreshType { collections, playlists }
///
/// Singleton pattern with reinitializable state. The controller is lazily
/// created and automatically recreated if disposed and later accessed.
class LibraryRefreshNotifier {
class LibraryRefreshNotifier extends BaseNotifier<LibraryRefreshType> {
static final LibraryRefreshNotifier _instance = LibraryRefreshNotifier._internal();
factory LibraryRefreshNotifier() => _instance;
LibraryRefreshNotifier._internal();
/// Unified stream controller (lazily created, reinitializable)
StreamController<LibraryRefreshType>? _controller;
/// Ensure controller exists (creates if null or closed)
StreamController<LibraryRefreshType> get _ensureController {
if (_controller == null || _controller!.isClosed) {
_controller = StreamController<LibraryRefreshType>.broadcast();
}
return _controller!;
}
/// Unified stream of all refresh events
Stream<LibraryRefreshType> get stream => _ensureController.stream;
/// Stream for collections tab (backward compatible)
Stream<void> get collectionsStream => stream.where((t) => t == LibraryRefreshType.collections).map((_) {});
@@ -36,17 +22,11 @@ class LibraryRefreshNotifier {
/// Notify that collections have changed
void notifyCollectionsChanged() {
_ensureController.add(LibraryRefreshType.collections);
notify(LibraryRefreshType.collections);
}
/// Notify that playlists have changed
void notifyPlaylistsChanged() {
_ensureController.add(LibraryRefreshType.playlists);
}
/// Dispose controller (can be reinitialized later by accessing stream)
void dispose() {
_controller?.close();
_controller = null;
notify(LibraryRefreshType.playlists);
}
}