mirror of
https://github.com/cypherstack/stack_wallet.git
synced 2024-12-24 12:29:37 +00:00
73 lines
1.8 KiB
Dart
73 lines
1.8 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:isar/isar.dart';
|
|
import 'package:stackwallet/providers/db/main_db_provider.dart';
|
|
import 'package:stackwallet/utilities/enums/coin_enum.dart';
|
|
import 'package:stackwallet/wallets/isar/models/wallet_info.dart';
|
|
|
|
final pAllWalletsInfo = Provider((ref) {
|
|
return ref.watch(_pAllWalletsInfo.select((value) => value.value));
|
|
});
|
|
|
|
final pAllWalletsInfoByCoin = Provider((ref) {
|
|
final infos = ref.watch(pAllWalletsInfo);
|
|
|
|
final Map<Coin, ({Coin coin, List<WalletInfo> wallets})> map = {};
|
|
|
|
for (final info in infos) {
|
|
if (map[info.coin] == null) {
|
|
map[info.coin] = (coin: info.coin, wallets: []);
|
|
}
|
|
|
|
map[info.coin]!.wallets.add(info);
|
|
}
|
|
|
|
final List<({Coin coin, List<WalletInfo> wallets})> results = [];
|
|
for (final coin in Coin.values) {
|
|
if (map[coin] != null) {
|
|
results.add(map[coin]!);
|
|
}
|
|
}
|
|
|
|
return results;
|
|
});
|
|
|
|
_WalletInfoWatcher? _globalInstance;
|
|
|
|
final _pAllWalletsInfo = ChangeNotifierProvider((ref) {
|
|
if (_globalInstance == null) {
|
|
final isar = ref.watch(mainDBProvider).isar;
|
|
_globalInstance = _WalletInfoWatcher(
|
|
isar.walletInfo.where().findAllSync(),
|
|
isar,
|
|
);
|
|
}
|
|
|
|
return _globalInstance!;
|
|
});
|
|
|
|
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) {
|
|
isar.walletInfo.where().findAll().then((value) {
|
|
_value = value;
|
|
notifyListeners();
|
|
});
|
|
});
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_streamSubscription.cancel();
|
|
super.dispose();
|
|
}
|
|
}
|