2023-05-29 14:16:23 -04:00
|
|
|
import 'package:contacts_plus_plus/auxiliary.dart';
|
|
|
|
import 'package:contacts_plus_plus/models/users/user_profile.dart';
|
|
|
|
import 'package:contacts_plus_plus/models/users/friend_status.dart';
|
|
|
|
import 'package:contacts_plus_plus/models/users/online_status.dart';
|
|
|
|
import 'package:contacts_plus_plus/models/users/user_status.dart';
|
|
|
|
|
|
|
|
class Friend implements Comparable {
|
|
|
|
static const _emptyId = "-1";
|
2023-09-30 06:22:32 -04:00
|
|
|
static const _resoniteBotId = "U-Resonite";
|
2023-05-29 14:16:23 -04:00
|
|
|
final String id;
|
|
|
|
final String username;
|
|
|
|
final String ownerId;
|
|
|
|
final UserStatus userStatus;
|
|
|
|
final UserProfile userProfile;
|
2023-09-29 09:33:43 -04:00
|
|
|
final FriendStatus contactStatus;
|
2023-05-29 14:16:23 -04:00
|
|
|
final DateTime latestMessageTime;
|
|
|
|
|
|
|
|
const Friend({required this.id, required this.username, required this.ownerId, required this.userStatus, required this.userProfile,
|
2023-09-29 09:33:43 -04:00
|
|
|
required this.contactStatus, required this.latestMessageTime,
|
2023-05-29 14:16:23 -04:00
|
|
|
});
|
|
|
|
|
2023-09-29 10:24:26 -04:00
|
|
|
bool get isHeadless => userStatus.outputDevice == "Headless";
|
2023-05-29 14:16:23 -04:00
|
|
|
|
|
|
|
factory Friend.fromMap(Map map) {
|
2023-09-29 09:33:43 -04:00
|
|
|
var userStatus = map["userStatus"] == null ? UserStatus.empty() : UserStatus.fromMap(map["userStatus"]);
|
2023-05-29 14:16:23 -04:00
|
|
|
return Friend(
|
|
|
|
id: map["id"],
|
2023-09-29 06:30:43 -04:00
|
|
|
username: map["contactUsername"],
|
2023-05-29 14:16:23 -04:00
|
|
|
ownerId: map["ownerId"] ?? map["id"],
|
|
|
|
// Neos bot status is always offline but should be displayed as online
|
2023-09-30 06:22:32 -04:00
|
|
|
userStatus: map["id"] == _resoniteBotId ? userStatus.copyWith(onlineStatus: OnlineStatus.online) : userStatus,
|
2023-05-29 14:16:23 -04:00
|
|
|
userProfile: UserProfile.fromMap(map["profile"] ?? {}),
|
2023-09-29 09:33:43 -04:00
|
|
|
contactStatus: FriendStatus.fromString(map["contactStatus"]),
|
2023-05-29 14:16:23 -04:00
|
|
|
latestMessageTime: map["latestMessageTime"] == null
|
|
|
|
? DateTime.fromMillisecondsSinceEpoch(0) : DateTime.parse(map["latestMessageTime"]),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Friend? fromMapOrNull(Map? map) {
|
|
|
|
if (map == null) return null;
|
|
|
|
return Friend.fromMap(map);
|
|
|
|
}
|
|
|
|
|
|
|
|
factory Friend.empty() {
|
|
|
|
return Friend(
|
|
|
|
id: _emptyId,
|
|
|
|
username: "",
|
|
|
|
ownerId: "",
|
|
|
|
userStatus: UserStatus.empty(),
|
|
|
|
userProfile: UserProfile.empty(),
|
2023-09-29 09:33:43 -04:00
|
|
|
contactStatus: FriendStatus.none,
|
2023-05-29 14:16:23 -04:00
|
|
|
latestMessageTime: DateTimeX.epoch
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool get isEmpty => id == _emptyId;
|
|
|
|
|
|
|
|
Friend copyWith({
|
|
|
|
String? id, String? username, String? ownerId, UserStatus? userStatus, UserProfile? userProfile,
|
2023-09-30 08:20:08 -04:00
|
|
|
FriendStatus? contactStatus, DateTime? latestMessageTime}) {
|
2023-05-29 14:16:23 -04:00
|
|
|
return Friend(
|
|
|
|
id: id ?? this.id,
|
|
|
|
username: username ?? this.username,
|
|
|
|
ownerId: ownerId ?? this.ownerId,
|
|
|
|
userStatus: userStatus ?? this.userStatus,
|
|
|
|
userProfile: userProfile ?? this.userProfile,
|
2023-09-30 08:20:08 -04:00
|
|
|
contactStatus: contactStatus ?? this.contactStatus,
|
2023-05-29 14:16:23 -04:00
|
|
|
latestMessageTime: latestMessageTime ?? this.latestMessageTime,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Map toMap({bool shallow=false}) {
|
|
|
|
return {
|
|
|
|
"id": id,
|
2023-09-29 09:33:43 -04:00
|
|
|
"contactUsername": username,
|
2023-05-29 14:16:23 -04:00
|
|
|
"ownerId": ownerId,
|
|
|
|
"userStatus": userStatus.toMap(shallow: shallow),
|
|
|
|
"profile": userProfile.toMap(),
|
2023-09-29 09:33:43 -04:00
|
|
|
"contactStatus": contactStatus.name,
|
2023-05-29 14:16:23 -04:00
|
|
|
"latestMessageTime": latestMessageTime.toIso8601String(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
int compareTo(covariant Friend other) {
|
|
|
|
return username.compareTo(other.username);
|
|
|
|
}
|
|
|
|
}
|