Files
fcs/lib/pages/discount/model/discount_model.dart

133 lines
3.5 KiB
Dart
Raw Normal View History

2020-10-15 10:53:41 +06:30
import 'dart:async';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:fcs/data/services/services.dart';
2020-10-22 04:14:53 +06:30
import 'package:fcs/domain/constants.dart';
2020-10-07 02:33:06 +06:30
import 'package:fcs/domain/entities/discount.dart';
2020-10-22 04:14:53 +06:30
import 'package:fcs/helpers/paginator.dart';
2020-10-07 02:33:06 +06:30
import 'package:fcs/pages/main/model/base_model.dart';
2020-10-15 10:53:41 +06:30
import 'package:logging/logging.dart';
2020-06-26 16:04:40 +06:30
class DiscountModel extends BaseModel {
2020-10-15 10:53:41 +06:30
final log = Logger('DiscountModel');
2021-09-10 12:02:08 +06:30
StreamSubscription<QuerySnapshot>? listener;
2020-06-26 16:04:40 +06:30
2020-10-22 04:14:53 +06:30
List<Discount> _discounts = [];
2021-09-13 11:20:35 +06:30
List<Discount> get discounts => _selectedIndex == 1
? _discounts
: List<Discount>.from(_used?.values ?? []);
2020-10-22 04:14:53 +06:30
2021-09-13 11:20:35 +06:30
Paginator? _used;
2020-10-22 04:14:53 +06:30
bool isLoading = false;
int _selectedIndex = 1;
set selectedIndex(int index) {
_selectedIndex = index;
notifyListeners();
}
2021-09-10 12:02:08 +06:30
int get selectedIndex => _selectedIndex;
2020-10-22 04:14:53 +06:30
initData() {
_selectedIndex = 1;
_load();
2021-09-10 16:48:20 +06:30
if (_getUsed() != null) _used = _getUsed();
2021-09-13 11:20:35 +06:30
_used?.load();
2020-10-22 04:14:53 +06:30
}
2020-06-26 16:04:40 +06:30
void initUser(user) {
super.initUser(user);
2020-10-15 10:53:41 +06:30
_load();
}
_load() {
2021-09-10 12:02:08 +06:30
if (listener != null) listener!.cancel();
2020-10-15 10:53:41 +06:30
try {
2021-09-10 16:48:20 +06:30
listener = FirebaseFirestore.instance
2020-10-22 04:14:53 +06:30
.collection("/$discounts_collection")
2020-10-15 10:53:41 +06:30
.orderBy("code", descending: false)
.snapshots()
.listen((snaps) {
2020-10-24 06:14:07 +06:30
_discounts.clear();
2021-09-10 16:48:20 +06:30
snaps.docs.forEach((d) {
2021-09-13 11:20:35 +06:30
_discounts.add(Discount.fromMap(d.data(), d.id));
2020-10-15 10:53:41 +06:30
});
notifyListeners();
});
} catch (e) {
log.warning("error:$e");
}
2020-06-26 16:04:40 +06:30
}
2021-09-10 16:48:20 +06:30
Paginator _getUsed() {
if (user == null || !user!.hasFcsShipments()) throw "No Privilege";
2020-10-22 04:14:53 +06:30
2021-09-10 16:48:20 +06:30
var pageQuery = FirebaseFirestore.instance
2020-10-22 04:14:53 +06:30
.collection("/$discounts_collection")
.where("status", isEqualTo: fcs_shipment_shipped_status)
.orderBy("code", descending: false);
var paginator = new Paginator(pageQuery, rowPerLoad: 20, toObj: (data, id) {
return Discount.fromMap(data, id);
});
return paginator;
}
2021-09-10 16:48:20 +06:30
Future<List<Discount?>?> getDiscount(String userID) async {
2020-10-24 06:14:07 +06:30
String path = "/$discounts_collection";
try {
2021-09-10 16:48:20 +06:30
var q = FirebaseFirestore.instance
2020-10-24 06:14:07 +06:30
.collection("$path")
.where("customer_id", isEqualTo: userID)
.where("status", isEqualTo: "available");
2021-09-10 16:48:20 +06:30
var snaps = await q.get(const GetOptions(source: Source.server));
2021-09-11 16:56:20 +06:30
var discounts = snaps.docs.map((snap) {
2020-10-24 06:14:07 +06:30
if (snap.exists) {
2021-09-11 16:56:20 +06:30
var s = Discount.fromMap(snap.data as Map<String, dynamic>, snap.id);
2020-10-24 06:14:07 +06:30
return s;
}
}).toList();
return discounts;
} catch (e) {
log.warning("Error!! $e");
}
return null;
}
2020-10-22 04:14:53 +06:30
Future<void> loadMore() async {
2021-09-13 11:20:35 +06:30
if (_used!.ended || _selectedIndex == 1) return;
2020-10-22 04:14:53 +06:30
isLoading = true;
notifyListeners();
2021-09-13 11:20:35 +06:30
await _used!.load(onFinished: () {
2020-10-22 04:14:53 +06:30
isLoading = false;
notifyListeners();
});
}
Future<void> refresh() async {
if (_selectedIndex == 1) return;
2021-09-13 11:20:35 +06:30
await _used!.refresh(onFinished: () {
2020-10-22 04:14:53 +06:30
notifyListeners();
});
}
2020-06-26 16:04:40 +06:30
@override
logout() async {
2021-09-10 12:02:08 +06:30
if (listener != null) await listener!.cancel();
2021-09-13 11:20:35 +06:30
if (_used != null) _used!.close();
2020-10-22 04:14:53 +06:30
_discounts = [];
2020-10-15 10:53:41 +06:30
}
Future<void> addDiscount(Discount discount) async {
return Services.instance.commonService.createDiscount(discount);
}
Future<void> updateDiscount(Discount discount) async {
return Services.instance.commonService.updateDiscount(discount);
}
Future<void> deleteDiscount(Discount discount) async {
2021-09-10 16:48:20 +06:30
return Services.instance.commonService.deleteDiscount(discount.id!);
2020-06-26 16:04:40 +06:30
}
}