Skip to content

Commit

Permalink
When leaving an edit page with a SimpleInputWidget, ensure it will …
Browse files Browse the repository at this point in the history
…be saved and restored correctly (#6250)
  • Loading branch information
g123k authored Jan 20, 2025
1 parent ebe1129 commit a0fb228
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ class _AddBasicDetailsPageState extends State<AddBasicDetailsPage> {
final bool? pleaseSave =
await MayExitPageHelper().openSaveBeforeLeavingDialog(context);
if (pleaseSave == null) {
_brandsHelper.restoreItemsBeforeLastAddition();
return false;
}
if (pleaseSave == false) {
Expand Down Expand Up @@ -300,6 +301,10 @@ class _AddBasicDetailsPageState extends State<AddBasicDetailsPage> {
hasChanged = true;
}

_brandsHelper.addItemsFromController(
_brandsController,
clearController: false,
);
if (_brandsHelper.getChangedProduct(result)) {
hasChanged = true;
}
Expand Down
8 changes: 7 additions & 1 deletion packages/smooth_app/lib/pages/product/simple_input_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,10 @@ class _SimpleInputPageState extends State<SimpleInputPage> {
bool added = false;
for (int i = 0; i < widget.helpers.length; i++) {
final AbstractSimpleInputPageHelper helper = widget.helpers[i];
if (helper.addItemsFromController(_controllers[i])) {
if (helper.addItemsFromController(
_controllers[i],
clearController: false,
)) {
added = true;
}
final Product changedProduct = Product(barcode: widget.product.barcode);
Expand All @@ -158,6 +161,9 @@ class _SimpleInputPageState extends State<SimpleInputPage> {
final bool? pleaseSave =
await MayExitPageHelper().openSaveBeforeLeavingDialog(context);
if (pleaseSave == null) {
for (int i = 0; i < widget.helpers.length; i++) {
widget.helpers[i].restoreItemsBeforeLastAddition();
}
return false;
}
if (pleaseSave == false) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,23 +194,40 @@ abstract class AbstractSimpleInputPageHelper extends ChangeNotifier {
@protected
OpenFoodFactsLanguage getLanguage() => ProductQuery.getLanguage();

List<String>? _itemsBeforeLastAddition;

/// Adds all the non-already existing items from the controller.
///
/// The item separator is the comma.
bool addItemsFromController(final TextEditingController controller) {
bool addItemsFromController(
final TextEditingController controller, {
bool clearController = true,
}) {
_itemsBeforeLastAddition = List<String>.from(_terms);

final List<String> input = controller.text.split(',');
bool result = false;
for (final String item in input) {
if (addTerm(item.trim())) {
result = true;
}
}
if (result) {
if (result && clearController) {
controller.text = '';
}
return result;
}

void restoreItemsBeforeLastAddition() {
if (_itemsBeforeLastAddition == null) {
return;
}
_terms = List<String>.from(_itemsBeforeLastAddition!);
_changed = true;
_itemsBeforeLastAddition = null;
notifyListeners();
}

/// Mainly used when reordering the list.
void replaceItems(final List<String> items) {
_terms = List<String>.of(items);
Expand Down

0 comments on commit a0fb228

Please sign in to comment.