Top Up Balance logic and Added fixes for routing

This commit is contained in:
Arseni
2025-12-05 20:29:43 +03:00
parent f7bf3138ac
commit bf39b1d401
27 changed files with 972 additions and 175 deletions

View File

@@ -12,7 +12,8 @@ enum PayoutDestination {
methods(Icons.credit_card, 'methods'),
payment(Icons.payment, 'payout'),
addrecipient(Icons.app_registration, 'add recipient'),
editwallet(Icons.wallet, 'edit wallet');
editwallet(Icons.wallet, 'edit wallet'),
walletTopUp(Icons.qr_code_2_outlined, 'wallet top up');
const PayoutDestination(this.icon, this.labelKey);
@@ -41,6 +42,8 @@ enum PayoutDestination {
return loc.addRecipient;
case PayoutDestination.editwallet:
return loc.editWallet;
case PayoutDestination.walletTopUp:
return loc.walletTopUpTitle;
}
}
}

View File

@@ -2,31 +2,29 @@ import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:go_router/go_router.dart';
import 'package:pshared/models/resources.dart';
import 'package:pshared/provider/permissions.dart';
import 'package:pshared/provider/recipient/provider.dart';
import 'package:pweb/pages/address_book/form/page.dart';
import 'package:pweb/pages/address_book/page/page.dart';
import 'package:pweb/pages/loader.dart';
import 'package:pweb/pages/payment_methods/page.dart';
import 'package:pweb/pages/payout_page/page.dart';
import 'package:pweb/pages/payout_page/wallet/edit/page.dart';
import 'package:pweb/pages/report/page.dart';
import 'package:pweb/pages/settings/profile/page.dart';
import 'package:pweb/pages/dashboard/dashboard.dart';
import 'package:pweb/providers/page_selector.dart';
import 'package:pweb/utils/logout.dart';
import 'package:pweb/widgets/appbar/app_bar.dart';
import 'package:pweb/widgets/error/snackbar.dart';
import 'package:pweb/widgets/sidebar/destinations.dart';
import 'package:pweb/widgets/sidebar/sidebar.dart';
import 'package:pweb/generated/i18n/app_localizations.dart';
import 'package:pweb/app/router/payout_routes.dart';
class PageSelector extends StatelessWidget {
const PageSelector({super.key});
final Widget child;
final GoRouterState routerState;
const PageSelector({
super.key,
required this.child,
required this.routerState,
});
@override
Widget build(BuildContext context) => PageViewLoader(
@@ -36,88 +34,29 @@ class PageSelector extends StatelessWidget {
final provider = context.watch<PageSelectorProvider>();
final loc = AppLocalizations.of(context)!;
final bool restrictedAccess = !permissions.canRead(ResourceType.chainWallets);
final allowedDestinations = restrictedAccess
? <PayoutDestination>{
PayoutDestination.settings,
PayoutDestination.methods,
PayoutDestination.editwallet,
PayoutDestination.walletTopUp,
}
: PayoutDestination.values.toSet();
final selected = allowedDestinations.contains(provider.selected)
? provider.selected
final routeDestination = _destinationFromState(routerState) ?? provider.selected;
final selected = allowedDestinations.contains(routeDestination)
? routeDestination
: (restrictedAccess ? PayoutDestination.settings : PayoutDestination.dashboard);
if (selected != provider.selected) {
WidgetsBinding.instance.addPostFrameCallback((_) => provider.selectPage(selected));
if (selected != routeDestination) {
WidgetsBinding.instance.addPostFrameCallback((_) {
context.goToPayout(selected);
});
}
Widget content;
switch (selected) {
case PayoutDestination.dashboard:
content = DashboardPage(
onRecipientSelected: (recipient) => provider.selectRecipient(recipient),
onGoToPaymentWithoutRecipient: provider.startPaymentWithoutRecipient,
);
break;
case PayoutDestination.recipients:
content = RecipientAddressBookPage(
onRecipientSelected: (recipient) =>
provider.selectRecipient(recipient, fromList: true),
onAddRecipient: provider.goToAddRecipient,
onEditRecipient: provider.editRecipient,
onDeleteRecipient: (recipient) => executeActionWithNotification(
context: context,
action: () async => context.read<RecipientsProvider>().delete(recipient.id),
successMessage: loc.recipientDeletedSuccessfully,
errorMessage: loc.errorDeleteRecipient,
),
);
break;
case PayoutDestination.addrecipient:
final recipient = provider.recipientProvider.currentObject;
content = AdressBookRecipientForm(
recipient: recipient,
onSaved: (_) => provider.selectPage(PayoutDestination.recipients),
);
break;
case PayoutDestination.payment:
content = PaymentPage(
onBack: (_) => provider.goBackFromPayment(),
);
break;
case PayoutDestination.settings:
content = ProfileSettingsPage();
break;
case PayoutDestination.reports:
content = OperationHistoryPage();
break;
case PayoutDestination.methods:
content = PaymentConfigPage(
onWalletTap: provider.selectWallet,
);
break;
case PayoutDestination.editwallet:
final wallet = provider.walletsProvider.selectedWallet;
content = wallet != null
? WalletEditPage(
onBack: provider.goBackFromWalletEdit,
)
: Center(child: Text(loc.noWalletSelected));
break;
default:
content = Text(selected.name);
if (provider.selected != selected) {
provider.syncDestination(selected);
}
return Scaffold(
@@ -134,14 +73,49 @@ class PageSelector extends StatelessWidget {
children: [
PayoutSidebar(
selected: selected,
onSelected: provider.selectPage,
onSelected: context.goToPayout,
onLogout: () => logoutUtil(context),
),
Expanded(child: content),
Expanded(child: child),
],
),
),
);
},
));
PayoutDestination? _destinationFromState(GoRouterState state) {
final byName = PayoutRoutes.destinationFor(state.name);
if (byName != null) return byName;
final location = state.matchedLocation;
if (location.startsWith(PayoutRoutes.editWalletPath)) {
return PayoutDestination.editwallet;
}
if (location.startsWith(PayoutRoutes.walletTopUpPath)) {
return PayoutDestination.walletTopUp;
}
if (location.startsWith(PayoutRoutes.methodsPath)) {
return PayoutDestination.methods;
}
if (location.startsWith(PayoutRoutes.paymentPath)) {
return PayoutDestination.payment;
}
if (location.startsWith(PayoutRoutes.addRecipientPath)) {
return PayoutDestination.addrecipient;
}
if (location.startsWith(PayoutRoutes.recipientsPath)) {
return PayoutDestination.recipients;
}
if (location.startsWith(PayoutRoutes.settingsPath)) {
return PayoutDestination.settings;
}
if (location.startsWith(PayoutRoutes.reportsPath)) {
return PayoutDestination.reports;
}
if (location.startsWith(PayoutRoutes.dashboardPath)) {
return PayoutDestination.dashboard;
}
return null;
}
}