This commit is contained in:
2026-05-26 12:28:12 +02:00
parent 2afe97c6db
commit 45455a16c4
12 changed files with 851 additions and 8 deletions

View File

@@ -0,0 +1,71 @@
import 'package:equatable/equatable.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flux/core/blocs/session/session_cubit.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 'dashboard_task_list_state.dart';
class DashboardTaskListCubit extends Cubit<DashboardTaskListState> {
final TaskRepository _repository = GetIt.I.get<TaskRepository>();
final SupabaseClient _supabase = GetIt.I.get<SupabaseClient>();
RealtimeChannel? _taskChannel;
DashboardTaskListCubit() : super(DashboardTaskListState());
void startListening({required String staffId}) {
emit(state.copyWith(status: DashboardTaskListStatus.loading));
_loadTasks(staffId: staffId);
_taskChannel?.unsubscribe();
_taskChannel = _supabase
.channel('public:tasks_staff_$staffId')
.onPostgresChanges(
event: PostgresChangeEvent.all,
schema: 'public',
table: 'tasks',
filter: PostgresChangeFilter(
type: PostgresChangeFilterType.eq,
column: 'staff_id',
value: staffId,
),
callback: (payload) {
_loadTasks(staffId: staffId);
},
);
_taskChannel?.subscribe();
}
Future<void> _loadTasks({required String staffId}) async {
try {
final tasks = await _repository.getTasks(
companyId: GetIt.I.get<SessionCubit>().state.company!.id!,
staffId: staffId,
statuses: [TaskStatus.open, TaskStatus.inProgress],
);
emit(
state.copyWith(
status: DashboardTaskListStatus.success,
tasks: tasks,
errorMessage: null,
),
);
} catch (e) {
emit(
state.copyWith(
status: DashboardTaskListStatus.failure,
errorMessage: e.toString(),
),
);
}
}
@override
Future<void> close() {
_taskChannel?.unsubscribe();
return super.close();
}
}

View File

@@ -0,0 +1,30 @@
part of 'dashboard_task_list_cubit.dart';
enum DashboardTaskListStatus { initial, loading, success, failure }
class DashboardTaskListState extends Equatable {
final DashboardTaskListStatus status;
final List<TaskModel> tasks;
final String? errorMessage;
const DashboardTaskListState({
this.status = DashboardTaskListStatus.initial,
this.tasks = const [],
this.errorMessage,
});
DashboardTaskListState copyWith({
DashboardTaskListStatus? status,
List<TaskModel>? tasks,
String? errorMessage,
}) {
return DashboardTaskListState(
status: status ?? this.status,
tasks: tasks ?? this.tasks,
errorMessage: errorMessage ?? this.errorMessage,
);
}
@override
List<Object?> get props => [status, tasks, errorMessage];
}