2020-05-29 07:45:27 +06:30
|
|
|
import 'dart:io';
|
|
|
|
|
import 'dart:typed_data';
|
|
|
|
|
|
|
|
|
|
import 'package:cloud_firestore/cloud_firestore.dart';
|
|
|
|
|
import 'package:firebase_auth/firebase_auth.dart';
|
|
|
|
|
import 'package:firebase_storage/firebase_storage.dart';
|
|
|
|
|
import 'package:logging/logging.dart';
|
|
|
|
|
import 'package:uuid/uuid.dart';
|
|
|
|
|
|
|
|
|
|
final log = Logger('firebaseHelper');
|
|
|
|
|
|
|
|
|
|
final FirebaseAuth auth = FirebaseAuth.instance;
|
|
|
|
|
|
|
|
|
|
Future<String> getToken() async {
|
|
|
|
|
FirebaseUser firebaseUser = await auth.currentUser();
|
2020-09-10 02:13:22 +06:30
|
|
|
// IdTokenResult token = await firebaseUser.getIdToken();
|
|
|
|
|
// return token.token;
|
|
|
|
|
return "";
|
2020-05-29 07:45:27 +06:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Stream<QuerySnapshot> getQuerySnapshot(String path) {
|
|
|
|
|
log.info("getQuerySnapshot Path: $path");
|
|
|
|
|
Stream<QuerySnapshot> snapshots =
|
|
|
|
|
Firestore.instance.collection(path).snapshots();
|
|
|
|
|
return snapshots;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Stream<QuerySnapshot> getQuerySnapshotByOrder(String path, String orderName) {
|
|
|
|
|
log.info("getQuerySnapshotByOrder Path: $path");
|
|
|
|
|
Stream<QuerySnapshot> snapshots = Firestore.instance
|
|
|
|
|
.collection(path)
|
|
|
|
|
.orderBy(orderName, descending: true)
|
|
|
|
|
.snapshots();
|
|
|
|
|
return snapshots;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Stream<QuerySnapshot> getFilterStatusSnapshot(
|
|
|
|
|
String path, String searchStatus, String orderName) {
|
|
|
|
|
log.info("getFilterStatusSnapshot Path: $path");
|
|
|
|
|
Stream<QuerySnapshot> snapshots = Firestore.instance
|
|
|
|
|
.collection(path)
|
|
|
|
|
.where('status', isEqualTo: searchStatus)
|
|
|
|
|
.orderBy(orderName, descending: true)
|
|
|
|
|
.snapshots();
|
|
|
|
|
return snapshots;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Stream<QuerySnapshot> getFilterSnapshot(
|
|
|
|
|
String path, bool descending, String orderName) {
|
|
|
|
|
log.info("getFilterSnapshot Path: $path");
|
|
|
|
|
Stream<QuerySnapshot> snapshots = Firestore.instance
|
|
|
|
|
.collection(path)
|
|
|
|
|
.orderBy(orderName, descending: descending)
|
|
|
|
|
.snapshots();
|
|
|
|
|
return snapshots;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Stream<QuerySnapshot> getDeliveryStatusSnapshot(
|
|
|
|
|
String path, String searchStatus, String orderName) {
|
|
|
|
|
log.info("getDeliveryStatusSnapshot Path: $path");
|
|
|
|
|
Stream<QuerySnapshot> snapshots = Firestore.instance
|
|
|
|
|
.collection(path)
|
|
|
|
|
.where('status', isEqualTo: searchStatus)
|
|
|
|
|
.where("in_delivery", isEqualTo: true)
|
|
|
|
|
.orderBy(orderName, descending: true)
|
|
|
|
|
.snapshots();
|
|
|
|
|
return snapshots;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Stream<QuerySnapshot> getFilterDateSnapshot(String path, String type,
|
|
|
|
|
DateTime startDate, DateTime endDate, String orderName) {
|
|
|
|
|
log.info("getFilterDateSnapshot Path: $path");
|
|
|
|
|
Firestore.instance
|
|
|
|
|
.collection(path)
|
|
|
|
|
.where(type, isGreaterThanOrEqualTo: startDate)
|
|
|
|
|
.where(type, isLessThanOrEqualTo: endDate)
|
|
|
|
|
.orderBy(type, descending: true)
|
|
|
|
|
.orderBy(orderName, descending: true)
|
|
|
|
|
.limit(1)
|
|
|
|
|
.getDocuments()
|
|
|
|
|
.then((s) {});
|
|
|
|
|
|
|
|
|
|
Stream<QuerySnapshot> snapshots = Firestore.instance
|
|
|
|
|
.collection(path)
|
|
|
|
|
.where(type, isGreaterThanOrEqualTo: startDate)
|
|
|
|
|
.where(type, isLessThanOrEqualTo: endDate)
|
|
|
|
|
.orderBy(type, descending: true)
|
|
|
|
|
.orderBy(orderName, descending: true)
|
|
|
|
|
.snapshots();
|
|
|
|
|
return snapshots;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Stream<QuerySnapshot> getDeliveryDateSnapshot(String path, String type,
|
|
|
|
|
DateTime startDate, DateTime endDate, String orderName) {
|
|
|
|
|
log.info("getDeliveryDateSnapshot Path: $path");
|
|
|
|
|
Firestore.instance
|
|
|
|
|
.collection(path)
|
|
|
|
|
.where(type, isGreaterThanOrEqualTo: startDate)
|
|
|
|
|
.where(type, isLessThanOrEqualTo: endDate)
|
|
|
|
|
.where("in_delivery", isEqualTo: true)
|
|
|
|
|
.orderBy(type, descending: true)
|
|
|
|
|
.orderBy(orderName, descending: true)
|
|
|
|
|
.limit(1)
|
|
|
|
|
.getDocuments()
|
|
|
|
|
.then((s) {});
|
|
|
|
|
|
|
|
|
|
Stream<QuerySnapshot> snapshots = Firestore.instance
|
|
|
|
|
.collection(path)
|
|
|
|
|
.where(type, isGreaterThanOrEqualTo: startDate)
|
|
|
|
|
.where(type, isLessThanOrEqualTo: endDate)
|
|
|
|
|
.where("in_delivery", isEqualTo: true)
|
|
|
|
|
.orderBy(type, descending: true)
|
|
|
|
|
.orderBy(orderName, descending: true)
|
|
|
|
|
.snapshots();
|
|
|
|
|
return snapshots;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Stream<QuerySnapshot> getDeliverySnapshot(String path, String type,
|
|
|
|
|
DateTime startDate, DateTime endDate, String orderName) {
|
|
|
|
|
log.info("getDeliverySnapshot Path: $path");
|
|
|
|
|
Firestore.instance
|
|
|
|
|
.collection(path)
|
|
|
|
|
.where(type, isGreaterThanOrEqualTo: startDate)
|
|
|
|
|
.where(type, isLessThanOrEqualTo: endDate)
|
|
|
|
|
.where("in_delivery", isEqualTo: true)
|
|
|
|
|
.orderBy(type, descending: true)
|
|
|
|
|
.orderBy(orderName, descending: true)
|
|
|
|
|
.limit(1)
|
|
|
|
|
.getDocuments()
|
|
|
|
|
.then((s) {});
|
|
|
|
|
|
|
|
|
|
Stream<QuerySnapshot> snapshots = Firestore.instance
|
|
|
|
|
.collection(path)
|
|
|
|
|
.where(type, isGreaterThanOrEqualTo: startDate)
|
|
|
|
|
.where(type, isLessThanOrEqualTo: endDate)
|
|
|
|
|
.where("in_delivery", isEqualTo: true)
|
|
|
|
|
.orderBy(type, descending: true)
|
|
|
|
|
.orderBy(orderName, descending: true)
|
|
|
|
|
.snapshots();
|
|
|
|
|
return snapshots;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Stream<QuerySnapshot> getFilterDataSnapshot(String path, String status,
|
|
|
|
|
String type, DateTime startDate, DateTime endDate, String orderName) {
|
|
|
|
|
log.info("getFilterDateSnapshot Path: $path");
|
|
|
|
|
Stream<QuerySnapshot> snapshots = Firestore.instance
|
|
|
|
|
.collection(path)
|
|
|
|
|
.where('status', isEqualTo: status)
|
|
|
|
|
.where(type, isGreaterThanOrEqualTo: startDate)
|
|
|
|
|
.where(type, isLessThanOrEqualTo: endDate)
|
|
|
|
|
.orderBy(type, descending: true)
|
|
|
|
|
.orderBy(orderName, descending: true)
|
|
|
|
|
.snapshots();
|
|
|
|
|
return snapshots;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Stream<QuerySnapshot> getDeliveryDataSnapshot(String path, String status,
|
|
|
|
|
String type, DateTime startDate, DateTime endDate, String orderName) {
|
|
|
|
|
log.info("getDeliveryDataSnapshot Path: $path");
|
|
|
|
|
Stream<QuerySnapshot> snapshots = Firestore.instance
|
|
|
|
|
.collection(path)
|
|
|
|
|
.where('status', isEqualTo: status)
|
|
|
|
|
.where("in_delivery", isEqualTo: true)
|
|
|
|
|
.where(type, isGreaterThanOrEqualTo: startDate)
|
|
|
|
|
.where(type, isLessThanOrEqualTo: endDate)
|
|
|
|
|
.orderBy(type, descending: true)
|
|
|
|
|
.orderBy(orderName, descending: true)
|
|
|
|
|
.snapshots();
|
|
|
|
|
return snapshots;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Stream<QuerySnapshot> getsearchBuyerSnapshot(String path, String buyer) {
|
|
|
|
|
log.info("getFilterDateSnapshot Path: $path");
|
|
|
|
|
Stream<QuerySnapshot> snapshots = Firestore.instance
|
|
|
|
|
.collection(path)
|
|
|
|
|
.where("user_name", isEqualTo: buyer)
|
|
|
|
|
.snapshots();
|
|
|
|
|
return snapshots;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Stream<DocumentSnapshot> getDocSnapshot(String path, String id) {
|
|
|
|
|
log.info("getDocSnapshot Path: $path, ID: $id");
|
|
|
|
|
Stream<DocumentSnapshot> snapshot =
|
|
|
|
|
Firestore.instance.collection(path).document(id).snapshots();
|
|
|
|
|
return snapshot;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Stream<QuerySnapshot> getQuerySnapshotF(String path, accountID) {
|
|
|
|
|
log.info("getQuerySnapshot Path: $path");
|
|
|
|
|
Stream<QuerySnapshot> snapshots = Firestore.instance
|
|
|
|
|
.collection(path)
|
|
|
|
|
.where("account_id", isEqualTo: accountID)
|
|
|
|
|
.snapshots();
|
|
|
|
|
return snapshots;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Stream<QuerySnapshot> getFilterDateSnapshotF(String path, String accountID,
|
|
|
|
|
String type, DateTime startDate, DateTime endDate, String orderName) {
|
|
|
|
|
log.info("getFilterDateSnapshot Path: $path");
|
|
|
|
|
Stream<QuerySnapshot> snapshots = Firestore.instance
|
|
|
|
|
.collection(path)
|
|
|
|
|
.where("account_id", isEqualTo: accountID)
|
|
|
|
|
.where(type, isGreaterThanOrEqualTo: startDate)
|
|
|
|
|
.where(type, isLessThanOrEqualTo: endDate)
|
|
|
|
|
.orderBy(orderName, descending: true)
|
|
|
|
|
.snapshots();
|
|
|
|
|
return snapshots;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<QuerySnapshot> getSnapshot(String path) {
|
|
|
|
|
log.info("getSnapshot Path: $path");
|
|
|
|
|
Future<QuerySnapshot> snapshots =
|
|
|
|
|
Firestore.instance.collection(path).getDocuments();
|
|
|
|
|
|
|
|
|
|
return snapshots;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<DocumentSnapshot> getDocSnap(String path, String id) {
|
|
|
|
|
log.info("getDocSnap Path: $path");
|
|
|
|
|
return Firestore.instance.collection(path).document(id).get();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<String> uploadStorage(String path, File file, {String fileName}) async {
|
|
|
|
|
if (fileName == null) {
|
|
|
|
|
fileName = Uuid().v4();
|
|
|
|
|
}
|
|
|
|
|
StorageReference storageReference =
|
|
|
|
|
FirebaseStorage.instance.ref().child('$path/$fileName');
|
|
|
|
|
StorageUploadTask uploadTask = storageReference.putFile(file);
|
|
|
|
|
await uploadTask.onComplete;
|
|
|
|
|
String downloadUrl = await storageReference.getDownloadURL();
|
|
|
|
|
print("name:${await storageReference.getName()}");
|
|
|
|
|
print("bucket:${await storageReference.getBucket()}");
|
|
|
|
|
print("path:${await storageReference.getPath()}");
|
|
|
|
|
print("meta:${await storageReference.getMetadata()}");
|
|
|
|
|
return downloadUrl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<String> uploadStorageData(String path, Uint8List data,
|
|
|
|
|
{String fileName}) async {
|
|
|
|
|
if (fileName == null) {
|
|
|
|
|
fileName = Uuid().v4();
|
|
|
|
|
}
|
|
|
|
|
StorageReference storageReference =
|
|
|
|
|
FirebaseStorage.instance.ref().child('$path/$fileName');
|
|
|
|
|
StorageUploadTask uploadTask = storageReference.putData(data);
|
|
|
|
|
await uploadTask.onComplete;
|
|
|
|
|
String downloadUrl = await storageReference.getDownloadURL();
|
|
|
|
|
return downloadUrl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> deleteStorage(String path, name) async {
|
|
|
|
|
try {
|
|
|
|
|
StorageReference storageReference =
|
|
|
|
|
FirebaseStorage.instance.ref().child('$path/$name');
|
|
|
|
|
await storageReference.delete();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
log.warning("deleteStorage:$e");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> deleteStorageFromUrl(String url) async {
|
|
|
|
|
try {
|
|
|
|
|
StorageReference storageReference =
|
|
|
|
|
await FirebaseStorage.instance.getReferenceFromUrl(url);
|
|
|
|
|
await storageReference.delete();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
log.warning("deleteStorage:$e");
|
|
|
|
|
}
|
|
|
|
|
}
|