From f872e4c88497db233aaca4f16aac7545d57ae3ee Mon Sep 17 00:00:00 2001 From: redDwarf03 Date: Mon, 22 Jul 2024 16:35:56 +0200 Subject: [PATCH] chore: :bug: Fix Oracle subscription --- .dart_tool/extension_discovery/vs_code.json | 2 +- lib/src/application/oracle/provider.dart | 37 +++++++++++++++------ lib/src/application/oracle/state.dart | 1 + pubspec.lock | 7 ++-- pubspec.yaml | 6 ++-- 5 files changed, 34 insertions(+), 19 deletions(-) diff --git a/.dart_tool/extension_discovery/vs_code.json b/.dart_tool/extension_discovery/vs_code.json index d1a1889..7ef5039 100644 --- a/.dart_tool/extension_discovery/vs_code.json +++ b/.dart_tool/extension_discovery/vs_code.json @@ -1 +1 @@ -{"version":2,"entries":[{"package":"archethic_dapp_framework_flutter","rootUri":"../","packageUri":"lib/"}]} \ No newline at end of file +{"version":2,"entries":[{"package":"archethic_dapp_framework_flutter","rootUri":"../","packageUri":"lib/"},{"package":"flutter_gen","rootUri":"flutter_gen/","packageUri":"flutter_gen/"}]} \ No newline at end of file diff --git a/lib/src/application/oracle/provider.dart b/lib/src/application/oracle/provider.dart index a898854..010c54e 100644 --- a/lib/src/application/oracle/provider.dart +++ b/lib/src/application/oracle/provider.dart @@ -1,40 +1,54 @@ /// SPDX-License-Identifier: AGPL-3.0-or-later + import 'dart:async'; + import 'package:archethic_dapp_framework_flutter/src/application/oracle/state.dart'; import 'package:archethic_dapp_framework_flutter/src/util/generic/get_it_instance.dart'; import 'package:archethic_lib_dart/archethic_lib_dart.dart'; +import 'package:logging/logging.dart'; import 'package:riverpod_annotation/riverpod_annotation.dart'; part 'provider.g.dart'; @Riverpod(keepAlive: true) class _ArchethicOracleUCONotifier extends Notifier { - ArchethicOracle? archethicOracle; + ArchethicOracle? archethicOracleSubscription; + + static final _logger = Logger('ArchethicOracleUCONotifier'); @override ArchethicOracleUCO build() { - ref.onDispose(() { - if (archethicOracle != null) { - sl - .get() - .closeOracleUpdatesSubscription(archethicOracle!); - } - }); + ref.onDispose(stopSubscription); + + _getValue(); + + startSubscription(); return const ArchethicOracleUCO(); } - Future init() async { - await _getValue(); + Future startSubscription() async { + if (archethicOracleSubscription != null) return; + + _logger.info('Start listening to Oracle'); await _subscribe(); } + Future stopSubscription() async { + _logger.info('Stop listening to Oracle'); + if (archethicOracleSubscription == null) return; + sl + .get() + .closeOracleUpdatesSubscription(archethicOracleSubscription!); + archethicOracleSubscription = null; + } + Future _getValue() async { final oracleUcoPrice = await sl.get().getOracleData(); _fillInfo(oracleUcoPrice); } Future _subscribe() async { - archethicOracle = await sl + archethicOracleSubscription = await sl .get() .subscribeToOracleUpdates((oracleUcoPrice) { _fillInfo(oracleUcoPrice!); @@ -42,6 +56,7 @@ class _ArchethicOracleUCONotifier extends Notifier { } void _fillInfo(OracleUcoPrice oracleUcoPrice) { + _logger.info('Oracle: ${oracleUcoPrice.timestamp}, ${oracleUcoPrice.uco}'); state = state.copyWith( timestamp: oracleUcoPrice.timestamp ?? 0, eur: oracleUcoPrice.uco!.eur ?? 0, diff --git a/lib/src/application/oracle/state.dart b/lib/src/application/oracle/state.dart index 77c2a1e..369cf06 100644 --- a/lib/src/application/oracle/state.dart +++ b/lib/src/application/oracle/state.dart @@ -1,4 +1,5 @@ /// SPDX-License-Identifier: AGPL-3.0-or-later + import 'package:freezed_annotation/freezed_annotation.dart'; part 'state.freezed.dart'; diff --git a/pubspec.lock b/pubspec.lock index fb3d9c2..84e5a2d 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -44,10 +44,9 @@ packages: archethic_wallet_client: dependency: "direct main" description: - name: archethic_wallet_client - sha256: "7d9ef9f23b285d4d5fc95bec08b3e4c51e8e636e1755f863426c5706ada2c175" - url: "https://pub.dev" - source: hosted + path: "../archethic-wallet-client-dart" + relative: true + source: path version: "2.0.4" archive: dependency: "direct main" diff --git a/pubspec.yaml b/pubspec.yaml index 899b667..8939cc5 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -19,9 +19,9 @@ dependencies: # path: ../libdart-2 # A client dart library to interact with Archethic Wallet RPC API. - archethic_wallet_client: ^2.0.4 - #archethic_wallet_client: - # path: ../archethic-wallet-client-dart + #archethic_wallet_client: ^2.0.4 + archethic_wallet_client: + path: ../archethic-wallet-client-dart # Compression archive: ^3.4.10