2020-09-20 05:34:49 +06:30
|
|
|
import 'package:cloud_firestore/cloud_firestore.dart';
|
2020-10-07 02:33:06 +06:30
|
|
|
import 'package:fcs/helpers/const.dart';
|
2020-09-22 03:52:48 +06:30
|
|
|
import 'package:flutter/foundation.dart';
|
2020-09-20 05:34:49 +06:30
|
|
|
import 'package:intl/intl.dart';
|
|
|
|
|
|
|
|
|
|
DateFormat dayFormat = DateFormat("MMM dd yyyy");
|
|
|
|
|
DateFormat timeFormat = DateFormat("HH:mm");
|
2020-10-07 02:33:06 +06:30
|
|
|
final DateFormat dateFormat = DateFormat("d MMM yyyy");
|
2020-09-13 21:49:39 +06:30
|
|
|
|
2020-08-27 22:32:40 +06:30
|
|
|
class User {
|
|
|
|
|
String id;
|
|
|
|
|
String name;
|
|
|
|
|
String phoneNumber;
|
2020-09-13 21:49:39 +06:30
|
|
|
String status;
|
2020-08-27 22:32:40 +06:30
|
|
|
String fcsID;
|
2020-09-20 05:34:49 +06:30
|
|
|
DateTime lastMessageTime;
|
|
|
|
|
String lastMessage;
|
|
|
|
|
int userUnseenCount;
|
|
|
|
|
int fcsUnseenCount;
|
2020-10-11 02:17:23 +06:30
|
|
|
String preferCurrency;
|
2020-09-20 05:34:49 +06:30
|
|
|
|
|
|
|
|
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";
|
2020-09-11 16:14:36 +06:30
|
|
|
|
2020-09-13 21:49:39 +06:30
|
|
|
List<String> privileges = [];
|
|
|
|
|
|
2020-08-27 22:32:40 +06:30
|
|
|
String get phone => phoneNumber != null && phoneNumber.startsWith("959")
|
|
|
|
|
? "0${phoneNumber.substring(2)}"
|
|
|
|
|
: phoneNumber;
|
2020-09-13 21:49:39 +06:30
|
|
|
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";
|
2020-09-20 05:34:49 +06:30
|
|
|
User(
|
|
|
|
|
{this.id,
|
|
|
|
|
this.name,
|
|
|
|
|
this.phoneNumber,
|
|
|
|
|
this.fcsID,
|
|
|
|
|
this.status,
|
|
|
|
|
this.privileges,
|
|
|
|
|
this.lastMessage,
|
|
|
|
|
this.lastMessageTime,
|
|
|
|
|
this.userUnseenCount,
|
2020-10-11 02:17:23 +06:30
|
|
|
this.fcsUnseenCount,
|
|
|
|
|
this.preferCurrency});
|
2020-08-27 22:32:40 +06:30
|
|
|
|
|
|
|
|
factory User.fromJson(Map<String, dynamic> json) {
|
|
|
|
|
return User(
|
2020-09-11 16:14:36 +06:30
|
|
|
id: json['id'],
|
|
|
|
|
name: json['user_name'],
|
2020-09-15 07:13:41 +06:30
|
|
|
fcsID: json['fcs_id'],
|
2020-09-11 16:14:36 +06:30
|
|
|
phoneNumber: json['phone_number'],
|
2020-09-13 21:49:39 +06:30
|
|
|
status: json['status'],
|
2020-09-20 05:34:49 +06:30
|
|
|
lastMessage: json['last_message'],
|
2020-09-11 16:14:36 +06:30
|
|
|
);
|
2020-08-27 22:32:40 +06:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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) {
|
2020-09-20 05:34:49 +06:30
|
|
|
var _date = (map['message_time'] as Timestamp);
|
|
|
|
|
|
2020-09-13 21:49:39 +06:30
|
|
|
List<String> _privileges =
|
|
|
|
|
map['privileges'] == null ? [] : map['privileges'].cast<String>();
|
|
|
|
|
|
2020-08-27 22:32:40 +06:30
|
|
|
return User(
|
2020-09-13 21:49:39 +06:30
|
|
|
id: docID,
|
|
|
|
|
name: map['user_name'],
|
|
|
|
|
phoneNumber: map['phone_number'],
|
|
|
|
|
status: map['status'],
|
|
|
|
|
fcsID: map['fcs_id'],
|
2020-09-20 05:34:49 +06:30
|
|
|
privileges: _privileges,
|
|
|
|
|
lastMessage: map['last_message'],
|
|
|
|
|
userUnseenCount: map['user_unseen_count'],
|
|
|
|
|
fcsUnseenCount: map['fcs_unseen_count'],
|
2020-10-11 02:17:23 +06:30
|
|
|
preferCurrency: map['preferred_currency'],
|
2020-09-20 05:34:49 +06:30
|
|
|
lastMessageTime: _date == null ? null : _date.toDate());
|
2020-08-27 22:32:40 +06:30
|
|
|
}
|
|
|
|
|
|
2020-09-22 03:52:48 +06:30
|
|
|
bool diffPrivileges(User another) {
|
|
|
|
|
another.privileges.sort((a, b) => a.compareTo(b));
|
|
|
|
|
privileges.sort((a, b) => a.compareTo(b));
|
|
|
|
|
return !listEquals(another.privileges, privileges);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-11 16:14:36 +06:30
|
|
|
bool isCustomer() {
|
|
|
|
|
return privileges == null || privileges.length == 0;
|
2020-08-27 22:32:40 +06:30
|
|
|
}
|
|
|
|
|
|
2020-09-11 16:14:36 +06:30
|
|
|
bool hasSysAdmin() {
|
|
|
|
|
return privileges != null ? privileges.contains('sa') : false;
|
2020-08-27 22:32:40 +06:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool hasAdmin() {
|
2020-09-11 16:14:36 +06:30
|
|
|
return privileges != null ? privileges.contains('admin') : false;
|
2020-08-27 22:32:40 +06:30
|
|
|
}
|
|
|
|
|
|
2020-09-13 21:49:39 +06:30
|
|
|
bool hasCustomers() {
|
|
|
|
|
return hasSysAdmin() ||
|
|
|
|
|
hasAdmin() ||
|
|
|
|
|
(privileges != null ? privileges.contains('c') : false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool hasStaffs() {
|
|
|
|
|
return hasSysAdmin() ||
|
|
|
|
|
hasAdmin() ||
|
|
|
|
|
(privileges != null ? privileges.contains('s') : false);
|
2020-08-27 22:32:40 +06:30
|
|
|
}
|
|
|
|
|
|
2020-09-13 21:49:39 +06:30
|
|
|
bool hasSupport() {
|
|
|
|
|
return hasSysAdmin() ||
|
|
|
|
|
hasAdmin() ||
|
|
|
|
|
(privileges != null ? privileges.contains('sp') : false);
|
2020-08-27 22:32:40 +06:30
|
|
|
}
|
|
|
|
|
|
2020-09-15 07:13:41 +06:30
|
|
|
bool hasPackages() {
|
|
|
|
|
return hasSysAdmin() ||
|
|
|
|
|
hasAdmin() ||
|
2020-09-17 06:02:48 +06:30
|
|
|
status == userStatusJoined ||
|
2020-09-15 07:13:41 +06:30
|
|
|
(privileges != null ? privileges.contains('p') : false);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-27 22:32:40 +06:30
|
|
|
@override
|
|
|
|
|
String toString() {
|
2020-09-22 03:52:48 +06:30
|
|
|
return 'User{id:$id, name: $name, phoneNumber: $phoneNumber,status:$status}';
|
2020-08-27 22:32:40 +06:30
|
|
|
}
|
|
|
|
|
}
|