My approach below feels way to complicated for a simple thing I am trying to achive:
I have a list of Task
s that is managed by a TaskBloc
. The UI lists all tasks and provides an execute
button per task. For each click on that button I want to create and store an Action
(basically the timestamp when the task-execution happened) and show a spinner while the action is created. I have an ActionBloc
that manages the actions (e.g. creation or getting the history per task).
I am confused how to setup the communication between the BLoCs.
This is my approach so far.
The ActionsState
just holds a list of all stored actions.
class ActionsState extends Equatable {
final List<Action> actions;
// ... copyWith, constructors etc.
}
Action
is a simple PODO class holding an id
and timestamp
.
The ActionsBloc
is capable of creating Action
s in response to it's ActionCreationStarted
event (holding a int taskId
). Since the Action
creation is performed in an async isolate there are also events ActionCreationSucceeded
and ActionCreationFailed
that are added by the isolate once the request finished. Both hold the Action
that was either created or whose creation failed.
The TaskState
:
class TaskState extends Equatable {
final Map<int, Task> tasks;
// ... copyWith, constructors, etc.
I added a executeStatus
to the Task
model to keep track of the status of the create request in the task list (a specific task cannot be executed multiple times in parallel, but only sequentially while different tasks can be executed in parallel):
enum Status { initial, loading, success, error }
class Task extends Equatable {
final int id;
final Status executeStatus;
// ...
}
I added events for the TaskBloc
:
class TaskExecutionStarted extends TaskEvent {
final int taskId;
// ...
}
class TaskExecutionSucceeded extends TaskEvent {
final int taskId;
// ...
}
class TaskExecutionFailed extends TaskEvent {
final int taskId;
// ...
}
In the TaskBloc
I implemented the mapEventToState
for the new events to set the task status depending on the event, e.g. for TaskExecutionStarted
:
Stream<TaskState> mapEventToState(TaskEvent event) async* {
// ...
if (event is TaskExecutionStarted) {
final taskId = event.taskId;
Task task = state.tasks[taskId]!;
yield state.copyWith(
tasks: {
...state.tasks,
taskId: task.copyWith(executeStatus: Status.loading),
},
);
}
// ...
}
So far this enables the UI to show a spinner per Task but the ActionBloc
does not yet know that it should record a new Action
for that task and the TaskBloc
does not know when to stop showing the spinner.
PROBLEM
Now the part where I am lost is that I need to actually trigger the ActionBloc
to create an action and get an TaskExecutionSucceeded
(or ...Failed
) event afterwards. I thought about using a listener on the ActionsBloc
, but it only provides the state and not the events of the ActionsBloc
(I would need to react to the ActionCreationSucceeded
event, but listening to events of an other bloc feels like an anti-pattern (?!) and I do not even know how to set it up).
The core of the problem is, that I may listen on the ActionsBloc
state but I don't know how to distinguish for which actions of the state I would need to trigger a TaskExecutionSucceeded
event.
Anyway, I gave the TaskBloc
a reference to ActionsBloc
:
class TaskBloc extends Bloc<TaskEvent, TaskState> {
final ActionsBloc actionsBloc;
late final StreamSubscription actionsSubscription;
// ...
TaskBloc({
// ...
required this.actionsBloc,
}) : super(TaskState.initial()) {
actionsSubscription = actionsBloc.listen((state) {
/* ... ??? ... Here I don't know how to distinguish for which actions of the state
I would somehow need to trigger a `TaskExecutionSucceeded` event. */
});
};
// ...
}
For the sake of completeness, triggering creation of the Action
is simple by adding the corresponding event to the ActionBloc
as response to the TaskExecutionStarted
:
Stream<TaskState> mapEventToState(TaskEvent event) async* {
// ...
// ... set executeStatus: Status.loading as shown above ...
// trigger creating a new action
actionsBloc.add(ActionCreationStarted(taskId: taskId));
// ...
Of course I aim at clear separation of concerns, single source of truth and other potential sources for accidential complexity regarding app state structure - but overall this approach (which still has said problem unsolved before working) feels way to complicated just to store a timestamp per action of a task and keep track of the action-creation-request.
I appreciate that you read so far (!) and I am very happy about hints towards a clean architecture for that use case.
ActionsBloc
but not to theTaskBloc
). Users can rate and comment on tasks, select their favorites etc which belongs to theTasksBloc
. Even if related, putting all in one BLoC is way to much stuff in a single BLoC I guess.