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_anypay.dart';
import 'package:cake_wallet/ionia/ionia_category.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_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/view_model/ionia/ionia_filter_view_model.dart';
import 'package:cake_wallet/ionia/ionia_service.dart'; import 'package:cake_wallet/ionia/ionia_service.dart';
import 'package:cake_wallet/ionia/ionia_api.dart'; import 'package:cake_wallet/ionia/ionia_api.dart';
@ -671,7 +672,17 @@ Future setup(
getIt.registerFactory(() => IoniaAuthViewModel(ioniaService: getIt.get<IoniaService>())); 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>())); getIt.registerFactory(() => IoniaAccountViewModel(ioniaService: getIt.get<IoniaService>()));
@ -691,21 +702,20 @@ Future setup(
getIt.registerFactoryParam<IoniaBuyGiftCardPage, List, void>((List args, _) { getIt.registerFactoryParam<IoniaBuyGiftCardPage, List, void>((List args, _) {
final merchant = args.first as IoniaMerchant; 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, _) { 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; final merchant = args.last as IoniaMerchant;
return IoniaBuyGiftCardDetailPage(getIt.get<IoniaMerchPurchaseViewModel>(param1: amount, param2: merchant));
return IoniaBuyGiftCardDetailPage(amount, getIt.get<IoniaMerchPurchaseViewModel>(), merchant);
}); });
getIt.registerFactoryParam<IoniaCustomTipPage, List, void>((List args, _) { getIt.registerFactoryParam<IoniaCustomTipPage, List, void>((List args, _) {
final amount = args.first as String; final amount = args.first as String;
final merchant = args.last as IoniaMerchant; 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>())); getIt.registerFactory(() => IoniaManageCardsPage(getIt.get<IoniaGiftCardsListViewModel>()));

View file

@ -25,16 +25,10 @@ import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:mobx/mobx.dart'; import 'package:mobx/mobx.dart';
class IoniaBuyGiftCardDetailPage extends StatelessWidget { class IoniaBuyGiftCardDetailPage extends StatelessWidget {
IoniaBuyGiftCardDetailPage(this.amount, this.ioniaPurchaseViewModel, this.merchant) { IoniaBuyGiftCardDetailPage(this.ioniaPurchaseViewModel);
ioniaPurchaseViewModel.setSelectedMerchant(merchant);
}
final IoniaMerchPurchaseViewModel ioniaPurchaseViewModel; final IoniaMerchPurchaseViewModel ioniaPurchaseViewModel;
final IoniaMerchant merchant;
final String amount;
ThemeBase get currentTheme => getIt.get<SettingsStore>().currentTheme; ThemeBase get currentTheme => getIt.get<SettingsStore>().currentTheme;
Color get backgroundLightColor => Colors.white; Color get backgroundLightColor => Colors.white;
@ -131,7 +125,6 @@ class IoniaBuyGiftCardDetailPage extends StatelessWidget {
} }
}); });
ioniaPurchaseViewModel.onAmountChanged(amount);
return Scaffold( return Scaffold(
backgroundColor: _backgroundColor, backgroundColor: _backgroundColor,
body: ScrollableWithBottomSection( body: ScrollableWithBottomSection(
@ -192,7 +185,7 @@ class IoniaBuyGiftCardDetailPage extends StatelessWidget {
), ),
SizedBox(height: 4), SizedBox(height: 4),
Text( Text(
'\$$amount', '\$${ioniaPurchaseViewModel.amount}',
style: textLargeSemiBold(), style: textLargeSemiBold(),
), ),
], ],
@ -231,14 +224,12 @@ class IoniaBuyGiftCardDetailPage extends StatelessWidget {
), ),
), ),
SizedBox(height: 4), SizedBox(height: 4),
TipButtonGroup( Observer(
selectedTip: tipAmount, builder: (_) => TipButtonGroup(
tipsList: [ selectedTip: ioniaPurchaseViewModel.selectedTip.percentage,
IoniaTip(percentage: 0, originalAmount: double.parse(amount)), tipsList: ioniaPurchaseViewModel.tips,
IoniaTip(percentage: 10, originalAmount: double.parse(amount)), onSelect: (value) => ioniaPurchaseViewModel.addTip(value),
IoniaTip(percentage: 20, originalAmount: double.parse(amount)), ),
],
onSelect: (value) => ioniaPurchaseViewModel.addTip(value),
) )
], ],
), ),
@ -262,7 +253,6 @@ class IoniaBuyGiftCardDetailPage extends StatelessWidget {
return LoadingPrimaryButton( return LoadingPrimaryButton(
isLoading: ioniaPurchaseViewModel.invoiceCreationState is IsExecutingState || isLoading: ioniaPurchaseViewModel.invoiceCreationState is IsExecutingState ||
ioniaPurchaseViewModel.invoiceCommittingState is IsExecutingState, ioniaPurchaseViewModel.invoiceCommittingState is IsExecutingState,
isDisabled: !ioniaPurchaseViewModel.enableCardPurchase,
onPressed: () => purchaseCard(context), onPressed: () => purchaseCard(context),
text: S.of(context).purchase_gift_card, text: S.of(context).purchase_gift_card,
color: Theme.of(context).accentTextTheme.body2.color, color: Theme.of(context).accentTextTheme.body2.color,
@ -291,7 +281,7 @@ class IoniaBuyGiftCardDetailPage extends StatelessWidget {
builder: (BuildContext context) { builder: (BuildContext context) {
return AlertWithOneAction( return AlertWithOneAction(
alertTitle: '', alertTitle: '',
alertContent: merchant.termsAndConditions, alertContent: ioniaPurchaseViewModel.ioniaMerchant.termsAndConditions,
buttonText: S.of(context).agree, buttonText: S.of(context).agree,
buttonAction: () => Navigator.of(context).pop(), buttonAction: () => Navigator.of(context).pop(),
); );
@ -550,10 +540,7 @@ class TipButtonGroup extends StatelessWidget {
final double selectedTip; final double selectedTip;
final List<IoniaTip> tipsList; final List<IoniaTip> tipsList;
bool _isSelected(String value) { bool _isSelected(double value) => selectedTip == value;
final tip = selectedTip.round().toString();
return tip == value;
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@ -562,7 +549,7 @@ class TipButtonGroup extends StatelessWidget {
...[ ...[
for (var i = 0; i < tipsList.length; i++) ...[ for (var i = 0; i < tipsList.length; i++) ...[
TipButton( TipButton(
isSelected: _isSelected(tipsList[i].originalAmount.toString()), isSelected: _isSelected(tipsList[i].percentage),
onTap: () => onSelect(tipsList[i]), onTap: () => onSelect(tipsList[i]),
caption: '${tipsList[i].percentage}%', caption: '${tipsList[i].percentage}%',
subTitle: '\$${tipsList[i].additionalAmount}', 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/primary_button.dart';
import 'package:cake_wallet/src/widgets/scollable_with_bottom_section.dart'; import 'package:cake_wallet/src/widgets/scollable_with_bottom_section.dart';
import 'package:cake_wallet/themes/theme_base.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:cake_wallet/view_model/ionia/ionia_purchase_merch_view_model.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
@ -16,18 +17,15 @@ import 'package:cake_wallet/generated/i18n.dart';
class IoniaBuyGiftCardPage extends BasePage { class IoniaBuyGiftCardPage extends BasePage {
IoniaBuyGiftCardPage( IoniaBuyGiftCardPage(
this.ioniaPurchaseViewModel, this.merchant, this.ioniaBuyCardViewModel,
) : _amountFieldFocus = FocusNode(), ) : _amountFieldFocus = FocusNode(),
_amountController = TextEditingController() { _amountController = TextEditingController() {
ioniaPurchaseViewModel.setSelectedMerchant(merchant);
_amountController.addListener(() { _amountController.addListener(() {
ioniaPurchaseViewModel.onAmountChanged(_amountController.text); ioniaBuyCardViewModel.onAmountChanged(_amountController.text);
}); });
} }
final IoniaMerchPurchaseViewModel ioniaPurchaseViewModel; final IoniaBuyCardViewModel ioniaBuyCardViewModel;
final IoniaMerchant merchant;
@override @override
String get title => S.current.enter_amount; String get title => S.current.enter_amount;
@ -49,7 +47,7 @@ class IoniaBuyGiftCardPage extends BasePage {
@override @override
Widget body(BuildContext context) { Widget body(BuildContext context) {
final _width = MediaQuery.of(context).size.width; final _width = MediaQuery.of(context).size.width;
final merchant = ioniaPurchaseViewModel.ioniaMerchant; final merchant = ioniaBuyCardViewModel.ioniaMerchant;
return KeyboardActions( return KeyboardActions(
disableScroll: true, disableScroll: true,
config: KeyboardActionsConfig( config: KeyboardActionsConfig(
@ -164,17 +162,14 @@ class IoniaBuyGiftCardPage extends BasePage {
onPressed: () => Navigator.of(context).pushNamed( onPressed: () => Navigator.of(context).pushNamed(
Routes.ioniaBuyGiftCardDetailPage, Routes.ioniaBuyGiftCardDetailPage,
arguments: [ arguments: [
ioniaPurchaseViewModel.amount, ioniaBuyCardViewModel.amount,
ioniaPurchaseViewModel.ioniaMerchant, ioniaBuyCardViewModel.ioniaMerchant,
], ],
), ),
text: S.of(context).continue_text, text: S.of(context).continue_text,
isDisabled: !ioniaPurchaseViewModel.enableCardPurchase, isDisabled: !ioniaBuyCardViewModel.isEnablePurchase,
color: Theme.of(context).accentTextTheme.body2.color, color: Theme.of(context).accentTextTheme.body2.color,
textColor: Theme.of(context) textColor: Theme.of(context).accentTextTheme.headline.decorationColor,
.accentTextTheme
.headline
.decorationColor,
), ),
); );
}), }),

View file

@ -16,20 +16,15 @@ import 'package:cake_wallet/generated/i18n.dart';
class IoniaCustomTipPage extends BasePage { class IoniaCustomTipPage extends BasePage {
IoniaCustomTipPage( IoniaCustomTipPage(
this.ioniaPurchaseViewModel, this.ioniaPurchaseViewModel,
this.billAmount,
this.merchant,
) : _amountFieldFocus = FocusNode(), ) : _amountFieldFocus = FocusNode(),
_amountController = TextEditingController() { _amountController = TextEditingController() {
ioniaPurchaseViewModel.setSelectedMerchant(merchant);
ioniaPurchaseViewModel.onAmountChanged(billAmount);
_amountController.addListener(() { _amountController.addListener(() {
// ioniaPurchaseViewModel.onTipChanged(_amountController.text); // ioniaPurchaseViewModel.onTipChanged(_amountController.text);
}); });
} }
final IoniaMerchPurchaseViewModel ioniaPurchaseViewModel; final IoniaMerchPurchaseViewModel ioniaPurchaseViewModel;
final String billAmount;
final IoniaMerchant merchant;
@override @override
String get title => S.current.enter_amount; String get title => S.current.enter_amount;
@ -135,7 +130,7 @@ class IoniaCustomTipPage extends BasePage {
children: [ children: [
TextSpan(text: ' ${S.of(context).is_percentage} '), TextSpan(text: ' ${S.of(context).is_percentage} '),
TextSpan(text: '${ioniaPurchaseViewModel.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_anypay.dart';
import 'package:cake_wallet/ionia/ionia_merchant.dart'; import 'package:cake_wallet/ionia/ionia_merchant.dart';
import 'package:cake_wallet/ionia/ionia_tip.dart'; import 'package:cake_wallet/ionia/ionia_tip.dart';
import 'package:flutter/foundation.dart';
import 'package:mobx/mobx.dart'; import 'package:mobx/mobx.dart';
part 'ionia_purchase_merch_view_model.g.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; class IoniaMerchPurchaseViewModel = IoniaMerchPurchaseViewModelBase with _$IoniaMerchPurchaseViewModel;
abstract class IoniaMerchPurchaseViewModelBase with Store { abstract class IoniaMerchPurchaseViewModelBase with Store {
IoniaMerchPurchaseViewModelBase(this.ioniaAnyPayService) { IoniaMerchPurchaseViewModelBase({
@required this.ioniaAnyPayService,
@required this.amount,
@required this.ioniaMerchant,
}) {
tipAmount = 0.0; tipAmount = 0.0;
percentage = 0.0; percentage = 0.0;
amount = ''; tips = <IoniaTip>[
enableCardPurchase = false; 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; AnyPayPayment invoice;
@ -32,39 +48,19 @@ abstract class IoniaMerchPurchaseViewModelBase with Store {
@observable @observable
ExecutionState invoiceCommittingState; ExecutionState invoiceCommittingState;
@observable
String amount;
@observable @observable
double percentage; double percentage;
@computed @computed
double get giftCardAmount => double.parse(amount) + tipAmount; double get giftCardAmount => amount + tipAmount;
@observable @observable
double tipAmount; 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 @action
void addTip(IoniaTip tip) { void addTip(IoniaTip tip) {
tipAmount = tip.additionalAmount; tipAmount = tip.additionalAmount;
selectedTip = tip;
} }
@action @action