stack_wallet/lib/services/wallets_service.dart

104 lines
3 KiB
Dart
Raw Normal View History

/*
2023-05-26 21:21:16 +00:00
* This file is part of Stack Wallet.
*
2023-05-26 21:21:16 +00:00
* 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 'dart:convert';
import 'package:flutter/material.dart';
2023-03-01 21:52:13 +00:00
import 'package:stackwallet/db/hive/db.dart';
2022-08-26 08:11:35 +00:00
import 'package:stackwallet/utilities/enums/coin_enum.dart';
import 'package:stackwallet/utilities/logger.dart';
@Deprecated("Legacy support only. Do not use.")
2022-08-26 08:11:35 +00:00
class WalletInfo {
final Coin coin;
final String walletId;
final String name;
@Deprecated("Legacy support only. Do not use.")
const WalletInfo({
required this.coin,
required this.walletId,
required this.name,
});
2022-08-26 08:11:35 +00:00
@Deprecated("Legacy support only. Do not use.")
2022-08-26 08:11:35 +00:00
factory WalletInfo.fromJson(Map<String, dynamic> jsonObject) {
return WalletInfo(
coin: Coin.values.byName(jsonObject["coin"] as String),
walletId: jsonObject["id"] as String,
name: jsonObject["name"] as String,
);
}
@Deprecated("Legacy support only. Do not use.")
2022-08-26 08:11:35 +00:00
Map<String, String> toMap() {
return {
"name": name,
"id": walletId,
"coin": coin.name,
};
}
@Deprecated("Legacy support only. Do not use.")
2022-08-26 08:11:35 +00:00
String toJsonString() {
return jsonEncode(toMap());
}
@override
@Deprecated("Legacy support only. Do not use.")
2022-08-26 08:11:35 +00:00
String toString() {
return "WalletInfo: ${toJsonString()}";
}
}
@Deprecated("Legacy support only. Do not use.")
2022-08-26 08:11:35 +00:00
class WalletsService extends ChangeNotifier {
@Deprecated("Legacy support only. Do not use.")
2022-08-26 08:11:35 +00:00
Future<Map<String, WalletInfo>>? _walletNames;
@Deprecated("Legacy support only. Do not use.")
2022-08-26 08:11:35 +00:00
Future<Map<String, WalletInfo>> get walletNames =>
_walletNames ??= _fetchWalletNames();
@Deprecated("Legacy support only. Do not use.")
WalletsService();
2022-08-26 08:11:35 +00:00
@Deprecated("Legacy support only. Do not use.")
2022-08-26 08:11:35 +00:00
Future<Map<String, WalletInfo>> _fetchWalletNames() async {
final names = DB.instance
.get<dynamic>(boxName: DB.boxNameAllWalletsData, key: 'names') as Map?;
if (names == null) {
Logging.instance.log(
"Fetched wallet 'names' returned null. Setting initializing 'names'",
level: LogLevel.Info);
await DB.instance.put<dynamic>(
boxName: DB.boxNameAllWalletsData,
key: 'names',
value: <String, dynamic>{});
return {};
}
2023-03-27 20:29:08 +00:00
Logging.instance.log("Fetched wallet names: $names", level: LogLevel.Info);
final mapped = Map<String, dynamic>.from(names);
mapped.removeWhere((name, dyn) {
final jsonObject = Map<String, dynamic>.from(dyn as Map);
try {
Coin.values.byName(jsonObject["coin"] as String);
return false;
} catch (e, s) {
Logging.instance.log("Error, ${jsonObject["coin"]} does not exist",
level: LogLevel.Error);
return true;
}
});
return mapped.map((name, dyn) => MapEntry(
name, WalletInfo.fromJson(Map<String, dynamic>.from(dyn as Map))));
}
2022-08-26 08:11:35 +00:00
}