2023-05-26 21:21:16 +00:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2022-08-26 08:11:35 +00:00
|
|
|
import 'package:flutter/cupertino.dart';
|
2024-05-27 23:56:22 +00:00
|
|
|
|
2024-05-23 00:37:06 +00:00
|
|
|
import '../db/hive/db.dart';
|
|
|
|
import '../models/exchange/response_objects/trade.dart';
|
2022-08-26 08:11:35 +00:00
|
|
|
|
|
|
|
class TradesService extends ChangeNotifier {
|
2022-10-03 00:54:35 +00:00
|
|
|
List<Trade> get trades {
|
|
|
|
final list = DB.instance.values<Trade>(boxName: DB.boxNameTradesV2);
|
2024-05-27 23:56:22 +00:00
|
|
|
list.sort(
|
|
|
|
(a, b) =>
|
|
|
|
b.timestamp.millisecondsSinceEpoch -
|
|
|
|
a.timestamp.millisecondsSinceEpoch,
|
|
|
|
);
|
2022-08-26 08:11:35 +00:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2022-11-29 19:29:03 +00:00
|
|
|
Trade? get(String tradeId) {
|
|
|
|
try {
|
|
|
|
return DB.instance
|
|
|
|
.values<Trade>(boxName: DB.boxNameTradesV2)
|
|
|
|
.firstWhere((e) => e.tradeId == tradeId);
|
|
|
|
} catch (_) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-26 08:11:35 +00:00
|
|
|
Future<void> add({
|
2022-10-03 00:54:35 +00:00
|
|
|
required Trade trade,
|
2022-08-26 08:11:35 +00:00
|
|
|
required bool shouldNotifyListeners,
|
|
|
|
}) async {
|
2022-10-03 00:54:35 +00:00
|
|
|
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({
|
2022-10-03 00:54:35 +00:00
|
|
|
required Trade trade,
|
2022-08-26 08:11:35 +00:00
|
|
|
required bool shouldNotifyListeners,
|
|
|
|
}) async {
|
2022-10-03 00:54:35 +00:00
|
|
|
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({
|
2022-10-03 00:54:35 +00:00
|
|
|
required Trade trade,
|
2022-08-26 08:11:35 +00:00
|
|
|
required bool shouldNotifyListeners,
|
|
|
|
}) async {
|
|
|
|
await deleteByUuid(
|
2024-05-27 23:56:22 +00:00
|
|
|
uuid: trade.uuid,
|
|
|
|
shouldNotifyListeners: shouldNotifyListeners,
|
|
|
|
);
|
2022-08-26 08:11:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> deleteByUuid({
|
|
|
|
required String uuid,
|
|
|
|
required bool shouldNotifyListeners,
|
|
|
|
}) async {
|
2022-10-03 00:54:35 +00:00
|
|
|
await DB.instance.delete<Trade>(boxName: DB.boxNameTradesV2, key: uuid);
|
2022-08-26 08:11:35 +00:00
|
|
|
|
|
|
|
if (shouldNotifyListeners) {
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|