97 lines
2.5 KiB
Dart
97 lines
2.5 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:cloud_firestore/cloud_firestore.dart';
|
|
import 'package:fcs/vo/notification.dart';
|
|
|
|
import 'base_model.dart';
|
|
import 'constants.dart';
|
|
import 'firebase_helper.dart';
|
|
|
|
class NotificationModel extends BaseModel {
|
|
int filer = 0;
|
|
List<Notification> notifications = [
|
|
Notification(
|
|
desc: 'Package delivered!',
|
|
status: 'A102A-34-#23',
|
|
time: DateTime(2020, 4, 28, 10, 32)),
|
|
Notification(
|
|
desc: 'Package in transit!',
|
|
status: 'A102A-34-#24',
|
|
time: DateTime(2020, 4, 26, 9, 32)),
|
|
Notification(
|
|
desc: 'Package delivered!',
|
|
status: 'A102A-34-#23',
|
|
time: DateTime(2020, 4, 24, 10, 32)),
|
|
Notification(
|
|
marketPlace: "Macy",
|
|
desc: "Audited received goods!",
|
|
status: 'ORDER # 114-0725982-9074639',
|
|
time: DateTime(2020, 4, 22, 12, 30)),
|
|
Notification(
|
|
marketPlace: "Amazon",
|
|
desc: "Receive goods!",
|
|
status: 'ORDER # 323-982-2308',
|
|
time: DateTime(2020, 4, 22, 12, 22))
|
|
];
|
|
|
|
var filterValues = {1: "po", 2: "do", 3: "buyer"};
|
|
List<Notification> get notis {
|
|
return notifications
|
|
.where((n) => filer == 0 || n.itemType == filterValues[filer])
|
|
.toList();
|
|
}
|
|
|
|
int unseen = 0;
|
|
|
|
void initUser(user) {
|
|
super.initUser(user);
|
|
_loadNotifications();
|
|
}
|
|
|
|
@override
|
|
logout() async {
|
|
notifications = [];
|
|
}
|
|
|
|
Future<void> _loadNotifications() async {
|
|
Stream<QuerySnapshot> snapshots = Firestore.instance
|
|
.collection(
|
|
"/$biz_collection/${setting.okEnergyId}/$user_collection/${user.docID}/$notification_collection")
|
|
.orderBy("time", descending: true)
|
|
.limit(50)
|
|
.snapshots();
|
|
|
|
snapshots.listen((snaps) async {
|
|
notifications.clear();
|
|
unseen = 0;
|
|
// snaps.documentChanges.forEach((c) {
|
|
// if (c.type == DocumentChangeType.added) {
|
|
// FlutterRingtonePlayer.play(
|
|
// android: AndroidSounds.notification,
|
|
// ios: IosSounds.glass,
|
|
// );
|
|
// }
|
|
// });
|
|
snaps.documents.forEach((d) {
|
|
var n = Notification.fromMap(d.data, d.documentID);
|
|
if (!n.seen) unseen++;
|
|
notifications.add(n);
|
|
});
|
|
notifyListeners();
|
|
});
|
|
}
|
|
|
|
void filter(int filter) {
|
|
this.filer = filter;
|
|
}
|
|
|
|
Future<void> seen() async {
|
|
await request("/notification/seen", "POST", token: await getToken());
|
|
}
|
|
|
|
Future<void> seenID(String id) async {
|
|
await request("/notification/seen/${user.docID}/$id", "POST",
|
|
token: await getToken());
|
|
}
|
|
}
|