From 8206972309f7f3936045489f09ebb333f2ea1c24 Mon Sep 17 00:00:00 2001 From: julian Date: Wed, 8 Feb 2023 08:43:42 -0600 Subject: [PATCH] add loading screen if not exchange data cache exists while waiting for it to be populated for the first time. Added checks for incognito mode --- lib/main.dart | 7 +- lib/pages/exchange_view/exchange_view.dart | 323 ++++++++++-------- .../wallet_initiated_exchange_view.dart | 187 ++++++---- .../desktop_exchange_view.dart | 169 ++++++--- .../exchange_data_loading_service.dart | 50 ++- 5 files changed, 468 insertions(+), 268 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 146d090ec..b83a89354 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -17,6 +17,7 @@ import 'package:hive_flutter/hive_flutter.dart'; import 'package:isar/isar.dart'; import 'package:keyboard_dismisser/keyboard_dismisser.dart'; import 'package:path_provider/path_provider.dart'; +import 'package:stackwallet/db/main_db.dart'; import 'package:stackwallet/hive/db.dart'; import 'package:stackwallet/models/exchange/change_now/exchange_transaction.dart'; import 'package:stackwallet/models/exchange/change_now/exchange_transaction_status.dart'; @@ -65,8 +66,6 @@ import 'package:stackwallet/utilities/theme/stack_colors.dart'; import 'package:stackwallet/utilities/util.dart'; import 'package:window_size/window_size.dart'; -import 'db/main_db.dart'; - final openedFromSWBFileStringStateProvider = StateProvider((ref) => null); @@ -290,10 +289,6 @@ class _MaterialAppWithThemeState extends ConsumerState // TODO: this should probably run unawaited. Keep commented out for now as proper community nodes ui hasn't been implemented yet // unawaited(_nodeService.updateCommunityNodes()); - print("================================================"); - print("${ref.read(prefsChangeNotifierProvider).externalCalls}"); - print("${await ref.read(prefsChangeNotifierProvider).isExternalCallsSet()}"); - print("================================================"); // run without awaiting if (ref.read(prefsChangeNotifierProvider).externalCalls && await ref.read(prefsChangeNotifierProvider).isExternalCallsSet()) { diff --git a/lib/pages/exchange_view/exchange_view.dart b/lib/pages/exchange_view/exchange_view.dart index f429b2c7b..054ced189 100644 --- a/lib/pages/exchange_view/exchange_view.dart +++ b/lib/pages/exchange_view/exchange_view.dart @@ -8,9 +8,12 @@ import 'package:stackwallet/pages/exchange_view/exchange_form.dart'; import 'package:stackwallet/pages/exchange_view/trade_details_view.dart'; import 'package:stackwallet/providers/global/trades_service_provider.dart'; import 'package:stackwallet/providers/providers.dart'; +import 'package:stackwallet/services/exchange/exchange_data_loading_service.dart'; import 'package:stackwallet/utilities/constants.dart'; import 'package:stackwallet/utilities/text_styles.dart'; import 'package:stackwallet/utilities/theme/stack_colors.dart'; +import 'package:stackwallet/widgets/conditional_parent.dart'; +import 'package:stackwallet/widgets/custom_loading_overlay.dart'; import 'package:stackwallet/widgets/trade_card.dart'; import 'package:tuple/tuple.dart'; @@ -24,8 +27,38 @@ class ExchangeView extends ConsumerStatefulWidget { } class _ExchangeViewState extends ConsumerState { + bool _initialCachePopulationUnderway = false; + @override void initState() { + if (!ref.read(prefsChangeNotifierProvider).externalCalls) { + if (ExchangeDataLoadingService.currentCacheVersion < + ExchangeDataLoadingService.cacheVersion) { + _initialCachePopulationUnderway = true; + ExchangeDataLoadingService.instance.onLoadingComplete = () { + WidgetsBinding.instance.addPostFrameCallback((timeStamp) { + setState(() { + _initialCachePopulationUnderway = false; + }); + }); + }; + } + ExchangeDataLoadingService.instance + .init() + .then((_) => ExchangeDataLoadingService.instance.loadAll()); + } else if (ExchangeDataLoadingService.instance.isLoading && + ExchangeDataLoadingService.currentCacheVersion < + ExchangeDataLoadingService.cacheVersion) { + _initialCachePopulationUnderway = true; + ExchangeDataLoadingService.instance.onLoadingComplete = () { + WidgetsBinding.instance.addPostFrameCallback((timeStamp) { + setState(() { + _initialCachePopulationUnderway = false; + }); + }); + }; + } + super.initState(); } @@ -38,154 +71,176 @@ class _ExchangeViewState extends ConsumerState { Widget build(BuildContext context) { debugPrint("BUILD: $runtimeType"); - return SafeArea( - child: NestedScrollView( - floatHeaderSlivers: true, - headerSliverBuilder: (context, innerBoxIsScrolled) { - return [ - SliverOverlapAbsorber( - handle: NestedScrollView.sliverOverlapAbsorberHandleFor(context), - sliver: const SliverToBoxAdapter( - child: Padding( - padding: EdgeInsets.only( - left: 16, - right: 16, - top: 16, - ), - child: ExchangeForm(), - ), + return ConditionalParent( + condition: _initialCachePopulationUnderway, + builder: (child) { + return Stack( + children: [ + child, + Material( + color: Theme.of(context) + .extension()! + .overlay + .withOpacity(0.6), + child: const CustomLoadingOverlay( + message: "Updating exchange data", + subMessage: "This could take a few minutes", + eventBus: null, ), ) - ]; - }, - body: Builder( - builder: (buildContext) { - final trades = ref - .watch(tradesServiceProvider.select((value) => value.trades)); - final tradeCount = trades.length; - final hasHistory = tradeCount > 0; - - return Padding( - padding: const EdgeInsets.symmetric(horizontal: 12), - child: CustomScrollView( - slivers: [ - SliverOverlapInjector( - handle: NestedScrollView.sliverOverlapAbsorberHandleFor( - buildContext, + ], + ); + }, + child: SafeArea( + child: NestedScrollView( + floatHeaderSlivers: true, + headerSliverBuilder: (context, innerBoxIsScrolled) { + return [ + SliverOverlapAbsorber( + handle: + NestedScrollView.sliverOverlapAbsorberHandleFor(context), + sliver: const SliverToBoxAdapter( + child: Padding( + padding: EdgeInsets.only( + left: 16, + right: 16, + top: 16, ), + child: ExchangeForm(), ), - SliverToBoxAdapter( - child: Padding( - padding: const EdgeInsets.symmetric(horizontal: 4), - child: Column( - crossAxisAlignment: CrossAxisAlignment.stretch, - children: [ - const SizedBox( - height: 12, - ), - Text( - "Trades", - style: STextStyles.itemSubtitle(context).copyWith( - color: Theme.of(context) - .extension()! - .textDark3, - ), - ), - const SizedBox( - height: 12, - ), - ], + ), + ) + ]; + }, + body: Builder( + builder: (buildContext) { + final trades = ref + .watch(tradesServiceProvider.select((value) => value.trades)); + final tradeCount = trades.length; + final hasHistory = tradeCount > 0; + + return Padding( + padding: const EdgeInsets.symmetric(horizontal: 12), + child: CustomScrollView( + slivers: [ + SliverOverlapInjector( + handle: NestedScrollView.sliverOverlapAbsorberHandleFor( + buildContext, ), ), - ), - if (hasHistory) - SliverList( - delegate: SliverChildBuilderDelegate((context, index) { - return Padding( - padding: const EdgeInsets.all(4), - child: TradeCard( - key: Key("tradeCard_${trades[index].uuid}"), - trade: trades[index], - onTap: () async { - final String tradeId = trades[index].tradeId; - - final lookup = ref - .read(tradeSentFromStackLookupProvider) - .all; - - //todo: check if print needed - // debugPrint("ALL: $lookup"); - - final String? txid = ref - .read(tradeSentFromStackLookupProvider) - .getTxidForTradeId(tradeId); - final List? walletIds = ref - .read(tradeSentFromStackLookupProvider) - .getWalletIdsForTradeId(tradeId); - - if (txid != null && - walletIds != null && - walletIds.isNotEmpty) { - final manager = ref - .read(walletsChangeNotifierProvider) - .getManager(walletIds.first); - - //todo: check if print needed - // debugPrint("name: ${manager.walletName}"); - - final tx = await MainDB.instance - .getTransactions(walletIds.first) - .filter() - .txidEqualTo(txid) - .findFirst(); - - if (mounted) { - unawaited(Navigator.of(context).pushNamed( - TradeDetailsView.routeName, - arguments: Tuple4(tradeId, tx, - walletIds.first, manager.walletName), - )); - } - } else { - unawaited(Navigator.of(context).pushNamed( - TradeDetailsView.routeName, - arguments: Tuple4( - tradeId, null, walletIds?.first, null), - )); - } - }, - ), - ); - }, childCount: tradeCount), - ), - if (!hasHistory) SliverToBoxAdapter( child: Padding( padding: const EdgeInsets.symmetric(horizontal: 4), - child: Container( - decoration: BoxDecoration( - color: Theme.of(context) - .extension()! - .popupBG, - borderRadius: BorderRadius.circular( - Constants.size.circularBorderRadius, + child: Column( + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + const SizedBox( + height: 12, ), - ), - child: Padding( - padding: const EdgeInsets.all(12), - child: Text( - "Trades will appear here", - textAlign: TextAlign.center, - style: STextStyles.itemSubtitle(context), + Text( + "Trades", + style: STextStyles.itemSubtitle(context).copyWith( + color: Theme.of(context) + .extension()! + .textDark3, + ), + ), + const SizedBox( + height: 12, + ), + ], + ), + ), + ), + if (hasHistory) + SliverList( + delegate: SliverChildBuilderDelegate((context, index) { + return Padding( + padding: const EdgeInsets.all(4), + child: TradeCard( + key: Key("tradeCard_${trades[index].uuid}"), + trade: trades[index], + onTap: () async { + final String tradeId = trades[index].tradeId; + + final lookup = ref + .read(tradeSentFromStackLookupProvider) + .all; + + //todo: check if print needed + // debugPrint("ALL: $lookup"); + + final String? txid = ref + .read(tradeSentFromStackLookupProvider) + .getTxidForTradeId(tradeId); + final List? walletIds = ref + .read(tradeSentFromStackLookupProvider) + .getWalletIdsForTradeId(tradeId); + + if (txid != null && + walletIds != null && + walletIds.isNotEmpty) { + final manager = ref + .read(walletsChangeNotifierProvider) + .getManager(walletIds.first); + + //todo: check if print needed + // debugPrint("name: ${manager.walletName}"); + + final tx = await MainDB.instance + .getTransactions(walletIds.first) + .filter() + .txidEqualTo(txid) + .findFirst(); + + if (mounted) { + unawaited(Navigator.of(context).pushNamed( + TradeDetailsView.routeName, + arguments: Tuple4(tradeId, tx, + walletIds.first, manager.walletName), + )); + } + } else { + unawaited(Navigator.of(context).pushNamed( + TradeDetailsView.routeName, + arguments: Tuple4( + tradeId, null, walletIds?.first, null), + )); + } + }, + ), + ); + }, childCount: tradeCount), + ), + if (!hasHistory) + SliverToBoxAdapter( + child: Padding( + padding: const EdgeInsets.symmetric(horizontal: 4), + child: Container( + decoration: BoxDecoration( + color: Theme.of(context) + .extension()! + .popupBG, + borderRadius: BorderRadius.circular( + Constants.size.circularBorderRadius, + ), + ), + child: Padding( + padding: const EdgeInsets.all(12), + child: Text( + "Trades will appear here", + textAlign: TextAlign.center, + style: STextStyles.itemSubtitle(context), + ), ), ), ), ), - ), - ], - ), - ); - }, + ], + ), + ); + }, + ), ), ), ); diff --git a/lib/pages/exchange_view/wallet_initiated_exchange_view.dart b/lib/pages/exchange_view/wallet_initiated_exchange_view.dart index 915fb33ba..e6ee7d3a3 100644 --- a/lib/pages/exchange_view/wallet_initiated_exchange_view.dart +++ b/lib/pages/exchange_view/wallet_initiated_exchange_view.dart @@ -4,11 +4,15 @@ import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:stackwallet/pages/exchange_view/exchange_form.dart'; import 'package:stackwallet/pages/exchange_view/sub_widgets/step_row.dart'; +import 'package:stackwallet/providers/global/prefs_provider.dart'; +import 'package:stackwallet/services/exchange/exchange_data_loading_service.dart'; import 'package:stackwallet/utilities/enums/coin_enum.dart'; import 'package:stackwallet/utilities/text_styles.dart'; import 'package:stackwallet/utilities/theme/stack_colors.dart'; import 'package:stackwallet/widgets/background.dart'; +import 'package:stackwallet/widgets/conditional_parent.dart'; import 'package:stackwallet/widgets/custom_buttons/app_bar_icon_button.dart'; +import 'package:stackwallet/widgets/custom_loading_overlay.dart'; class WalletInitiatedExchangeView extends ConsumerStatefulWidget { const WalletInitiatedExchangeView({ @@ -32,10 +36,41 @@ class _WalletInitiatedExchangeViewState late final String walletId; late final Coin coin; + bool _initialCachePopulationUnderway = false; + @override void initState() { walletId = widget.walletId; coin = widget.coin; + + if (!ref.read(prefsChangeNotifierProvider).externalCalls) { + if (ExchangeDataLoadingService.currentCacheVersion < + ExchangeDataLoadingService.cacheVersion) { + _initialCachePopulationUnderway = true; + ExchangeDataLoadingService.instance.onLoadingComplete = () { + WidgetsBinding.instance.addPostFrameCallback((timeStamp) { + setState(() { + _initialCachePopulationUnderway = false; + }); + }); + }; + } + ExchangeDataLoadingService.instance + .init() + .then((_) => ExchangeDataLoadingService.instance.loadAll()); + } else if (ExchangeDataLoadingService.instance.isLoading && + ExchangeDataLoadingService.currentCacheVersion < + ExchangeDataLoadingService.cacheVersion) { + _initialCachePopulationUnderway = true; + ExchangeDataLoadingService.instance.onLoadingComplete = () { + WidgetsBinding.instance.addPostFrameCallback((timeStamp) { + setState(() { + _initialCachePopulationUnderway = false; + }); + }); + }; + } + super.initState(); } @@ -48,76 +83,98 @@ class _WalletInitiatedExchangeViewState Widget build(BuildContext context) { debugPrint("BUILD: $runtimeType"); - return Background( - child: Scaffold( - backgroundColor: Theme.of(context).extension()!.background, - appBar: AppBar( - leading: AppBarBackButton( - onPressed: () async { - if (FocusScope.of(context).hasFocus) { - FocusScope.of(context).unfocus(); - await Future.delayed(const Duration(milliseconds: 75)); - } - if (mounted) { - Navigator.of(context).pop(); - } - }, + return ConditionalParent( + condition: _initialCachePopulationUnderway, + builder: (child) { + return Stack( + children: [ + child, + Material( + color: Theme.of(context) + .extension()! + .overlay + .withOpacity(0.6), + child: const CustomLoadingOverlay( + message: "Updating exchange data", + subMessage: "This could take a few minutes", + eventBus: null, + ), + ) + ], + ); + }, + child: Background( + child: Scaffold( + backgroundColor: + Theme.of(context).extension()!.background, + appBar: AppBar( + leading: AppBarBackButton( + onPressed: () async { + if (FocusScope.of(context).hasFocus) { + FocusScope.of(context).unfocus(); + await Future.delayed(const Duration(milliseconds: 75)); + } + if (mounted) { + Navigator.of(context).pop(); + } + }, + ), + title: Text( + "Exchange", + style: STextStyles.navBarTitle(context), + ), ), - title: Text( - "Exchange", - style: STextStyles.navBarTitle(context), - ), - ), - body: LayoutBuilder( - builder: (context, constraints) { - final width = MediaQuery.of(context).size.width - 32; - return Padding( - padding: const EdgeInsets.all(12), - child: SingleChildScrollView( - child: ConstrainedBox( - constraints: BoxConstraints( - minHeight: constraints.maxHeight - 24, - ), - child: IntrinsicHeight( - child: Padding( - padding: const EdgeInsets.all(4), - child: Column( - crossAxisAlignment: CrossAxisAlignment.stretch, - children: [ - StepRow( - count: 4, - current: 0, - width: width, - ), - const SizedBox( - height: 14, - ), - Text( - "Exchange amount", - style: STextStyles.pageTitleH1(context), - ), - const SizedBox( - height: 8, - ), - Text( - "Network fees and other exchange charges are included in the rate.", - style: STextStyles.itemSubtitle(context), - ), - const SizedBox( - height: 24, - ), - ExchangeForm( - walletId: walletId, - coin: coin, - ), - ], + body: LayoutBuilder( + builder: (context, constraints) { + final width = MediaQuery.of(context).size.width - 32; + return Padding( + padding: const EdgeInsets.all(12), + child: SingleChildScrollView( + child: ConstrainedBox( + constraints: BoxConstraints( + minHeight: constraints.maxHeight - 24, + ), + child: IntrinsicHeight( + child: Padding( + padding: const EdgeInsets.all(4), + child: Column( + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + StepRow( + count: 4, + current: 0, + width: width, + ), + const SizedBox( + height: 14, + ), + Text( + "Exchange amount", + style: STextStyles.pageTitleH1(context), + ), + const SizedBox( + height: 8, + ), + Text( + "Network fees and other exchange charges are included in the rate.", + style: STextStyles.itemSubtitle(context), + ), + const SizedBox( + height: 24, + ), + ExchangeForm( + walletId: walletId, + coin: coin, + ), + ], + ), ), ), ), ), - ), - ); - }, + ); + }, + ), ), ), ); diff --git a/lib/pages_desktop_specific/desktop_exchange/desktop_exchange_view.dart b/lib/pages_desktop_specific/desktop_exchange/desktop_exchange_view.dart index 105c485f0..19484e376 100644 --- a/lib/pages_desktop_specific/desktop_exchange/desktop_exchange_view.dart +++ b/lib/pages_desktop_specific/desktop_exchange/desktop_exchange_view.dart @@ -1,79 +1,142 @@ import 'package:flutter/material.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:stackwallet/pages/exchange_view/exchange_form.dart'; import 'package:stackwallet/pages_desktop_specific/desktop_exchange/subwidgets/desktop_trade_history.dart'; +import 'package:stackwallet/providers/global/prefs_provider.dart'; +import 'package:stackwallet/services/exchange/exchange_data_loading_service.dart'; import 'package:stackwallet/utilities/text_styles.dart'; +import 'package:stackwallet/utilities/theme/stack_colors.dart'; +import 'package:stackwallet/widgets/conditional_parent.dart'; +import 'package:stackwallet/widgets/custom_loading_overlay.dart'; import 'package:stackwallet/widgets/desktop/desktop_app_bar.dart'; import 'package:stackwallet/widgets/desktop/desktop_scaffold.dart'; import 'package:stackwallet/widgets/rounded_white_container.dart'; -class DesktopExchangeView extends StatefulWidget { +class DesktopExchangeView extends ConsumerStatefulWidget { const DesktopExchangeView({Key? key}) : super(key: key); static const String routeName = "/desktopExchange"; @override - State createState() => _DesktopExchangeViewState(); + ConsumerState createState() => + _DesktopExchangeViewState(); } -class _DesktopExchangeViewState extends State { +class _DesktopExchangeViewState extends ConsumerState { + bool _initialCachePopulationUnderway = false; + + @override + void initState() { + if (!ref.read(prefsChangeNotifierProvider).externalCalls) { + if (ExchangeDataLoadingService.currentCacheVersion < + ExchangeDataLoadingService.cacheVersion) { + _initialCachePopulationUnderway = true; + ExchangeDataLoadingService.instance.onLoadingComplete = () { + WidgetsBinding.instance.addPostFrameCallback((timeStamp) { + setState(() { + _initialCachePopulationUnderway = false; + }); + }); + }; + } + ExchangeDataLoadingService.instance + .init() + .then((_) => ExchangeDataLoadingService.instance.loadAll()); + } else if (ExchangeDataLoadingService.instance.isLoading && + ExchangeDataLoadingService.currentCacheVersion < + ExchangeDataLoadingService.cacheVersion) { + _initialCachePopulationUnderway = true; + ExchangeDataLoadingService.instance.onLoadingComplete = () { + WidgetsBinding.instance.addPostFrameCallback((timeStamp) { + setState(() { + _initialCachePopulationUnderway = false; + }); + }); + }; + } + + super.initState(); + } + @override Widget build(BuildContext context) { - return DesktopScaffold( - appBar: DesktopAppBar( - isCompactHeight: true, - leading: Padding( + return ConditionalParent( + condition: _initialCachePopulationUnderway, + builder: (child) { + return Stack( + children: [ + child, + Material( + color: Theme.of(context) + .extension()! + .overlay + .withOpacity(0.6), + child: const CustomLoadingOverlay( + message: "Updating exchange data", + subMessage: "This could take a few minutes", + eventBus: null, + ), + ) + ], + ); + }, + child: DesktopScaffold( + appBar: DesktopAppBar( + isCompactHeight: true, + leading: Padding( + padding: const EdgeInsets.only( + left: 24, + ), + child: Text( + "Exchange", + style: STextStyles.desktopH3(context), + ), + ), + ), + body: Padding( padding: const EdgeInsets.only( left: 24, + right: 24, + bottom: 24, ), - child: Text( - "Exchange", - style: STextStyles.desktopH3(context), + child: Row( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Expanded( + child: Column( + mainAxisSize: MainAxisSize.min, + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + "Exchange details", + style: STextStyles.desktopTextExtraExtraSmall(context), + ), + const SizedBox( + height: 16, + ), + const RoundedWhiteContainer( + padding: EdgeInsets.all(24), + child: ExchangeForm(), + ), + ], + ), + ), + const SizedBox( + width: 16, + ), + Expanded( + child: Row( + children: const [ + Expanded( + child: DesktopTradeHistory(), + ), + ], + ), + ), + ], ), ), ), - body: Padding( - padding: const EdgeInsets.only( - left: 24, - right: 24, - bottom: 24, - ), - child: Row( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Expanded( - child: Column( - mainAxisSize: MainAxisSize.min, - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Text( - "Exchange details", - style: STextStyles.desktopTextExtraExtraSmall(context), - ), - const SizedBox( - height: 16, - ), - const RoundedWhiteContainer( - padding: EdgeInsets.all(24), - child: ExchangeForm(), - ), - ], - ), - ), - const SizedBox( - width: 16, - ), - Expanded( - child: Row( - children: const [ - Expanded( - child: DesktopTradeHistory(), - ), - ], - ), - ), - ], - ), - ), ); } } diff --git a/lib/services/exchange/exchange_data_loading_service.dart b/lib/services/exchange/exchange_data_loading_service.dart index 8298aed96..3d29e038d 100644 --- a/lib/services/exchange/exchange_data_loading_service.dart +++ b/lib/services/exchange/exchange_data_loading_service.dart @@ -1,5 +1,6 @@ import 'package:flutter/foundation.dart'; import 'package:isar/isar.dart'; +import 'package:stackwallet/hive/db.dart'; import 'package:stackwallet/models/isar/exchange_cache/currency.dart'; import 'package:stackwallet/models/isar/exchange_cache/pair.dart'; import 'package:stackwallet/services/exchange/change_now/change_now_exchange.dart'; @@ -16,6 +17,25 @@ class ExchangeDataLoadingService { Isar? _isar; Isar get isar => _isar!; + VoidCallback? onLoadingError; + VoidCallback? onLoadingComplete; + + static const int cacheVersion = 1; + + static int get currentCacheVersion => + DB.instance.get( + boxName: DB.boxNameDBInfo, + key: "exchange_data_cache_version") as int? ?? + 0; + + Future _updateCurrentCacheVersion(int version) async { + await DB.instance.put( + boxName: DB.boxNameDBInfo, + key: "exchange_data_cache_version", + value: version, + ); + } + Future init() async { if (_isar != null && isar.isOpen) return; _isar = await Isar.open( @@ -25,18 +45,23 @@ class ExchangeDataLoadingService { ], directory: (await StackFileSystem.applicationIsarDirectory()).path, inspector: kDebugMode, + // inspector: false, name: "exchange_cache", ); } + bool get isLoading => _locked; + bool _locked = false; Future loadAll() async { - print("LOADINGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG: LOCKED=$_locked"); if (!_locked) { _locked = true; - print("LOADINGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG"); - final time = DateTime.now(); + Logging.instance.log( + "ExchangeDataLoadingService.loadAll starting...", + level: LogLevel.Info, + ); + final start = DateTime.now(); try { await Future.wait([ _loadChangeNowCurrencies(), @@ -46,13 +71,18 @@ class ExchangeDataLoadingService { // loadSimpleswapFloatingRateCurrencies(ref), loadMajesticBankCurrencies(), ]); - - print( - "LOADINGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG done in ${DateTime.now().difference(time).inSeconds} seconds"); + Logging.instance.log( + "ExchangeDataLoadingService.loadAll finished in ${DateTime.now().difference(start).inSeconds} seconds", + level: LogLevel.Info, + ); + onLoadingComplete?.call(); + await _updateCurrentCacheVersion(cacheVersion); } catch (e, s) { Logging.instance.log( - "ExchangeDataLoadingService.loadAll failed: $e\n$s", - level: LogLevel.Error); + "ExchangeDataLoadingService.loadAll failed after ${DateTime.now().difference(start).inSeconds} seconds: $e\n$s", + level: LogLevel.Error, + ); + onLoadingError?.call(); } _locked = false; } @@ -82,7 +112,7 @@ class ExchangeDataLoadingService { Future _loadChangeNowFixedRatePairs() async { final exchange = ChangeNowExchange.instance; - final responsePairs = await exchange.getAllPairs(true); + final responsePairs = await compute(exchange.getAllPairs, true); if (responsePairs.value != null) { await isar.writeTxn(() async { @@ -107,7 +137,7 @@ class ExchangeDataLoadingService { Future _loadChangeNowEstimatedRatePairs() async { final exchange = ChangeNowExchange.instance; - final responsePairs = await exchange.getAllPairs(false); + final responsePairs = await compute(exchange.getAllPairs, false); if (responsePairs.value != null) { await isar.writeTxn(() async {