2023-11-03 19:46:55 +00:00
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
import 'package:isar/isar.dart';
|
2024-05-23 00:37:06 +00:00
|
|
|
import '../../../providers/db/main_db_provider.dart';
|
|
|
|
import '../../../app_config.dart';
|
|
|
|
import '../../crypto_currency/crypto_currency.dart';
|
|
|
|
import '../models/wallet_info.dart';
|
2023-11-03 19:46:55 +00:00
|
|
|
|
|
|
|
final pAllWalletsInfo = Provider((ref) {
|
2023-11-15 16:59:03 +00:00
|
|
|
return ref.watch(_pAllWalletsInfo.select((value) => value.value));
|
|
|
|
});
|
|
|
|
|
|
|
|
final pAllWalletsInfoByCoin = Provider((ref) {
|
|
|
|
final infos = ref.watch(pAllWalletsInfo);
|
|
|
|
|
2024-05-15 21:20:45 +00:00
|
|
|
final Map<CryptoCurrency, ({CryptoCurrency coin, List<WalletInfo> wallets})>
|
|
|
|
map = {};
|
2023-11-15 16:59:03 +00:00
|
|
|
|
|
|
|
for (final info in infos) {
|
|
|
|
if (map[info.coin] == null) {
|
|
|
|
map[info.coin] = (coin: info.coin, wallets: []);
|
|
|
|
}
|
|
|
|
|
|
|
|
map[info.coin]!.wallets.add(info);
|
|
|
|
}
|
|
|
|
|
2024-05-15 21:20:45 +00:00
|
|
|
final List<({CryptoCurrency coin, List<WalletInfo> wallets})> results = [];
|
2024-05-22 19:38:49 +00:00
|
|
|
for (final coin in AppConfig.coins) {
|
2023-11-15 16:59:03 +00:00
|
|
|
if (map[coin] != null) {
|
|
|
|
results.add(map[coin]!);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
});
|
|
|
|
|
|
|
|
_WalletInfoWatcher? _globalInstance;
|
|
|
|
|
|
|
|
final _pAllWalletsInfo = ChangeNotifierProvider((ref) {
|
2023-11-03 19:46:55 +00:00
|
|
|
if (_globalInstance == null) {
|
|
|
|
final isar = ref.watch(mainDBProvider).isar;
|
|
|
|
_globalInstance = _WalletInfoWatcher(
|
2024-05-21 18:48:48 +00:00
|
|
|
isar.walletInfo
|
|
|
|
.where()
|
|
|
|
.filter()
|
|
|
|
.anyOf<String, CryptoCurrency>(
|
2024-05-22 19:38:49 +00:00
|
|
|
AppConfig.coins.map((e) => e.identifier),
|
2024-05-21 18:48:48 +00:00
|
|
|
(q, element) => q.coinNameMatches(element),
|
|
|
|
)
|
|
|
|
.findAllSync(),
|
2023-11-03 19:46:55 +00:00
|
|
|
isar,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-11-15 16:59:03 +00:00
|
|
|
return _globalInstance!;
|
2023-11-03 19:46:55 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
class _WalletInfoWatcher extends ChangeNotifier {
|
|
|
|
late final StreamSubscription<void> _streamSubscription;
|
|
|
|
|
|
|
|
List<WalletInfo> _value;
|
|
|
|
|
|
|
|
List<WalletInfo> get value => _value;
|
|
|
|
|
|
|
|
_WalletInfoWatcher(this._value, Isar isar) {
|
|
|
|
_streamSubscription =
|
|
|
|
isar.walletInfo.watchLazy(fireImmediately: true).listen((event) {
|
2024-05-21 18:48:48 +00:00
|
|
|
isar.walletInfo
|
|
|
|
.where()
|
|
|
|
.filter()
|
|
|
|
.anyOf<String, CryptoCurrency>(
|
2024-05-22 19:38:49 +00:00
|
|
|
AppConfig.coins.map((e) => e.identifier),
|
2024-05-21 18:48:48 +00:00
|
|
|
(q, element) => q.coinNameMatches(element),
|
|
|
|
)
|
|
|
|
.findAll()
|
|
|
|
.then((value) {
|
2023-11-03 19:46:55 +00:00
|
|
|
_value = value;
|
|
|
|
notifyListeners();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
_streamSubscription.cancel();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
}
|