boh
This commit is contained in:
195
lib/features/tasks/blocs/task_form_cubit.dart
Normal file
195
lib/features/tasks/blocs/task_form_cubit.dart
Normal file
@@ -0,0 +1,195 @@
|
||||
import 'package:collection/collection.dart';
|
||||
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/master_data/staff/models/staff_member_model.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';
|
||||
part 'task_form_state.dart';
|
||||
|
||||
class TaskFormCubit extends Cubit<TaskFormState> {
|
||||
final TaskRepository _taskRepository = GetIt.I.get<TaskRepository>();
|
||||
final TaskModel? _initialTask;
|
||||
final String? _initialTaskId;
|
||||
// Cache in memoria proveniente dallo StaffCubit!
|
||||
final List<StaffMemberModel> _globalStaff;
|
||||
|
||||
final String currentCompanyId = GetIt.I
|
||||
.get<SessionCubit>()
|
||||
.state
|
||||
.company!
|
||||
.id!;
|
||||
final String? currentStoreId = GetIt.I
|
||||
.get<SessionCubit>()
|
||||
.state
|
||||
.currentStore
|
||||
?.id;
|
||||
|
||||
TaskFormCubit({
|
||||
required List<StaffMemberModel> globalStaff, // Iniettiamo la lista qui
|
||||
TaskModel? initialTask,
|
||||
String? initialTaskId,
|
||||
}) : _globalStaff = globalStaff,
|
||||
_initialTask = initialTask,
|
||||
_initialTaskId = initialTaskId,
|
||||
super(const TaskFormState()) {
|
||||
_initForm(task: initialTask, initialTaskId: initialTaskId);
|
||||
}
|
||||
|
||||
// --- 1. INIZIALIZZAZIONE SINCRONA ---
|
||||
void _initForm({TaskModel? task, String? initialTaskId}) {
|
||||
final isGlobalMode = task != null
|
||||
? task.storeId == null
|
||||
: currentStoreId == null;
|
||||
|
||||
// MAGIA: Estraiamo gli ID dagli oggetti staff, o facciamo fallback su assignedToIds se c'è
|
||||
final existingStaffIds =
|
||||
task?.assignedToStaff.map((s) => s.id!).toList() ??
|
||||
task?.assignedToIds ??
|
||||
[];
|
||||
|
||||
emit(
|
||||
state.copyWith(
|
||||
id: task?.id,
|
||||
title: task?.title ?? '',
|
||||
description: task?.description ?? '',
|
||||
dueDate: task?.dueDate,
|
||||
taskStatus: task?.status ?? TaskStatus.open,
|
||||
isGlobal: isGlobalMode,
|
||||
selectedStaffIds:
|
||||
existingStaffIds, // Ora non si perde più i dipendenti!
|
||||
),
|
||||
);
|
||||
|
||||
_updateStaffScope(isGlobalMode);
|
||||
}
|
||||
|
||||
// --- 2. SWITCH SCOPE E RAGGRUPPAMENTO ---
|
||||
void toggleGlobalScope(bool isGlobal) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
isGlobal: isGlobal,
|
||||
selectedStaffIds: [], // Resettiamo la selezione se si cambia scope
|
||||
),
|
||||
);
|
||||
_updateStaffScope(isGlobal);
|
||||
}
|
||||
|
||||
void _updateStaffScope(bool isGlobal) {
|
||||
// 1. Filtriamo in memoria
|
||||
final filteredStaff = isGlobal
|
||||
? _globalStaff
|
||||
: _globalStaff.where((s) => s.store?.id == currentStoreId).toList();
|
||||
|
||||
// 2. Raggruppiamo per nome negozio (usando groupBy del pacchetto collection)
|
||||
final groupedStaff = groupBy(
|
||||
filteredStaff,
|
||||
(StaffMemberModel s) => s.store?.name ?? 'Direzione / HQ',
|
||||
);
|
||||
|
||||
// 3. Emettiamo il nuovo stato all'istante
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: TaskFormStatus.initial,
|
||||
groupedAvailableStaff: groupedStaff,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// --- 3. SELEZIONE AVANZATA (SINGOLA E PER NEGOZIO) ---
|
||||
void toggleStaffSelection(String staffId) {
|
||||
final currentList = List<String>.from(state.selectedStaffIds);
|
||||
if (currentList.contains(staffId)) {
|
||||
currentList.remove(staffId);
|
||||
} else {
|
||||
currentList.add(staffId);
|
||||
}
|
||||
emit(state.copyWith(selectedStaffIds: currentList));
|
||||
}
|
||||
|
||||
void toggleStoreSelection(String storeName, bool selectAll) {
|
||||
// Recupera tutti i membri di quel gruppo
|
||||
final staffInStore = state.groupedAvailableStaff[storeName] ?? [];
|
||||
final idsInStore = staffInStore.map((s) => s.id!).toList();
|
||||
|
||||
final currentSelection = Set<String>.from(state.selectedStaffIds);
|
||||
|
||||
if (selectAll) {
|
||||
currentSelection.addAll(idsInStore);
|
||||
} else {
|
||||
currentSelection.removeAll(idsInStore);
|
||||
}
|
||||
|
||||
emit(state.copyWith(selectedStaffIds: currentSelection.toList()));
|
||||
}
|
||||
|
||||
// --- 4. AGGIORNAMENTO CAMPI STANDARD ---
|
||||
void updateTitle(String title) => emit(state.copyWith(title: title));
|
||||
|
||||
void updateDescription(String desc) =>
|
||||
emit(state.copyWith(description: desc));
|
||||
|
||||
void updateDueDate(DateTime? date) =>
|
||||
emit(state.copyWith(dueDate: date, clearDueDate: date == null));
|
||||
|
||||
void updateLinkedTicket(String? ticketId) =>
|
||||
emit(state.copyWith(linkedTicketId: ticketId));
|
||||
|
||||
// --- 5. LOGICA DEI REMINDER FINTI ---
|
||||
void addMockReminder(String type, int minutes) {
|
||||
final currentReminders = List<TaskReminder>.from(state.reminders);
|
||||
currentReminders.add(TaskReminder(type: type, minutesBefore: minutes));
|
||||
emit(state.copyWith(reminders: currentReminders));
|
||||
}
|
||||
|
||||
void removeMockReminder(int index) {
|
||||
final currentReminders = List<TaskReminder>.from(state.reminders);
|
||||
currentReminders.removeAt(index);
|
||||
emit(state.copyWith(reminders: currentReminders));
|
||||
}
|
||||
|
||||
// --- 6. SALVATAGGIO FINALE ---
|
||||
Future<void> saveTask({required String currentUserId}) async {
|
||||
if (!state.isFormValid) return;
|
||||
|
||||
emit(state.copyWith(status: TaskFormStatus.submitting));
|
||||
|
||||
try {
|
||||
final taskToSave = TaskModel(
|
||||
id: state.id,
|
||||
companyId: currentCompanyId,
|
||||
storeId: state.isGlobal
|
||||
? null
|
||||
: currentStoreId, // La vera discriminante Globale/Store!
|
||||
createdById: state.id == null
|
||||
? currentUserId
|
||||
: null, // Lo settiamo solo alla creazione
|
||||
title: state.title.trim(),
|
||||
description: state.description.trim().isEmpty
|
||||
? null
|
||||
: state.description.trim(),
|
||||
dueDate: state.dueDate,
|
||||
status: state.taskStatus,
|
||||
assignedToIds: state
|
||||
.selectedStaffIds, // L'array che andrà a popolare la tabella di giunzione
|
||||
);
|
||||
|
||||
if (state.id == null) {
|
||||
await _taskRepository.createTask(taskToSave);
|
||||
} else {
|
||||
await _taskRepository.updateTask(taskToSave);
|
||||
}
|
||||
|
||||
emit(state.copyWith(status: TaskFormStatus.success));
|
||||
} catch (e) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: TaskFormStatus.failure,
|
||||
errorMessage: 'Errore durante il salvataggio: $e',
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
109
lib/features/tasks/blocs/task_form_state.dart
Normal file
109
lib/features/tasks/blocs/task_form_state.dart
Normal file
@@ -0,0 +1,109 @@
|
||||
part of 'task_form_cubit.dart';
|
||||
|
||||
enum TaskFormStatus { initial, loading, submitting, success, failure }
|
||||
|
||||
/// Placeholder finto per i futuri reminder (pg_cron)
|
||||
class TaskReminder extends Equatable {
|
||||
final String type; // es. 'email', 'push'
|
||||
final int minutesBefore;
|
||||
const TaskReminder({required this.type, required this.minutesBefore});
|
||||
@override
|
||||
List<Object?> get props => [type, minutesBefore];
|
||||
}
|
||||
|
||||
class TaskFormState extends Equatable {
|
||||
final TaskFormStatus status;
|
||||
final String? id; // Null se stiamo creando un nuovo task
|
||||
final String title;
|
||||
final String description;
|
||||
final DateTime? dueDate;
|
||||
final TaskStatus taskStatus;
|
||||
|
||||
// --- SCOPING & ASSIGNMENTS ---
|
||||
final bool isGlobal;
|
||||
final List<String> selectedStaffIds;
|
||||
final List<StaffMemberModel> availableStaff;
|
||||
final Map<String, List<StaffMemberModel>> groupedAvailableStaff;
|
||||
|
||||
// --- FUTURI ANCORAGGI ---
|
||||
final List<TaskReminder> reminders;
|
||||
final String? linkedTicketId;
|
||||
final String? linkedEmailId;
|
||||
|
||||
final String? errorMessage;
|
||||
|
||||
const TaskFormState({
|
||||
this.status = TaskFormStatus.initial,
|
||||
this.id,
|
||||
this.title = '',
|
||||
this.description = '',
|
||||
this.dueDate,
|
||||
this.taskStatus = TaskStatus.open,
|
||||
this.isGlobal = false,
|
||||
this.selectedStaffIds = const [],
|
||||
this.availableStaff = const [],
|
||||
this.groupedAvailableStaff = const {},
|
||||
this.reminders = const [],
|
||||
this.linkedTicketId,
|
||||
this.linkedEmailId,
|
||||
this.errorMessage,
|
||||
});
|
||||
|
||||
// MAGIA: Il form è valido solo se c'è un titolo e, opzionalmente, altre regole.
|
||||
bool get isFormValid => title.trim().isNotEmpty;
|
||||
|
||||
TaskFormState copyWith({
|
||||
TaskFormStatus? status,
|
||||
String? id,
|
||||
String? title,
|
||||
String? description,
|
||||
DateTime? dueDate,
|
||||
bool clearDueDate = false, // Trucco Ninja per rimettere a null una data!
|
||||
TaskStatus? taskStatus,
|
||||
bool? isGlobal,
|
||||
List<String>? selectedStaffIds,
|
||||
List<StaffMemberModel>? availableStaff,
|
||||
Map<String, List<StaffMemberModel>>? groupedAvailableStaff,
|
||||
List<TaskReminder>? reminders,
|
||||
String? linkedTicketId,
|
||||
String? linkedEmailId,
|
||||
String? errorMessage,
|
||||
}) {
|
||||
return TaskFormState(
|
||||
status: status ?? this.status,
|
||||
id: id ?? this.id,
|
||||
title: title ?? this.title,
|
||||
description: description ?? this.description,
|
||||
dueDate: clearDueDate ? null : (dueDate ?? this.dueDate),
|
||||
taskStatus: taskStatus ?? this.taskStatus,
|
||||
isGlobal: isGlobal ?? this.isGlobal,
|
||||
selectedStaffIds: selectedStaffIds ?? this.selectedStaffIds,
|
||||
availableStaff: availableStaff ?? this.availableStaff,
|
||||
groupedAvailableStaff:
|
||||
groupedAvailableStaff ?? this.groupedAvailableStaff,
|
||||
reminders: reminders ?? this.reminders,
|
||||
linkedTicketId: linkedTicketId ?? this.linkedTicketId,
|
||||
linkedEmailId: linkedEmailId ?? this.linkedEmailId,
|
||||
errorMessage:
|
||||
errorMessage, // Se copyWith è chiamato senza errore, lo pulisce in automatico
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [
|
||||
status,
|
||||
id,
|
||||
title,
|
||||
description,
|
||||
dueDate,
|
||||
taskStatus,
|
||||
isGlobal,
|
||||
selectedStaffIds,
|
||||
availableStaff,
|
||||
groupedAvailableStaff,
|
||||
reminders,
|
||||
linkedTicketId,
|
||||
linkedEmailId,
|
||||
errorMessage,
|
||||
];
|
||||
}
|
||||
Reference in New Issue
Block a user