Navigation now flows entirely through go_router
This commit is contained in:
@@ -1,264 +0,0 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:pshared/models/payment/methods/data.dart';
|
||||
|
||||
import 'package:pshared/models/payment/methods/type.dart';
|
||||
import 'package:pshared/models/payment/type.dart';
|
||||
import 'package:pshared/models/recipient/recipient.dart';
|
||||
import 'package:pshared/provider/recipient/pmethods.dart';
|
||||
import 'package:pshared/provider/recipient/provider.dart';
|
||||
|
||||
import 'package:pweb/models/wallet.dart';
|
||||
import 'package:pweb/providers/wallets.dart';
|
||||
//import 'package:pweb/services/amplitude.dart';
|
||||
import 'package:pweb/app/router/payout_routes.dart';
|
||||
import 'package:pweb/widgets/sidebar/destinations.dart';
|
||||
|
||||
|
||||
class PageSelectorProvider extends ChangeNotifier {
|
||||
PayoutDestination _selected = PayoutDestination.dashboard;
|
||||
PaymentType? _type;
|
||||
bool _cameFromRecipientList = false;
|
||||
PayoutDestination? _previousDestination;
|
||||
|
||||
late RecipientsProvider recipientProvider;
|
||||
late WalletsProvider walletsProvider;
|
||||
late PaymentMethodsProvider methodsProvider;
|
||||
|
||||
PayoutDestination get selected => _selected;
|
||||
PaymentType? get type => _type;
|
||||
bool get cameFromRecipientList => _cameFromRecipientList;
|
||||
|
||||
PageSelectorProvider();
|
||||
|
||||
void update(
|
||||
RecipientsProvider recipientProv,
|
||||
WalletsProvider walletsProv,
|
||||
PaymentMethodsProvider methodsProv,
|
||||
) {
|
||||
recipientProvider = recipientProv;
|
||||
walletsProvider = walletsProv;
|
||||
methodsProvider = methodsProv;
|
||||
}
|
||||
|
||||
void syncDestination(PayoutDestination destination) {
|
||||
if (_selected == destination) return;
|
||||
_selected = destination;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void selectPage(
|
||||
BuildContext context,
|
||||
PayoutDestination dest, {
|
||||
bool replace = true,
|
||||
}) {
|
||||
_selected = dest;
|
||||
notifyListeners();
|
||||
_navigateTo(context, dest, replace: replace);
|
||||
}
|
||||
|
||||
void selectRecipient(
|
||||
BuildContext context,
|
||||
Recipient? recipient, {
|
||||
bool fromList = false,
|
||||
}) {
|
||||
final previousDestination = _selected;
|
||||
recipientProvider.setCurrentObject(recipient?.id);
|
||||
_cameFromRecipientList = fromList;
|
||||
_setPreviousDestination();
|
||||
_selected = PayoutDestination.payment;
|
||||
notifyListeners();
|
||||
if (previousDestination != PayoutDestination.payment) {
|
||||
_navigateTo(context, PayoutDestination.payment, replace: false);
|
||||
}
|
||||
}
|
||||
|
||||
void editRecipient(
|
||||
BuildContext context,
|
||||
Recipient? recipient, {
|
||||
bool fromList = false,
|
||||
}) {
|
||||
final previousDestination = _selected;
|
||||
recipientProvider.setCurrentObject(recipient?.id);
|
||||
_cameFromRecipientList = fromList;
|
||||
_selected = PayoutDestination.addrecipient;
|
||||
notifyListeners();
|
||||
if (previousDestination != PayoutDestination.addrecipient) {
|
||||
_navigateTo(context, PayoutDestination.addrecipient, replace: false);
|
||||
}
|
||||
}
|
||||
|
||||
void goToAddRecipient(BuildContext context) {
|
||||
// AmplitudeService.recipientAddStarted();
|
||||
final previousDestination = _selected;
|
||||
recipientProvider.setCurrentObject(null);
|
||||
_selected = PayoutDestination.addrecipient;
|
||||
_cameFromRecipientList = false;
|
||||
notifyListeners();
|
||||
if (previousDestination != PayoutDestination.addrecipient) {
|
||||
_navigateTo(context, PayoutDestination.addrecipient, replace: false);
|
||||
}
|
||||
}
|
||||
|
||||
void startPaymentWithoutRecipient(
|
||||
BuildContext context,
|
||||
PaymentType type,
|
||||
) {
|
||||
final previousDestination = _selected;
|
||||
recipientProvider.setCurrentObject(null);
|
||||
_type = type;
|
||||
_cameFromRecipientList = false;
|
||||
_setPreviousDestination();
|
||||
_selected = PayoutDestination.payment;
|
||||
notifyListeners();
|
||||
if (previousDestination != PayoutDestination.payment) {
|
||||
_navigateTo(context, PayoutDestination.payment, replace: false);
|
||||
}
|
||||
}
|
||||
|
||||
void goBackFromPayment(BuildContext context) {
|
||||
if (Navigator.of(context).canPop()) {
|
||||
Navigator.of(context).pop();
|
||||
} else {
|
||||
_navigateTo(
|
||||
context,
|
||||
_previousDestination ??
|
||||
(_cameFromRecipientList
|
||||
? PayoutDestination.recipients
|
||||
: PayoutDestination.dashboard),
|
||||
);
|
||||
}
|
||||
_selected = _previousDestination ??
|
||||
(_cameFromRecipientList
|
||||
? PayoutDestination.recipients
|
||||
: PayoutDestination.dashboard);
|
||||
_type = null;
|
||||
_previousDestination = null;
|
||||
_cameFromRecipientList = false;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void goBackFromWalletEdit(BuildContext context) {
|
||||
selectPage(context, PayoutDestination.methods);
|
||||
}
|
||||
|
||||
void selectWallet(BuildContext context, Wallet wallet) {
|
||||
final previousDestination = _selected;
|
||||
walletsProvider.selectWallet(wallet);
|
||||
_selected = PayoutDestination.editwallet;
|
||||
notifyListeners();
|
||||
if (previousDestination != PayoutDestination.editwallet) {
|
||||
_navigateTo(context, PayoutDestination.editwallet, replace: false);
|
||||
}
|
||||
}
|
||||
|
||||
void startPaymentFromWallet(BuildContext context, Wallet wallet) {
|
||||
final previousDestination = _selected;
|
||||
_type = PaymentType.wallet;
|
||||
_cameFromRecipientList = false;
|
||||
_setPreviousDestination();
|
||||
_selected = PayoutDestination.payment;
|
||||
notifyListeners();
|
||||
if (previousDestination != PayoutDestination.payment) {
|
||||
_navigateTo(context, PayoutDestination.payment, replace: false);
|
||||
}
|
||||
}
|
||||
|
||||
void openWalletTopUp(BuildContext context, Wallet wallet) {
|
||||
final previousDestination = _selected;
|
||||
_setPreviousDestination();
|
||||
walletsProvider.selectWallet(wallet);
|
||||
_selected = PayoutDestination.walletTopUp;
|
||||
notifyListeners();
|
||||
if (previousDestination != PayoutDestination.walletTopUp) {
|
||||
_navigateTo(context, PayoutDestination.walletTopUp, replace: false);
|
||||
}
|
||||
}
|
||||
|
||||
void goBackFromWalletTopUp(BuildContext context) {
|
||||
if (Navigator.of(context).canPop()) {
|
||||
Navigator.of(context).pop();
|
||||
} else {
|
||||
_navigateTo(
|
||||
context,
|
||||
_previousDestination ?? PayoutDestination.dashboard,
|
||||
);
|
||||
}
|
||||
_selected = _previousDestination ?? PayoutDestination.dashboard;
|
||||
_previousDestination = null;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
PaymentMethod? getPaymentMethodForWallet(Wallet wallet) {
|
||||
if (methodsProvider.methods.isEmpty) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return methodsProvider.methods.firstWhereOrNull(
|
||||
(method) => method.type == PaymentType.wallet && (method.description?.contains(wallet.walletUserID) ?? false),
|
||||
);
|
||||
}
|
||||
|
||||
MethodMap getAvailablePaymentTypes() {
|
||||
final recipient = selectedRecipient;
|
||||
if ((recipient == null) || !methodsProvider.isReady) return {};
|
||||
|
||||
final methodsForRecipient = methodsProvider.methods.where(
|
||||
(method) => !method.isArchived && method.recipientRef == recipient.id,
|
||||
);
|
||||
|
||||
return {
|
||||
for (final method in methodsForRecipient) method.type: method.data,
|
||||
};
|
||||
}
|
||||
|
||||
PaymentType getDefaultPaymentType() {
|
||||
final availableTypes = getAvailablePaymentTypes();
|
||||
final currentType = _type ?? PaymentType.bankAccount;
|
||||
|
||||
if (availableTypes.containsKey(currentType)) {
|
||||
return currentType;
|
||||
}
|
||||
if (availableTypes.isNotEmpty) {
|
||||
return availableTypes.keys.first;
|
||||
}
|
||||
return PaymentType.bankAccount;
|
||||
}
|
||||
|
||||
bool shouldShowPaymentForm() {
|
||||
return selectedRecipient == null;
|
||||
}
|
||||
|
||||
void handleWalletAutoSelection() {
|
||||
if (selectedWallet != null) {
|
||||
final wallet = selectedWallet!;
|
||||
final matchingMethod = getPaymentMethodForWallet(wallet);
|
||||
if (matchingMethod != null) {
|
||||
methodsProvider.setCurrentObject(matchingMethod.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void _setPreviousDestination() {
|
||||
if (_selected != PayoutDestination.payment &&
|
||||
_selected != PayoutDestination.walletTopUp) {
|
||||
_previousDestination = _selected;
|
||||
}
|
||||
}
|
||||
|
||||
void _navigateTo(
|
||||
BuildContext context,
|
||||
PayoutDestination destination, {
|
||||
bool replace = true,
|
||||
}) {
|
||||
if (replace) {
|
||||
context.goToPayout(destination);
|
||||
} else {
|
||||
context.pushToPayout(destination);
|
||||
}
|
||||
}
|
||||
|
||||
Recipient? get selectedRecipient => recipientProvider.currentObject;
|
||||
Wallet? get selectedWallet => walletsProvider.selectedWallet;
|
||||
}
|
||||
110
frontend/pweb/lib/providers/payment_flow.dart
Normal file
110
frontend/pweb/lib/providers/payment_flow.dart
Normal file
@@ -0,0 +1,110 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
import 'package:pshared/models/payment/methods/data.dart';
|
||||
import 'package:pshared/models/payment/type.dart';
|
||||
import 'package:pshared/models/recipient/recipient.dart';
|
||||
|
||||
|
||||
class PaymentFlowProvider extends ChangeNotifier {
|
||||
PaymentType _selectedType;
|
||||
PaymentMethodData? _manualPaymentData;
|
||||
|
||||
PaymentFlowProvider({
|
||||
required PaymentType initialType,
|
||||
}) : _selectedType = initialType;
|
||||
|
||||
PaymentType get selectedType => _selectedType;
|
||||
PaymentMethodData? get manualPaymentData => _manualPaymentData;
|
||||
|
||||
void sync({
|
||||
required Recipient? recipient,
|
||||
required MethodMap availableTypes,
|
||||
PaymentType? preferredType,
|
||||
}) {
|
||||
final resolvedType = _resolveSelectedType(
|
||||
recipient: recipient,
|
||||
availableTypes: availableTypes,
|
||||
preferredType: preferredType,
|
||||
);
|
||||
|
||||
var hasChanges = false;
|
||||
if (resolvedType != _selectedType) {
|
||||
_selectedType = resolvedType;
|
||||
hasChanges = true;
|
||||
}
|
||||
|
||||
if (recipient != null && _manualPaymentData != null) {
|
||||
_manualPaymentData = null;
|
||||
hasChanges = true;
|
||||
}
|
||||
|
||||
if (hasChanges) notifyListeners();
|
||||
}
|
||||
|
||||
void reset({
|
||||
required Recipient? recipient,
|
||||
required MethodMap availableTypes,
|
||||
PaymentType? preferredType,
|
||||
}) {
|
||||
final resolvedType = _resolveSelectedType(
|
||||
recipient: recipient,
|
||||
availableTypes: availableTypes,
|
||||
preferredType: preferredType,
|
||||
);
|
||||
|
||||
var hasChanges = false;
|
||||
|
||||
if (resolvedType != _selectedType) {
|
||||
_selectedType = resolvedType;
|
||||
hasChanges = true;
|
||||
}
|
||||
|
||||
if (_manualPaymentData != null) {
|
||||
_manualPaymentData = null;
|
||||
hasChanges = true;
|
||||
}
|
||||
|
||||
if (hasChanges) notifyListeners();
|
||||
}
|
||||
|
||||
void selectType(PaymentType type, {bool resetManualData = false}) {
|
||||
if (_selectedType == type && (!resetManualData || _manualPaymentData == null)) {
|
||||
return;
|
||||
}
|
||||
|
||||
_selectedType = type;
|
||||
if (resetManualData) {
|
||||
_manualPaymentData = null;
|
||||
}
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void setManualPaymentData(PaymentMethodData? data) {
|
||||
_manualPaymentData = data;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
PaymentType _resolveSelectedType({
|
||||
required Recipient? recipient,
|
||||
required MethodMap availableTypes,
|
||||
PaymentType? preferredType,
|
||||
}) {
|
||||
if (recipient == null) {
|
||||
return preferredType ?? _selectedType;
|
||||
}
|
||||
|
||||
if (availableTypes.isEmpty) {
|
||||
return preferredType ?? PaymentType.bankAccount;
|
||||
}
|
||||
|
||||
if (availableTypes.keys.contains(_selectedType)) {
|
||||
return _selectedType;
|
||||
}
|
||||
|
||||
if (preferredType != null && availableTypes.keys.contains(preferredType)) {
|
||||
return preferredType;
|
||||
}
|
||||
|
||||
return availableTypes.keys.first;
|
||||
}
|
||||
}
|
||||
@@ -1,79 +0,0 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:pshared/models/payment/methods/data.dart';
|
||||
|
||||
import 'package:pshared/models/payment/type.dart';
|
||||
import 'package:pshared/models/recipient/recipient.dart';
|
||||
|
||||
import 'package:pweb/providers/page_selector.dart';
|
||||
|
||||
|
||||
class PaymentFlowProvider extends ChangeNotifier {
|
||||
PaymentType _selectedType;
|
||||
PaymentMethodData? _manualPaymentData;
|
||||
|
||||
PaymentFlowProvider({
|
||||
required PaymentType initialType,
|
||||
}) : _selectedType = initialType;
|
||||
|
||||
PaymentType get selectedType => _selectedType;
|
||||
PaymentMethodData? get manualPaymentData => _manualPaymentData;
|
||||
|
||||
void syncWithSelector(PageSelectorProvider selector) {
|
||||
final recipient = selector.selectedRecipient;
|
||||
final resolvedType = _resolveSelectedType(selector, recipient);
|
||||
|
||||
var hasChanges = false;
|
||||
if (resolvedType != _selectedType) {
|
||||
_selectedType = resolvedType;
|
||||
hasChanges = true;
|
||||
}
|
||||
|
||||
if (recipient != null && _manualPaymentData != null) {
|
||||
_manualPaymentData = null;
|
||||
hasChanges = true;
|
||||
}
|
||||
|
||||
if (hasChanges) notifyListeners();
|
||||
}
|
||||
|
||||
void reset(PageSelectorProvider selector) {
|
||||
_selectedType = selector.getDefaultPaymentType();
|
||||
_manualPaymentData = null;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void selectType(PaymentType type, {bool resetManualData = false}) {
|
||||
if (_selectedType == type && (!resetManualData || _manualPaymentData == null)) {
|
||||
return;
|
||||
}
|
||||
|
||||
_selectedType = type;
|
||||
if (resetManualData) {
|
||||
_manualPaymentData = null;
|
||||
}
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void setManualPaymentData(PaymentMethodData? data) {
|
||||
_manualPaymentData = data;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
PaymentType _resolveSelectedType(
|
||||
PageSelectorProvider selector,
|
||||
Recipient? recipient,
|
||||
) {
|
||||
final available = selector.getAvailablePaymentTypes();
|
||||
final current = _selectedType;
|
||||
|
||||
if (recipient == null) {
|
||||
return current;
|
||||
}
|
||||
|
||||
if (available.keys.contains(current)) {
|
||||
return current;
|
||||
}
|
||||
|
||||
return selector.getDefaultPaymentType();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user