diff --git a/app/lib/screens/wallets/transactions.dart b/app/lib/screens/wallets/transactions.dart index 3f681e548..002888540 100644 --- a/app/lib/screens/wallets/transactions.dart +++ b/app/lib/screens/wallets/transactions.dart @@ -18,35 +18,49 @@ class WalletTransactionsWidget extends StatefulWidget { class _WalletTransactionsWidgetState extends State { final _pageSize = 10; - final PagingController _pagingController = - PagingController(firstPageKey: 1); // Start from page 1 + final PagingController _pagingController = + PagingController(firstPageKey: null); - Future _listTransactions(int pageKey) async { + void _listTransactions(String? pagingToken) async { try { - final offset = (pageKey - 1) * _pageSize; - final txs = await listTransactions( - widget.wallet.stellarSecret, offset, _pageSize); - final isLastPage = txs.length < _pageSize; - if (isLastPage) { + final txStream = listTransactions( + widget.wallet.stellarSecret, + pagingToken, + _pageSize, + ); + + final txs = await txStream.take(_pageSize).toList(); + + if (txs.isEmpty) { + _pagingController.appendLastPage([]); + return; + } + + final lastTx = txs.last as PaymentTransaction; + final adjustedPagingToken = + (BigInt.parse(lastTx.pagingToken) - BigInt.one).toString(); + + if (txs.length < _pageSize) { _pagingController.appendLastPage(txs); } else { - _pagingController.appendPage(txs, pageKey + 1); + _pagingController.appendPage(txs, adjustedPagingToken); } } catch (e) { - logger.e('Failed to load transactions due to $e'); - _pagingController.error(e); - if (context.mounted) { - ScaffoldMessenger.of(context).showSnackBar(SnackBar( - content: Text( - 'Failed to load transaction', - style: Theme.of(context) - .textTheme - .bodyMedium! - .copyWith(color: Theme.of(context).colorScheme.errorContainer), + logger.e('Failed to load transactions: $e'); + if (mounted) { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text( + 'Failed to load transactions', + style: Theme.of(context).textTheme.bodyMedium!.copyWith( + color: Theme.of(context).colorScheme.errorContainer, + ), + ), + duration: const Duration(seconds: 3), ), - duration: const Duration(seconds: 3), - )); + ); } + _pagingController.error = e; } } @@ -78,7 +92,7 @@ class _WalletTransactionsWidgetState extends State { ); } - return PagedListView( + return PagedListView( pagingController: _pagingController, builderDelegate: PagedChildBuilderDelegate( itemBuilder: (context, item, index) => Column( diff --git a/app/lib/services/stellar_service.dart b/app/lib/services/stellar_service.dart index ac448ae4e..262b27099 100644 --- a/app/lib/services/stellar_service.dart +++ b/app/lib/services/stellar_service.dart @@ -46,11 +46,17 @@ Future getBalance(String secret) async { return getBalanceByClient(client); } -Future> listTransactions( - String secret, int offset, int limit) async { +Stream listTransactions( + String secret, String? pagingToken, int limit) async* { final client = Client(NetworkType.PUBLIC, secret); - return await client.getTransactions( - assetCodeFilter: 'TFT', limit: limit, offset: offset); + + await for (var response in client.getTransactions( + assetCodeFilter: 'TFT', + limit: limit, + pagingToken: pagingToken, + )) { + yield response; + } } Future?> listVestedAccounts(String secret) async { diff --git a/app/pubspec.lock b/app/pubspec.lock index 03cdf7bba..802b54eb5 100644 --- a/app/pubspec.lock +++ b/app/pubspec.lock @@ -1715,7 +1715,7 @@ packages: description: path: "packages/stellar_client" ref: development - resolved-ref: f3afde66922dfbe836d0915fac8e152c5817626b + resolved-ref: "0ce3b575e2f3dcd371be2152042978113b4eadb1" url: "https://github.com/threefoldtech/tfgrid-sdk-dart" source: git version: "0.1.0"