This commit is contained in:
2025-03-31 12:44:48 +02:00
parent 92977dad28
commit 5553ded528
2 changed files with 18 additions and 5 deletions

View File

@@ -4,16 +4,26 @@ import 'dart:async';
/// A contrller used to control Futures/Streams to present them effeciantly. /// A contrller used to control Futures/Streams to present them effeciantly.
class AstromicPresenterController { class AstromicPresenterController {
late final Map<String, (Future Function()? fetch, int c)> _futures; late final Map<String, (Future Function()? fetch, int c)> _futures;
late final Map<String, Stream?> _streams; late final Map<String, Stream Function(Map<String, dynamic> args)?> _streams;
late final Map<String, StreamController<void>> _futureRefreshers; late final Map<String, StreamController<void>> _futureRefreshers;
AstromicPresenterController({ AstromicPresenterController({
Map<String, (Future Function()? fetch, int startCycle)> futures = const {}, Map<String, (Future Function()? fetch, int startCycle)> futures = const {},
Map<String, Stream?> streams = const {}, Map<String, Stream Function(Map<String, dynamic> args)?> streams = const {},
}) : _futures = futures.map((k, v) => MapEntry(k, (v.$1, v.$2))), }) : _futures = futures.map((k, v) => MapEntry(k, (v.$1, v.$2))),
_futureRefreshers = futures.map((k, v) => MapEntry(k, StreamController<void>.broadcast())), _futureRefreshers = futures.map((k, v) => MapEntry(k, StreamController<void>.broadcast())),
_streams = streams; _streams = streams;
Map<String,dynamic> providedArguments = {};
void setProvidedArguments(Map<String,dynamic> args){
providedArguments = args;
}
Map<String,dynamic> getProvidedArguments(){
return providedArguments;
}
/// Get the current cycle of this future ID. /// Get the current cycle of this future ID.
int getFutureCycle(String id) { int getFutureCycle(String id) {
@@ -32,7 +42,7 @@ class AstromicPresenterController {
Stream<T?>? getStream<T>(String id) { Stream<T?>? getStream<T>(String id) {
assert(_streams.containsKey(id), 'did you add a stream with this id?'); assert(_streams.containsKey(id), 'did you add a stream with this id?');
return _streams[id]! as Stream<T?>?; return _streams[id]!(getProvidedArguments()) as Stream<T?>?;
} }
/// Get the refresh stream of a future using it's ID. /// Get the refresh stream of a future using it's ID.

View File

@@ -19,6 +19,7 @@ class AstromicStreamPresenter<T> extends StatefulWidget {
//SECTION - Widget Arguments //SECTION - Widget Arguments
final AstromicPresenterController controller; final AstromicPresenterController controller;
final String id; final String id;
final Map<String,dynamic>? neededArguments;
// //
final Map<AstromicPresenterState, Widget Function(PresenterReturnModel<T?>? r)> stateBuilder; final Map<AstromicPresenterState, Widget Function(PresenterReturnModel<T?>? r)> stateBuilder;
final AstromicPresenterConfiguration? configuration; final AstromicPresenterConfiguration? configuration;
@@ -30,6 +31,7 @@ class AstromicStreamPresenter<T> extends StatefulWidget {
required this.id, required this.id,
required this.stateBuilder, required this.stateBuilder,
this.configuration, this.configuration,
this.neededArguments,
}); });
@override @override
@@ -122,7 +124,8 @@ class _AstromicStreamPresenterState<T> extends State<AstromicStreamPresenter<T>>
//S1 -- Method to reinitialize or update `_future` with a new instance //S1 -- Method to reinitialize or update `_future` with a new instance
void _initializeStream() { void _initializeStream() {
_stream = widget.controller.getStream<T?>(widget.id)?.asyncMap((dynamic result) => result); widget.controller.setProvidedArguments(widget.neededArguments ?? <String,dynamic>{});
_stream = widget.controller.getStream(widget.id)?.asyncMap((dynamic result) => result as T?);
} }
//!SECTION //!SECTION