Skip to content

Commit

Permalink
fix: 🐛 Upgrade OracleService usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chralu committed Dec 17, 2024
1 parent c2d7bce commit 2b8b64f
Show file tree
Hide file tree
Showing 12 changed files with 164 additions and 138 deletions.
8 changes: 4 additions & 4 deletions lib/application/aeswap/dex_token.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ Future<double> _estimateTokenInFiat(
String tokenAddress,
) async {
if (tokenAddress.isUCO) {
return ref.watch(
aedappfm.ArchethicOracleUCOProviders.archethicOracleUCO
.select((value) => value.usd),
);
return (await ref.watch(
aedappfm.ArchethicOracleUCOProviders.archethicOracleUCO.future,
))
.usd;
} else {
final environment = ref.watch(environmentProvider);
return await ref.watch(
Expand Down
2 changes: 1 addition & 1 deletion lib/application/aeswap/dex_token.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions lib/application/market_price.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ Future<MarketPrice> _currencyMarketPrice(
Future<MarketPrice> _selectedCurrencyMarketPrice(
Ref ref,
) async {
final archethicOracleUCO =
ref.watch(aedappfm.ArchethicOracleUCOProviders.archethicOracleUCO);
final archethicOracleUCO = await ref.watch(
aedappfm.ArchethicOracleUCOProviders.archethicOracleUCO.future,
);
return MarketPrice(
amount: archethicOracleUCO.usd,
lastLoading: archethicOracleUCO.timestamp,
Expand Down
2 changes: 1 addition & 1 deletion lib/application/market_price.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 0 additions & 22 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -171,17 +171,6 @@ class AppState extends ConsumerState<App> with WidgetsBindingObserver {
switch (state) {
case AppLifecycleState.paused:
isDeviceSecured = await SecurityManager().isDeviceSecured();

await ref
.read(
aedappfm.ArchethicOracleUCOProviders.archethicOracleUCO.notifier,
)
.stopSubscription();
await ref
.read(
aedappfm.CoinPriceProviders.coinPrices.notifier,
)
.stopTimer();
break;
case AppLifecycleState.resumed:
updateDefaultLocale();
Expand All @@ -192,17 +181,6 @@ class AppState extends ConsumerState<App> with WidgetsBindingObserver {
rootNavigatorKey.currentState!.overlay!.context,
);
}

await ref
.read(
aedappfm.ArchethicOracleUCOProviders.archethicOracleUCO.notifier,
)
.startSubscription();
await ref
.read(
aedappfm.CoinPriceProviders.coinPrices.notifier,
)
.startTimer();
break;
case AppLifecycleState.inactive:
case AppLifecycleState.detached:
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/aeswap/application/pool/dex_pool.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

150 changes: 71 additions & 79 deletions lib/ui/views/main/bloc/providers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,86 +21,78 @@ part 'providers.g.dart';
///
/// Add Watch here for any provider you want to init when app is displayed.
/// Those providers will be kept alive during application lifetime.
@riverpod
Future<void> homePage(Ref ref) async {
ref
..onCancel(() {
ref
.read(
aedappfm.ArchethicOracleUCOProviders.archethicOracleUCO.notifier,
)
.stopSubscription();
ref
.read(
aedappfm.CoinPriceProviders.coinPrices.notifier,
)
.stopTimer();
})
..watch(DexPoolProviders.getPoolList)
..watch(DexPoolProviders.getPoolListRaw)
..watch(DexTokensProviders.tokensCommonBases)
..watch(verifiedTokensProvider)
..watch(DexTokensProviders.tokensFromAccount)
..watch(farmLockFormFarmLockProvider)
..watch(
aedappfm.ArchethicOracleUCOProviders.archethicOracleUCO.notifier,
)
..watch(aedappfm.CoinPriceProviders.coinPrices)
..listen(
connectivityStatusProviders,
(previous, next) async {
if (previous != next && next == ConnectivityStatus.isConnected) {
ref
..invalidate(environmentProvider)
..invalidate(ContactProviders.fetchContacts)
..invalidate(MarketPriceProviders.currencyMarketPrice);

final poolListRaw =
await ref.read(DexPoolProviders.getPoolListRaw.future);

await (await ref
.read(AccountProviders.accounts.notifier)
.selectedAccountNotifier)
?.refreshRecentTransactions(poolListRaw);
}
if (next == ConnectivityStatus.isDisconnected) {
/// When network becomes offline, start the subscriptions again
// TODO(Chralu): Uncomment when https://github.com/archethic-foundation/libdart/issues/155 is fixed
// ref
// .read(
// aedappfm
// .ArchethicOracleUCOProviders.archethicOracleUCO.notifier,
// )
// .stopSubscription();

await ref
.read(
aedappfm.CoinPriceProviders.coinPrices.notifier,
)
.stopTimer();

return;
}

/// When network becomes online, start the subscriptions again
await ref
.read(
aedappfm.ArchethicOracleUCOProviders.archethicOracleUCO.notifier,
)
.startSubscription();

ref.invalidate(aedappfm.ArchethicOracleUCOProviders.archethicOracleUCO);

await ref
.read(
aedappfm.CoinPriceProviders.coinPrices.notifier,
)
.startTimer();

ref.invalidate(aedappfm.CoinPriceProviders.coinPrices);
},
);
class HomePage extends _$HomePage {
@override
Future<void> build() async {
ref
..watch(DexPoolProviders.getPoolList)
..watch(DexPoolProviders.getPoolListRaw)
..watch(DexTokensProviders.tokensCommonBases)
..watch(verifiedTokensProvider)
..watch(DexTokensProviders.tokensFromAccount)
..watch(farmLockFormFarmLockProvider)
..watch(
aedappfm.ArchethicOracleUCOProviders.archethicOracleUCO,
)
..watch(aedappfm.CoinPriceProviders.coinPrices)
..listen(
connectivityStatusProviders,
(previous, next) async {
if (previous != next && next == ConnectivityStatus.isConnected) {
ref
..invalidate(environmentProvider)
..invalidate(ContactProviders.fetchContacts)
..invalidate(MarketPriceProviders.currencyMarketPrice);

final poolListRaw =
await ref.read(DexPoolProviders.getPoolListRaw.future);

await (await ref
.read(AccountProviders.accounts.notifier)
.selectedAccountNotifier)
?.refreshRecentTransactions(poolListRaw);
}
if (next == ConnectivityStatus.isDisconnected) {
/// When network becomes offline, stops subscriptions.
await stopSubscriptions();
return;
}

/// When network becomes online, start the subscriptions again
await startSubscriptions();
},
);
}

Future<void> startSubscriptions() async {
await ref
.read(
aedappfm.ArchethicOracleUCOProviders.archethicOracleUCO.notifier,
)
.startSubscription();

await ref
.read(
aedappfm.CoinPriceProviders.coinPrices.notifier,
)
.startTimer();
}

Future<void> stopSubscriptions() async {
await ref
.read(
aedappfm.ArchethicOracleUCOProviders.archethicOracleUCO.notifier,
)
.stopSubscription();

await ref
.read(
aedappfm.CoinPriceProviders.coinPrices.notifier,
)
.stopTimer();
}
}

final mainTabControllerProvider =
Expand Down
15 changes: 7 additions & 8 deletions lib/ui/views/main/bloc/providers.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 59 additions & 0 deletions lib/ui/views/main/components/home_providers_keepalive.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import 'package:aewallet/ui/views/main/bloc/providers.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

/// Eagerly initializes providers (https://riverpod.dev/docs/essentials/eager_initialization).
///
/// Add Watch here for any provider you want to init when app is displayed.
/// Those providers will be kept alive during application lifetime.
class HomeProvidersKeepalive extends ConsumerStatefulWidget {
const HomeProvidersKeepalive({required this.child, super.key});

final Widget child;

@override
ConsumerState<ConsumerStatefulWidget> createState() =>
_LoggedInProvidersState();
}

class _LoggedInProvidersState extends ConsumerState<HomeProvidersKeepalive>
with WidgetsBindingObserver {
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}

@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}

@override
void didChangeAppLifecycleState(AppLifecycleState state) {
didChangeAppLifecycleStateAsync(state);
}

Future<void> didChangeAppLifecycleStateAsync(AppLifecycleState state) async {
switch (state) {
case AppLifecycleState.paused:
await ref.read(homePageProvider.notifier).stopSubscriptions();
break;
case AppLifecycleState.resumed:
await ref.read(homePageProvider.notifier).startSubscriptions();
break;
case AppLifecycleState.inactive:
case AppLifecycleState.detached:
case AppLifecycleState.hidden:
break;
}
super.didChangeAppLifecycleState(state);
}

@override
Widget build(BuildContext context) {
ref.watch(homePageProvider);
return widget.child;
}
}
21 changes: 9 additions & 12 deletions lib/ui/views/main/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'package:aewallet/ui/views/aeswap_earn/layouts/earn_tab.dart';
import 'package:aewallet/ui/views/aeswap_swap/layouts/swap_tab.dart';
import 'package:aewallet/ui/views/main/account_tab.dart';
import 'package:aewallet/ui/views/main/bloc/providers.dart';
import 'package:aewallet/ui/views/main/components/home_providers_keepalive.dart';
import 'package:aewallet/ui/views/main/components/main_appbar.dart';
import 'package:aewallet/ui/views/main/components/recovery_phrase_banner.dart';
import 'package:aewallet/ui/views/main/transactions_tab.dart';
Expand Down Expand Up @@ -56,18 +57,14 @@ class _HomePageState extends ConsumerState<HomePage>

@override
Widget build(BuildContext context) {
/// Eagerly initializes providers (https://riverpod.dev/docs/essentials/eager_initialization).
///
/// Add Watch here for any provider you want to init when app is displayed.
/// Those providers will be kept alive during application lifetime.
ref.watch(homePageProvider);

return SheetSkeleton(
appBar: getAppBar(context, ref),
bottomNavigationBar: getFloatingActionButton(context, ref),
sheetContent: getSheetContent(context, ref),
menu: true,
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
return HomeProvidersKeepalive(
child: SheetSkeleton(
appBar: getAppBar(context, ref),
bottomNavigationBar: getFloatingActionButton(context, ref),
sheetContent: getSheetContent(context, ref),
menu: true,
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
),
);
}

Expand Down
Loading

0 comments on commit 2b8b64f

Please sign in to comment.