Files

205 lines
5.4 KiB
Dart
Raw Permalink Normal View History

2020-09-20 05:34:49 +06:30
import 'package:cloud_firestore/cloud_firestore.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';
2024-09-22 16:49:59 +06:30
import '../../constants.dart';
2020-10-12 03:34:05 +06:30
2020-09-20 05:34:49 +06:30
DateFormat dayFormat = DateFormat("MMM dd yyyy");
2024-03-01 17:27:56 +06:30
DateFormat timeFormat = DateFormat("hh:mm a");
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 {
2021-09-10 14:27:38 +06:30
String? id;
String? name;
String? phoneNumber;
String? status;
String? fcsID;
DateTime? lastMessageTime;
String? lastMessage;
2021-09-10 15:15:20 +06:30
int userUnseenCount;
int fcsUnseenCount;
2021-09-10 14:27:38 +06:30
String? preferCurrency;
bool enablePinLogin;
String? pinDigit;
2024-10-04 13:55:59 +06:30
List<String> privileges = [];
String? recoveryEmail;
2021-09-10 14:27:38 +06:30
String get initial =>
name != null && name != "" ? name!.substring(0, 1) : "?";
2020-09-20 05:34:49 +06:30
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();
2021-09-10 14:27:38 +06:30
if (lastMessageTime!.year == today.year &&
lastMessageTime!.month == today.month &&
lastMessageTime!.day == today.day) {
return timeFormat.format(lastMessageTime!);
2020-09-20 05:34:49 +06:30
} else {
2021-09-10 14:27:38 +06:30
return dateFormat.format(lastMessageTime!);
2020-09-20 05:34:49 +06:30
}
}
2024-01-23 16:28:08 +06:30
String get getUserUnseenCount =>
userUnseenCount > 100 ? "99+" : userUnseenCount.toString();
String get getFcsUnseenCount =>
fcsUnseenCount > 100 ? "99+" : fcsUnseenCount.toString();
2020-09-11 16:14:36 +06:30
2024-10-04 13:55:59 +06:30
// for pin login
String? pinToken;
bool get isPinLogin => pinToken != null;
2020-09-13 21:49:39 +06:30
2021-09-10 14:27:38 +06:30
String get phone => phoneNumber != null && phoneNumber!.startsWith("959")
? "0${phoneNumber!.substring(2)}"
: phoneNumber!;
2020-10-17 01:40:24 +06:30
bool get joined => status != null && status == user_joined_status;
bool get invited => status != null && status == user_invited_status;
bool get requested => status != null && status == user_requested_status;
bool get disabled => status != null && status == user_disabled_status;
2020-09-13 21:49:39 +06:30
String get share => "Your phone number:$phoneNumber";
User(
{this.id,
this.name,
this.phoneNumber,
this.fcsID,
this.status,
this.privileges = const [],
this.lastMessage,
this.lastMessageTime,
this.userUnseenCount = 0,
this.fcsUnseenCount = 0,
this.preferCurrency,
this.enablePinLogin = false,
this.pinDigit,
this.recoveryEmail});
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) {
2024-01-23 16:28:08 +06:30
var _date =
map['message_time'] == null ? null : (map['message_time'] as Timestamp);
2020-09-20 05:34:49 +06:30
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'] ?? 0,
fcsUnseenCount: map['fcs_unseen_count'] ?? 0,
2020-10-11 02:17:23 +06:30
preferCurrency: map['preferred_currency'],
lastMessageTime: _date?.toDate(),
2024-10-04 13:55:59 +06:30
enablePinLogin: map['enable_pin_login'] ?? false,
pinDigit: map['pin'] ?? '',
recoveryEmail: map['email'] ?? "");
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() {
2025-02-18 16:50:34 +06:30
return privileges.isEmpty;
2020-08-27 22:32:40 +06:30
}
2020-09-11 16:14:36 +06:30
bool hasSysAdmin() {
2020-10-12 03:34:05 +06:30
return _has(privilege_sys_admin);
2020-08-27 22:32:40 +06:30
}
bool hasAdmin() {
2020-10-12 03:34:05 +06:30
return _has(privilege_admin);
2020-08-27 22:32:40 +06:30
}
2020-09-13 21:49:39 +06:30
bool hasCustomers() {
2020-10-12 03:34:05 +06:30
return hasSysAdmin() || hasAdmin() || _has(privilege_customer);
}
bool hasFcsShipments() {
return hasSysAdmin() || hasAdmin() || _has(privilege_fcs_shipment);
2020-09-13 21:49:39 +06:30
}
bool hasStaffs() {
2020-10-12 03:34:05 +06:30
return hasSysAdmin() || hasAdmin() || _has(privilege_staff);
2020-08-27 22:32:40 +06:30
}
2020-09-13 21:49:39 +06:30
bool hasSupport() {
2020-10-12 03:34:05 +06:30
return hasSysAdmin() || hasAdmin() || _has(privilege_support);
2020-08-27 22:32:40 +06:30
}
2020-09-15 07:13:41 +06:30
bool hasPackages() {
2020-10-12 03:34:05 +06:30
return hasSysAdmin() || hasAdmin() || _has(privilege_package);
}
bool hasReceiving() {
return hasSysAdmin() || hasAdmin() || _has(privilege_receiving);
}
bool hasProcessing() {
return hasSysAdmin() || hasAdmin() || _has(privilege_processing);
}
bool hasDeliveries() {
return hasSysAdmin() || hasAdmin() || _has(privilege_delivery);
}
bool hasInvoices() {
return hasSysAdmin() || hasAdmin() || _has(privilege_invoice);
}
bool hasShipment() {
return hasSysAdmin() || hasAdmin() || _has(privilege_shipment);
}
bool hasCarton() {
return hasSysAdmin() || hasAdmin() || _has(privilege_carton);
}
2025-02-18 16:50:34 +06:30
bool hasPinLogin() {
return _has(privilege_pin);
}
2020-10-12 03:34:05 +06:30
bool _has(String privilege) {
2024-01-23 16:28:08 +06:30
return (privileges.contains(privilege));
2020-09-15 07:13:41 +06:30
}
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
}
}