Files
sendico/frontend/pweb/lib/providers/page_selector.dart

265 lines
7.8 KiB
Dart

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;
}