clean up
This commit is contained in:
159
lib/domain/entities/user.dart
Normal file
159
lib/domain/entities/user.dart
Normal file
@@ -0,0 +1,159 @@
|
||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
import 'package:fcs/helpers/const.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
|
||||
DateFormat dayFormat = DateFormat("MMM dd yyyy");
|
||||
DateFormat timeFormat = DateFormat("HH:mm");
|
||||
final DateFormat dateFormat = DateFormat("d MMM yyyy");
|
||||
|
||||
class User {
|
||||
String id;
|
||||
String name;
|
||||
String phoneNumber;
|
||||
String status;
|
||||
String fcsID;
|
||||
DateTime lastMessageTime;
|
||||
String lastMessage;
|
||||
int userUnseenCount;
|
||||
int fcsUnseenCount;
|
||||
|
||||
String get initial => name != null && name != "" ? name.substring(0, 1) : "?";
|
||||
|
||||
String get getLastMessage {
|
||||
var msg = lastMessage ?? "Say hi to $name";
|
||||
if (msg.length > 30) return msg.substring(0, 30) + " ... ";
|
||||
return msg;
|
||||
}
|
||||
|
||||
String get getLastMessageTime {
|
||||
if (lastMessageTime == null) return "";
|
||||
DateTime today = DateTime.now();
|
||||
if (lastMessageTime.year == today.year &&
|
||||
lastMessageTime.month == today.month &&
|
||||
lastMessageTime.day == today.day) {
|
||||
return timeFormat.format(lastMessageTime);
|
||||
} else {
|
||||
return dateFormat.format(lastMessageTime);
|
||||
}
|
||||
}
|
||||
|
||||
String get getUserUnseenCount => userUnseenCount != null
|
||||
? userUnseenCount > 100 ? "99+" : userUnseenCount.toString()
|
||||
: "0";
|
||||
String get getFcsUnseenCount => fcsUnseenCount != null
|
||||
? fcsUnseenCount > 100 ? "99+" : fcsUnseenCount.toString()
|
||||
: "0";
|
||||
|
||||
List<String> privileges = [];
|
||||
|
||||
String get phone => phoneNumber != null && phoneNumber.startsWith("959")
|
||||
? "0${phoneNumber.substring(2)}"
|
||||
: phoneNumber;
|
||||
bool get joined => status != null && status == userStatusJoined;
|
||||
bool get invited => status != null && status == userStatusInvited;
|
||||
bool get requested => status != null && status == userStatusRequested;
|
||||
String get share => "Your phone number:$phoneNumber";
|
||||
User(
|
||||
{this.id,
|
||||
this.name,
|
||||
this.phoneNumber,
|
||||
this.fcsID,
|
||||
this.status,
|
||||
this.privileges,
|
||||
this.lastMessage,
|
||||
this.lastMessageTime,
|
||||
this.userUnseenCount,
|
||||
this.fcsUnseenCount});
|
||||
|
||||
factory User.fromJson(Map<String, dynamic> json) {
|
||||
return User(
|
||||
id: json['id'],
|
||||
name: json['user_name'],
|
||||
fcsID: json['fcs_id'],
|
||||
phoneNumber: json['phone_number'],
|
||||
status: json['status'],
|
||||
lastMessage: json['last_message'],
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() => {
|
||||
'id': id,
|
||||
'user_name': name,
|
||||
'phone_number': phoneNumber,
|
||||
};
|
||||
|
||||
Map<String, dynamic> toMap() {
|
||||
return {
|
||||
'user_name': name,
|
||||
'phone_number': phoneNumber,
|
||||
};
|
||||
}
|
||||
|
||||
factory User.fromMap(Map<String, dynamic> map, String docID) {
|
||||
var _date = (map['message_time'] as Timestamp);
|
||||
|
||||
List<String> _privileges =
|
||||
map['privileges'] == null ? [] : map['privileges'].cast<String>();
|
||||
|
||||
return User(
|
||||
id: docID,
|
||||
name: map['user_name'],
|
||||
phoneNumber: map['phone_number'],
|
||||
status: map['status'],
|
||||
fcsID: map['fcs_id'],
|
||||
privileges: _privileges,
|
||||
lastMessage: map['last_message'],
|
||||
userUnseenCount: map['user_unseen_count'],
|
||||
fcsUnseenCount: map['fcs_unseen_count'],
|
||||
lastMessageTime: _date == null ? null : _date.toDate());
|
||||
}
|
||||
|
||||
bool diffPrivileges(User another) {
|
||||
another.privileges.sort((a, b) => a.compareTo(b));
|
||||
privileges.sort((a, b) => a.compareTo(b));
|
||||
return !listEquals(another.privileges, privileges);
|
||||
}
|
||||
|
||||
bool isCustomer() {
|
||||
return privileges == null || privileges.length == 0;
|
||||
}
|
||||
|
||||
bool hasSysAdmin() {
|
||||
return privileges != null ? privileges.contains('sa') : false;
|
||||
}
|
||||
|
||||
bool hasAdmin() {
|
||||
return privileges != null ? privileges.contains('admin') : false;
|
||||
}
|
||||
|
||||
bool hasCustomers() {
|
||||
return hasSysAdmin() ||
|
||||
hasAdmin() ||
|
||||
(privileges != null ? privileges.contains('c') : false);
|
||||
}
|
||||
|
||||
bool hasStaffs() {
|
||||
return hasSysAdmin() ||
|
||||
hasAdmin() ||
|
||||
(privileges != null ? privileges.contains('s') : false);
|
||||
}
|
||||
|
||||
bool hasSupport() {
|
||||
return hasSysAdmin() ||
|
||||
hasAdmin() ||
|
||||
(privileges != null ? privileges.contains('sp') : false);
|
||||
}
|
||||
|
||||
bool hasPackages() {
|
||||
return hasSysAdmin() ||
|
||||
hasAdmin() ||
|
||||
status == userStatusJoined ||
|
||||
(privileges != null ? privileges.contains('p') : false);
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'User{id:$id, name: $name, phoneNumber: $phoneNumber,status:$status}';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user