169 lines
4.8 KiB
Dart
169 lines
4.8 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
import 'package:collection/collection.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/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 selectPage(PayoutDestination dest) {
|
|
_selected = dest;
|
|
notifyListeners();
|
|
}
|
|
|
|
void selectRecipient(Recipient? recipient, {bool fromList = false}) {
|
|
recipientProvider.setCurrentObject(recipient?.id);
|
|
_cameFromRecipientList = fromList;
|
|
_setPreviousDestination();
|
|
_selected = PayoutDestination.payment;
|
|
notifyListeners();
|
|
}
|
|
|
|
void editRecipient(Recipient? recipient, {bool fromList = false}) {
|
|
recipientProvider.setCurrentObject(recipient?.id);
|
|
_cameFromRecipientList = fromList;
|
|
_selected = PayoutDestination.addrecipient;
|
|
notifyListeners();
|
|
}
|
|
|
|
void goToAddRecipient() {
|
|
AmplitudeService.recipientAddStarted();
|
|
recipientProvider.setCurrentObject(null);
|
|
_selected = PayoutDestination.addrecipient;
|
|
_cameFromRecipientList = false;
|
|
notifyListeners();
|
|
}
|
|
|
|
void startPaymentWithoutRecipient(PaymentType type) {
|
|
recipientProvider.setCurrentObject(null);
|
|
_type = type;
|
|
_cameFromRecipientList = false;
|
|
_setPreviousDestination();
|
|
_selected = PayoutDestination.payment;
|
|
notifyListeners();
|
|
}
|
|
|
|
void goBackFromPayment() {
|
|
_selected = _previousDestination ??
|
|
(_cameFromRecipientList
|
|
? PayoutDestination.recipients
|
|
: PayoutDestination.dashboard);
|
|
_type = null;
|
|
_previousDestination = null;
|
|
_cameFromRecipientList = false;
|
|
notifyListeners();
|
|
}
|
|
|
|
void goBackFromWalletEdit() {
|
|
selectPage(PayoutDestination.methods);
|
|
}
|
|
|
|
void selectWallet(Wallet wallet) {
|
|
walletsProvider.selectWallet(wallet);
|
|
_selected = PayoutDestination.editwallet;
|
|
notifyListeners();
|
|
}
|
|
|
|
void startPaymentFromWallet(Wallet wallet) {
|
|
_type = PaymentType.wallet;
|
|
_cameFromRecipientList = false;
|
|
_setPreviousDestination();
|
|
_selected = PayoutDestination.payment;
|
|
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),
|
|
);
|
|
}
|
|
|
|
Map<PaymentType, Object> 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) {
|
|
_previousDestination = _selected;
|
|
}
|
|
}
|
|
|
|
Recipient? get selectedRecipient => recipientProvider.currentObject;
|
|
Wallet? get selectedWallet => walletsProvider.selectedWallet;
|
|
}
|