Skip to content

Commit

Permalink
Merge pull request #848 from threefoldtech/development_paginate_trans…
Browse files Browse the repository at this point in the history
…actions

Paginate transactions
  • Loading branch information
zaelgohary authored Jan 19, 2025
2 parents 908f1c5 + fc58d70 commit 640ee19
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 65 deletions.
125 changes: 65 additions & 60 deletions app/lib/screens/wallets/transactions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:threebotlogin/models/wallet.dart';
import 'package:threebotlogin/services/stellar_service.dart';
import 'package:threebotlogin/widgets/wallets/transaction.dart';
import 'package:threebotlogin/widgets/wallets/vertical_divider.dart';
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';

class WalletTransactionsWidget extends StatefulWidget {
const WalletTransactionsWidget({super.key, required this.wallet});
Expand All @@ -16,25 +17,26 @@ class WalletTransactionsWidget extends StatefulWidget {
}

class _WalletTransactionsWidgetState extends State<WalletTransactionsWidget> {
List<PaymentTransaction?> transactions = [];
bool loading = true;
final _pageSize = 10;
final PagingController<int, ITransaction> _pagingController =
PagingController(firstPageKey: 1); // Start from page 1

_listTransactions() async {
setState(() {
loading = true;
});
Future<void> _listTransactions(int pageKey) async {
try {
final txs = await listTransactions(widget.wallet.stellarSecret);
final transactionsList = txs.map((tx) {
if (tx is PaymentTransaction) {
return tx;
}
}).toList();
transactions = transactionsList.where((tx) => tx != null).toList();
final offset = (pageKey - 1) * _pageSize;
final txs = await listTransactions(
widget.wallet.stellarSecret, offset, _pageSize);
final isLastPage = txs.length < _pageSize;
if (isLastPage) {
_pagingController.appendLastPage(txs);
} else {
_pagingController.appendPage(txs, pageKey + 1);
}
} catch (e) {
logger.e('Failed to load transactions due to $e');
_pagingController.error(e);
if (context.mounted) {
final loadingFarmsFailure = SnackBar(
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(
'Failed to load transaction',
style: Theme.of(context)
Expand All @@ -43,50 +45,29 @@ class _WalletTransactionsWidgetState extends State<WalletTransactionsWidget> {
.copyWith(color: Theme.of(context).colorScheme.errorContainer),
),
duration: const Duration(seconds: 3),
);
ScaffoldMessenger.of(context).clearSnackBars();
ScaffoldMessenger.of(context).showSnackBar(loadingFarmsFailure);
));
}
} finally {
setState(() {
loading = false;
});
}
}

@override
void initState() {
super.initState();
if (widget.wallet.stellarBalance != '-1') {
_listTransactions();
} else {
setState(() {
loading = false;
transactions = [];
});
_pagingController.addPageRequestListener(_listTransactions);
}
}

@override
void dispose() {
_pagingController.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
Widget mainWidget;
if (loading) {
mainWidget = Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const CircularProgressIndicator(),
const SizedBox(height: 15),
Text(
'Loading Transactions...',
style: Theme.of(context).textTheme.bodyLarge!.copyWith(
color: Theme.of(context).colorScheme.onSurface,
fontWeight: FontWeight.bold),
),
],
));
} else if (transactions.isEmpty) {
mainWidget = Center(
if (widget.wallet.stellarBalance == '-1') {
return Center(
child: Text(
'No transactions yet.',
style: Theme.of(context)
Expand All @@ -95,21 +76,45 @@ class _WalletTransactionsWidgetState extends State<WalletTransactionsWidget> {
.copyWith(color: Theme.of(context).colorScheme.onSurface),
),
);
} else {
mainWidget = ListView(
children: [
for (final tx in transactions)
Column(
children: [
TransactionWidget(transaction: tx!),
tx == transactions.last
? const SizedBox()
: const CustomVerticalDivider()
],
)
],
);
}
return mainWidget;

return PagedListView<int, ITransaction>(
pagingController: _pagingController,
builderDelegate: PagedChildBuilderDelegate<ITransaction>(
itemBuilder: (context, item, index) => Column(
children: [
TransactionWidget(transaction: item as PaymentTransaction),
if (index < _pagingController.itemList!.length - 1)
const CustomVerticalDivider()
else
const SizedBox(height: 5),
],
),
firstPageProgressIndicatorBuilder: (context) => Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const CircularProgressIndicator(),
const SizedBox(height: 15),
Text(
'Loading Transactions...',
style: Theme.of(context).textTheme.bodyLarge!.copyWith(
color: Theme.of(context).colorScheme.onSurface,
fontWeight: FontWeight.bold),
),
],
),
),
noItemsFoundIndicatorBuilder: (context) => Center(
child: Text(
'No transactions yet.',
style: Theme.of(context)
.textTheme
.bodyLarge!
.copyWith(color: Theme.of(context).colorScheme.onSurface),
),
),
),
);
}
}
2 changes: 1 addition & 1 deletion app/lib/screens/wallets/wallet_assets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class _WalletAssetsWidgetState extends State<WalletAssetsWidget> {
_listVestedAccounts() async {
vestedWallets =
await Stellar.listVestedAccounts(widget.wallet.stellarSecret);
setState(() {});
if (mounted) setState(() {});
}

_reloadBalances() async {
Expand Down
7 changes: 4 additions & 3 deletions app/lib/services/stellar_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ Future<String> getBalance(String secret) async {
return getBalanceByClient(client);
}

Future<List<ITransaction>> listTransactions(String secret) async {
Future<List<ITransaction>> listTransactions(
String secret, int offset, int limit) async {
final client = Client(NetworkType.PUBLIC, secret);
final transactions = await client.getTransactions(assetCodeFilter: 'TFT');
return transactions;
return await client.getTransactions(
assetCodeFilter: 'TFT', limit: limit, offset: offset);
}

Future<List<VestingAccount>?> listVestedAccounts(String secret) async {
Expand Down
26 changes: 25 additions & 1 deletion app/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.5.1"
flutter_staggered_grid_view:
dependency: transitive
description:
name: flutter_staggered_grid_view
sha256: "19e7abb550c96fbfeb546b23f3ff356ee7c59a019a651f8f102a4ba9b7349395"
url: "https://pub.dev"
source: hosted
version: "0.7.0"
flutter_svg:
dependency: "direct main"
description:
Expand Down Expand Up @@ -762,6 +770,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "4.3.0"
infinite_scroll_pagination:
dependency: "direct main"
description:
name: infinite_scroll_pagination
sha256: "4047eb8191e8b33573690922a9e995af64c3949dc87efc844f936b039ea279df"
url: "https://pub.dev"
source: hosted
version: "4.1.0"
intl:
dependency: "direct overridden"
description:
Expand Down Expand Up @@ -1456,6 +1472,14 @@ packages:
description: flutter
source: sdk
version: "0.0.99"
sliver_tools:
dependency: transitive
description:
name: sliver_tools
sha256: eae28220badfb9d0559207badcbbc9ad5331aac829a88cb0964d330d2a4636a6
url: "https://pub.dev"
source: hosted
version: "0.2.12"
smart_auth:
dependency: transitive
description:
Expand Down Expand Up @@ -1573,7 +1597,7 @@ packages:
description:
path: "packages/stellar_client"
ref: development
resolved-ref: "6e1987b236c480912e9b0876267c3f8ecdb901d5"
resolved-ref: "2f7f0a8e729c87dc0ab9a973e1eac42b7686bc1c"
url: "https://github.com/threefoldtech/tfgrid-sdk-dart"
source: git
version: "0.1.0"
Expand Down
1 change: 1 addition & 0 deletions app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ dependencies:
flutter_keyboard_visibility: ^6.0.0
mutex: ^3.1.0
decimal: ^3.1.0
infinite_scroll_pagination: ^4.1.0
dev_dependencies:
flutter_test:
sdk: flutter
Expand Down

0 comments on commit 640ee19

Please sign in to comment.