Skip to content

Commit

Permalink
providers: introduce package, add balance provider
Browse files Browse the repository at this point in the history
  • Loading branch information
octobocto committed Oct 30, 2023
1 parent 1bba387 commit 3cc30dc
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
7 changes: 7 additions & 0 deletions lib/config/dependencies.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:get_it/get_it.dart';
import 'package:sidesail/providers/balance_provider.dart';
import 'package:sidesail/routing/router.dart';
import 'package:sidesail/rpc/rpc.dart';
import 'package:sidesail/storage/client_settings.dart';
Expand All @@ -18,4 +19,10 @@ Future<void> initGetitDependencies() async {
GetIt.I.registerLazySingleton<ClientSettings>(
() => ClientSettings(store: SecureStore()),
);

GetIt.I.registerLazySingleton<BalanceProvider>(
// by registering an instance of the balance provider,
// we start polling for balance updates
() => BalanceProvider(),
);
}
20 changes: 15 additions & 5 deletions lib/pages/tabs/withdrawal_tab_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:get_it/get_it.dart';
import 'package:sail_ui/sail_ui.dart';
import 'package:sail_ui/widgets/core/sail_text.dart';
import 'package:sidesail/logger.dart';
import 'package:sidesail/providers/balance_provider.dart';
import 'package:sidesail/rpc/rpc.dart';
import 'package:sidesail/withdrawals.dart';
import 'package:stacked/stacked.dart';
Expand Down Expand Up @@ -123,10 +124,9 @@ class WithdrawalTabPage extends StatelessWidget {

class WithdrawalTabPageViewModel extends MultipleFutureViewModel {
RPC get _rpc => GetIt.I.get<RPC>();
BalanceProvider get _balanceProvider => GetIt.I.get<BalanceProvider>();

// TODO: how to take this on creation? Async operation, possible
// to sync this with global state somehow? Pls help, BO
double get sidechainBalance => dataMap?[_SidechainBalanceFuture];
double get sidechainBalance => _balanceProvider.balance;

double withdrawalAmount = 0.0;
double get transactionFee => dataMap?[_SidechainFeeFuture];
Expand All @@ -139,6 +139,12 @@ class WithdrawalTabPageViewModel extends MultipleFutureViewModel {
Timer? balanceTimer;

WithdrawalTabPageViewModel() {
// by adding a listener, we subscribe to changes to the balance
// provider. We don't use the updates for anything other than
// showing the new value though, so we keep it simple, and just
// pass notifyListeners of this view model directly
_balanceProvider.addListener(notifyListeners);

balanceTimer = Timer.periodic(const Duration(seconds: 1), (timer) async {
// fetchAndUpdateBalance();
});
Expand All @@ -147,7 +153,6 @@ class WithdrawalTabPageViewModel extends MultipleFutureViewModel {
@override
Map<String, Future Function()> get futuresMap => {
_SidechainFeeFuture: estimateSidechainFee,
_SidechainBalanceFuture: _rpc.getBalance,
};

Future<double> estimateSidechainFee() async {
Expand Down Expand Up @@ -273,7 +278,12 @@ class WithdrawalTabPageViewModel extends MultipleFutureViewModel {
),
);
}

@override
void dispose() {
super.dispose();
_balanceProvider.removeListener(notifyListeners);
}
}

const _SidechainFeeFuture = 'sidechainFee';
const _SidechainBalanceFuture = 'sidechainBalance';
46 changes: 46 additions & 0 deletions lib/providers/balance_provider.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
import 'package:sidesail/rpc/rpc.dart';

class BalanceProvider extends ChangeNotifier {
RPC get _rpc => GetIt.I.get<RPC>();

// because the class extends ChangeNotifier, any subscribers
// to this class will be notified of changes to this
// variable.
double balance = 0;
bool initialized = false;

// used for polling
late Timer _timer;

BalanceProvider() {
fetch();
_startPolling();
}

// call this function from anywhere to refresh the balance
Future<void> fetch() async {
balance = await _rpc.getBalance();
// TODO: Handle error?

initialized = true;
notifyListeners();
}

void _startPolling() {
_timer = Timer.periodic(const Duration(seconds: 5), (timer) async {
await fetch();
notifyListeners();
});
}

@override
void dispose() {
super.dispose();
// Cancel timer when provider is disposed (never?)
_timer.cancel();
}
}

0 comments on commit 3cc30dc

Please sign in to comment.