2023-04-29 15:26:12 -04:00
|
|
|
import 'dart:developer';
|
|
|
|
|
2023-05-01 16:00:56 -04:00
|
|
|
import 'package:contacts_plus_plus/models/session.dart';
|
2023-05-01 13:13:40 -04:00
|
|
|
import 'package:contacts_plus_plus/models/user_profile.dart';
|
2023-04-29 15:26:12 -04:00
|
|
|
|
|
|
|
class Friend extends Comparable {
|
2023-04-29 13:18:46 -04:00
|
|
|
final String id;
|
|
|
|
final String username;
|
|
|
|
final UserStatus userStatus;
|
2023-04-30 07:39:09 -04:00
|
|
|
final UserProfile userProfile;
|
2023-04-30 17:14:29 -04:00
|
|
|
final FriendStatus friendStatus;
|
2023-04-29 13:18:46 -04:00
|
|
|
|
2023-04-30 17:14:29 -04:00
|
|
|
Friend({required this.id, required this.username, required this.userStatus, required this.userProfile,
|
|
|
|
required this.friendStatus,
|
|
|
|
});
|
2023-04-29 15:26:12 -04:00
|
|
|
|
|
|
|
factory Friend.fromMap(Map map) {
|
2023-04-30 07:39:09 -04:00
|
|
|
return Friend(
|
|
|
|
id: map["id"],
|
|
|
|
username: map["friendUsername"],
|
|
|
|
userStatus: UserStatus.fromMap(map["userStatus"]),
|
2023-04-30 17:14:29 -04:00
|
|
|
userProfile: UserProfile.fromMap(map["profile"] ?? {}),
|
|
|
|
friendStatus: FriendStatus.fromString(map["friendStatus"]),
|
2023-04-30 07:39:09 -04:00
|
|
|
);
|
2023-04-29 15:26:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
int compareTo(other) {
|
|
|
|
if (userStatus.onlineStatus == other.userStatus.onlineStatus) {
|
|
|
|
return userStatus.lastStatusChange.compareTo(other.userStatus.lastStatusChange);
|
|
|
|
} else {
|
|
|
|
if (userStatus.onlineStatus == OnlineStatus.online) {
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-29 13:18:46 -04:00
|
|
|
}
|
|
|
|
|
2023-04-30 17:14:29 -04:00
|
|
|
enum FriendStatus {
|
|
|
|
none,
|
|
|
|
searchResult,
|
|
|
|
requested,
|
|
|
|
ignored,
|
|
|
|
blocked,
|
|
|
|
accepted;
|
|
|
|
|
|
|
|
factory FriendStatus.fromString(String text) {
|
|
|
|
return FriendStatus.values.firstWhere((element) => element.name.toLowerCase() == text.toLowerCase(),
|
|
|
|
orElse: () => FriendStatus.none,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-29 13:18:46 -04:00
|
|
|
enum OnlineStatus {
|
2023-04-29 15:26:12 -04:00
|
|
|
unknown,
|
2023-04-29 13:18:46 -04:00
|
|
|
offline,
|
2023-04-29 15:26:12 -04:00
|
|
|
away,
|
|
|
|
busy,
|
2023-04-30 17:14:29 -04:00
|
|
|
online;
|
|
|
|
|
|
|
|
factory OnlineStatus.fromString(String? text) {
|
|
|
|
return OnlineStatus.values.firstWhere((element) => element.name.toLowerCase() == text?.toLowerCase(),
|
|
|
|
orElse: () => OnlineStatus.unknown,
|
|
|
|
);
|
|
|
|
}
|
2023-04-29 13:18:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
class UserStatus {
|
|
|
|
final OnlineStatus onlineStatus;
|
|
|
|
final DateTime lastStatusChange;
|
2023-04-30 17:14:29 -04:00
|
|
|
final List<Session> activeSessions;
|
2023-04-29 13:18:46 -04:00
|
|
|
|
2023-04-30 17:14:29 -04:00
|
|
|
|
|
|
|
UserStatus({required this.onlineStatus, required this.lastStatusChange, required this.activeSessions});
|
2023-04-29 15:26:12 -04:00
|
|
|
|
|
|
|
factory UserStatus.fromMap(Map map) {
|
|
|
|
final statusString = map["onlineStatus"] as String?;
|
2023-04-30 17:14:29 -04:00
|
|
|
final status = OnlineStatus.fromString(statusString);
|
2023-04-29 15:26:12 -04:00
|
|
|
if (status == OnlineStatus.unknown && statusString != null) {
|
|
|
|
log("Unknown OnlineStatus '$statusString' in response");
|
|
|
|
}
|
|
|
|
return UserStatus(
|
|
|
|
onlineStatus: status,
|
|
|
|
lastStatusChange: DateTime.parse(map["lastStatusChange"]),
|
2023-04-30 17:14:29 -04:00
|
|
|
activeSessions: (map["activeSessions"] as List? ?? []).map((e) => Session.fromMap(e)).toList(),
|
2023-04-29 15:26:12 -04:00
|
|
|
);
|
|
|
|
}
|
2023-04-29 13:18:46 -04:00
|
|
|
}
|