2023-01-10 21:25:20 +00:00
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
import 'package:stackwallet/hive/db.dart';
|
|
|
|
import 'package:stackwallet/models/buy/response_objects/buy.dart';
|
|
|
|
|
|
|
|
class BuysService extends ChangeNotifier {
|
|
|
|
List<Buy> get Buys {
|
|
|
|
final list = DB.instance.values<Buy>(boxName: DB.boxNameBuys);
|
|
|
|
list.sort((a, b) =>
|
|
|
|
b.timestamp.millisecondsSinceEpoch -
|
|
|
|
a.timestamp.millisecondsSinceEpoch);
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2023-01-11 15:54:39 +00:00
|
|
|
Buy? get(String buyId) {
|
2023-01-10 21:25:20 +00:00
|
|
|
try {
|
|
|
|
return DB.instance
|
|
|
|
.values<Buy>(boxName: DB.boxNameBuys)
|
2023-01-11 15:54:39 +00:00
|
|
|
.firstWhere((e) => e.buyId == buyId);
|
2023-01-10 21:25:20 +00:00
|
|
|
} catch (_) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> add({
|
2023-01-11 15:54:39 +00:00
|
|
|
required Buy buy,
|
2023-01-10 21:25:20 +00:00
|
|
|
required bool shouldNotifyListeners,
|
|
|
|
}) async {
|
|
|
|
await DB.instance
|
2023-01-11 15:54:39 +00:00
|
|
|
.put<Buy>(boxName: DB.boxNameBuys, key: buy.uuid, value: buy);
|
2023-01-10 21:25:20 +00:00
|
|
|
|
|
|
|
if (shouldNotifyListeners) {
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> edit({
|
2023-01-11 15:54:39 +00:00
|
|
|
required Buy buy,
|
2023-01-10 21:25:20 +00:00
|
|
|
required bool shouldNotifyListeners,
|
|
|
|
}) async {
|
2023-01-11 15:54:39 +00:00
|
|
|
if (DB.instance.get<Buy>(boxName: DB.boxNameBuys, key: buy.uuid) == null) {
|
2023-01-10 21:25:20 +00:00
|
|
|
throw Exception("Attempted to edit a Buy that does not exist in Hive!");
|
|
|
|
}
|
|
|
|
|
|
|
|
// add overwrites so this edit function is just a wrapper with an extra check
|
2023-01-11 15:54:39 +00:00
|
|
|
await add(buy: buy, shouldNotifyListeners: shouldNotifyListeners);
|
2023-01-10 21:25:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> delete({
|
2023-01-11 15:54:39 +00:00
|
|
|
required Buy buy,
|
2023-01-10 21:25:20 +00:00
|
|
|
required bool shouldNotifyListeners,
|
|
|
|
}) async {
|
|
|
|
await deleteByUuid(
|
2023-01-11 15:54:39 +00:00
|
|
|
uuid: buy.uuid, shouldNotifyListeners: shouldNotifyListeners);
|
2023-01-10 21:25:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> deleteByUuid({
|
|
|
|
required String uuid,
|
|
|
|
required bool shouldNotifyListeners,
|
|
|
|
}) async {
|
|
|
|
await DB.instance.delete<Buy>(boxName: DB.boxNameBuys, key: uuid);
|
|
|
|
|
|
|
|
if (shouldNotifyListeners) {
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|