Skip to content

Commit

Permalink
feat: 5418 - Prices - "Are you sure you want to exit?" feature
Browse files Browse the repository at this point in the history
Impacted files:
* `price_add_product_card.dart`: now accessing amount items through specific methods
* `price_amount_card.dart`: now accessing amount items through specific methods
* `price_amount_model.dart`: new `hasChanged` getter
* `price_model.dart`: new `hasChanged` getter; now giving access to amount items through specific methods
* `price_proof_card.dart`: minor refactoring
* `product_price_add_page.dart`: used `WillPopScore2` in order to check for changes against back gesture; now accessing amount items through specific methods
* `user_preferences_prices.dart`: usage of `AppNavigator` for a better management of back gesture
  • Loading branch information
monsieurtanuki committed Oct 25, 2024
1 parent ccbe094 commit c88a91f
Show file tree
Hide file tree
Showing 7 changed files with 221 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:smooth_app/helpers/launch_url_helper.dart';
import 'package:smooth_app/pages/navigator/app_navigator.dart';
import 'package:smooth_app/pages/preferences/abstract_user_preferences.dart';
import 'package:smooth_app/pages/preferences/lazy_counter.dart';
import 'package:smooth_app/pages/preferences/lazy_counter_widget.dart';
Expand Down Expand Up @@ -130,6 +131,12 @@ class UserPreferencesPrices extends AbstractUserPreferences {
];
}

// we need the [AppNavigator] for a better back-gesture management.
@override
Future<void> runHeaderAction() async => AppNavigator.of(context).push(
AppRoutes.PREFERENCES(PreferencePageType.PRICES),
);

UserPreferencesItem _getPriceListTile(
final String title,
final String path,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ class _PriceAddProductCardState extends State<PriceAddProductCard> {
context,
listen: false,
);
for (final PriceAmountModel model in priceModel.priceAmountModels) {
for (int i = 0; i < priceModel.length; i++) {
final PriceAmountModel model = priceModel.elementAt(i);
if (model.product.barcode == barcode) {
await showDialog<void>(
context: context,
Expand All @@ -111,7 +112,7 @@ class _PriceAddProductCardState extends State<PriceAddProductCard> {
return;
}
}
priceModel.priceAmountModels.add(
priceModel.add(
PriceAmountModel(
product: PriceMetaProduct.unknown(
barcode,
Expand Down
14 changes: 5 additions & 9 deletions packages/smooth_app/lib/pages/prices/price_amount_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class _PriceAmountCardState extends State<PriceAmountCard> {
final PriceAmountModel model = Provider.of<PriceModel>(
context,
listen: false,
).priceAmountModels[widget.index];
).elementAt(widget.index);
_controllerPaid = TextEditingController(text: model.paidPrice);
_controllerWithoutDiscount =
TextEditingController(text: model.priceWithoutDiscount);
Expand All @@ -49,8 +49,8 @@ class _PriceAmountCardState extends State<PriceAmountCard> {
Widget build(BuildContext context) {
final AppLocalizations appLocalizations = AppLocalizations.of(context);
final PriceModel priceModel = Provider.of<PriceModel>(context);
final PriceAmountModel model = priceModel.priceAmountModels[widget.index];
final int total = priceModel.priceAmountModels.length;
final PriceAmountModel model = priceModel.elementAt(widget.index);
final int total = priceModel.length;

return SmoothCard(
child: Column(
Expand All @@ -62,12 +62,8 @@ class _PriceAmountCardState extends State<PriceAmountCard> {
PriceProductListTile(
product: model.product,
trailingIconData: total == 1 ? null : Icons.clear,
onPressed: total == 1
? null
: () {
priceModel.priceAmountModels.removeAt(widget.index);
priceModel.notifyListeners();
},
onPressed:
total == 1 ? null : () => priceModel.removeAt(widget.index),
),
SmoothLargeButtonWithIcon(
icon: model.promo ? Icons.check_box : Icons.check_box_outline_blank,
Expand Down
34 changes: 30 additions & 4 deletions packages/smooth_app/lib/pages/prices/price_amount_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,29 @@ class PriceAmountModel {
required this.product,
});

PriceMetaProduct product;
final PriceMetaProduct product;

String paidPrice = '';
String priceWithoutDiscount = '';
bool _hasChanged = false;

bool get hasChanged => _hasChanged;

String _paidPrice = '';

String get paidPrice => _paidPrice;

set paidPrice(final String value) {
_hasChanged = true;
_paidPrice = value;
}

String _priceWithoutDiscount = '';

String get priceWithoutDiscount => _priceWithoutDiscount;

set priceWithoutDiscount(final String value) {
_hasChanged = true;
_priceWithoutDiscount = value;
}

late double _checkedPaidPrice;
double? _checkedPriceWithoutDiscount;
Expand All @@ -20,7 +39,14 @@ class PriceAmountModel {

double? get checkedPriceWithoutDiscount => _checkedPriceWithoutDiscount;

bool promo = false;
bool _promo = false;

bool get promo => _promo;

set promo(final bool value) {
_hasChanged = true;
_promo = value;
}

static double? validateDouble(final String value) =>
double.tryParse(value) ??
Expand Down
55 changes: 48 additions & 7 deletions packages/smooth_app/lib/pages/prices/price_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,43 @@ class PriceModel with ChangeNotifier {
_date = DateTime.now(),
_currency = currency,
_locations = locations,
priceAmountModels = <PriceAmountModel>[
_priceAmountModels = <PriceAmountModel>[
if (initialProduct != null) PriceAmountModel(product: initialProduct),
];

PriceModel.proof({
required Proof proof,
}) : priceAmountModels = <PriceAmountModel>[] {
setProof(proof);
}) : _priceAmountModels = <PriceAmountModel>[] {
setProof(proof, init: true);
}

void setProof(final Proof proof) {
bool _hasChanged = false;

bool get hasChanged {
if (_hasChanged) {
return true;
}
for (final PriceAmountModel priceAmountModel in _priceAmountModels) {
if (priceAmountModel.hasChanged) {
return true;
}
}
return false;
}

void setProof(final Proof proof, {final bool init = false}) {
if (!init) {
_hasChanged = true;
}
_proof = proof;
_cropParameters = null;
_proofType = proof.type!;
_date = proof.date!;
_locations = null;
_currency = proof.currency!;
if (!init) {
notifyListeners();
}
}

/// Checks if a proof cannot be reused for prices adding.
Expand All @@ -58,13 +78,30 @@ class PriceModel with ChangeNotifier {

bool get hasImage => _proof != null || _cropParameters != null;

final List<PriceAmountModel> priceAmountModels;
final List<PriceAmountModel> _priceAmountModels;

void add(final PriceAmountModel priceAmountModel) {
_hasChanged = true;
_priceAmountModels.add(priceAmountModel);
notifyListeners();
}

void removeAt(final int index) {
_hasChanged = true;
_priceAmountModels.removeAt(index);
notifyListeners();
}

PriceAmountModel elementAt(final int index) => _priceAmountModels[index];

int get length => _priceAmountModels.length;

CropParameters? _cropParameters;

CropParameters? get cropParameters => _cropParameters;

set cropParameters(final CropParameters? value) {
_hasChanged = true;
_cropParameters = value;
_proof = null;
notifyListeners();
Expand All @@ -79,6 +116,7 @@ class PriceModel with ChangeNotifier {
ProofType get proofType => _proof != null ? _proof!.type! : _proofType;

set proofType(final ProofType proofType) {
_hasChanged = true;
_proofType = proofType;
notifyListeners();
}
Expand All @@ -88,6 +126,7 @@ class PriceModel with ChangeNotifier {
DateTime get date => _date;

set date(final DateTime date) {
_hasChanged = true;
_date = date;
notifyListeners();
}
Expand All @@ -100,6 +139,7 @@ class PriceModel with ChangeNotifier {
List<OsmLocation>? get locations => _locations;

set locations(final List<OsmLocation>? locations) {
_hasChanged = true;
_locations = locations;
notifyListeners();
}
Expand All @@ -113,6 +153,7 @@ class PriceModel with ChangeNotifier {
Currency get currency => _currency;

set currency(final Currency currency) {
_hasChanged = true;
_currency = currency;
notifyListeners();
}
Expand All @@ -133,7 +174,7 @@ class PriceModel with ChangeNotifier {
}
}

for (final PriceAmountModel priceAmountModel in priceAmountModels) {
for (final PriceAmountModel priceAmountModel in _priceAmountModels) {
final String? checkParameters = priceAmountModel.checkParameters(context);
if (checkParameters != null) {
return checkParameters;
Expand All @@ -152,7 +193,7 @@ class PriceModel with ChangeNotifier {
final List<bool> pricesAreDiscounted = <bool>[];
final List<double> prices = <double>[];
final List<double?> pricesWithoutDiscount = <double?>[];
for (final PriceAmountModel priceAmountModel in priceAmountModels) {
for (final PriceAmountModel priceAmountModel in _priceAmountModels) {
barcodes.add(priceAmountModel.product.barcode);
pricesAreDiscounted.add(priceAmountModel.promo);
prices.add(priceAmountModel.checkedPaidPrice);
Expand Down
1 change: 0 additions & 1 deletion packages/smooth_app/lib/pages/prices/price_proof_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ enum _ProofSource {
);
if (proof != null) {
model.setProof(proof);
model.notifyListeners();
}
return;
case _ProofSource.camera:
Expand Down
Loading

0 comments on commit c88a91f

Please sign in to comment.