refactor dashboard operation list e task list with applifecycle
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flux/features/tasks/data/task_repository.dart';
|
||||
import 'package:flux/features/tasks/models/task_model.dart';
|
||||
@@ -11,37 +12,41 @@ part 'dashboard_task_list_state.dart';
|
||||
|
||||
class DashboardTaskListCubit extends Cubit<DashboardTaskListState> {
|
||||
final TasksRepository _repository = GetIt.I.get<TasksRepository>();
|
||||
final String staffId;
|
||||
final String companyId;
|
||||
StreamSubscription<void>? _taskSubscription;
|
||||
final String? staffId;
|
||||
final String? companyId;
|
||||
StreamSubscription<void>? _tasksSubscription;
|
||||
|
||||
DashboardTaskListCubit({required this.staffId, required this.companyId})
|
||||
: super(const DashboardTaskListState()) {
|
||||
_initRealtime();
|
||||
}
|
||||
: super(const DashboardTaskListState());
|
||||
|
||||
void _initRealtime() {
|
||||
void startListening() {
|
||||
emit(state.copyWith(status: DashboardTaskListStatus.loading));
|
||||
|
||||
// Primo caricamento
|
||||
_loadTasksSilently();
|
||||
|
||||
// Inizio ascolto campanello
|
||||
_taskSubscription = _repository.watchCompanyTasks(companyId).listen((_) {
|
||||
// Quando il campanello suona (qualcosa è cambiato a DB), ricarichiamo!
|
||||
_loadTasksSilently();
|
||||
});
|
||||
try {
|
||||
_tasksSubscription = _repository.watchCompanyTasks(companyId!).listen((
|
||||
_,
|
||||
) {
|
||||
// Quando il campanello suona (qualcosa è cambiato a DB), ricarichiamo!
|
||||
_loadTasksSilently();
|
||||
});
|
||||
} on Exception catch (e) {
|
||||
debugPrint(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> loadTasks() async {
|
||||
emit(state.copyWith(status: DashboardTaskListStatus.loading));
|
||||
await _loadTasksSilently();
|
||||
void stopListening() {
|
||||
_tasksSubscription?.cancel();
|
||||
_tasksSubscription = null;
|
||||
}
|
||||
|
||||
Future<void> _loadTasksSilently() async {
|
||||
try {
|
||||
final tasks = await _repository.getTasks(
|
||||
companyId: companyId,
|
||||
companyId: companyId!,
|
||||
staffId: staffId,
|
||||
statuses: [TaskStatus.open, TaskStatus.inProgress],
|
||||
limit: 10,
|
||||
@@ -66,8 +71,7 @@ class DashboardTaskListCubit extends Cubit<DashboardTaskListState> {
|
||||
|
||||
@override
|
||||
Future<void> close() {
|
||||
// Stacchiamo l'abbonamento. Il controller.onCancel nel Repo farà il resto!
|
||||
_taskSubscription?.cancel();
|
||||
stopListening();
|
||||
return super.close();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user