replace get supportsTor with an exchangesWithTorSupport List<Exchange>

more convenient to use elsewhere
This commit is contained in:
sneurlax 2023-09-11 18:07:59 -05:00
parent dcfb6e9dac
commit 45e6354fb6
4 changed files with 64 additions and 20 deletions

View file

@ -24,6 +24,7 @@ import 'package:stackwallet/themes/stack_colors.dart';
import 'package:stackwallet/utilities/assets.dart';
import 'package:stackwallet/utilities/constants.dart';
import 'package:stackwallet/utilities/enums/coin_enum.dart';
import 'package:stackwallet/utilities/prefs.dart';
import 'package:stackwallet/utilities/text_styles.dart';
import 'package:stackwallet/utilities/util.dart';
import 'package:stackwallet/widgets/background.dart';
@ -39,6 +40,8 @@ import 'package:stackwallet/widgets/stack_dialog.dart';
import 'package:stackwallet/widgets/stack_text_field.dart';
import 'package:stackwallet/widgets/textfield_icon_button.dart';
import '../../../services/exchange/exchange.dart';
class ExchangeCurrencySelectionView extends StatefulWidget {
const ExchangeCurrencySelectionView({
Key? key,
@ -165,20 +168,25 @@ class _ExchangeCurrencySelectionViewState
.rateTypeEqualTo(SupportedRateType.both)
.or()
.rateTypeEqualTo(SupportedRateType.fixed)
.and()
.not()
.exchangeNameEqualTo(ChangeNowExchange.exchangeName)
: q
.rateTypeEqualTo(SupportedRateType.both)
.or()
.rateTypeEqualTo(SupportedRateType.estimated)
.and()
.not()
.exchangeNameEqualTo(ChangeNowExchange.exchangeName))
.rateTypeEqualTo(SupportedRateType.estimated))
.sortByIsStackCoin()
.thenByName()
.findAll();
// If using Tor, filter exchanges which do not support Tor.
if (Prefs.instance.useTor) {
if (Exchange.exchangesWithTorSupport.isNotEmpty) {
currencies
.removeWhere((element) => !Exchange.exchangesWithTorSupport.any(
(e) => e.name == element.exchangeName,
));
// Could have also filtered using the List<String> Exchange.exchangeNamesWithTorSupport. But I didn't. This is fancier.
}
}
return _getDistinctCurrenciesFrom(currencies);
}

View file

@ -31,15 +31,19 @@ import 'package:stackwallet/pages/exchange_view/sub_widgets/rate_type_toggle.dar
import 'package:stackwallet/pages_desktop_specific/desktop_exchange/exchange_steps/step_scaffold.dart';
import 'package:stackwallet/providers/providers.dart';
import 'package:stackwallet/services/exchange/change_now/change_now_exchange.dart';
import 'package:stackwallet/services/exchange/exchange.dart';
import 'package:stackwallet/services/exchange/exchange_data_loading_service.dart';
import 'package:stackwallet/services/exchange/exchange_response.dart';
import 'package:stackwallet/services/exchange/majestic_bank/majestic_bank_exchange.dart';
import 'package:stackwallet/services/exchange/trocador/trocador_exchange.dart';
import 'package:stackwallet/services/tor_service.dart';
import 'package:stackwallet/themes/stack_colors.dart';
import 'package:stackwallet/utilities/amount/amount_unit.dart';
import 'package:stackwallet/utilities/assets.dart';
import 'package:stackwallet/utilities/constants.dart';
import 'package:stackwallet/utilities/enums/coin_enum.dart';
import 'package:stackwallet/utilities/enums/exchange_rate_type_enum.dart';
import 'package:stackwallet/utilities/prefs.dart';
import 'package:stackwallet/utilities/text_styles.dart';
import 'package:stackwallet/utilities/util.dart';
import 'package:stackwallet/widgets/conditional_parent.dart';
@ -55,8 +59,6 @@ import 'package:stackwallet/widgets/textfields/exchange_textfield.dart';
import 'package:tuple/tuple.dart';
import 'package:uuid/uuid.dart';
import '../../services/exchange/exchange_response.dart';
class ExchangeForm extends ConsumerStatefulWidget {
const ExchangeForm({
Key? key,
@ -78,7 +80,7 @@ class _ExchangeFormState extends ConsumerState<ExchangeForm> {
late final Coin? coin;
late final bool walletInitiated;
final exchanges = [
var exchanges = [
MajesticBankExchange.instance,
ChangeNowExchange.instance,
TrocadorExchange.instance,
@ -773,6 +775,14 @@ class _ExchangeFormState extends ConsumerState<ExchangeForm> {
});
}
// Instantiate the Tor service.
torService = TorService.sharedInstance;
// Filter exchanges based on Tor support.
if (Prefs.instance.useTor) {
exchanges = Exchange.exchangesWithTorSupport;
}
super.initState();
}
@ -1007,4 +1017,7 @@ class _ExchangeFormState extends ConsumerState<ExchangeForm> {
],
);
}
// TorService instance.
late TorService torService;
}

View file

@ -91,6 +91,24 @@ abstract class Exchange {
required bool reversed,
});
// Flag to indicate that the exchange supports Tor.
bool get supportsTor => false;
/// List of exchanges which support Tor.
///
/// Add to this list when adding a new exchange which supports Tor.
static List<Exchange> get exchangesWithTorSupport => [
MajesticBankExchange.instance,
TrocadorExchange.instance,
];
/// List of exchange names which support Tor.
///
/// Convenience method for when you just want to check for a String
/// .exchangeName instead of Exchange instances. Shouldn't need to be updated
/// as long as the above List is updated.
static List<String> get exchangeNamesWithTorSupport =>
exchangesWithTorSupport.map((exchange) => exchange.name).toList();
// Instead of using this, you can do like:
// currencies
// .removeWhere((element) => !Exchange.exchangesWithTorSupport.any(
// (e) => e.name == element.exchangeName,
// ));
}

View file

@ -20,6 +20,7 @@ import 'package:stackwallet/services/exchange/majestic_bank/majestic_bank_exchan
import 'package:stackwallet/services/exchange/trocador/trocador_exchange.dart';
import 'package:stackwallet/utilities/enums/exchange_rate_type_enum.dart';
import 'package:stackwallet/utilities/logger.dart';
import 'package:stackwallet/utilities/prefs.dart';
import 'package:stackwallet/utilities/stack_file_system.dart';
import 'package:tuple/tuple.dart';
@ -145,6 +146,7 @@ class ExchangeDataLoadingService {
final start = DateTime.now();
try {
/*
// Old exchange data loading code.
await Future.wait([
_loadChangeNowCurrencies(),
// _loadChangeNowFixedRatePairs(),
@ -159,16 +161,19 @@ class ExchangeDataLoadingService {
// await _loadChangeNowFixedRatePairs();
// await _loadChangeNowEstimatedRatePairs();
*/
// Use the supportsTor flag to filter out unsupported exchanges.
if (ChangeNowExchange.instance.supportsTor) {
// If using Tor, don't load data for exchanges which don't support Tor.
//
// Add to this list when adding an exchange which doesn't supports Tor.
if (!Prefs.instance.useTor) {
await _loadChangeNowCurrencies();
}
if (MajesticBankExchange.instance.supportsTor) {
await loadMajesticBankCurrencies();
}
if (TrocadorExchange.instance.supportsTor) {
await loadTrocadorCurrencies();
}
// Exchanges which support Tor just get treated normally.
await Future.wait([
loadMajesticBankCurrencies(),
loadTrocadorCurrencies(),
]);
Logging.instance.log(
"ExchangeDataLoadingService.loadAll finished in ${DateTime.now().difference(start).inSeconds} seconds",