2020-09-18 04:04:21 +06:30
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
|
|
import 'package:cloud_firestore/cloud_firestore.dart';
|
2020-10-07 02:33:06 +06:30
|
|
|
import 'package:fcs/data/services/services.dart';
|
|
|
|
|
import 'package:fcs/domain/entities/payment_method.dart';
|
|
|
|
|
import 'package:fcs/pages/main/model/base_model.dart';
|
2020-09-18 04:04:21 +06:30
|
|
|
import 'package:logging/logging.dart';
|
|
|
|
|
|
|
|
|
|
class PaymentMethodModel extends BaseModel {
|
|
|
|
|
final log = Logger('PaymentMethodModel');
|
|
|
|
|
|
|
|
|
|
List<PaymentMethod> paymentMethods = [];
|
|
|
|
|
|
|
|
|
|
PaymentMethod getPaymentMethod(String id) {
|
2021-09-10 16:48:21 +06:30
|
|
|
return paymentMethods.firstWhere((e) => e.id == id);
|
2020-09-18 04:04:21 +06:30
|
|
|
}
|
|
|
|
|
|
2021-09-10 16:48:21 +06:30
|
|
|
StreamSubscription<QuerySnapshot>? listener;
|
2020-09-18 04:04:21 +06:30
|
|
|
|
|
|
|
|
PaymentMethodModel() {
|
2021-09-10 16:48:21 +06:30
|
|
|
if (listener != null) listener!.cancel();
|
2020-09-18 04:04:21 +06:30
|
|
|
try {
|
2021-09-10 16:48:21 +06:30
|
|
|
listener = FirebaseFirestore.instance
|
2020-09-18 04:04:21 +06:30
|
|
|
.collection("/payment_methods")
|
|
|
|
|
.snapshots()
|
|
|
|
|
.listen((snaps) {
|
|
|
|
|
paymentMethods.clear();
|
2021-09-10 16:48:21 +06:30
|
|
|
snaps.docs.forEach((d) {
|
2021-09-11 16:56:20 +06:30
|
|
|
paymentMethods.add(PaymentMethod.fromMap(d.data(), d.id));
|
2020-09-18 04:04:21 +06:30
|
|
|
});
|
|
|
|
|
notifyListeners();
|
|
|
|
|
});
|
|
|
|
|
} catch (e) {
|
|
|
|
|
log.warning("error:$e");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> addPaymentMethod(PaymentMethod paymentMethod) async {
|
|
|
|
|
Services.instance.commonService.createPaymentMethod(paymentMethod);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> updatePaymentMethod(PaymentMethod paymentMethod) async {
|
|
|
|
|
Services.instance.commonService.updatePaymentMethod(paymentMethod);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> deletePaymentMethod(String id) async {
|
|
|
|
|
Services.instance.commonService.deletePayment(id);
|
|
|
|
|
}
|
|
|
|
|
}
|