/* * This file is part of Stack Wallet. * * Copyright (c) 2023 Cypher Stack * All Rights Reserved. * The code is distributed under GPLv3 license, see LICENSE file for details. * Generated by Cypher Stack on 2023-05-26 * */ import 'package:flutter/cupertino.dart'; import '../db/hive/db.dart'; import '../models/exchange/response_objects/trade.dart'; class TradesService extends ChangeNotifier { List get trades { final list = DB.instance.values(boxName: DB.boxNameTradesV2); list.sort( (a, b) => b.timestamp.millisecondsSinceEpoch - a.timestamp.millisecondsSinceEpoch, ); return list; } Trade? get(String tradeId) { try { return DB.instance .values(boxName: DB.boxNameTradesV2) .firstWhere((e) => e.tradeId == tradeId); } catch (_) { return null; } } Future add({ required Trade trade, required bool shouldNotifyListeners, }) async { await DB.instance .put(boxName: DB.boxNameTradesV2, key: trade.uuid, value: trade); if (shouldNotifyListeners) { notifyListeners(); } } Future edit({ required Trade trade, required bool shouldNotifyListeners, }) async { if (DB.instance.get(boxName: DB.boxNameTradesV2, key: trade.uuid) == null) { throw Exception("Attempted to edit a trade that does not exist in Hive!"); } // add overwrites so this edit function is just a wrapper with an extra check await add(trade: trade, shouldNotifyListeners: shouldNotifyListeners); } Future delete({ required Trade trade, required bool shouldNotifyListeners, }) async { await deleteByUuid( uuid: trade.uuid, shouldNotifyListeners: shouldNotifyListeners, ); } Future deleteByUuid({ required String uuid, required bool shouldNotifyListeners, }) async { await DB.instance.delete(boxName: DB.boxNameTradesV2, key: uuid); if (shouldNotifyListeners) { notifyListeners(); } } }