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
|
|
|
|
|
|
|
class _Watcher extends ChangeNotifier {
|
2023-11-08 21:43:01 +00:00
|
|
|
final bool isFavourite;
|
2023-11-03 19:46:55 +00:00
|
|
|
late final StreamSubscription<List<WalletInfo>> _streamSubscription;
|
|
|
|
|
|
|
|
List<WalletInfo> _value;
|
|
|
|
|
|
|
|
List<WalletInfo> get value => _value;
|
|
|
|
|
2023-11-08 21:43:01 +00:00
|
|
|
_Watcher(this._value, this.isFavourite, Isar isar) {
|
2023-11-03 19:46:55 +00:00
|
|
|
_streamSubscription = isar.walletInfo
|
|
|
|
.filter()
|
2024-05-21 18:48:48 +00:00
|
|
|
.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),
|
|
|
|
)
|
2023-11-08 21:43:01 +00:00
|
|
|
.isFavouriteEqualTo(isFavourite)
|
|
|
|
.sortByFavouriteOrderIndex()
|
|
|
|
.watch(fireImmediately: true)
|
2023-11-03 19:46:55 +00:00
|
|
|
.listen((event) {
|
|
|
|
_value = event;
|
|
|
|
notifyListeners();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
_streamSubscription.cancel();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-08 21:43:01 +00:00
|
|
|
final _wiProvider = ChangeNotifierProvider.family<_Watcher, bool>(
|
|
|
|
(ref, isFavourite) {
|
2023-11-03 19:46:55 +00:00
|
|
|
final isar = ref.watch(mainDBProvider).isar;
|
|
|
|
|
|
|
|
final watcher = _Watcher(
|
|
|
|
isar.walletInfo
|
|
|
|
.filter()
|
2024-05-21 18:48:48 +00:00
|
|
|
.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),
|
|
|
|
)
|
2023-11-08 21:43:01 +00:00
|
|
|
.isFavouriteEqualTo(isFavourite)
|
2023-11-03 19:46:55 +00:00
|
|
|
.sortByFavouriteOrderIndex()
|
|
|
|
.findAllSync(),
|
2023-11-08 21:43:01 +00:00
|
|
|
isFavourite,
|
2023-11-03 19:46:55 +00:00
|
|
|
isar,
|
|
|
|
);
|
|
|
|
|
|
|
|
ref.onDispose(() => watcher.dispose());
|
|
|
|
|
|
|
|
return watcher;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2023-11-08 21:43:01 +00:00
|
|
|
final pFavouriteWalletInfos = Provider.family<List<WalletInfo>, bool>(
|
|
|
|
(ref, isFavourite) {
|
|
|
|
return ref.watch(_wiProvider(isFavourite)).value;
|
2023-11-03 19:46:55 +00:00
|
|
|
},
|
|
|
|
);
|