tasks
This commit is contained in:
@@ -1,59 +1,50 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:equatable/equatable.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';
|
||||
import 'package:flux/features/tasks/models/task_status.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
import 'package:supabase_flutter/supabase_flutter.dart';
|
||||
|
||||
part 'task_list_state.dart';
|
||||
|
||||
class TaskListCubit extends Cubit<TaskListState> {
|
||||
final SupabaseClient _supabase = GetIt.I.get<SupabaseClient>();
|
||||
RealtimeChannel? _taskChannel;
|
||||
final TaskRepository _repository = GetIt.I.get<TaskRepository>();
|
||||
final String currentCompanyId;
|
||||
final String? currentStoreId;
|
||||
|
||||
TaskListCubit() : super(TaskListState(status: TaskListStatus.initial));
|
||||
// Il nostro abbonamento allo stream del repository
|
||||
StreamSubscription<void>? _taskSubscription;
|
||||
|
||||
// --- AVVIA L'ASCOLTO IN TEMPO REALE ---
|
||||
void startListening({required String companyId, String? storeId}) {
|
||||
emit(state.copyWith(status: TaskListStatus.loading));
|
||||
|
||||
// Facciamo subito il caricamento manuale, chiedendo SOLO quelli attivi
|
||||
_loadTasks(companyId: companyId, storeId: storeId);
|
||||
|
||||
_taskChannel?.unsubscribe();
|
||||
|
||||
_taskChannel = _supabase
|
||||
.channel('public:tasks_company_$companyId')
|
||||
.onPostgresChanges(
|
||||
event: PostgresChangeEvent.all,
|
||||
schema: 'public',
|
||||
table: 'tasks',
|
||||
filter: PostgresChangeFilter(
|
||||
type: PostgresChangeFilterType.eq,
|
||||
column: 'company_id',
|
||||
value: companyId,
|
||||
),
|
||||
callback: (payload) {
|
||||
// Ricarica la lista applicando sempre i filtri di stato
|
||||
_loadTasks(companyId: companyId, storeId: storeId);
|
||||
},
|
||||
);
|
||||
|
||||
_taskChannel?.subscribe();
|
||||
TaskListCubit({required this.currentCompanyId, this.currentStoreId})
|
||||
: super(const TaskListState()) {
|
||||
_initRealtime();
|
||||
}
|
||||
|
||||
// --- HELPER DI CARICAMENTO ---
|
||||
Future<void> _loadTasks({required String companyId, String? storeId}) async {
|
||||
void _initRealtime() {
|
||||
emit(state.copyWith(status: TaskListStatus.loading));
|
||||
|
||||
// Primo caricamento
|
||||
_loadTasksSilently();
|
||||
|
||||
// Ci mettiamo in ascolto del campanello del Repository
|
||||
_taskSubscription = _repository.watchCompanyTasks(currentCompanyId).listen((
|
||||
_,
|
||||
) {
|
||||
// Quando il campanello suona (qualcosa è cambiato a DB), ricarichiamo!
|
||||
_loadTasksSilently();
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> loadTasks() async {
|
||||
emit(state.copyWith(status: TaskListStatus.loading));
|
||||
await _loadTasksSilently();
|
||||
}
|
||||
|
||||
Future<void> _loadTasksSilently() async {
|
||||
try {
|
||||
final tasks = await _repository.getTasks(
|
||||
companyId: companyId,
|
||||
storeId: storeId,
|
||||
// CHICCA: Passiamo solo gli stati attivi!
|
||||
statuses: [TaskStatus.open, TaskStatus.inProgress],
|
||||
companyId: currentCompanyId,
|
||||
storeId: currentStoreId,
|
||||
);
|
||||
|
||||
emit(
|
||||
@@ -73,20 +64,10 @@ class TaskListCubit extends Cubit<TaskListState> {
|
||||
}
|
||||
}
|
||||
|
||||
// --- OPERAZIONI MANUALI ---
|
||||
// (Le lasciamo gestire al repository o le metti qui se preferisci,
|
||||
// tanto il risultato lo vedrai magicamente aggiornato dallo stream sopra!)
|
||||
|
||||
/*
|
||||
Future<void> markAsCompleted(String taskId) async { ... }
|
||||
Future<void> deleteTask(String taskId) async { ... }
|
||||
*/
|
||||
|
||||
// --- PULIZIA FONDAMENTALE ---
|
||||
@override
|
||||
Future<void> close() {
|
||||
// Chiudiamo il rubinetto quando usciamo dalla pagina per non intasarci la memoria!
|
||||
_taskChannel?.unsubscribe();
|
||||
// Stacchiamo l'abbonamento. Il controller.onCancel nel Repo farà il resto!
|
||||
_taskSubscription?.cancel();
|
||||
return super.close();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,8 +21,7 @@ class TaskListState extends Equatable {
|
||||
return TaskListState(
|
||||
status: status ?? this.status,
|
||||
tasks: tasks ?? this.tasks,
|
||||
// Se lo status è success o loading, puliamo eventuali errori precedenti
|
||||
errorMessage: errorMessage ?? this.errorMessage,
|
||||
errorMessage: errorMessage,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user