Skip to content

Commit

Permalink
Merge pull request #95 from pvdthings/checkout-too-many-dialogs
Browse files Browse the repository at this point in the history
replace some dialogs in checkout process with text field errors
  • Loading branch information
dillonfagan authored Nov 18, 2024
2 parents bc00147 + 7bec2c0 commit 7c1f4cf
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,88 +26,72 @@ class ConnectedThingSearchField extends StatelessWidget {

@override
Widget build(BuildContext context) {
return TextField(
controller: _textController,
onSubmitted: (_) => _submit(),
decoration: InputDecoration(
hintText: 'Enter Item Number',
prefixIcon: const Icon(Icons.numbers),
suffixIcon: IconButton(
tooltip: 'Add Item',
onPressed: () => _submit(),
icon: const Icon(Icons.add_rounded),
),
),
inputFormatters: [
FilteringTextInputFormatter.digitsOnly,
],
);
return ListenableBuilder(
listenable: controller,
builder: (context, child) {
return TextField(
controller: _textController,
onSubmitted: (_) => _submit(),
decoration: InputDecoration(
errorText: controller.errorText,
hintText: 'Enter Item Number',
prefixIcon: const Icon(Icons.numbers),
suffixIcon: IconButton(
tooltip: 'Add Item',
onPressed: () => _submit(),
icon: const Icon(Icons.add_rounded),
),
),
inputFormatters: [
FilteringTextInputFormatter.digitsOnly,
],
);
});
}
}

class ThingSearchController {
class ThingSearchController extends ChangeNotifier {
final BuildContext context;
final List<ItemModel> items;
final InventoryRepository repository;
final void Function(ItemModel) onMatchFound;

bool isLoading = false;

String? errorText;

ThingSearchController({
required this.context,
required this.items,
required this.repository,
required this.onMatchFound,
});

Future<void> search(String value) async {
final itemNumber = int.parse(value);

if (items.any((t) => t.number == itemNumber)) {
errorText = '#$value is already added to this loan.';
notifyListeners();
return;
}

isLoading = true;
final match = await repository.getItem(number: int.parse(value));
final match = await repository.getItem(number: itemNumber);
isLoading = false;

if (match != null) {
if (!match.available) {
_showThingCheckedOutDialog(match);
} else {
onMatchFound(match);
}
} else {
_showUnknownThingDialog(value);
if (match == null) {
errorText = '#$value could not be found.';
notifyListeners();
return;
}
}

void _showThingCheckedOutDialog(ItemModel thing) {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: const Text("Item Unavailable"),
content: Text(
"Item #${thing.number} is checked out or not available for lending."),
actions: [
TextButton(
child: const Text("OK"),
onPressed: () => Navigator.pop(context),
)
],
);
},
);
}
if (!match.available) {
errorText = '#$value is unavailable.';
notifyListeners();
return;
}

void _showUnknownThingDialog(String searchValue) {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text("Item #$searchValue does not exist."),
content: const Text("Try another number."),
actions: [
TextButton(
child: const Text("OK"),
onPressed: () => Navigator.pop(context),
)
],
);
},
);
onMatchFound(match);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import 'package:librarian_app/modules/things/providers/things_repository_provide
import 'package:librarian_app/widgets/item_card.dart';

import 'connected_thing_search_field.dart';
import 'existing_item_dialog.dart';
import 'eye_protection_dialog.dart';
import 'suggested_things_dialog.dart';

Expand All @@ -27,33 +26,24 @@ Step buildItemsStep({
ConnectedThingSearchField(
controller: ThingSearchController(
context: context,
onMatchFound: (thing) {
if (items.any((t) => t.id == thing.id)) {
showDialog(
context: context,
builder: (context) {
return ExistingItemDialog(number: thing.number);
},
);
return;
}

onAddItem(thing);
items: items,
onMatchFound: (item) {
onAddItem(item);

if (thing.eyeProtection && !didPromptForEyeProtection) {
if (item.eyeProtection && !didPromptForEyeProtection) {
showDialog(
context: context,
builder: (_) => const EyeProtectionDialog(),
);
onPromptForEyeProtection();
}

if (thing.linkedThingIds.isNotEmpty) {
if (item.linkedThingIds.isNotEmpty) {
showDialog(
context: context,
builder: (_) => SuggestedThingsDialog(
thingName: thing.name,
thingIds: thing.linkedThingIds,
thingName: item.name,
thingIds: item.linkedThingIds,
),
);
}
Expand Down

0 comments on commit 7c1f4cf

Please sign in to comment.