1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-26 23:09:21 +00:00
comunicmobile/lib/models/conversation.dart

89 lines
3.0 KiB
Dart
Raw Permalink Normal View History

import 'package:comunic/helpers/database/database_contract.dart';
import 'package:comunic/models/cache_model.dart';
2019-05-01 07:24:50 +00:00
import 'package:comunic/utils/account_utils.dart';
import 'package:comunic/utils/list_utils.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 }
class Conversation extends CacheModel implements Comparable {
2019-04-23 12:35:41 +00:00
final int ownerID;
final int lastActive;
final String name;
final bool following;
final bool sawLastMessage;
final List<int> 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
const Conversation({
@required int id,
2019-04-23 12:35:41 +00:00
@required this.ownerID,
@required this.lastActive,
@required this.name,
@required this.following,
@required this.sawLastMessage,
@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),
assert(ownerID != null),
assert(lastActive != null),
assert(following != null),
assert(sawLastMessage != null),
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),
assert(isHavingCall != null),
super(id: id);
/// 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-05-01 07:24:50 +00:00
/// Check out whether current user of the application is the owner of it or
/// not
bool get isOwner => this.ownerID == userID();
Conversation.fromMap(Map<String, dynamic> 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,
2020-04-20 08:37:59 +00:00
members =
listToIntList(map[ConversationTableContract.C_MEMBERS].split(",")),
2020-04-26 12:02:57 +00:00
canEveryoneAddMembers =
map[ConversationTableContract.C_CAN_EVERYONE_ADD_MEMBERS] == 1,
2020-04-20 08:37:59 +00:00
// By default, we can not do any call
callCapabilities = CallCapabilities.NONE,
isHavingCall = false,
super.fromMap(map);
@override
Map<String, dynamic> 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(","),
2020-04-26 12:02:57 +00:00
ConversationTableContract.C_CAN_EVERYONE_ADD_MEMBERS:
canEveryoneAddMembers ? 1 : 0
};
}
@override
int compareTo(other) {
return other.lastActive.compareTo(this.lastActive);
}
2019-04-23 12:35:41 +00:00
}