stack_wallet/lib/services/trade_service.dart

58 lines
1.6 KiB
Dart
Raw Normal View History

2022-08-26 08:11:35 +00:00
import 'package:flutter/cupertino.dart';
import 'package:stackwallet/hive/db.dart';
import 'package:stackwallet/models/exchange/response_objects/trade.dart';
2022-08-26 08:11:35 +00:00
class TradesService extends ChangeNotifier {
List<Trade> get trades {
final list = DB.instance.values<Trade>(boxName: DB.boxNameTradesV2);
2022-08-26 08:11:35 +00:00
list.sort((a, b) =>
b.timestamp.millisecondsSinceEpoch -
a.timestamp.millisecondsSinceEpoch);
2022-08-26 08:11:35 +00:00
return list;
}
Future<void> add({
required Trade trade,
2022-08-26 08:11:35 +00:00
required bool shouldNotifyListeners,
}) async {
await DB.instance
.put<Trade>(boxName: DB.boxNameTradesV2, key: trade.uuid, value: trade);
2022-08-26 08:11:35 +00:00
if (shouldNotifyListeners) {
notifyListeners();
}
}
Future<void> edit({
required Trade trade,
2022-08-26 08:11:35 +00:00
required bool shouldNotifyListeners,
}) async {
if (DB.instance.get<Trade>(boxName: DB.boxNameTradesV2, key: trade.uuid) ==
2022-08-26 08:11:35 +00:00
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<void> delete({
required Trade trade,
2022-08-26 08:11:35 +00:00
required bool shouldNotifyListeners,
}) async {
await deleteByUuid(
uuid: trade.uuid, shouldNotifyListeners: shouldNotifyListeners);
}
Future<void> deleteByUuid({
required String uuid,
required bool shouldNotifyListeners,
}) async {
await DB.instance.delete<Trade>(boxName: DB.boxNameTradesV2, key: uuid);
2022-08-26 08:11:35 +00:00
if (shouldNotifyListeners) {
notifyListeners();
}
}
}