b
This commit is contained in:
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(
|
||||
padding: const EdgeInsets.all(16),
|
||||
children: [
|
||||
_settingsSection('Utente', [
|
||||
_settingsTile(
|
||||
title: 'Impostazioni Promemoria',
|
||||
icon: Icons.notifications,
|
||||
subtitle: 'Notifiche predefinite',
|
||||
context: context,
|
||||
onTap: () => context.pushNamed(Routes.reminderSettings),
|
||||
),
|
||||
]),
|
||||
_settingsSection('Azienda', [
|
||||
_settingsTile(
|
||||
title: 'Impostazioni Azienda',
|
||||
@@ -83,6 +92,7 @@ class SettingsScreen extends StatelessWidget {
|
||||
onTap: () => context.pushNamed(Routes.themeSettings),
|
||||
),
|
||||
]),
|
||||
|
||||
const SizedBox(height: 24),
|
||||
TextButton.icon(
|
||||
onPressed: () => context.read<SessionCubit>().signOut(),
|
||||
Reference in New Issue
Block a user