161 lines
5.2 KiB
Dart
161 lines
5.2 KiB
Dart
import 'package:hive/hive.dart';
|
|
import 'package:equatable/equatable.dart';
|
|
|
|
part 'notification_settings.g.dart';
|
|
|
|
/// Paramètres de notification pour le chat
|
|
///
|
|
/// Permet à l'utilisateur de configurer ses préférences de notification
|
|
|
|
@HiveType(typeId: 25)
|
|
class NotificationSettings extends HiveObject with EquatableMixin {
|
|
@HiveField(0)
|
|
final bool enableNotifications;
|
|
|
|
@HiveField(1)
|
|
final bool soundEnabled;
|
|
|
|
@HiveField(2)
|
|
final bool vibrationEnabled;
|
|
|
|
@HiveField(3)
|
|
final List<String> mutedConversations;
|
|
|
|
@HiveField(4)
|
|
final bool showPreview;
|
|
|
|
@HiveField(5)
|
|
final Map<String, bool> conversationNotifications;
|
|
|
|
@HiveField(6)
|
|
final bool doNotDisturb;
|
|
|
|
@HiveField(7)
|
|
final DateTime? doNotDisturbStart;
|
|
|
|
@HiveField(8)
|
|
final DateTime? doNotDisturbEnd;
|
|
|
|
@HiveField(9)
|
|
final String? deviceToken;
|
|
|
|
NotificationSettings({
|
|
this.enableNotifications = true,
|
|
this.soundEnabled = true,
|
|
this.vibrationEnabled = true,
|
|
this.mutedConversations = const [],
|
|
this.showPreview = true,
|
|
this.conversationNotifications = const {},
|
|
this.doNotDisturb = false,
|
|
this.doNotDisturbStart,
|
|
this.doNotDisturbEnd,
|
|
this.deviceToken,
|
|
});
|
|
|
|
/// Crée une instance depuis le JSON
|
|
factory NotificationSettings.fromJson(Map<String, dynamic> json) {
|
|
return NotificationSettings(
|
|
enableNotifications: json['enable_notifications'] as bool? ?? true,
|
|
soundEnabled: json['sound_enabled'] as bool? ?? true,
|
|
vibrationEnabled: json['vibration_enabled'] as bool? ?? true,
|
|
mutedConversations: List<String>.from(json['muted_conversations'] ?? []),
|
|
showPreview: json['show_preview'] as bool? ?? true,
|
|
conversationNotifications: Map<String, bool>.from(json['conversation_notifications'] ?? {}),
|
|
doNotDisturb: json['do_not_disturb'] as bool? ?? false,
|
|
doNotDisturbStart: json['do_not_disturb_start'] != null
|
|
? DateTime.parse(json['do_not_disturb_start'])
|
|
: null,
|
|
doNotDisturbEnd: json['do_not_disturb_end'] != null
|
|
? DateTime.parse(json['do_not_disturb_end'])
|
|
: null,
|
|
deviceToken: json['device_token'] as String?,
|
|
);
|
|
}
|
|
|
|
/// Convertit l'instance en JSON
|
|
Map<String, dynamic> toJson() {
|
|
return {
|
|
'enable_notifications': enableNotifications,
|
|
'sound_enabled': soundEnabled,
|
|
'vibration_enabled': vibrationEnabled,
|
|
'muted_conversations': mutedConversations,
|
|
'show_preview': showPreview,
|
|
'conversation_notifications': conversationNotifications,
|
|
'do_not_disturb': doNotDisturb,
|
|
'do_not_disturb_start': doNotDisturbStart?.toIso8601String(),
|
|
'do_not_disturb_end': doNotDisturbEnd?.toIso8601String(),
|
|
'device_token': deviceToken,
|
|
};
|
|
}
|
|
|
|
/// Crée une copie modifiée de l'instance
|
|
NotificationSettings copyWith({
|
|
bool? enableNotifications,
|
|
bool? soundEnabled,
|
|
bool? vibrationEnabled,
|
|
List<String>? mutedConversations,
|
|
bool? showPreview,
|
|
Map<String, bool>? conversationNotifications,
|
|
bool? doNotDisturb,
|
|
DateTime? doNotDisturbStart,
|
|
DateTime? doNotDisturbEnd,
|
|
String? deviceToken,
|
|
}) {
|
|
return NotificationSettings(
|
|
enableNotifications: enableNotifications ?? this.enableNotifications,
|
|
soundEnabled: soundEnabled ?? this.soundEnabled,
|
|
vibrationEnabled: vibrationEnabled ?? this.vibrationEnabled,
|
|
mutedConversations: mutedConversations ?? this.mutedConversations,
|
|
showPreview: showPreview ?? this.showPreview,
|
|
conversationNotifications: conversationNotifications ?? this.conversationNotifications,
|
|
doNotDisturb: doNotDisturb ?? this.doNotDisturb,
|
|
doNotDisturbStart: doNotDisturbStart ?? this.doNotDisturbStart,
|
|
doNotDisturbEnd: doNotDisturbEnd ?? this.doNotDisturbEnd,
|
|
deviceToken: deviceToken ?? this.deviceToken,
|
|
);
|
|
}
|
|
|
|
/// Vérifie si une conversation est en mode silencieux
|
|
bool isConversationMuted(String conversationId) {
|
|
return mutedConversations.contains(conversationId);
|
|
}
|
|
|
|
/// Vérifie si les notifications sont activées pour une conversation
|
|
bool areNotificationsEnabled(String conversationId) {
|
|
if (!enableNotifications) return false;
|
|
if (isConversationMuted(conversationId)) return false;
|
|
if (doNotDisturb && _isInDoNotDisturbPeriod()) return false;
|
|
|
|
return conversationNotifications[conversationId] ?? true;
|
|
}
|
|
|
|
/// Vérifie si on est dans la période "Ne pas déranger"
|
|
bool _isInDoNotDisturbPeriod() {
|
|
if (!doNotDisturb) return false;
|
|
if (doNotDisturbStart == null || doNotDisturbEnd == null) return false;
|
|
|
|
final now = DateTime.now();
|
|
if (doNotDisturbStart!.isBefore(doNotDisturbEnd!)) {
|
|
// Période normale (ex: 22h à 8h)
|
|
return now.isAfter(doNotDisturbStart!) && now.isBefore(doNotDisturbEnd!);
|
|
} else {
|
|
// Période qui chevauche minuit (ex: 20h à 6h)
|
|
return now.isAfter(doNotDisturbStart!) || now.isBefore(doNotDisturbEnd!);
|
|
}
|
|
}
|
|
|
|
@override
|
|
List<Object?> get props => [
|
|
enableNotifications,
|
|
soundEnabled,
|
|
vibrationEnabled,
|
|
mutedConversations,
|
|
showPreview,
|
|
conversationNotifications,
|
|
doNotDisturb,
|
|
doNotDisturbStart,
|
|
doNotDisturbEnd,
|
|
deviceToken,
|
|
];
|
|
}
|