b
This commit is contained in:
@@ -18,7 +18,6 @@ import 'package:flux/features/customers/models/customer_model.dart';
|
|||||||
import 'package:flux/features/customers/ui/customer_detail_screen.dart';
|
import 'package:flux/features/customers/ui/customer_detail_screen.dart';
|
||||||
import 'package:flux/features/customers/ui/customer_form_screen.dart';
|
import 'package:flux/features/customers/ui/customer_form_screen.dart';
|
||||||
import 'package:flux/features/customers/ui/customers_list_screen.dart';
|
import 'package:flux/features/customers/ui/customers_list_screen.dart';
|
||||||
import 'package:flux/features/home/dashboard_task_list/blocs/dashboard_task_list_cubit.dart';
|
|
||||||
import 'package:flux/features/home/ui/home_screen.dart';
|
import 'package:flux/features/home/ui/home_screen.dart';
|
||||||
import 'package:flux/features/master_data/master_data_hub_content.dart';
|
import 'package:flux/features/master_data/master_data_hub_content.dart';
|
||||||
import 'package:flux/features/master_data/products/blocs/product_cubit.dart';
|
import 'package:flux/features/master_data/products/blocs/product_cubit.dart';
|
||||||
@@ -43,8 +42,10 @@ import 'package:flux/features/operations/blocs/operation_form_cubit.dart';
|
|||||||
import 'package:flux/features/operations/models/operation_model.dart';
|
import 'package:flux/features/operations/models/operation_model.dart';
|
||||||
import 'package:flux/features/operations/ui/operation_form_screen.dart';
|
import 'package:flux/features/operations/ui/operation_form_screen.dart';
|
||||||
import 'package:flux/features/operations/ui/operation_list_screen.dart';
|
import 'package:flux/features/operations/ui/operation_list_screen.dart';
|
||||||
import 'package:flux/features/settings/settings_screen.dart';
|
import 'package:flux/features/settings/blocs/reminder_defaults_cubit.dart';
|
||||||
import 'package:flux/features/settings/theme_settings_view.dart';
|
import 'package:flux/features/settings/ui/reminder_settings_screen.dart';
|
||||||
|
import 'package:flux/features/settings/ui/settings_screen.dart';
|
||||||
|
import 'package:flux/features/settings/ui/theme_settings_view.dart';
|
||||||
import 'package:flux/features/tasks/blocs/task_form_cubit.dart';
|
import 'package:flux/features/tasks/blocs/task_form_cubit.dart';
|
||||||
import 'package:flux/features/tasks/blocs/task_list_cubit.dart';
|
import 'package:flux/features/tasks/blocs/task_list_cubit.dart';
|
||||||
import 'package:flux/features/tasks/models/task_model.dart';
|
import 'package:flux/features/tasks/models/task_model.dart';
|
||||||
@@ -141,14 +142,7 @@ class AppRouter {
|
|||||||
path: '/',
|
path: '/',
|
||||||
name: Routes.home,
|
name: Routes.home,
|
||||||
builder: (context, state) {
|
builder: (context, state) {
|
||||||
return MultiBlocProvider(
|
return const HomeScreen();
|
||||||
providers: [
|
|
||||||
BlocProvider<DashboardTaskListCubit>(
|
|
||||||
create: (context) => DashboardTaskListCubit(),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
child: HomeScreen(),
|
|
||||||
);
|
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
|
||||||
@@ -218,6 +212,16 @@ class AppRouter {
|
|||||||
name: Routes.themeSettings,
|
name: Routes.themeSettings,
|
||||||
builder: (context, state) => const ThemeSettingsView(),
|
builder: (context, state) => const ThemeSettingsView(),
|
||||||
),
|
),
|
||||||
|
GoRoute(
|
||||||
|
path: 'reminderSettings',
|
||||||
|
name: Routes.reminderSettings,
|
||||||
|
builder: (context, state) =>
|
||||||
|
BlocProvider<ReminderDefaultsCubit>(
|
||||||
|
create: (context) => ReminderDefaultsCubit(),
|
||||||
|
|
||||||
|
child: const ReminderSettingsScreen(),
|
||||||
|
),
|
||||||
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
|
||||||
|
|||||||
@@ -26,4 +26,5 @@ class Routes {
|
|||||||
static const String notes = 'notes';
|
static const String notes = 'notes';
|
||||||
static const String tasks = 'tasks';
|
static const String tasks = 'tasks';
|
||||||
static const String taskForm = 'task-form';
|
static const String taskForm = 'task-form';
|
||||||
|
static const String reminderSettings = 'reminder-settings';
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,49 +1,50 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
|
||||||
import 'package:equatable/equatable.dart';
|
import 'package:equatable/equatable.dart';
|
||||||
import 'package:flutter_bloc/flutter_bloc.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/data/task_repository.dart';
|
||||||
import 'package:flux/features/tasks/models/task_model.dart';
|
import 'package:flux/features/tasks/models/task_model.dart';
|
||||||
import 'package:flux/features/tasks/models/task_status.dart';
|
import 'package:flux/features/tasks/models/task_status.dart';
|
||||||
import 'package:get_it/get_it.dart';
|
import 'package:get_it/get_it.dart';
|
||||||
import 'package:supabase_flutter/supabase_flutter.dart';
|
|
||||||
|
|
||||||
part 'dashboard_task_list_state.dart';
|
part 'dashboard_task_list_state.dart';
|
||||||
|
|
||||||
class DashboardTaskListCubit extends Cubit<DashboardTaskListState> {
|
class DashboardTaskListCubit extends Cubit<DashboardTaskListState> {
|
||||||
final TaskRepository _repository = GetIt.I.get<TaskRepository>();
|
final TaskRepository _repository = GetIt.I.get<TaskRepository>();
|
||||||
final SupabaseClient _supabase = GetIt.I.get<SupabaseClient>();
|
final String staffId;
|
||||||
RealtimeChannel? _taskChannel;
|
final String companyId;
|
||||||
|
StreamSubscription<void>? _taskSubscription;
|
||||||
|
|
||||||
DashboardTaskListCubit() : super(DashboardTaskListState());
|
DashboardTaskListCubit({required this.staffId, required this.companyId})
|
||||||
|
: super(const DashboardTaskListState()) {
|
||||||
void startListening({required String staffId}) async {
|
_initRealtime();
|
||||||
emit(state.copyWith(status: DashboardTaskListStatus.loading));
|
|
||||||
await _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 {
|
void _initRealtime() {
|
||||||
|
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();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> loadTasks() async {
|
||||||
|
emit(state.copyWith(status: DashboardTaskListStatus.loading));
|
||||||
|
await _loadTasksSilently();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _loadTasksSilently() async {
|
||||||
try {
|
try {
|
||||||
final tasks = await _repository.getTasks(
|
final tasks = await _repository.getTasks(
|
||||||
companyId: GetIt.I.get<SessionCubit>().state.company!.id!,
|
companyId: companyId,
|
||||||
staffId: staffId,
|
staffId: staffId,
|
||||||
statuses: [TaskStatus.open, TaskStatus.inProgress],
|
statuses: [TaskStatus.open, TaskStatus.inProgress],
|
||||||
|
limit: 10,
|
||||||
);
|
);
|
||||||
|
|
||||||
emit(
|
emit(
|
||||||
@@ -65,7 +66,8 @@ class DashboardTaskListCubit extends Cubit<DashboardTaskListState> {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> close() {
|
Future<void> close() {
|
||||||
_taskChannel?.unsubscribe();
|
// Stacchiamo l'abbonamento. Il controller.onCancel nel Repo farà il resto!
|
||||||
|
_taskSubscription?.cancel();
|
||||||
return super.close();
|
return super.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import 'package:flux/core/blocs/session/session_cubit.dart';
|
|||||||
import 'package:flux/core/routes/routes.dart';
|
import 'package:flux/core/routes/routes.dart';
|
||||||
import 'package:flux/core/theme/theme.dart';
|
import 'package:flux/core/theme/theme.dart';
|
||||||
import 'package:flux/features/home/dashboard_task_list/blocs/dashboard_task_list_cubit.dart';
|
import 'package:flux/features/home/dashboard_task_list/blocs/dashboard_task_list_cubit.dart';
|
||||||
|
import 'package:get_it/get_it.dart';
|
||||||
import 'package:go_router/go_router.dart';
|
import 'package:go_router/go_router.dart';
|
||||||
import 'package:flux/features/tasks/models/task_status.dart';
|
import 'package:flux/features/tasks/models/task_status.dart';
|
||||||
|
|
||||||
@@ -14,11 +15,12 @@ class DashboardTasksCard extends StatelessWidget {
|
|||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
// Recuperiamo lo staff (o l'utente) loggato
|
// Recuperiamo lo staff (o l'utente) loggato
|
||||||
// Adatta il getter in base a come è strutturato il tuo SessionState
|
// Adatta il getter in base a come è strutturato il tuo SessionState
|
||||||
final currentStaffId = context
|
final currentStaffId = GetIt.I
|
||||||
.read<SessionCubit>()
|
.get<SessionCubit>()
|
||||||
.state
|
.state
|
||||||
.currentStaffMember
|
.currentStaffMember
|
||||||
?.id;
|
?.id;
|
||||||
|
final companyId = GetIt.I.get<SessionCubit>().state.company!.id!;
|
||||||
|
|
||||||
if (currentStaffId == null) {
|
if (currentStaffId == null) {
|
||||||
return const SizedBox.shrink(); // Sicurezza se lo stato non è pronto
|
return const SizedBox.shrink(); // Sicurezza se lo stato non è pronto
|
||||||
@@ -26,7 +28,7 @@ class DashboardTasksCard extends StatelessWidget {
|
|||||||
|
|
||||||
return BlocProvider(
|
return BlocProvider(
|
||||||
create: (context) =>
|
create: (context) =>
|
||||||
DashboardTaskListCubit()..startListening(staffId: currentStaffId),
|
DashboardTaskListCubit(staffId: currentStaffId, companyId: companyId),
|
||||||
child: const _DashboardTasksCardContent(),
|
child: const _DashboardTasksCardContent(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
108
lib/features/settings/blocs/reminder_defaults_cubit.dart
Normal file
108
lib/features/settings/blocs/reminder_defaults_cubit.dart
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
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/settings/data/settings_repository.dart'; // O dove hai messo i metodi del DB
|
||||||
|
import 'package:flux/features/tasks/models/reminder_default_model.dart';
|
||||||
|
import 'package:get_it/get_it.dart';
|
||||||
|
|
||||||
|
part 'reminder_defaults_state.dart';
|
||||||
|
|
||||||
|
class ReminderDefaultsCubit extends Cubit<ReminderDefaultsState> {
|
||||||
|
final SettingsRepository _repository = GetIt.I.get<SettingsRepository>();
|
||||||
|
final SessionCubit _sessionCubit = GetIt.I.get<SessionCubit>();
|
||||||
|
|
||||||
|
ReminderDefaultsCubit() : super(const ReminderDefaultsState());
|
||||||
|
|
||||||
|
String get _companyId => _sessionCubit.state.company!.id!;
|
||||||
|
String get _staffId => _sessionCubit.state.currentStaffMember!.id!;
|
||||||
|
|
||||||
|
Future<void> loadReminders() async {
|
||||||
|
emit(state.copyWith(status: ReminderDefaultsStatus.loading));
|
||||||
|
try {
|
||||||
|
final reminders = await _repository.getMyReminderDefaults(
|
||||||
|
companyId: _companyId,
|
||||||
|
staffId: _staffId,
|
||||||
|
);
|
||||||
|
emit(
|
||||||
|
state.copyWith(
|
||||||
|
status: ReminderDefaultsStatus.success,
|
||||||
|
reminders: reminders,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
} catch (e) {
|
||||||
|
emit(
|
||||||
|
state.copyWith(
|
||||||
|
status: ReminderDefaultsStatus.failure,
|
||||||
|
errorMessage: e.toString(),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> addReminder({
|
||||||
|
required int minutesBefore,
|
||||||
|
required String channel,
|
||||||
|
}) async {
|
||||||
|
emit(state.copyWith(status: ReminderDefaultsStatus.loading));
|
||||||
|
try {
|
||||||
|
final newReminder = ReminderDefaultModel(
|
||||||
|
companyId: _companyId,
|
||||||
|
staffId: _staffId,
|
||||||
|
minutesBefore: minutesBefore,
|
||||||
|
channel: channel,
|
||||||
|
);
|
||||||
|
|
||||||
|
final savedReminder = await _repository.addReminderDefault(newReminder);
|
||||||
|
|
||||||
|
// Aggiungiamo alla lista locale e ordiniamo per minuti
|
||||||
|
final updatedList = List<ReminderDefaultModel>.from(state.reminders)
|
||||||
|
..add(savedReminder);
|
||||||
|
updatedList.sort((a, b) => a.minutesBefore.compareTo(b.minutesBefore));
|
||||||
|
|
||||||
|
emit(
|
||||||
|
state.copyWith(
|
||||||
|
status: ReminderDefaultsStatus.success,
|
||||||
|
reminders: updatedList,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
} catch (e) {
|
||||||
|
emit(
|
||||||
|
state.copyWith(
|
||||||
|
status: ReminderDefaultsStatus.failure,
|
||||||
|
errorMessage: e.toString(),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
// Ricarichiamo per sicurezza lo stato precedente
|
||||||
|
loadReminders();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> deleteReminder(String reminderId) async {
|
||||||
|
// Salviamo la lista vecchia nel caso fallisca la cancellazione
|
||||||
|
final oldList = List<ReminderDefaultModel>.from(state.reminders);
|
||||||
|
|
||||||
|
// Aggiornamento ottimistico (rimuoviamo subito dalla UI)
|
||||||
|
final optimisticList = state.reminders
|
||||||
|
.where((r) => r.id != reminderId)
|
||||||
|
.toList();
|
||||||
|
emit(
|
||||||
|
state.copyWith(
|
||||||
|
status: ReminderDefaultsStatus.success,
|
||||||
|
reminders: optimisticList,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
try {
|
||||||
|
await _repository.deleteReminderDefault(reminderId);
|
||||||
|
} catch (e) {
|
||||||
|
// Rollback se il DB fallisce
|
||||||
|
emit(
|
||||||
|
state.copyWith(
|
||||||
|
status: ReminderDefaultsStatus.failure,
|
||||||
|
errorMessage: e.toString(),
|
||||||
|
reminders: oldList,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
33
lib/features/settings/blocs/reminder_defaults_state.dart
Normal file
33
lib/features/settings/blocs/reminder_defaults_state.dart
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
part of 'reminder_defaults_cubit.dart';
|
||||||
|
|
||||||
|
enum ReminderDefaultsStatus { initial, loading, success, failure }
|
||||||
|
|
||||||
|
class ReminderDefaultsState extends Equatable {
|
||||||
|
final ReminderDefaultsStatus status;
|
||||||
|
final List<ReminderDefaultModel> reminders;
|
||||||
|
final String? errorMessage;
|
||||||
|
|
||||||
|
const ReminderDefaultsState({
|
||||||
|
this.status = ReminderDefaultsStatus.initial,
|
||||||
|
this.reminders = const [],
|
||||||
|
this.errorMessage,
|
||||||
|
});
|
||||||
|
|
||||||
|
ReminderDefaultsState copyWith({
|
||||||
|
ReminderDefaultsStatus? status,
|
||||||
|
List<ReminderDefaultModel>? reminders,
|
||||||
|
String? errorMessage,
|
||||||
|
}) {
|
||||||
|
return ReminderDefaultsState(
|
||||||
|
status: status ?? this.status,
|
||||||
|
reminders: reminders ?? this.reminders,
|
||||||
|
// Se passiamo un nuovo status di successo o loading, puliamo l'errore
|
||||||
|
errorMessage:
|
||||||
|
errorMessage ??
|
||||||
|
(status != ReminderDefaultsStatus.failure ? null : this.errorMessage),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object?> get props => [status, reminders, errorMessage];
|
||||||
|
}
|
||||||
63
lib/features/settings/data/settings_repository.dart
Normal file
63
lib/features/settings/data/settings_repository.dart
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
import 'package:flux/features/tasks/models/reminder_default_model.dart';
|
||||||
|
import 'package:get_it/get_it.dart';
|
||||||
|
import 'package:supabase_flutter/supabase_flutter.dart';
|
||||||
|
|
||||||
|
class SettingsRepository {
|
||||||
|
final _supabase = GetIt.I.get<SupabaseClient>();
|
||||||
|
|
||||||
|
// --- PREFERENZE REMINDER ---
|
||||||
|
|
||||||
|
/// Legge i default dell'utente corrente
|
||||||
|
Future<List<ReminderDefaultModel>> getMyReminderDefaults({
|
||||||
|
required String companyId,
|
||||||
|
required String staffId,
|
||||||
|
}) async {
|
||||||
|
try {
|
||||||
|
final response = await _supabase
|
||||||
|
.from('staff_task_reminder_defaults')
|
||||||
|
.select()
|
||||||
|
.eq('company_id', companyId)
|
||||||
|
.eq('staff_id', staffId)
|
||||||
|
.order('minutes_before', ascending: true);
|
||||||
|
|
||||||
|
return (response as List)
|
||||||
|
.map((map) => ReminderDefaultModel.fromMap(map))
|
||||||
|
.toList();
|
||||||
|
} catch (e) {
|
||||||
|
throw Exception('Errore nel caricamento delle preferenze notifiche: $e');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Aggiunge una nuova regola (es. Push 15 min prima)
|
||||||
|
Future<ReminderDefaultModel> addReminderDefault(
|
||||||
|
ReminderDefaultModel reminder,
|
||||||
|
) async {
|
||||||
|
try {
|
||||||
|
final response = await _supabase
|
||||||
|
.from('staff_task_reminder_defaults')
|
||||||
|
.insert(reminder.toMap())
|
||||||
|
.select()
|
||||||
|
.single();
|
||||||
|
|
||||||
|
return ReminderDefaultModel.fromMap(response);
|
||||||
|
} catch (e) {
|
||||||
|
// Catturiamo l'errore UNIQUE se l'utente prova ad aggiungere due volte la stessa identica regola
|
||||||
|
if (e is PostgrestException && e.code == '23505') {
|
||||||
|
throw Exception('Hai già impostato questo identico promemoria.');
|
||||||
|
}
|
||||||
|
throw Exception('Errore salvataggio promemoria: $e');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Elimina una regola
|
||||||
|
Future<void> deleteReminderDefault(String reminderId) async {
|
||||||
|
try {
|
||||||
|
await _supabase
|
||||||
|
.from('staff_task_reminder_defaults')
|
||||||
|
.delete()
|
||||||
|
.eq('id', reminderId);
|
||||||
|
} catch (e) {
|
||||||
|
throw Exception('Errore durante l\'eliminazione: $e');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
269
lib/features/settings/ui/reminder_settings_screen.dart
Normal file
269
lib/features/settings/ui/reminder_settings_screen.dart
Normal file
@@ -0,0 +1,269 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
|
import 'package:flux/features/settings/blocs/reminder_defaults_cubit.dart';
|
||||||
|
|
||||||
|
class ReminderSettingsScreen extends StatefulWidget {
|
||||||
|
const ReminderSettingsScreen({super.key});
|
||||||
|
|
||||||
|
@override
|
||||||
|
State<ReminderSettingsScreen> createState() => _ReminderSettingsScreenState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _ReminderSettingsScreenState extends State<ReminderSettingsScreen> {
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
// Carichiamo i dati all'avvio
|
||||||
|
context.read<ReminderDefaultsCubit>().loadReminders();
|
||||||
|
}
|
||||||
|
|
||||||
|
void _showAddReminderBottomSheet(BuildContext context) {
|
||||||
|
// Valori preselezionati
|
||||||
|
int selectedMinutes = 15;
|
||||||
|
String selectedChannel = 'push';
|
||||||
|
|
||||||
|
showModalBottomSheet(
|
||||||
|
context: context,
|
||||||
|
shape: const RoundedRectangleBorder(
|
||||||
|
borderRadius: BorderRadius.vertical(top: Radius.circular(20)),
|
||||||
|
),
|
||||||
|
builder: (bottomSheetContext) {
|
||||||
|
return StatefulBuilder(
|
||||||
|
builder: (context, setModalState) {
|
||||||
|
return SafeArea(
|
||||||
|
child: Padding(
|
||||||
|
padding: const EdgeInsets.all(24.0),
|
||||||
|
child: Column(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||||
|
children: [
|
||||||
|
const Text(
|
||||||
|
'Nuova Regola di Avviso',
|
||||||
|
style: TextStyle(
|
||||||
|
fontSize: 20,
|
||||||
|
fontWeight: FontWeight.bold,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 24),
|
||||||
|
|
||||||
|
// --- SELEZIONE TEMPO ---
|
||||||
|
DropdownButtonFormField<int>(
|
||||||
|
decoration: const InputDecoration(
|
||||||
|
labelText: 'Quando vuoi essere avvisato?',
|
||||||
|
border: OutlineInputBorder(),
|
||||||
|
),
|
||||||
|
initialValue: selectedMinutes,
|
||||||
|
items: const [
|
||||||
|
DropdownMenuItem(
|
||||||
|
value: 5,
|
||||||
|
child: Text('5 minuti prima'),
|
||||||
|
),
|
||||||
|
DropdownMenuItem(
|
||||||
|
value: 15,
|
||||||
|
child: Text('15 minuti prima'),
|
||||||
|
),
|
||||||
|
DropdownMenuItem(value: 60, child: Text('1 ora prima')),
|
||||||
|
DropdownMenuItem(
|
||||||
|
value: 120,
|
||||||
|
child: Text('2 ore prima'),
|
||||||
|
),
|
||||||
|
DropdownMenuItem(
|
||||||
|
value: 1440,
|
||||||
|
child: Text('1 giorno prima'),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
onChanged: (val) {
|
||||||
|
if (val != null)
|
||||||
|
setModalState(() => selectedMinutes = val);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
const SizedBox(height: 16),
|
||||||
|
|
||||||
|
// --- SELEZIONE CANALE ---
|
||||||
|
DropdownButtonFormField<String>(
|
||||||
|
decoration: const InputDecoration(
|
||||||
|
labelText: 'Come vuoi essere avvisato?',
|
||||||
|
border: OutlineInputBorder(),
|
||||||
|
),
|
||||||
|
initialValue: selectedChannel,
|
||||||
|
items: const [
|
||||||
|
DropdownMenuItem(
|
||||||
|
value: 'push',
|
||||||
|
child: Row(
|
||||||
|
children: [
|
||||||
|
Icon(
|
||||||
|
Icons.notifications_active,
|
||||||
|
size: 20,
|
||||||
|
color: Colors.orange,
|
||||||
|
),
|
||||||
|
SizedBox(width: 8),
|
||||||
|
Text('Notifica App (Push)'),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
DropdownMenuItem(
|
||||||
|
value: 'email',
|
||||||
|
child: Row(
|
||||||
|
children: [
|
||||||
|
Icon(Icons.email, size: 20, color: Colors.blue),
|
||||||
|
SizedBox(width: 8),
|
||||||
|
Text('Email'),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
onChanged: (val) {
|
||||||
|
if (val != null)
|
||||||
|
setModalState(() => selectedChannel = val);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
const SizedBox(height: 32),
|
||||||
|
|
||||||
|
// --- SALVATAGGIO ---
|
||||||
|
FilledButton(
|
||||||
|
style: FilledButton.styleFrom(
|
||||||
|
padding: const EdgeInsets.symmetric(vertical: 16),
|
||||||
|
),
|
||||||
|
onPressed: () {
|
||||||
|
context.read<ReminderDefaultsCubit>().addReminder(
|
||||||
|
minutesBefore: selectedMinutes,
|
||||||
|
channel: selectedChannel,
|
||||||
|
);
|
||||||
|
Navigator.pop(context);
|
||||||
|
},
|
||||||
|
child: const Text('Aggiungi Regola'),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return Scaffold(
|
||||||
|
appBar: AppBar(title: const Text('Preferenze Promemoria')),
|
||||||
|
floatingActionButton: FloatingActionButton.extended(
|
||||||
|
onPressed: () => _showAddReminderBottomSheet(context),
|
||||||
|
icon: const Icon(Icons.add_alert),
|
||||||
|
label: const Text('Aggiungi'),
|
||||||
|
backgroundColor: Colors.orange,
|
||||||
|
),
|
||||||
|
body: BlocConsumer<ReminderDefaultsCubit, ReminderDefaultsState>(
|
||||||
|
listener: (context, state) {
|
||||||
|
if (state.status == ReminderDefaultsStatus.failure &&
|
||||||
|
state.errorMessage != null) {
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(
|
||||||
|
content: Text(state.errorMessage!),
|
||||||
|
backgroundColor: Theme.of(context).colorScheme.error,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
builder: (context, state) {
|
||||||
|
if (state.status == ReminderDefaultsStatus.loading &&
|
||||||
|
state.reminders.isEmpty) {
|
||||||
|
return const Center(child: CircularProgressIndicator());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (state.reminders.isEmpty) {
|
||||||
|
return Center(
|
||||||
|
child: Padding(
|
||||||
|
padding: const EdgeInsets.all(32.0),
|
||||||
|
child: Column(
|
||||||
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
|
children: [
|
||||||
|
Icon(
|
||||||
|
Icons.notifications_off_outlined,
|
||||||
|
size: 64,
|
||||||
|
color: Theme.of(context).dividerColor,
|
||||||
|
),
|
||||||
|
const SizedBox(height: 16),
|
||||||
|
const Text(
|
||||||
|
'Nessun promemoria predefinito.',
|
||||||
|
style: TextStyle(
|
||||||
|
fontSize: 18,
|
||||||
|
fontWeight: FontWeight.bold,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 8),
|
||||||
|
const Text(
|
||||||
|
'Aggiungi una regola per ricevere in automatico le notifiche quando ti viene assegnato un task.',
|
||||||
|
textAlign: TextAlign.center,
|
||||||
|
style: TextStyle(color: Colors.grey),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ListView.builder(
|
||||||
|
padding: const EdgeInsets.only(
|
||||||
|
top: 16,
|
||||||
|
bottom: 80,
|
||||||
|
left: 16,
|
||||||
|
right: 16,
|
||||||
|
),
|
||||||
|
itemCount: state.reminders.length,
|
||||||
|
itemBuilder: (context, index) {
|
||||||
|
final reminder = state.reminders[index];
|
||||||
|
final isPush = reminder.channel == 'push';
|
||||||
|
|
||||||
|
return Card(
|
||||||
|
elevation: 0,
|
||||||
|
margin: const EdgeInsets.only(bottom: 12),
|
||||||
|
shape: RoundedRectangleBorder(
|
||||||
|
borderRadius: BorderRadius.circular(12),
|
||||||
|
side: BorderSide(
|
||||||
|
color: Theme.of(
|
||||||
|
context,
|
||||||
|
).dividerColor.withValues(alpha: 0.5),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
child: ListTile(
|
||||||
|
contentPadding: const EdgeInsets.symmetric(
|
||||||
|
horizontal: 20,
|
||||||
|
vertical: 8,
|
||||||
|
),
|
||||||
|
leading: CircleAvatar(
|
||||||
|
backgroundColor: isPush
|
||||||
|
? Colors.orange.withValues(alpha: 0.1)
|
||||||
|
: Colors.blue.withValues(alpha: 0.1),
|
||||||
|
child: Icon(
|
||||||
|
isPush ? Icons.notifications_active : Icons.email,
|
||||||
|
color: isPush ? Colors.orange : Colors.blue,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
title: Text(
|
||||||
|
reminder.friendlyTime, // Usiamo l'helper del Model!
|
||||||
|
style: const TextStyle(fontWeight: FontWeight.bold),
|
||||||
|
),
|
||||||
|
subtitle: Text(
|
||||||
|
isPush ? 'Tramite Notifica App' : 'Tramite Email',
|
||||||
|
),
|
||||||
|
trailing: IconButton(
|
||||||
|
icon: const Icon(
|
||||||
|
Icons.delete_outline,
|
||||||
|
color: Colors.redAccent,
|
||||||
|
),
|
||||||
|
onPressed: () {
|
||||||
|
context.read<ReminderDefaultsCubit>().deleteReminder(
|
||||||
|
reminder.id!,
|
||||||
|
);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -18,6 +18,15 @@ class SettingsScreen extends StatelessWidget {
|
|||||||
body: ListView(
|
body: ListView(
|
||||||
padding: const EdgeInsets.all(16),
|
padding: const EdgeInsets.all(16),
|
||||||
children: [
|
children: [
|
||||||
|
_settingsSection('Utente', [
|
||||||
|
_settingsTile(
|
||||||
|
title: 'Impostazioni Promemoria',
|
||||||
|
icon: Icons.notifications,
|
||||||
|
subtitle: 'Notifiche predefinite',
|
||||||
|
context: context,
|
||||||
|
onTap: () => context.pushNamed(Routes.reminderSettings),
|
||||||
|
),
|
||||||
|
]),
|
||||||
_settingsSection('Azienda', [
|
_settingsSection('Azienda', [
|
||||||
_settingsTile(
|
_settingsTile(
|
||||||
title: 'Impostazioni Azienda',
|
title: 'Impostazioni Azienda',
|
||||||
@@ -83,6 +92,7 @@ class SettingsScreen extends StatelessWidget {
|
|||||||
onTap: () => context.pushNamed(Routes.themeSettings),
|
onTap: () => context.pushNamed(Routes.themeSettings),
|
||||||
),
|
),
|
||||||
]),
|
]),
|
||||||
|
|
||||||
const SizedBox(height: 24),
|
const SizedBox(height: 24),
|
||||||
TextButton.icon(
|
TextButton.icon(
|
||||||
onPressed: () => context.read<SessionCubit>().signOut(),
|
onPressed: () => context.read<SessionCubit>().signOut(),
|
||||||
@@ -2,208 +2,159 @@ import 'package:equatable/equatable.dart';
|
|||||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
import 'package:flux/core/blocs/session/session_cubit.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/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/data/task_repository.dart';
|
||||||
import 'package:flux/features/tasks/models/task_model.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:flux/features/tasks/models/task_status.dart';
|
||||||
import 'package:get_it/get_it.dart';
|
import 'package:get_it/get_it.dart';
|
||||||
part 'task_form_state.dart';
|
part 'task_form_state.dart';
|
||||||
|
|
||||||
class TaskFormCubit extends Cubit<TaskFormState> {
|
class TaskFormCubit extends Cubit<TaskFormState> {
|
||||||
final TaskRepository _taskRepository = GetIt.I.get<TaskRepository>();
|
final TaskRepository _repository = GetIt.I.get<TaskRepository>();
|
||||||
final List<StaffMemberModel> _globalStaff;
|
final SettingsRepository _settingsRepository = GetIt.I
|
||||||
final String currentCompanyId = GetIt.I<SessionCubit>().state.company!.id!;
|
.get<SettingsRepository>();
|
||||||
final String? currentStoreId = GetIt.I<SessionCubit>().state.currentStore?.id;
|
final SessionCubit _sessionCubit = GetIt.I.get<SessionCubit>();
|
||||||
|
|
||||||
TaskFormCubit({
|
TaskFormCubit({TaskModel? existingTask})
|
||||||
required List<StaffMemberModel> globalStaff,
|
: super(
|
||||||
|
TaskFormState(
|
||||||
TaskModel? initialTask, // Arriva dalla navigazione interna (extra)
|
id: existingTask?.id,
|
||||||
String? initialTaskId, // Arriva dal Deep Link (parametro URL)
|
title: existingTask?.title ?? '',
|
||||||
}) : _globalStaff = globalStaff,
|
description: existingTask?.description ?? '',
|
||||||
super(const TaskFormState()) {
|
dueDate: existingTask?.dueDate,
|
||||||
_initForm(initialTask, initialTaskId);
|
isGlobal: existingTask?.isGlobal ?? false,
|
||||||
|
selectedStaffIds: existingTask?.assignedToIds ?? [],
|
||||||
|
),
|
||||||
|
) {
|
||||||
|
if (existingTask == null) {
|
||||||
|
_initializeNewTaskReminders();
|
||||||
|
} else {
|
||||||
|
_loadExistingTaskReminders(existingTask.id!);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _initForm(TaskModel? task, String? taskId) async {
|
String get _companyId => _sessionCubit.state.company!.id!;
|
||||||
// 1. Mettiamo subito il form in caricamento
|
String get _currentUserId => _sessionCubit.state.currentStaffMember!.id!;
|
||||||
emit(state.copyWith(status: TaskFormStatus.loading));
|
|
||||||
|
|
||||||
TaskModel? taskToLoad = task;
|
// --- INIT REMINDER NUOVO TASK ---
|
||||||
|
Future<void> _initializeNewTaskReminders() async {
|
||||||
// 2. SCENARIO DEEP LINK: Non abbiamo l'oggetto, ma abbiamo un ID valido
|
|
||||||
if (taskToLoad == null && taskId != null && taskId != 'new') {
|
|
||||||
try {
|
try {
|
||||||
taskToLoad = await _taskRepository.getTaskById(taskId);
|
final defaults = await _settingsRepository.getMyReminderDefaults(
|
||||||
} catch (e) {
|
companyId: _companyId,
|
||||||
emit(
|
staffId: _currentUserId,
|
||||||
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);
|
final initialReminders = defaults
|
||||||
}
|
.map(
|
||||||
|
(d) => TaskReminderConfig(
|
||||||
// --- 2. SWITCH SCOPE E RAGGRUPPAMENTO ---
|
minutesBefore: d.minutesBefore,
|
||||||
void toggleGlobalScope(bool isGlobal) {
|
channel: d.channel,
|
||||||
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();
|
.toList();
|
||||||
|
|
||||||
// 2. Raggruppamento M2M (Ciclo manuale)
|
emit(state.copyWith(reminders: initialReminders));
|
||||||
final Map<String, List<StaffMemberModel>> groupedStaff = {};
|
} catch (e) {
|
||||||
|
// Fallback in caso di errore
|
||||||
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(
|
emit(
|
||||||
state.copyWith(
|
state.copyWith(
|
||||||
status: TaskFormStatus.initial,
|
reminders: const [
|
||||||
groupedAvailableStaff: groupedStaff,
|
TaskReminderConfig(minutesBefore: 15, channel: 'push'),
|
||||||
|
],
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- 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) {
|
// --- INIT REMINDER TASK ESISTENTE ---
|
||||||
// Recupera tutti i membri di quel gruppo
|
Future<void> _loadExistingTaskReminders(String taskId) async {
|
||||||
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 {
|
try {
|
||||||
final taskToSave = TaskModel(
|
// Recuperiamo SOLO i reminder non forzati dell'utente loggato per popolare il form
|
||||||
id: state.id,
|
final existingConfigs = await _repository.fetchPersonalReminders(
|
||||||
companyId: currentCompanyId,
|
taskId: taskId,
|
||||||
storeId: state.isGlobal
|
staffId: _currentUserId,
|
||||||
? null
|
);
|
||||||
: currentStoreId, // La vera discriminante Globale/Store!
|
emit(state.copyWith(reminders: existingConfigs));
|
||||||
createdById: state.id == null
|
} catch (e) {
|
||||||
? currentUserId
|
print('Errore caricamento reminder: $e');
|
||||||
: null, // Lo settiamo solo alla creazione
|
}
|
||||||
title: state.title.trim(),
|
}
|
||||||
description: state.description.trim().isEmpty
|
|
||||||
? null
|
// --- UPDATE CAMPI BASE ---
|
||||||
: state.description.trim(),
|
void updateTitle(String t) => emit(state.copyWith(title: t));
|
||||||
dueDate: state.dueDate,
|
void updateDescription(String d) => emit(state.copyWith(description: d));
|
||||||
status: state.taskStatus,
|
void updateDueDate(DateTime? d) => emit(state.copyWith(dueDate: d));
|
||||||
assignedToIds: state
|
void toggleGlobalScope(bool g) => emit(state.copyWith(isGlobal: g));
|
||||||
.selectedStaffIds, // L'array che andrà a popolare la tabella di giunzione
|
|
||||||
|
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 (state.id == null) {
|
if (!updated.contains(newConfig)) {
|
||||||
await _taskRepository.createTask(taskToSave);
|
updated.add(newConfig);
|
||||||
} else {
|
updated.sort((a, b) => a.minutesBefore.compareTo(b.minutesBefore));
|
||||||
await _taskRepository.updateTask(taskToSave);
|
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));
|
emit(state.copyWith(status: TaskFormStatus.success));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
emit(
|
emit(
|
||||||
state.copyWith(
|
state.copyWith(
|
||||||
status: TaskFormStatus.failure,
|
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 }
|
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 {
|
class TaskFormState extends Equatable {
|
||||||
|
final String? id;
|
||||||
final TaskFormStatus status;
|
final TaskFormStatus status;
|
||||||
final String? id; // Null se stiamo creando un nuovo task
|
|
||||||
final String title;
|
final String title;
|
||||||
final String description;
|
final String description;
|
||||||
final DateTime? dueDate;
|
final DateTime? dueDate;
|
||||||
final TaskStatus taskStatus;
|
|
||||||
|
|
||||||
// --- SCOPING & ASSIGNMENTS ---
|
|
||||||
final bool isGlobal;
|
final bool isGlobal;
|
||||||
final List<String> selectedStaffIds;
|
final List<String> selectedStaffIds;
|
||||||
final List<StaffMemberModel> availableStaff;
|
final List<TaskReminderConfig>
|
||||||
final Map<String, List<StaffMemberModel>> groupedAvailableStaff;
|
reminders; // I promemoria (solo dell'utente loggato)
|
||||||
|
|
||||||
// --- FUTURI ANCORAGGI ---
|
|
||||||
final List<TaskReminder> reminders;
|
|
||||||
final String? linkedTicketId;
|
|
||||||
final String? linkedEmailId;
|
|
||||||
|
|
||||||
final String? errorMessage;
|
final String? errorMessage;
|
||||||
|
|
||||||
const TaskFormState({
|
const TaskFormState({
|
||||||
this.status = TaskFormStatus.initial,
|
|
||||||
this.id,
|
this.id,
|
||||||
|
this.status = TaskFormStatus.initial,
|
||||||
this.title = '',
|
this.title = '',
|
||||||
this.description = '',
|
this.description = '',
|
||||||
this.dueDate,
|
this.dueDate,
|
||||||
this.taskStatus = TaskStatus.open,
|
|
||||||
this.isGlobal = false,
|
this.isGlobal = false,
|
||||||
this.selectedStaffIds = const [],
|
this.selectedStaffIds = const [],
|
||||||
this.availableStaff = const [],
|
|
||||||
this.groupedAvailableStaff = const {},
|
|
||||||
this.reminders = const [],
|
this.reminders = const [],
|
||||||
this.linkedTicketId,
|
|
||||||
this.linkedEmailId,
|
|
||||||
this.errorMessage,
|
this.errorMessage,
|
||||||
});
|
});
|
||||||
|
|
||||||
// MAGIA: Il form è valido solo se c'è un titolo e, opzionalmente, altre regole.
|
|
||||||
bool get isFormValid => title.trim().isNotEmpty;
|
bool get isFormValid => title.trim().isNotEmpty;
|
||||||
|
|
||||||
TaskFormState copyWith({
|
TaskFormState copyWith({
|
||||||
TaskFormStatus? status,
|
|
||||||
String? id,
|
String? id,
|
||||||
|
TaskFormStatus? status,
|
||||||
String? title,
|
String? title,
|
||||||
String? description,
|
String? description,
|
||||||
DateTime? dueDate,
|
DateTime? dueDate,
|
||||||
bool clearDueDate = false, // Trucco Ninja per rimettere a null una data!
|
|
||||||
TaskStatus? taskStatus,
|
|
||||||
bool? isGlobal,
|
bool? isGlobal,
|
||||||
List<String>? selectedStaffIds,
|
List<String>? selectedStaffIds,
|
||||||
List<StaffMemberModel>? availableStaff,
|
List<TaskReminderConfig>? reminders,
|
||||||
Map<String, List<StaffMemberModel>>? groupedAvailableStaff,
|
|
||||||
List<TaskReminder>? reminders,
|
|
||||||
String? linkedTicketId,
|
|
||||||
String? linkedEmailId,
|
|
||||||
String? errorMessage,
|
String? errorMessage,
|
||||||
}) {
|
}) {
|
||||||
return TaskFormState(
|
return TaskFormState(
|
||||||
status: status ?? this.status,
|
|
||||||
id: id ?? this.id,
|
id: id ?? this.id,
|
||||||
|
status: status ?? this.status,
|
||||||
title: title ?? this.title,
|
title: title ?? this.title,
|
||||||
description: description ?? this.description,
|
description: description ?? this.description,
|
||||||
dueDate: clearDueDate ? null : (dueDate ?? this.dueDate),
|
dueDate: dueDate ?? this.dueDate,
|
||||||
taskStatus: taskStatus ?? this.taskStatus,
|
|
||||||
isGlobal: isGlobal ?? this.isGlobal,
|
isGlobal: isGlobal ?? this.isGlobal,
|
||||||
selectedStaffIds: selectedStaffIds ?? this.selectedStaffIds,
|
selectedStaffIds: selectedStaffIds ?? this.selectedStaffIds,
|
||||||
availableStaff: availableStaff ?? this.availableStaff,
|
|
||||||
groupedAvailableStaff:
|
|
||||||
groupedAvailableStaff ?? this.groupedAvailableStaff,
|
|
||||||
reminders: reminders ?? this.reminders,
|
reminders: reminders ?? this.reminders,
|
||||||
linkedTicketId: linkedTicketId ?? this.linkedTicketId,
|
errorMessage: errorMessage,
|
||||||
linkedEmailId: linkedEmailId ?? this.linkedEmailId,
|
|
||||||
errorMessage:
|
|
||||||
errorMessage, // Se copyWith è chiamato senza errore, lo pulisce in automatico
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
List<Object?> get props => [
|
List<Object?> get props => [
|
||||||
status,
|
|
||||||
id,
|
id,
|
||||||
|
status,
|
||||||
title,
|
title,
|
||||||
description,
|
description,
|
||||||
dueDate,
|
dueDate,
|
||||||
taskStatus,
|
|
||||||
isGlobal,
|
isGlobal,
|
||||||
selectedStaffIds,
|
selectedStaffIds,
|
||||||
availableStaff,
|
|
||||||
groupedAvailableStaff,
|
|
||||||
reminders,
|
reminders,
|
||||||
linkedTicketId,
|
|
||||||
linkedEmailId,
|
|
||||||
errorMessage,
|
errorMessage,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,11 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
|
||||||
|
import 'package:flutter/foundation.dart';
|
||||||
|
import 'package:flux/core/blocs/session/session_cubit.dart';
|
||||||
import 'package:flux/core/enums_and_consts/consts.dart';
|
import 'package:flux/core/enums_and_consts/consts.dart';
|
||||||
|
import 'package:flux/features/tasks/models/task_reminder_config.dart';
|
||||||
import 'package:flux/features/tasks/models/task_status.dart';
|
import 'package:flux/features/tasks/models/task_status.dart';
|
||||||
|
import 'package:get_it/get_it.dart';
|
||||||
import 'package:supabase_flutter/supabase_flutter.dart';
|
import 'package:supabase_flutter/supabase_flutter.dart';
|
||||||
// Sostituisci con i percorsi corretti di FLUX
|
// Sostituisci con i percorsi corretti di FLUX
|
||||||
import 'package:flux/features/tasks/models/task_model.dart';
|
import 'package:flux/features/tasks/models/task_model.dart';
|
||||||
@@ -103,35 +107,104 @@ class TaskRepository {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- 2. CREAZIONE DEL TASK ---
|
Future<void> createTask({
|
||||||
Future<TaskModel> createTask(TaskModel task) async {
|
required TaskModel task,
|
||||||
|
required List<String> assignedStaffIds,
|
||||||
|
required String currentUserId,
|
||||||
|
required List<TaskReminderConfig> currentUserCustomReminders,
|
||||||
|
TaskReminderConfig?
|
||||||
|
managerForcedOverride, // Opzionale: l'avviso forzato del manager
|
||||||
|
}) async {
|
||||||
try {
|
try {
|
||||||
final taskData = task.toMap();
|
// 1. Inserimento del Task principale -> otteniamo il taskId
|
||||||
|
// 2. Inserimento dei record in task_assignments
|
||||||
|
final String taskId = task.id!;
|
||||||
|
|
||||||
// Rimuoviamo l'array prima di inviare i dati alla tabella principale,
|
List<Map<String, dynamic>> remindersToInsert = [];
|
||||||
// la "Strada C" impone che la verità assoluta derivi dalla tabella di giunzione!
|
|
||||||
taskData.remove('assigned_to_ids');
|
|
||||||
|
|
||||||
// 1. Inseriamo il record base nella tabella tasks
|
// 3. Recuperiamo i default degli ALTRI utenti assegnati
|
||||||
final response = await _supabase
|
final otherStaffIds = assignedStaffIds
|
||||||
.from(Tables.tasks)
|
.where((id) => id != currentUserId)
|
||||||
.insert(taskData)
|
.toList();
|
||||||
|
List<dynamic> otherDefaults = [];
|
||||||
|
if (otherStaffIds.isNotEmpty) {
|
||||||
|
otherDefaults = await _supabase
|
||||||
|
.from('staff_task_reminder_defaults')
|
||||||
.select()
|
.select()
|
||||||
.single();
|
.inFilter('staff_id', otherStaffIds);
|
||||||
|
|
||||||
final newTask = TaskModel.fromMap(response);
|
|
||||||
|
|
||||||
// 2. Se l'utente ha assegnato il task a qualcuno, popoliamo la giunzione
|
|
||||||
if (task.assignedToIds.isNotEmpty && newTask.id != null) {
|
|
||||||
await _syncAssignments(newTask.id!, task.assignedToIds);
|
|
||||||
|
|
||||||
// 3. Ricarichiamo il task appena creato per ottenere l'array e il JOIN aggiornati dal trigger!
|
|
||||||
return await getTaskById(newTask.id!);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return newTask;
|
// 4. CICLO DI COSTRUZIONE DELLA CODA REMINDER
|
||||||
|
for (var staffId in assignedStaffIds) {
|
||||||
|
// CASO A: È l'utente loggato che sta creando/partecipando al task
|
||||||
|
if (staffId == currentUserId) {
|
||||||
|
for (var config in currentUserCustomReminders) {
|
||||||
|
final triggerAt = task.dueDate?.subtract(
|
||||||
|
Duration(minutes: config.minutesBefore),
|
||||||
|
);
|
||||||
|
if (triggerAt != null && triggerAt.isAfter(DateTime.now())) {
|
||||||
|
remindersToInsert.add({
|
||||||
|
'company_id': task.companyId,
|
||||||
|
'task_id': taskId,
|
||||||
|
'staff_id': currentUserId,
|
||||||
|
'minutes_before': config.minutesBefore,
|
||||||
|
'channel': config.channel,
|
||||||
|
'trigger_at': triggerAt.toIso8601String(),
|
||||||
|
'is_forced': false,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// CASO B: Sono gli altri assegnatari -> ereditano i loro default personali dal DB
|
||||||
|
else {
|
||||||
|
final staffRules = otherDefaults.where(
|
||||||
|
(row) => row['staff_id'] == staffId,
|
||||||
|
);
|
||||||
|
for (var rule in staffRules) {
|
||||||
|
final minutesBefore = rule['minutes_before'] as int;
|
||||||
|
final triggerAt = task.dueDate?.subtract(
|
||||||
|
Duration(minutes: minutesBefore),
|
||||||
|
);
|
||||||
|
if (triggerAt != null && triggerAt.isAfter(DateTime.now())) {
|
||||||
|
remindersToInsert.add({
|
||||||
|
'company_id': task.companyId,
|
||||||
|
'task_id': taskId,
|
||||||
|
'staff_id': staffId,
|
||||||
|
'minutes_before': minutesBefore,
|
||||||
|
'channel': rule['channel'],
|
||||||
|
'trigger_at': triggerAt.toIso8601String(),
|
||||||
|
'is_forced': false,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// CASO C: Il creatore ha impostato un avviso forzato (Override molto importante)
|
||||||
|
if (managerForcedOverride != null && task.dueDate != null) {
|
||||||
|
final triggerAt = task.dueDate!.subtract(
|
||||||
|
Duration(minutes: managerForcedOverride.minutesBefore),
|
||||||
|
);
|
||||||
|
if (triggerAt.isAfter(DateTime.now())) {
|
||||||
|
remindersToInsert.add({
|
||||||
|
'company_id': task.companyId,
|
||||||
|
'task_id': taskId,
|
||||||
|
'staff_id': staffId, // Lo beccheranno tutti
|
||||||
|
'minutes_before': managerForcedOverride.minutesBefore,
|
||||||
|
'channel': managerForcedOverride.channel,
|
||||||
|
'trigger_at': triggerAt.toIso8601String(),
|
||||||
|
'is_forced':
|
||||||
|
true, // Chiude la possibilità di cancellarlo lato utente
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 5. Sparata unica di Bulk Insert su task_reminders
|
||||||
|
if (remindersToInsert.isNotEmpty) {
|
||||||
|
await _supabase.from('task_reminders').insert(remindersToInsert);
|
||||||
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
throw Exception('Errore nella creazione del task: $e');
|
throw Exception('Errore creazione task: $e');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -160,6 +233,107 @@ class TaskRepository {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> createTask({
|
||||||
|
required TaskModel task,
|
||||||
|
required List<String> assignedStaffIds,
|
||||||
|
required String currentUserId,
|
||||||
|
required List<TaskReminderConfig> currentUserCustomReminders,
|
||||||
|
TaskReminderConfig?
|
||||||
|
managerForcedOverride, // Opzionale: l'avviso forzato del manager
|
||||||
|
}) async {
|
||||||
|
try {
|
||||||
|
// 1. Inserimento del Task principale -> otteniamo il taskId
|
||||||
|
// 2. Inserimento dei record in task_assignments
|
||||||
|
final String taskId = task.id;
|
||||||
|
|
||||||
|
List<Map<String, dynamic>> remindersToInsert = [];
|
||||||
|
|
||||||
|
// 3. Recuperiamo i default degli ALTRI utenti assegnati
|
||||||
|
final otherStaffIds = assignedStaffIds
|
||||||
|
.where((id) => id != currentUserId)
|
||||||
|
.toList();
|
||||||
|
List<dynamic> otherDefaults = [];
|
||||||
|
if (otherStaffIds.isNotEmpty) {
|
||||||
|
otherDefaults = await _supabase
|
||||||
|
.from('staff_task_reminder_defaults')
|
||||||
|
.select()
|
||||||
|
.inFilter('staff_id', otherStaffIds);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. CICLO DI COSTRUZIONE DELLA CODA REMINDER
|
||||||
|
for (var staffId in assignedStaffIds) {
|
||||||
|
// CASO A: È l'utente loggato che sta creando/partecipando al task
|
||||||
|
if (staffId == currentUserId) {
|
||||||
|
for (var config in currentUserCustomReminders) {
|
||||||
|
final triggerAt = task.dueDate?.subtract(
|
||||||
|
Duration(minutes: config.minutesBefore),
|
||||||
|
);
|
||||||
|
if (triggerAt != null && triggerAt.isAfter(DateTime.now())) {
|
||||||
|
remindersToInsert.add({
|
||||||
|
'company_id': task.companyId,
|
||||||
|
'task_id': taskId,
|
||||||
|
'staff_id': currentUserId,
|
||||||
|
'minutes_before': config.minutesBefore,
|
||||||
|
'channel': config.channel,
|
||||||
|
'trigger_at': triggerAt.toIso8601String(),
|
||||||
|
'is_forced': false,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// CASO B: Sono gli altri assegnatari -> ereditano i loro default personali dal DB
|
||||||
|
else {
|
||||||
|
final staffRules = otherDefaults.where(
|
||||||
|
(row) => row['staff_id'] == staffId,
|
||||||
|
);
|
||||||
|
for (var rule in staffRules) {
|
||||||
|
final minutesBefore = rule['minutes_before'] as int;
|
||||||
|
final triggerAt = task.dueDate?.subtract(
|
||||||
|
Duration(minutes: minutesBefore),
|
||||||
|
);
|
||||||
|
if (triggerAt != null && triggerAt.isAfter(DateTime.now())) {
|
||||||
|
remindersToInsert.add({
|
||||||
|
'company_id': task.companyId,
|
||||||
|
'task_id': taskId,
|
||||||
|
'staff_id': staffId,
|
||||||
|
'minutes_before': minutesBefore,
|
||||||
|
'channel': rule['channel'],
|
||||||
|
'trigger_at': triggerAt.toIso8601String(),
|
||||||
|
'is_forced': false,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// CASO C: Il creatore ha impostato un avviso forzato (Override molto importante)
|
||||||
|
if (managerForcedOverride != null && task.dueDate != null) {
|
||||||
|
final triggerAt = task.dueDate!.subtract(
|
||||||
|
Duration(minutes: managerForcedOverride.minutesBefore),
|
||||||
|
);
|
||||||
|
if (triggerAt.isAfter(DateTime.now())) {
|
||||||
|
remindersToInsert.add({
|
||||||
|
'company_id': task.companyId,
|
||||||
|
'task_id': taskId,
|
||||||
|
'staff_id': staffId, // Lo beccheranno tutti
|
||||||
|
'minutes_before': managerForcedOverride.minutesBefore,
|
||||||
|
'channel': managerForcedOverride.channel,
|
||||||
|
'trigger_at': triggerAt.toIso8601String(),
|
||||||
|
'is_forced':
|
||||||
|
true, // Chiude la possibilità di cancellarlo lato utente
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 5. Sparata unica di Bulk Insert su task_reminders
|
||||||
|
if (remindersToInsert.isNotEmpty) {
|
||||||
|
await _supabase.from('task_reminders').insert(remindersToInsert);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
throw Exception('Errore creazione task: $e');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// --- 4. ELIMINAZIONE DEL TASK ---
|
// --- 4. ELIMINAZIONE DEL TASK ---
|
||||||
Future<void> deleteTask(String taskId) async {
|
Future<void> deleteTask(String taskId) async {
|
||||||
try {
|
try {
|
||||||
@@ -202,10 +376,116 @@ class TaskRepository {
|
|||||||
// 2. Inseriamo le nuove assegnazioni (se ce ne sono)
|
// 2. Inseriamo le nuove assegnazioni (se ce ne sono)
|
||||||
if (staffIds.isNotEmpty) {
|
if (staffIds.isNotEmpty) {
|
||||||
final List<Map<String, dynamic>> assignments = staffIds
|
final List<Map<String, dynamic>> assignments = staffIds
|
||||||
.map((staffId) => {'task_id': taskId, 'staff_id': staffId})
|
.map(
|
||||||
|
(staffId) => {
|
||||||
|
'task_id': taskId,
|
||||||
|
'staff_id': staffId,
|
||||||
|
'company_id': GetIt.I.get<SessionCubit>().state.company!.id!,
|
||||||
|
},
|
||||||
|
)
|
||||||
.toList();
|
.toList();
|
||||||
|
|
||||||
await _supabase.from(Tables.taskAssignments).insert(assignments);
|
await _supabase.from(Tables.taskAssignments).insert(assignments);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --- IL MOTORE DELLA MAGIA ---
|
||||||
|
|
||||||
|
Future<void> generateTaskReminders({
|
||||||
|
required String taskId,
|
||||||
|
required String companyId,
|
||||||
|
required List<String> assignedStaffIds,
|
||||||
|
required DateTime? taskDueDate,
|
||||||
|
}) async {
|
||||||
|
if (assignedStaffIds.isEmpty) return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 1. Recuperiamo i default di TUTTI i collaboratori coinvolti in un colpo solo
|
||||||
|
final response = await _supabase
|
||||||
|
.from('staff_task_reminder_defaults')
|
||||||
|
.select()
|
||||||
|
.eq('company_id', companyId)
|
||||||
|
.inFilter('staff_id', assignedStaffIds);
|
||||||
|
|
||||||
|
final List<Map<String, dynamic>> remindersToInsert = [];
|
||||||
|
|
||||||
|
for (var staffId in assignedStaffIds) {
|
||||||
|
// Cerchiamo le preferenze di questo specifico membro dello staff
|
||||||
|
final staffDefaults = response.where(
|
||||||
|
(row) => row['staff_id'] == staffId,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (staffDefaults.isEmpty) {
|
||||||
|
// STRATEGIA FALLBACK: Se l'utente non ha mai configurato i suoi default,
|
||||||
|
// creiamo un reminder standard (es. una push 15 min prima) per non lasciarlo scoperto.
|
||||||
|
if (taskDueDate != null) {
|
||||||
|
remindersToInsert.add({
|
||||||
|
'company_id': companyId,
|
||||||
|
'task_id': taskId,
|
||||||
|
'staff_id': staffId,
|
||||||
|
'minutes_before': 15,
|
||||||
|
'channel': 'push',
|
||||||
|
'trigger_at': taskDueDate
|
||||||
|
.subtract(const Duration(minutes: 15))
|
||||||
|
.toIso8601String(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// E spariamo la push di creazione immediata come comportamento standard
|
||||||
|
_triggerImmediateNotification(staffId, taskId, 'push_creation');
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. GESTIONE NOTIFICHE ISTANTANEE (EVENT-DRIVEN)
|
||||||
|
// Prendiamo la prima riga delle impostazioni dell'utente (tanto le colonne nuove sono speculari)
|
||||||
|
final userSetting = staffDefaults.first;
|
||||||
|
if (userSetting['notify_on_creation_push'] == true) {
|
||||||
|
_triggerImmediateNotification(staffId, taskId, 'push_creation');
|
||||||
|
}
|
||||||
|
if (userSetting['notify_on_creation_email'] == true) {
|
||||||
|
_triggerImmediateNotification(staffId, taskId, 'email_creation');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. GESTIONE REMINDER TEMPORIZZATI (TIME-DRIVEN)
|
||||||
|
if (taskDueDate != null) {
|
||||||
|
for (var rule in staffDefaults) {
|
||||||
|
final minutesBefore = rule['minutes_before'] as int;
|
||||||
|
final triggerAt = taskDueDate.subtract(
|
||||||
|
Duration(minutes: minutesBefore),
|
||||||
|
);
|
||||||
|
|
||||||
|
// Se il task scade tra 5 minuti e il reminder è impostato a 1 ora prima,
|
||||||
|
// il trigger_at sarebbe nel passato. Lo inseriamo solo se è nel futuro!
|
||||||
|
if (triggerAt.isAfter(DateTime.now())) {
|
||||||
|
remindersToInsert.add({
|
||||||
|
'company_id': companyId,
|
||||||
|
'task_id': taskId,
|
||||||
|
'staff_id': staffId,
|
||||||
|
'minutes_before': minutesBefore,
|
||||||
|
'channel': rule['channel'],
|
||||||
|
'trigger_at': triggerAt.toIso8601String(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. Bulk Insert dei reminder temporizzati nella coda operativa
|
||||||
|
if (remindersToInsert.isNotEmpty) {
|
||||||
|
await _supabase.from('task_reminders').insert(remindersToInsert);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
debugPrint('Errore nella generazione dei reminder: $e');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void _triggerImmediateNotification(
|
||||||
|
String staffId,
|
||||||
|
String taskId,
|
||||||
|
String type,
|
||||||
|
) {
|
||||||
|
// Questa funzione chiamerà direttamente l'Edge Function di Supabase
|
||||||
|
// per far squillare il telefono o mandare la mail ADESSO.
|
||||||
|
// La implementeremo appena il backend sarà pronto!
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
65
lib/features/tasks/models/reminder_default_model.dart
Normal file
65
lib/features/tasks/models/reminder_default_model.dart
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
import 'package:equatable/equatable.dart';
|
||||||
|
|
||||||
|
class ReminderDefaultModel extends Equatable {
|
||||||
|
final String? id;
|
||||||
|
final String companyId;
|
||||||
|
final String staffId;
|
||||||
|
final int minutesBefore;
|
||||||
|
final String channel; // 'push' o 'email'
|
||||||
|
|
||||||
|
const ReminderDefaultModel({
|
||||||
|
this.id,
|
||||||
|
required this.companyId,
|
||||||
|
required this.staffId,
|
||||||
|
required this.minutesBefore,
|
||||||
|
required this.channel,
|
||||||
|
});
|
||||||
|
|
||||||
|
ReminderDefaultModel copyWith({
|
||||||
|
String? id,
|
||||||
|
String? companyId,
|
||||||
|
String? staffId,
|
||||||
|
int? minutesBefore,
|
||||||
|
String? channel,
|
||||||
|
}) {
|
||||||
|
return ReminderDefaultModel(
|
||||||
|
id: id ?? this.id,
|
||||||
|
companyId: companyId ?? this.companyId,
|
||||||
|
staffId: staffId ?? this.staffId,
|
||||||
|
minutesBefore: minutesBefore ?? this.minutesBefore,
|
||||||
|
channel: channel ?? this.channel,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, dynamic> toMap() {
|
||||||
|
return {
|
||||||
|
if (id != null) 'id': id,
|
||||||
|
'company_id': companyId,
|
||||||
|
'staff_id': staffId,
|
||||||
|
'minutes_before': minutesBefore,
|
||||||
|
'channel': channel,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
factory ReminderDefaultModel.fromMap(Map<String, dynamic> map) {
|
||||||
|
return ReminderDefaultModel(
|
||||||
|
id: map['id'] as String?,
|
||||||
|
companyId: map['company_id'] as String,
|
||||||
|
staffId: map['staff_id'] as String,
|
||||||
|
minutesBefore: map['minutes_before'] as int,
|
||||||
|
channel: map['channel'] as String,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object?> get props => [id, companyId, staffId, minutesBefore, channel];
|
||||||
|
|
||||||
|
// Helper per la UI: formatta i minuti in qualcosa di leggibile (es. "1 ora prima")
|
||||||
|
String get friendlyTime {
|
||||||
|
if (minutesBefore < 60) return '$minutesBefore minuti prima';
|
||||||
|
if (minutesBefore == 60) return '1 ora prima';
|
||||||
|
if (minutesBefore < 1440) return '${minutesBefore ~/ 60} ore prima';
|
||||||
|
if (minutesBefore == 1440) return '1 giorno prima';
|
||||||
|
return '${minutesBefore ~/ 1440} giorni prima';
|
||||||
|
}
|
||||||
|
}
|
||||||
22
lib/features/tasks/models/task_reminder_config.dart
Normal file
22
lib/features/tasks/models/task_reminder_config.dart
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
import 'package:equatable/equatable.dart';
|
||||||
|
|
||||||
|
class TaskReminderConfig extends Equatable {
|
||||||
|
final int minutesBefore;
|
||||||
|
final String channel; // 'push' o 'email'
|
||||||
|
|
||||||
|
const TaskReminderConfig({
|
||||||
|
required this.minutesBefore,
|
||||||
|
required this.channel,
|
||||||
|
});
|
||||||
|
|
||||||
|
String get friendlyTime {
|
||||||
|
if (minutesBefore < 60) return '$minutesBefore minuti prima';
|
||||||
|
if (minutesBefore == 60) return '1 ora prima';
|
||||||
|
if (minutesBefore < 1440) return '${minutesBefore ~/ 60} ore prima';
|
||||||
|
if (minutesBefore == 1440) return '1 giorno prima';
|
||||||
|
return '${minutesBefore ~/ 1440} giorni prima';
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object?> get props => [minutesBefore, channel];
|
||||||
|
}
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
|
import 'package:flux/core/blocs/session/session_cubit.dart';
|
||||||
import 'package:flux/features/tasks/blocs/task_form_cubit.dart';
|
import 'package:flux/features/tasks/blocs/task_form_cubit.dart';
|
||||||
|
import 'package:get_it/get_it.dart';
|
||||||
import 'package:go_router/go_router.dart';
|
import 'package:go_router/go_router.dart';
|
||||||
|
|
||||||
class TaskFormScreen extends StatefulWidget {
|
class TaskFormScreen extends StatefulWidget {
|
||||||
@@ -31,6 +33,62 @@ class _TaskFormScreenState extends State<TaskFormScreen> {
|
|||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _showAddReminderDialog(BuildContext context, TaskFormCubit cubit) {
|
||||||
|
int minutes = 15;
|
||||||
|
String channel = 'push';
|
||||||
|
|
||||||
|
showDialog(
|
||||||
|
context: context,
|
||||||
|
builder: (dialogContext) {
|
||||||
|
return AlertDialog(
|
||||||
|
title: const Text('Aggiungi Promemoria'),
|
||||||
|
content: Column(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
children: [
|
||||||
|
DropdownButtonFormField<int>(
|
||||||
|
initialValue: minutes,
|
||||||
|
decoration: const InputDecoration(labelText: 'Preavviso'),
|
||||||
|
items: const [
|
||||||
|
DropdownMenuItem(value: 5, child: Text('5 minuti prima')),
|
||||||
|
DropdownMenuItem(value: 15, child: Text('15 minuti prima')),
|
||||||
|
DropdownMenuItem(value: 60, child: Text('1 ora prima')),
|
||||||
|
DropdownMenuItem(value: 1440, child: Text('1 giorno prima')),
|
||||||
|
],
|
||||||
|
onChanged: (v) => {if (v != null) minutes = v},
|
||||||
|
),
|
||||||
|
const SizedBox(height: 16),
|
||||||
|
DropdownButtonFormField<String>(
|
||||||
|
initialValue: channel,
|
||||||
|
decoration: const InputDecoration(labelText: 'Canale'),
|
||||||
|
items: const [
|
||||||
|
DropdownMenuItem(value: 'push', child: Text('Notifica Push')),
|
||||||
|
DropdownMenuItem(value: 'email', child: Text('Email')),
|
||||||
|
],
|
||||||
|
onChanged: (v) => {if (v != null) channel = v},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
actions: [
|
||||||
|
TextButton(
|
||||||
|
onPressed: () => Navigator.pop(dialogContext),
|
||||||
|
child: const Text('Annulla'),
|
||||||
|
),
|
||||||
|
TextButton(
|
||||||
|
onPressed: () {
|
||||||
|
cubit.addReminderRule(minutes, channel);
|
||||||
|
Navigator.pop(dialogContext);
|
||||||
|
},
|
||||||
|
child: const Text(
|
||||||
|
'Inserisci',
|
||||||
|
style: TextStyle(color: Colors.orange),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return BlocConsumer<TaskFormCubit, TaskFormState>(
|
return BlocConsumer<TaskFormCubit, TaskFormState>(
|
||||||
@@ -79,7 +137,13 @@ class _TaskFormScreenState extends State<TaskFormScreen> {
|
|||||||
else
|
else
|
||||||
TextButton.icon(
|
TextButton.icon(
|
||||||
onPressed: state.isFormValid
|
onPressed: state.isFormValid
|
||||||
? () => cubit.saveTask(currentUserId: 'TODO_USER_ID')
|
? () => cubit.saveTask(
|
||||||
|
currentUserId: GetIt.I
|
||||||
|
.get<SessionCubit>()
|
||||||
|
.state
|
||||||
|
.currentStaffMember!
|
||||||
|
.id!,
|
||||||
|
)
|
||||||
: null,
|
: null,
|
||||||
icon: const Icon(Icons.save),
|
icon: const Icon(Icons.save),
|
||||||
label: const Text('Salva'),
|
label: const Text('Salva'),
|
||||||
@@ -162,7 +226,8 @@ class _TaskFormScreenState extends State<TaskFormScreen> {
|
|||||||
TaskFormState state,
|
TaskFormState state,
|
||||||
TaskFormCubit cubit,
|
TaskFormCubit cubit,
|
||||||
) {
|
) {
|
||||||
return Column(
|
return FocusTraversalGroup(
|
||||||
|
child: Column(
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
children: [
|
children: [
|
||||||
Card(
|
Card(
|
||||||
@@ -220,7 +285,8 @@ class _TaskFormScreenState extends State<TaskFormScreen> {
|
|||||||
leading: const Icon(Icons.calendar_today, color: Colors.orange),
|
leading: const Icon(Icons.calendar_today, color: Colors.orange),
|
||||||
title: Text(
|
title: Text(
|
||||||
state.dueDate != null
|
state.dueDate != null
|
||||||
? 'Scadenza: ${state.dueDate!.day}/${state.dueDate!.month}/${state.dueDate!.year}'
|
// Formattiamo aggiungendo gli zeri (es. 05/09/2026 alle 09:05)
|
||||||
|
? 'Scadenza: ${state.dueDate!.day.toString().padLeft(2, '0')}/${state.dueDate!.month.toString().padLeft(2, '0')}/${state.dueDate!.year} alle ${state.dueDate!.hour.toString().padLeft(2, '0')}:${state.dueDate!.minute.toString().padLeft(2, '0')}'
|
||||||
: 'Nessuna scadenza impostata',
|
: 'Nessuna scadenza impostata',
|
||||||
),
|
),
|
||||||
trailing: state.dueDate != null
|
trailing: state.dueDate != null
|
||||||
@@ -230,16 +296,111 @@ class _TaskFormScreenState extends State<TaskFormScreen> {
|
|||||||
)
|
)
|
||||||
: null,
|
: null,
|
||||||
onTap: () async {
|
onTap: () async {
|
||||||
|
// 1. Chiediamo prima la Data
|
||||||
final date = await showDatePicker(
|
final date = await showDatePicker(
|
||||||
context: context,
|
context: context,
|
||||||
initialDate: state.dueDate ?? DateTime.now(),
|
initialDate: state.dueDate ?? DateTime.now(),
|
||||||
firstDate: DateTime.now(),
|
firstDate: DateTime.now(),
|
||||||
lastDate: DateTime(2100),
|
lastDate: DateTime(2100),
|
||||||
);
|
);
|
||||||
if (date != null) cubit.updateDueDate(date);
|
|
||||||
|
// Se l'utente chiude il calendario senza scegliere, ci fermiamo
|
||||||
|
if (date == null || !context.mounted) return;
|
||||||
|
|
||||||
|
// 2. Chiediamo subito dopo l'Orario
|
||||||
|
final time = await showTimePicker(
|
||||||
|
context: context,
|
||||||
|
initialTime: state.dueDate != null
|
||||||
|
? TimeOfDay.fromDateTime(state.dueDate!)
|
||||||
|
: const TimeOfDay(hour: 9, minute: 0), // Default ore 09:00
|
||||||
|
);
|
||||||
|
|
||||||
|
// Se l'utente chiude l'orologio senza scegliere, ci fermiamo
|
||||||
|
if (time == null) return;
|
||||||
|
|
||||||
|
// 3. Fondiamo Data e Ora in un nuovo oggetto DateTime
|
||||||
|
final finalDateTime = DateTime(
|
||||||
|
date.year,
|
||||||
|
date.month,
|
||||||
|
date.day,
|
||||||
|
time.hour,
|
||||||
|
time.minute,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Aggiorniamo lo stato tramite il Cubit
|
||||||
|
cubit.updateDueDate(finalDateTime);
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
if (state.dueDate != null) ...[
|
||||||
|
const SizedBox(height: 24),
|
||||||
|
Text(
|
||||||
|
'Promemoria del Task',
|
||||||
|
style: Theme.of(
|
||||||
|
context,
|
||||||
|
).textTheme.titleMedium?.copyWith(fontWeight: FontWeight.bold),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 8),
|
||||||
|
Card(
|
||||||
|
elevation: 0,
|
||||||
|
shape: RoundedRectangleBorder(
|
||||||
|
borderRadius: BorderRadius.circular(12),
|
||||||
|
side: BorderSide(
|
||||||
|
color: Theme.of(context).dividerColor.withValues(alpha: 0.3),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
child: Padding(
|
||||||
|
padding: const EdgeInsets.all(12.0),
|
||||||
|
child: Column(
|
||||||
|
children: [
|
||||||
|
// Elenco dei promemoria attuali del form
|
||||||
|
ListView.builder(
|
||||||
|
shrinkWrap: true,
|
||||||
|
physics: const NeverScrollableScrollPhysics(),
|
||||||
|
itemCount: state.reminders.length,
|
||||||
|
itemBuilder: (context, index) {
|
||||||
|
final reminder = state.reminders[index];
|
||||||
|
final isPush = reminder.channel == 'push';
|
||||||
|
|
||||||
|
return ListTile(
|
||||||
|
dense: true,
|
||||||
|
leading: Icon(
|
||||||
|
isPush
|
||||||
|
? Icons.notifications_active_outlined
|
||||||
|
: Icons.mail_outline,
|
||||||
|
color: isPush ? Colors.orange : Colors.blue,
|
||||||
|
),
|
||||||
|
title: Text(
|
||||||
|
reminder.friendlyTime,
|
||||||
|
style: const TextStyle(fontWeight: FontWeight.w600),
|
||||||
|
),
|
||||||
|
trailing: IconButton(
|
||||||
|
icon: const Icon(
|
||||||
|
Icons.close,
|
||||||
|
size: 18,
|
||||||
|
color: Colors.redAccent,
|
||||||
|
),
|
||||||
|
onPressed: () => cubit.removeReminderRule(index),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
const Divider(),
|
||||||
|
// Tasto di aggiunta rapida promemoria
|
||||||
|
TextButton.icon(
|
||||||
|
onPressed: () => _showAddReminderDialog(context, cubit),
|
||||||
|
icon: const Icon(Icons.add, size: 18),
|
||||||
|
label: const Text('Aggiungi un promemoria a questo task'),
|
||||||
|
style: TextButton.styleFrom(
|
||||||
|
foregroundColor: Colors.orange,
|
||||||
|
),
|
||||||
|
),
|
||||||
],
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
],
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import 'package:flux/features/auth/bloc/auth_cubit.dart';
|
|||||||
import 'package:flux/features/company/data/company_repository.dart';
|
import 'package:flux/features/company/data/company_repository.dart';
|
||||||
import 'package:flux/features/notes/blocs/notes_bloc.dart';
|
import 'package:flux/features/notes/blocs/notes_bloc.dart';
|
||||||
import 'package:flux/features/notes/data/notes_repository.dart';
|
import 'package:flux/features/notes/data/notes_repository.dart';
|
||||||
|
import 'package:flux/features/settings/data/settings_repository.dart';
|
||||||
import 'package:flux/features/tasks/data/task_repository.dart';
|
import 'package:flux/features/tasks/data/task_repository.dart';
|
||||||
import 'package:flux/features/tickets/data/tickets_shipping_repository.dart';
|
import 'package:flux/features/tickets/data/tickets_shipping_repository.dart';
|
||||||
import 'package:flux/features/master_data/providers/blocs/provider_list_cubit.dart';
|
import 'package:flux/features/master_data/providers/blocs/provider_list_cubit.dart';
|
||||||
@@ -40,7 +41,7 @@ import 'package:flux/features/master_data/staff/blocs/staff_cubit.dart';
|
|||||||
import 'package:flux/features/master_data/staff/data/staff_repository.dart';
|
import 'package:flux/features/master_data/staff/data/staff_repository.dart';
|
||||||
import 'package:flux/features/master_data/store/bloc/store_cubit.dart';
|
import 'package:flux/features/master_data/store/bloc/store_cubit.dart';
|
||||||
import 'package:flux/features/master_data/store/data/store_repository.dart';
|
import 'package:flux/features/master_data/store/data/store_repository.dart';
|
||||||
import 'package:flux/features/settings/settings.dart';
|
import 'package:flux/features/settings/ui/settings.dart';
|
||||||
import 'package:flutter_web_plugins/url_strategy.dart';
|
import 'package:flutter_web_plugins/url_strategy.dart';
|
||||||
import 'package:url_launcher/url_launcher.dart';
|
import 'package:url_launcher/url_launcher.dart';
|
||||||
|
|
||||||
@@ -138,6 +139,7 @@ Future<void> setupLocator() async {
|
|||||||
);
|
);
|
||||||
getIt.registerLazySingleton<NotesRepository>(() => NotesRepository());
|
getIt.registerLazySingleton<NotesRepository>(() => NotesRepository());
|
||||||
getIt.registerLazySingleton<TaskRepository>(() => TaskRepository());
|
getIt.registerLazySingleton<TaskRepository>(() => TaskRepository());
|
||||||
|
getIt.registerLazySingleton<SettingsRepository>(() => SettingsRepository());
|
||||||
}
|
}
|
||||||
|
|
||||||
class FluxApp extends StatefulWidget {
|
class FluxApp extends StatefulWidget {
|
||||||
|
|||||||
Reference in New Issue
Block a user