64 lines
1.8 KiB
Dart
64 lines
1.8 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:cloud_firestore/cloud_firestore.dart';
|
|
import 'package:fcs/fcs/common/domain/constants.dart';
|
|
import 'package:fcs/fcs/common/domain/entities/package.dart';
|
|
import 'package:fcs/fcs/common/domain/entities/user.dart';
|
|
import 'package:fcs/fcs/common/pages/model/base_model.dart';
|
|
import 'package:fcs/fcs/common/services/services.dart';
|
|
import 'package:logging/logging.dart';
|
|
|
|
class PackageModel extends BaseModel {
|
|
final log = Logger('PackageModel');
|
|
|
|
StreamSubscription<QuerySnapshot> listener;
|
|
List<Package> packages = [];
|
|
|
|
void initUser(user) {
|
|
super.initUser(user);
|
|
_loadPackages();
|
|
}
|
|
|
|
@override
|
|
logout() async {
|
|
if (listener != null) await listener.cancel();
|
|
packages = [];
|
|
}
|
|
|
|
Future<void> _loadPackages() async {
|
|
if (user == null) return;
|
|
String path = "";
|
|
if (user.isCustomer()) {
|
|
path = "/$user_collection/${user.id}/$packages_collection";
|
|
} else {
|
|
path = "/$packages_collection";
|
|
}
|
|
try {
|
|
listener = Firestore.instance
|
|
.collection("$path")
|
|
.where("is_delivered", isEqualTo: false)
|
|
.snapshots()
|
|
.listen((QuerySnapshot snapshot) {
|
|
packages.clear();
|
|
packages = snapshot.documents.map((documentSnapshot) {
|
|
var package = Package.fromMap(
|
|
documentSnapshot.data, documentSnapshot.documentID);
|
|
return package;
|
|
}).toList();
|
|
notifyListeners();
|
|
});
|
|
} catch (e) {
|
|
log.warning("Error!! $e");
|
|
}
|
|
}
|
|
|
|
Future<List<User>> searchUser(String term) {
|
|
return Services.instance.userService.searchUser(term);
|
|
}
|
|
|
|
Future<void> createPackages(User user, List<Package> packages) {
|
|
return Services.instance.packageService
|
|
.createPackages(packages, user.fcsID);
|
|
}
|
|
}
|