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 get Buys { final list = DB.instance.values(boxName: DB.boxNameBuys); list.sort((a, b) => b.timestamp.millisecondsSinceEpoch - a.timestamp.millisecondsSinceEpoch); return list; } Buy? get(String buyId) { try { return DB.instance .values(boxName: DB.boxNameBuys) .firstWhere((e) => e.buyId == buyId); } catch (_) { return null; } } Future add({ required Buy buy, required bool shouldNotifyListeners, }) async { await DB.instance .put(boxName: DB.boxNameBuys, key: buy.uuid, value: buy); if (shouldNotifyListeners) { notifyListeners(); } } Future edit({ required Buy buy, required bool shouldNotifyListeners, }) async { if (DB.instance.get(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 await add(buy: buy, shouldNotifyListeners: shouldNotifyListeners); } Future delete({ required Buy buy, required bool shouldNotifyListeners, }) async { await deleteByUuid( uuid: buy.uuid, shouldNotifyListeners: shouldNotifyListeners); } Future deleteByUuid({ required String uuid, required bool shouldNotifyListeners, }) async { await DB.instance.delete(boxName: DB.boxNameBuys, key: uuid); if (shouldNotifyListeners) { notifyListeners(); } } }