b
This commit is contained in:
@@ -2,208 +2,159 @@ 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/settings/data/settings_repository.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_reminder_config.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 List<StaffMemberModel> _globalStaff;
|
||||
final String currentCompanyId = GetIt.I<SessionCubit>().state.company!.id!;
|
||||
final String? currentStoreId = GetIt.I<SessionCubit>().state.currentStore?.id;
|
||||
final TaskRepository _repository = GetIt.I.get<TaskRepository>();
|
||||
final SettingsRepository _settingsRepository = GetIt.I
|
||||
.get<SettingsRepository>();
|
||||
final SessionCubit _sessionCubit = GetIt.I.get<SessionCubit>();
|
||||
|
||||
TaskFormCubit({
|
||||
required List<StaffMemberModel> globalStaff,
|
||||
|
||||
TaskModel? initialTask, // Arriva dalla navigazione interna (extra)
|
||||
String? initialTaskId, // Arriva dal Deep Link (parametro URL)
|
||||
}) : _globalStaff = globalStaff,
|
||||
super(const TaskFormState()) {
|
||||
_initForm(initialTask, initialTaskId);
|
||||
}
|
||||
|
||||
Future<void> _initForm(TaskModel? task, String? taskId) async {
|
||||
// 1. Mettiamo subito il form in caricamento
|
||||
emit(state.copyWith(status: TaskFormStatus.loading));
|
||||
|
||||
TaskModel? taskToLoad = task;
|
||||
|
||||
// 2. SCENARIO DEEP LINK: Non abbiamo l'oggetto, ma abbiamo un ID valido
|
||||
if (taskToLoad == null && taskId != null && taskId != 'new') {
|
||||
try {
|
||||
taskToLoad = await _taskRepository.getTaskById(taskId);
|
||||
} catch (e) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: TaskFormStatus.failure,
|
||||
errorMessage: 'Impossibile caricare il task dal link: $e',
|
||||
),
|
||||
);
|
||||
return; // Ci fermiamo qui
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Popoliamo lo stato con i dati (sia che arrivino dall'extra, sia dal DB, sia nulli)
|
||||
final isGlobalMode = taskToLoad != null
|
||||
? taskToLoad.storeId == null
|
||||
: currentStoreId == null;
|
||||
final existingStaffIds =
|
||||
taskToLoad?.assignedToStaff.map((s) => s.id!).toList() ??
|
||||
taskToLoad?.assignedToIds ??
|
||||
[];
|
||||
|
||||
emit(
|
||||
state.copyWith(
|
||||
id: taskToLoad?.id,
|
||||
title: taskToLoad?.title ?? '',
|
||||
description: taskToLoad?.description ?? '',
|
||||
dueDate: taskToLoad?.dueDate,
|
||||
taskStatus: taskToLoad?.status ?? TaskStatus.open,
|
||||
isGlobal: isGlobalMode,
|
||||
selectedStaffIds: existingStaffIds,
|
||||
status: TaskFormStatus.initial, // Caricamento finito, form pronto!
|
||||
),
|
||||
);
|
||||
|
||||
_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: cerchiamo nell'array degli ID!
|
||||
final filteredStaff = isGlobal
|
||||
? _globalStaff
|
||||
: _globalStaff
|
||||
.where((s) => s.assignedStoreIds.contains(currentStoreId))
|
||||
.toList();
|
||||
|
||||
// 2. Raggruppamento M2M (Ciclo manuale)
|
||||
final Map<String, List<StaffMemberModel>> groupedStaff = {};
|
||||
|
||||
for (final staff in filteredStaff) {
|
||||
// Se non ha nessun negozio assegnato, finisce in Direzione
|
||||
if (staff.assignedStores.isEmpty) {
|
||||
groupedStaff.putIfAbsent('Direzione / HQ', () => []).add(staff);
|
||||
} else {
|
||||
// Se ha più negozi, clona la sua presenza in ogni gruppo!
|
||||
for (final store in staff.assignedStores) {
|
||||
final storeName = store.name;
|
||||
groupedStaff.putIfAbsent(storeName, () => []).add(staff);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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);
|
||||
TaskFormCubit({TaskModel? existingTask})
|
||||
: super(
|
||||
TaskFormState(
|
||||
id: existingTask?.id,
|
||||
title: existingTask?.title ?? '',
|
||||
description: existingTask?.description ?? '',
|
||||
dueDate: existingTask?.dueDate,
|
||||
isGlobal: existingTask?.isGlobal ?? false,
|
||||
selectedStaffIds: existingTask?.assignedToIds ?? [],
|
||||
),
|
||||
) {
|
||||
if (existingTask == null) {
|
||||
_initializeNewTaskReminders();
|
||||
} else {
|
||||
currentList.add(staffId);
|
||||
_loadExistingTaskReminders(existingTask.id!);
|
||||
}
|
||||
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));
|
||||
String get _companyId => _sessionCubit.state.company!.id!;
|
||||
String get _currentUserId => _sessionCubit.state.currentStaffMember!.id!;
|
||||
|
||||
// --- INIT REMINDER NUOVO TASK ---
|
||||
Future<void> _initializeNewTaskReminders() async {
|
||||
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
|
||||
final defaults = await _settingsRepository.getMyReminderDefaults(
|
||||
companyId: _companyId,
|
||||
staffId: _currentUserId,
|
||||
);
|
||||
|
||||
if (state.id == null) {
|
||||
await _taskRepository.createTask(taskToSave);
|
||||
} else {
|
||||
await _taskRepository.updateTask(taskToSave);
|
||||
}
|
||||
final initialReminders = defaults
|
||||
.map(
|
||||
(d) => TaskReminderConfig(
|
||||
minutesBefore: d.minutesBefore,
|
||||
channel: d.channel,
|
||||
),
|
||||
)
|
||||
.toList();
|
||||
|
||||
emit(state.copyWith(reminders: initialReminders));
|
||||
} catch (e) {
|
||||
// Fallback in caso di errore
|
||||
emit(
|
||||
state.copyWith(
|
||||
reminders: const [
|
||||
TaskReminderConfig(minutesBefore: 15, channel: 'push'),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// --- INIT REMINDER TASK ESISTENTE ---
|
||||
Future<void> _loadExistingTaskReminders(String taskId) async {
|
||||
try {
|
||||
// Recuperiamo SOLO i reminder non forzati dell'utente loggato per popolare il form
|
||||
final existingConfigs = await _repository.fetchPersonalReminders(
|
||||
taskId: taskId,
|
||||
staffId: _currentUserId,
|
||||
);
|
||||
emit(state.copyWith(reminders: existingConfigs));
|
||||
} catch (e) {
|
||||
print('Errore caricamento reminder: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// --- UPDATE CAMPI BASE ---
|
||||
void updateTitle(String t) => emit(state.copyWith(title: t));
|
||||
void updateDescription(String d) => emit(state.copyWith(description: d));
|
||||
void updateDueDate(DateTime? d) => emit(state.copyWith(dueDate: d));
|
||||
void toggleGlobalScope(bool g) => emit(state.copyWith(isGlobal: g));
|
||||
|
||||
void toggleStaffSelection(String staffId) {
|
||||
final updated = List<String>.from(state.selectedStaffIds);
|
||||
updated.contains(staffId) ? updated.remove(staffId) : updated.add(staffId);
|
||||
emit(state.copyWith(selectedStaffIds: updated));
|
||||
}
|
||||
|
||||
// --- GESTIONE REMINDER NEL FORM ---
|
||||
void addReminderRule(int minutesBefore, String channel) {
|
||||
final updated = List<TaskReminderConfig>.from(state.reminders);
|
||||
final newConfig = TaskReminderConfig(
|
||||
minutesBefore: minutesBefore,
|
||||
channel: channel,
|
||||
);
|
||||
|
||||
if (!updated.contains(newConfig)) {
|
||||
updated.add(newConfig);
|
||||
updated.sort((a, b) => a.minutesBefore.compareTo(b.minutesBefore));
|
||||
emit(state.copyWith(reminders: updated));
|
||||
}
|
||||
}
|
||||
|
||||
void removeReminderRule(int index) {
|
||||
final updated = List<TaskReminderConfig>.from(state.reminders)
|
||||
..removeAt(index);
|
||||
emit(state.copyWith(reminders: updated));
|
||||
}
|
||||
|
||||
// --- SALVATAGGIO FINALE ---
|
||||
Future<void> saveTask() async {
|
||||
if (!state.isFormValid) return;
|
||||
emit(state.copyWith(status: TaskFormStatus.submitting));
|
||||
|
||||
final taskToSave = TaskModel(
|
||||
id: state.id,
|
||||
companyId: _companyId,
|
||||
createdBy: _currentUserId,
|
||||
title: state.title.trim(),
|
||||
description: state.description.trim(),
|
||||
dueDate: state.dueDate,
|
||||
isGlobal: state.isGlobal,
|
||||
assignedToIds: state.selectedStaffIds,
|
||||
);
|
||||
|
||||
try {
|
||||
if (state.id == null) {
|
||||
// NUOVO TASK -> CREATE
|
||||
// Qui potresti passare un managerForcedOverride se implementi la UI per quello
|
||||
await _repository.createTask(
|
||||
task: taskToSave,
|
||||
assignedStaffIds: state.selectedStaffIds,
|
||||
currentUserId: _currentUserId,
|
||||
currentUserCustomReminders: state.reminders,
|
||||
);
|
||||
} else {
|
||||
// VECCHIO TASK -> UPDATE
|
||||
await _repository.updateTask(
|
||||
task: taskToSave,
|
||||
assignedStaffIds: state.selectedStaffIds,
|
||||
currentUserId: _currentUserId,
|
||||
currentUserCustomReminders: state.reminders,
|
||||
);
|
||||
}
|
||||
emit(state.copyWith(status: TaskFormStatus.success));
|
||||
} catch (e) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: TaskFormStatus.failure,
|
||||
errorMessage: 'Errore durante il salvataggio: $e',
|
||||
errorMessage: e.toString(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -2,108 +2,66 @@ 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 String? id;
|
||||
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 List<TaskReminderConfig>
|
||||
reminders; // I promemoria (solo dell'utente loggato)
|
||||
final String? errorMessage;
|
||||
|
||||
const TaskFormState({
|
||||
this.status = TaskFormStatus.initial,
|
||||
this.id,
|
||||
this.status = TaskFormStatus.initial,
|
||||
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,
|
||||
TaskFormStatus? status,
|
||||
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,
|
||||
List<TaskReminderConfig>? reminders,
|
||||
String? errorMessage,
|
||||
}) {
|
||||
return TaskFormState(
|
||||
status: status ?? this.status,
|
||||
id: id ?? this.id,
|
||||
status: status ?? this.status,
|
||||
title: title ?? this.title,
|
||||
description: description ?? this.description,
|
||||
dueDate: clearDueDate ? null : (dueDate ?? this.dueDate),
|
||||
taskStatus: taskStatus ?? this.taskStatus,
|
||||
dueDate: dueDate ?? this.dueDate,
|
||||
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
|
||||
errorMessage: errorMessage,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [
|
||||
status,
|
||||
id,
|
||||
status,
|
||||
title,
|
||||
description,
|
||||
dueDate,
|
||||
taskStatus,
|
||||
isGlobal,
|
||||
selectedStaffIds,
|
||||
availableStaff,
|
||||
groupedAvailableStaff,
|
||||
reminders,
|
||||
linkedTicketId,
|
||||
linkedEmailId,
|
||||
errorMessage,
|
||||
];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user