add messaging

This commit is contained in:
Sai Naw Wun
2020-09-20 05:34:49 +06:30
parent cc56a18613
commit cb622b004b
38 changed files with 1931 additions and 180 deletions

View File

@@ -5,6 +5,7 @@ const setting_doc_id = "setting";
const privilege_collection = "privileges";
const markets_collection = "markets";
const packages_collection = "packages";
const messages_collection = "messages";
const user_requested_status = "requested";
const user_invited_status = "invited";
@@ -14,6 +15,10 @@ const pkg_files_path = "/packages";
// Link page
const page_payment_methods = "payment_methods";
const page_buying_instructions = "buying_instructions";
// Message type
const message_type_package = "t_p";
//////////////////////////////
const ok_doc_id = "ok";

View File

@@ -1,4 +1,10 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:fcs/fcs/common/helpers/const.dart';
import 'package:fcs/fcs/common/pages/package/package_info.dart';
import 'package:intl/intl.dart';
DateFormat dayFormat = DateFormat("MMM dd yyyy");
DateFormat timeFormat = DateFormat("HH:mm");
class User {
String id;
@@ -6,6 +12,37 @@ class User {
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 = [];
@@ -16,14 +53,17 @@ class User {
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,
});
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(
@@ -32,6 +72,7 @@ class User {
fcsID: json['fcs_id'],
phoneNumber: json['phone_number'],
status: json['status'],
lastMessage: json['last_message'],
);
}
@@ -49,6 +90,8 @@ class User {
}
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>();
@@ -58,7 +101,11 @@ class User {
phoneNumber: map['phone_number'],
status: map['status'],
fcsID: map['fcs_id'],
privileges: _privileges);
privileges: _privileges,
lastMessage: map['last_message'],
userUnseenCount: map['user_unseen_count'],
fcsUnseenCount: map['fcs_unseen_count'],
lastMessageTime: _date == null ? null : _date.toDate());
}
bool isCustomer() {

View File

@@ -0,0 +1,58 @@
import 'package:cloud_firestore/cloud_firestore.dart';
class Message {
String id;
String message;
DateTime date;
String receiverID;
String receiverName;
String senderID;
String senderName;
String messageType;
String messageID;
Message(
{this.id,
this.message,
this.date,
this.receiverID,
this.receiverName,
this.senderID,
this.senderName,
this.messageType,
this.messageID});
bool fromToday() {
var now = DateTime.now();
return date.day == now.day &&
date.month == now.month &&
date.year == now.year;
}
Map<String, dynamic> toMap() {
return {
'message': message,
"receiver_id": receiverID,
};
}
bool sameDay(Message another) {
return date.year == another.date.year &&
date.month == another.date.month &&
date.day == another.date.day;
}
factory Message.fromMap(Map<String, dynamic> map, String id) {
var date = (map['date'] as Timestamp);
return Message(
id: id,
message: map['message'],
senderID: map['sender_id'],
senderName: map['sender_name'],
receiverID: map['receiver_id'],
receiverName: map['receiver_name'],
messageType: map['msg_type'],
messageID: map['msg_id'],
date: date != null ? date.toDate() : null,
);
}
}