refactor tips (#406)

* refactor tips

* refactor ionia tips implementation
This commit is contained in:
Godwin Asuquo 2022-07-07 20:46:56 +03:00 committed by GitHub
parent 73cbdc4546
commit f320e749b8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 92 additions and 78 deletions

View file

@ -4,6 +4,7 @@ import 'package:cake_wallet/entities/wake_lock.dart';
import 'package:cake_wallet/ionia/ionia_anypay.dart';
import 'package:cake_wallet/ionia/ionia_category.dart';
import 'package:cake_wallet/view_model/ionia/ionia_auth_view_model.dart';
import 'package:cake_wallet/view_model/ionia/ionia_buy_card_view_model.dart';
import 'package:cake_wallet/view_model/ionia/ionia_filter_view_model.dart';
import 'package:cake_wallet/ionia/ionia_service.dart';
import 'package:cake_wallet/ionia/ionia_api.dart';
@ -671,7 +672,17 @@ Future setup(
getIt.registerFactory(() => IoniaAuthViewModel(ioniaService: getIt.get<IoniaService>()));
getIt.registerFactory(() => IoniaMerchPurchaseViewModel(getIt.get<IoniaAnyPay>()));
getIt.registerFactoryParam<IoniaMerchPurchaseViewModel, double, IoniaMerchant>((double amount, merchant) {
return IoniaMerchPurchaseViewModel(
ioniaAnyPayService: getIt.get<IoniaAnyPay>(),
amount: amount,
ioniaMerchant: merchant,
);
});
getIt.registerFactoryParam<IoniaBuyCardViewModel, IoniaMerchant, void>((IoniaMerchant merchant, _) {
return IoniaBuyCardViewModel(ioniaMerchant: merchant);
});
getIt.registerFactory(() => IoniaAccountViewModel(ioniaService: getIt.get<IoniaService>()));
@ -691,21 +702,20 @@ Future setup(
getIt.registerFactoryParam<IoniaBuyGiftCardPage, List, void>((List args, _) {
final merchant = args.first as IoniaMerchant;
return IoniaBuyGiftCardPage(getIt.get<IoniaMerchPurchaseViewModel>(), merchant);
return IoniaBuyGiftCardPage(getIt.get<IoniaBuyCardViewModel>(param1: merchant));
});
getIt.registerFactoryParam<IoniaBuyGiftCardDetailPage, List, void>((List args, _) {
final amount = args.first as String;
final amount = args.first as double;
final merchant = args.last as IoniaMerchant;
return IoniaBuyGiftCardDetailPage(amount, getIt.get<IoniaMerchPurchaseViewModel>(), merchant);
return IoniaBuyGiftCardDetailPage(getIt.get<IoniaMerchPurchaseViewModel>(param1: amount, param2: merchant));
});
getIt.registerFactoryParam<IoniaCustomTipPage, List, void>((List args, _) {
final amount = args.first as String;
final merchant = args.last as IoniaMerchant;
return IoniaCustomTipPage(getIt.get<IoniaMerchPurchaseViewModel>(), amount, merchant);
return IoniaCustomTipPage(getIt.get<IoniaMerchPurchaseViewModel>(param1: amount, param2: merchant));
});
getIt.registerFactory(() => IoniaManageCardsPage(getIt.get<IoniaGiftCardsListViewModel>()));

View file

@ -25,16 +25,10 @@ import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:mobx/mobx.dart';
class IoniaBuyGiftCardDetailPage extends StatelessWidget {
IoniaBuyGiftCardDetailPage(this.amount, this.ioniaPurchaseViewModel, this.merchant) {
ioniaPurchaseViewModel.setSelectedMerchant(merchant);
}
IoniaBuyGiftCardDetailPage(this.ioniaPurchaseViewModel);
final IoniaMerchPurchaseViewModel ioniaPurchaseViewModel;
final IoniaMerchant merchant;
final String amount;
ThemeBase get currentTheme => getIt.get<SettingsStore>().currentTheme;
Color get backgroundLightColor => Colors.white;
@ -131,7 +125,6 @@ class IoniaBuyGiftCardDetailPage extends StatelessWidget {
}
});
ioniaPurchaseViewModel.onAmountChanged(amount);
return Scaffold(
backgroundColor: _backgroundColor,
body: ScrollableWithBottomSection(
@ -192,7 +185,7 @@ class IoniaBuyGiftCardDetailPage extends StatelessWidget {
),
SizedBox(height: 4),
Text(
'\$$amount',
'\$${ioniaPurchaseViewModel.amount}',
style: textLargeSemiBold(),
),
],
@ -231,14 +224,12 @@ class IoniaBuyGiftCardDetailPage extends StatelessWidget {
),
),
SizedBox(height: 4),
TipButtonGroup(
selectedTip: tipAmount,
tipsList: [
IoniaTip(percentage: 0, originalAmount: double.parse(amount)),
IoniaTip(percentage: 10, originalAmount: double.parse(amount)),
IoniaTip(percentage: 20, originalAmount: double.parse(amount)),
],
onSelect: (value) => ioniaPurchaseViewModel.addTip(value),
Observer(
builder: (_) => TipButtonGroup(
selectedTip: ioniaPurchaseViewModel.selectedTip.percentage,
tipsList: ioniaPurchaseViewModel.tips,
onSelect: (value) => ioniaPurchaseViewModel.addTip(value),
),
)
],
),
@ -262,7 +253,6 @@ class IoniaBuyGiftCardDetailPage extends StatelessWidget {
return LoadingPrimaryButton(
isLoading: ioniaPurchaseViewModel.invoiceCreationState is IsExecutingState ||
ioniaPurchaseViewModel.invoiceCommittingState is IsExecutingState,
isDisabled: !ioniaPurchaseViewModel.enableCardPurchase,
onPressed: () => purchaseCard(context),
text: S.of(context).purchase_gift_card,
color: Theme.of(context).accentTextTheme.body2.color,
@ -291,7 +281,7 @@ class IoniaBuyGiftCardDetailPage extends StatelessWidget {
builder: (BuildContext context) {
return AlertWithOneAction(
alertTitle: '',
alertContent: merchant.termsAndConditions,
alertContent: ioniaPurchaseViewModel.ioniaMerchant.termsAndConditions,
buttonText: S.of(context).agree,
buttonAction: () => Navigator.of(context).pop(),
);
@ -550,10 +540,7 @@ class TipButtonGroup extends StatelessWidget {
final double selectedTip;
final List<IoniaTip> tipsList;
bool _isSelected(String value) {
final tip = selectedTip.round().toString();
return tip == value;
}
bool _isSelected(double value) => selectedTip == value;
@override
Widget build(BuildContext context) {
@ -562,7 +549,7 @@ class TipButtonGroup extends StatelessWidget {
...[
for (var i = 0; i < tipsList.length; i++) ...[
TipButton(
isSelected: _isSelected(tipsList[i].originalAmount.toString()),
isSelected: _isSelected(tipsList[i].percentage),
onTap: () => onSelect(tipsList[i]),
caption: '${tipsList[i].percentage}%',
subTitle: '\$${tipsList[i].additionalAmount}',

View file

@ -7,6 +7,7 @@ import 'package:cake_wallet/src/widgets/keyboard_done_button.dart';
import 'package:cake_wallet/src/widgets/primary_button.dart';
import 'package:cake_wallet/src/widgets/scollable_with_bottom_section.dart';
import 'package:cake_wallet/themes/theme_base.dart';
import 'package:cake_wallet/view_model/ionia/ionia_buy_card_view_model.dart';
import 'package:cake_wallet/view_model/ionia/ionia_purchase_merch_view_model.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
@ -16,18 +17,15 @@ import 'package:cake_wallet/generated/i18n.dart';
class IoniaBuyGiftCardPage extends BasePage {
IoniaBuyGiftCardPage(
this.ioniaPurchaseViewModel, this.merchant,
this.ioniaBuyCardViewModel,
) : _amountFieldFocus = FocusNode(),
_amountController = TextEditingController() {
ioniaPurchaseViewModel.setSelectedMerchant(merchant);
_amountController.addListener(() {
ioniaPurchaseViewModel.onAmountChanged(_amountController.text);
ioniaBuyCardViewModel.onAmountChanged(_amountController.text);
});
}
final IoniaMerchPurchaseViewModel ioniaPurchaseViewModel;
final IoniaMerchant merchant;
final IoniaBuyCardViewModel ioniaBuyCardViewModel;
@override
String get title => S.current.enter_amount;
@ -49,7 +47,7 @@ class IoniaBuyGiftCardPage extends BasePage {
@override
Widget body(BuildContext context) {
final _width = MediaQuery.of(context).size.width;
final merchant = ioniaPurchaseViewModel.ioniaMerchant;
final merchant = ioniaBuyCardViewModel.ioniaMerchant;
return KeyboardActions(
disableScroll: true,
config: KeyboardActionsConfig(
@ -164,17 +162,14 @@ class IoniaBuyGiftCardPage extends BasePage {
onPressed: () => Navigator.of(context).pushNamed(
Routes.ioniaBuyGiftCardDetailPage,
arguments: [
ioniaPurchaseViewModel.amount,
ioniaPurchaseViewModel.ioniaMerchant,
ioniaBuyCardViewModel.amount,
ioniaBuyCardViewModel.ioniaMerchant,
],
),
text: S.of(context).continue_text,
isDisabled: !ioniaPurchaseViewModel.enableCardPurchase,
isDisabled: !ioniaBuyCardViewModel.isEnablePurchase,
color: Theme.of(context).accentTextTheme.body2.color,
textColor: Theme.of(context)
.accentTextTheme
.headline
.decorationColor,
textColor: Theme.of(context).accentTextTheme.headline.decorationColor,
),
);
}),

View file

@ -16,20 +16,15 @@ import 'package:cake_wallet/generated/i18n.dart';
class IoniaCustomTipPage extends BasePage {
IoniaCustomTipPage(
this.ioniaPurchaseViewModel,
this.billAmount,
this.merchant,
) : _amountFieldFocus = FocusNode(),
_amountController = TextEditingController() {
ioniaPurchaseViewModel.setSelectedMerchant(merchant);
ioniaPurchaseViewModel.onAmountChanged(billAmount);
_amountController.addListener(() {
// ioniaPurchaseViewModel.onTipChanged(_amountController.text);
});
}
final IoniaMerchPurchaseViewModel ioniaPurchaseViewModel;
final String billAmount;
final IoniaMerchant merchant;
@override
String get title => S.current.enter_amount;
@ -135,7 +130,7 @@ class IoniaCustomTipPage extends BasePage {
children: [
TextSpan(text: ' ${S.of(context).is_percentage} '),
TextSpan(text: '${ioniaPurchaseViewModel.percentage}%'),
TextSpan(text: ' ${S.of(context).percentageOf(billAmount)} '),
TextSpan(text: ' ${S.of(context).percentageOf(ioniaPurchaseViewModel.amount.toString())} '),
],
),
);

View file

@ -0,0 +1,31 @@
import 'package:cake_wallet/ionia/ionia_merchant.dart';
import 'package:mobx/mobx.dart';
part 'ionia_buy_card_view_model.g.dart';
class IoniaBuyCardViewModel = IoniaBuyCardViewModelBase with _$IoniaBuyCardViewModel;
abstract class IoniaBuyCardViewModelBase with Store {
IoniaBuyCardViewModelBase({this.ioniaMerchant}) {
isEnablePurchase = false;
amount = 0;
}
final IoniaMerchant ioniaMerchant;
@observable
double amount;
@observable
bool isEnablePurchase;
@action
void onAmountChanged(String input) {
if (input.isEmpty) return;
amount = double.parse(input);
final min = ioniaMerchant.minimumCardPurchase;
final max = ioniaMerchant.maximumCardPurchase;
isEnablePurchase = amount >= min && amount <= max;
}
}

View file

@ -4,6 +4,7 @@ import 'package:cake_wallet/core/execution_state.dart';
import 'package:cake_wallet/ionia/ionia_anypay.dart';
import 'package:cake_wallet/ionia/ionia_merchant.dart';
import 'package:cake_wallet/ionia/ionia_tip.dart';
import 'package:flutter/foundation.dart';
import 'package:mobx/mobx.dart';
part 'ionia_purchase_merch_view_model.g.dart';
@ -11,16 +12,31 @@ part 'ionia_purchase_merch_view_model.g.dart';
class IoniaMerchPurchaseViewModel = IoniaMerchPurchaseViewModelBase with _$IoniaMerchPurchaseViewModel;
abstract class IoniaMerchPurchaseViewModelBase with Store {
IoniaMerchPurchaseViewModelBase(this.ioniaAnyPayService) {
IoniaMerchPurchaseViewModelBase({
@required this.ioniaAnyPayService,
@required this.amount,
@required this.ioniaMerchant,
}) {
tipAmount = 0.0;
percentage = 0.0;
amount = '';
enableCardPurchase = false;
tips = <IoniaTip>[
IoniaTip(percentage: 0, originalAmount: amount),
IoniaTip(percentage: 10, originalAmount: amount),
IoniaTip(percentage: 20, originalAmount: amount),
];
selectedTip = tips.first;
}
IoniaMerchant ioniaMerchant;
final double amount;
IoniaAnyPay ioniaAnyPayService;
List<IoniaTip> tips;
@observable
IoniaTip selectedTip;
final IoniaMerchant ioniaMerchant;
final IoniaAnyPay ioniaAnyPayService;
AnyPayPayment invoice;
@ -32,39 +48,19 @@ abstract class IoniaMerchPurchaseViewModelBase with Store {
@observable
ExecutionState invoiceCommittingState;
@observable
String amount;
@observable
double percentage;
@computed
double get giftCardAmount => double.parse(amount) + tipAmount;
double get giftCardAmount => amount + tipAmount;
@observable
double tipAmount;
@observable
bool enableCardPurchase;
@action
void onAmountChanged(String input) {
if (input.isEmpty) return;
amount = input;
final inputAmount = double.parse(input);
final min = ioniaMerchant.minimumCardPurchase;
final max = ioniaMerchant.maximumCardPurchase;
enableCardPurchase = inputAmount >= min && inputAmount <= max;
}
void setSelectedMerchant(IoniaMerchant merchant) {
ioniaMerchant = merchant;
}
@action
void addTip(IoniaTip tip) {
tipAmount = tip.additionalAmount;
selectedTip = tip;
}
@action