import 'package:comunic/helpers/database/database_contract.dart'; import 'package:comunic/models/cache_model.dart'; import 'package:comunic/utils/account_utils.dart'; import 'package:comunic/utils/list_utils.dart'; import 'package:meta/meta.dart'; /// Conversation model /// /// @author Pierre HUBERT class Conversation extends CacheModel implements Comparable { final int ownerID; final int lastActive; final String name; final bool following; final bool sawLastMessage; final List members; const Conversation({ @required int id, @required this.ownerID, @required this.lastActive, @required this.name, @required this.following, @required this.sawLastMessage, @required this.members, }) : assert(id != null), assert(ownerID != null), assert(lastActive != null), assert(following != null), assert(sawLastMessage != null), assert(members != null), super(id: id); /// Check out whether a conversation has a fixed name or not bool get hasName => this.name != null; /// Check out whether current user of the application is the owner of it or /// not bool get isOwner => this.ownerID == userID(); Conversation.fromMap(Map map) : ownerID = map[ConversationTableContract.C_OWNER_ID], lastActive = map[ConversationTableContract.C_LAST_ACTIVE], name = map[ConversationTableContract.C_NAME], following = map[ConversationTableContract.C_FOLLOWING] == 1, sawLastMessage = map[ConversationTableContract.C_SAW_LAST_MESSAGE] == 1, members = listToIntList( map[ConversationTableContract.C_MEMBERS].split(",")), super.fromMap(map); @override Map toMap() { return { ConversationTableContract.C_ID: id, ConversationTableContract.C_OWNER_ID: ownerID, ConversationTableContract.C_LAST_ACTIVE: lastActive, ConversationTableContract.C_NAME: name, ConversationTableContract.C_FOLLOWING: following ? 1 : 0, ConversationTableContract.C_SAW_LAST_MESSAGE: sawLastMessage ? 1 : 0, ConversationTableContract.C_MEMBERS: members.join(","), }; } @override int compareTo(other) { return other.lastActive.compareTo(this.lastActive); } }