stack_wallet/lib/services/buys_service.dart

67 lines
1.7 KiB
Dart
Raw Normal View History

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) {
try {
return DB.instance
.values<Buy>(boxName: DB.boxNameBuys)
2023-01-11 15:54:39 +00:00
.firstWhere((e) => e.buyId == buyId);
} catch (_) {
return null;
}
}
Future<void> add({
2023-01-11 15:54:39 +00:00
required Buy buy,
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);
if (shouldNotifyListeners) {
notifyListeners();
}
}
Future<void> edit({
2023-01-11 15:54:39 +00:00
required Buy buy,
required bool shouldNotifyListeners,
}) async {
2023-01-11 15:54:39 +00:00
if (DB.instance.get<Buy>(boxName: DB.boxNameBuys, key: buy.uuid) == null) {
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);
}
Future<void> delete({
2023-01-11 15:54:39 +00:00
required Buy buy,
required bool shouldNotifyListeners,
}) async {
await deleteByUuid(
2023-01-11 15:54:39 +00:00
uuid: buy.uuid, shouldNotifyListeners: shouldNotifyListeners);
}
Future<void> deleteByUuid({
required String uuid,
required bool shouldNotifyListeners,
}) async {
await DB.instance.delete<Buy>(boxName: DB.boxNameBuys, key: uuid);
if (shouldNotifyListeners) {
notifyListeners();
}
}
}