107 lines
2.6 KiB
Dart
107 lines
2.6 KiB
Dart
import 'package:hive/hive.dart';
|
|
|
|
part 'message.g.dart';
|
|
|
|
/// Modèle simple de message
|
|
@HiveType(typeId: 51)
|
|
class Message extends HiveObject {
|
|
@HiveField(0)
|
|
final String id;
|
|
|
|
@HiveField(1)
|
|
final String roomId;
|
|
|
|
@HiveField(2)
|
|
final String content;
|
|
|
|
@HiveField(3)
|
|
final int senderId;
|
|
|
|
@HiveField(4)
|
|
final String senderName;
|
|
|
|
@HiveField(5)
|
|
final DateTime sentAt;
|
|
|
|
@HiveField(6)
|
|
final bool isMe;
|
|
|
|
@HiveField(7)
|
|
final bool isRead;
|
|
|
|
@HiveField(8)
|
|
final String? senderFirstName;
|
|
|
|
@HiveField(9)
|
|
final int? readCount;
|
|
|
|
@HiveField(10)
|
|
final bool isSynced;
|
|
|
|
Message({
|
|
required this.id,
|
|
required this.roomId,
|
|
required this.content,
|
|
required this.senderId,
|
|
required this.senderName,
|
|
required this.sentAt,
|
|
this.isMe = false,
|
|
this.isRead = false,
|
|
this.senderFirstName,
|
|
this.readCount,
|
|
this.isSynced = true,
|
|
});
|
|
|
|
// Simple factory depuis JSON
|
|
factory Message.fromJson(Map<String, dynamic> json, int currentUserId, {String? roomId}) {
|
|
return Message(
|
|
id: json['id'] ?? '',
|
|
roomId: roomId ?? json['room_id'] ?? json['fk_room'] ?? '',
|
|
content: json['content'] ?? '',
|
|
senderId: json['sender_id'] ?? json['fk_user'] ?? 0,
|
|
senderName: json['sender_name'] ?? 'Anonyme',
|
|
sentAt: DateTime.parse(json['sent_at'] ?? json['date_sent']),
|
|
isMe: json['is_mine'] ?? (json['sender_id'] == currentUserId || json['fk_user'] == currentUserId),
|
|
isRead: json['is_read'] ?? json['statut'] == 'lu' ?? false,
|
|
senderFirstName: json['sender_first_name'],
|
|
readCount: json['read_count'],
|
|
isSynced: json['is_synced'] ?? true,
|
|
);
|
|
}
|
|
|
|
// Simple conversion en JSON pour envoi
|
|
Map<String, dynamic> toJson() => {
|
|
'fk_room': roomId,
|
|
'content': content,
|
|
'fk_user': senderId,
|
|
};
|
|
|
|
// Méthode copyWith pour faciliter les mises à jour
|
|
Message copyWith({
|
|
String? id,
|
|
String? roomId,
|
|
String? content,
|
|
int? senderId,
|
|
String? senderName,
|
|
DateTime? sentAt,
|
|
bool? isMe,
|
|
bool? isRead,
|
|
String? senderFirstName,
|
|
int? readCount,
|
|
bool? isSynced,
|
|
}) {
|
|
return Message(
|
|
id: id ?? this.id,
|
|
roomId: roomId ?? this.roomId,
|
|
content: content ?? this.content,
|
|
senderId: senderId ?? this.senderId,
|
|
senderName: senderName ?? this.senderName,
|
|
sentAt: sentAt ?? this.sentAt,
|
|
isMe: isMe ?? this.isMe,
|
|
isRead: isRead ?? this.isRead,
|
|
senderFirstName: senderFirstName ?? this.senderFirstName,
|
|
readCount: readCount ?? this.readCount,
|
|
isSynced: isSynced ?? this.isSynced,
|
|
);
|
|
}
|
|
} |