2021-03-10 16:54:41 +00:00
|
|
|
import 'package:comunic/helpers/serialization/base_serialization_helper.dart';
|
|
|
|
import 'package:comunic/models/conversation_member.dart';
|
2019-05-01 07:24:50 +00:00
|
|
|
import 'package:comunic/utils/account_utils.dart';
|
2021-03-10 17:04:29 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2019-04-23 12:35:41 +00:00
|
|
|
import 'package:meta/meta.dart';
|
|
|
|
|
|
|
|
/// Conversation model
|
|
|
|
///
|
|
|
|
/// @author Pierre HUBERT
|
|
|
|
|
2020-04-20 08:37:59 +00:00
|
|
|
enum CallCapabilities { NONE, AUDIO, VIDEO }
|
|
|
|
|
2021-03-10 16:54:41 +00:00
|
|
|
class Conversation extends SerializableElement<Conversation> {
|
|
|
|
final int id;
|
|
|
|
final int lastActivity;
|
2019-04-23 12:35:41 +00:00
|
|
|
final String name;
|
2021-03-10 17:04:29 +00:00
|
|
|
final Color color;
|
2021-03-10 16:54:41 +00:00
|
|
|
final String logoURL;
|
|
|
|
final int groupID;
|
|
|
|
final List<ConversationMember> members;
|
2020-04-26 12:02:57 +00:00
|
|
|
final bool canEveryoneAddMembers;
|
2020-04-20 08:37:59 +00:00
|
|
|
final CallCapabilities callCapabilities;
|
|
|
|
final bool isHavingCall;
|
2019-04-23 12:35:41 +00:00
|
|
|
|
2021-03-10 16:54:41 +00:00
|
|
|
Conversation({
|
|
|
|
@required this.id,
|
|
|
|
@required this.lastActivity,
|
2019-04-23 12:35:41 +00:00
|
|
|
@required this.name,
|
2021-03-10 16:54:41 +00:00
|
|
|
@required this.color,
|
|
|
|
@required this.logoURL,
|
|
|
|
@required this.groupID,
|
2019-04-23 12:35:41 +00:00
|
|
|
@required this.members,
|
2020-04-26 12:02:57 +00:00
|
|
|
@required this.canEveryoneAddMembers,
|
2020-04-20 08:37:59 +00:00
|
|
|
this.callCapabilities = CallCapabilities.NONE,
|
|
|
|
this.isHavingCall = false,
|
2019-04-23 12:35:41 +00:00
|
|
|
}) : assert(id != null),
|
2021-03-10 16:54:41 +00:00
|
|
|
assert(lastActivity != null),
|
2019-04-24 12:25:46 +00:00
|
|
|
assert(members != null),
|
2020-04-26 12:02:57 +00:00
|
|
|
assert(canEveryoneAddMembers != null),
|
2020-04-20 08:37:59 +00:00
|
|
|
assert(callCapabilities != null),
|
2021-03-10 16:54:41 +00:00
|
|
|
assert(isHavingCall != null);
|
2019-04-23 15:29:58 +00:00
|
|
|
|
|
|
|
/// Check out whether a conversation has a fixed name or not
|
2019-04-25 09:14:05 +00:00
|
|
|
bool get hasName => this.name != null;
|
2019-04-24 12:25:46 +00:00
|
|
|
|
2021-03-10 16:54:41 +00:00
|
|
|
/// Get current user membership
|
|
|
|
ConversationMember get membership =>
|
|
|
|
members.firstWhere((m) => m.userID == userID());
|
2019-05-01 07:24:50 +00:00
|
|
|
|
2021-03-10 16:54:41 +00:00
|
|
|
/// Check out whether current user of the application is an admin
|
|
|
|
bool get isAdmin => membership.isAdmin;
|
|
|
|
|
|
|
|
/// Check it current user is following the conversation or not
|
|
|
|
bool get following => membership.following;
|
|
|
|
|
|
|
|
/// Get the list of members in the conversation
|
|
|
|
Set<int> get membersID => members.map((e) => e.userID).toSet();
|
|
|
|
|
|
|
|
/// Check if the last message has been seen or not
|
|
|
|
bool get sawLastMessage => lastActivity <= membership.lastAccessTime;
|
|
|
|
|
|
|
|
Conversation.fromJson(Map<String, dynamic> map)
|
|
|
|
: id = map["id"],
|
|
|
|
name = map["name"],
|
2021-03-10 17:04:29 +00:00
|
|
|
color = map["color"] == null ? null : Color(map["color"]),
|
2021-03-10 16:54:41 +00:00
|
|
|
logoURL = map["logoURL"],
|
|
|
|
groupID = map["groupID"],
|
|
|
|
lastActivity = map["lastActivity"],
|
|
|
|
members = map["members"]
|
|
|
|
.map((el) => ConversationMember.fromJSON(el))
|
2021-03-10 23:02:41 +00:00
|
|
|
.toList()
|
|
|
|
.cast<ConversationMember>(),
|
2021-03-10 16:54:41 +00:00
|
|
|
canEveryoneAddMembers = map["canEveryoneAddMembers"],
|
2020-04-20 08:37:59 +00:00
|
|
|
|
|
|
|
// By default, we can not do any call
|
|
|
|
callCapabilities = CallCapabilities.NONE,
|
2021-03-10 16:54:41 +00:00
|
|
|
isHavingCall = false;
|
2019-04-24 12:25:46 +00:00
|
|
|
|
2021-03-10 16:54:41 +00:00
|
|
|
Map<String, dynamic> toJson() {
|
2019-04-24 12:25:46 +00:00
|
|
|
return {
|
2021-03-10 16:54:41 +00:00
|
|
|
"id": id,
|
|
|
|
"name": name,
|
2021-03-10 17:04:29 +00:00
|
|
|
"color": color?.value,
|
2021-03-10 16:54:41 +00:00
|
|
|
"logoURL": logoURL,
|
|
|
|
"groupID": groupID,
|
|
|
|
"lastActivity": lastActivity,
|
|
|
|
"members": members.map((e) => e.toJson()).toList(),
|
|
|
|
"canEveryoneAddMembers": canEveryoneAddMembers,
|
2019-04-24 12:25:46 +00:00
|
|
|
};
|
|
|
|
}
|
2019-04-24 13:03:36 +00:00
|
|
|
|
|
|
|
@override
|
2021-03-10 16:54:41 +00:00
|
|
|
int compareTo(Conversation other) {
|
|
|
|
return other.lastActivity.compareTo(this.lastActivity);
|
2019-04-24 13:03:36 +00:00
|
|
|
}
|
2019-04-23 12:35:41 +00:00
|
|
|
}
|