-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from pvdthings/safeguard-duplicate-thing
Librarian: Safeguard duplicate thing
- Loading branch information
Showing
3 changed files
with
157 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
apps/librarian/lib/src/features/inventory/providers/find_things_by_name.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import 'package:flutter_riverpod/flutter_riverpod.dart'; | ||
import 'package:librarian_app/src/features/inventory/providers/things_repository_provider.dart'; | ||
|
||
import '../models/thing_model.dart'; | ||
|
||
Provider<Future<List<ThingModel>>> findThingsByName(String name) { | ||
return Provider((ref) async => await ref | ||
.read(thingsRepositoryProvider.notifier) | ||
.getThings(filter: name)); | ||
} |
219 changes: 146 additions & 73 deletions
219
apps/librarian/lib/src/features/inventory/widgets/dialogs/create_thing_dialog.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,94 +1,167 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_riverpod/flutter_riverpod.dart'; | ||
import 'package:librarian_app/src/features/inventory/providers/find_things_by_name.dart'; | ||
import 'package:librarian_app/src/widgets/input_decoration.dart'; | ||
|
||
class CreateThingDialog extends StatelessWidget { | ||
CreateThingDialog({super.key, this.onCreate}); | ||
import '../../models/thing_model.dart'; | ||
import '../../providers/things_repository_provider.dart'; | ||
|
||
class CreateThingDialog extends ConsumerStatefulWidget { | ||
const CreateThingDialog({super.key}); | ||
|
||
@override | ||
ConsumerState<ConsumerStatefulWidget> createState() { | ||
return _CreateThingDialogState(); | ||
} | ||
} | ||
|
||
class _CreateThingDialogState extends ConsumerState<CreateThingDialog> { | ||
final _formKey = GlobalKey<FormState>(); | ||
final void Function(String name, String? spanishName)? onCreate; | ||
|
||
final _name = TextEditingController(); | ||
final _spanishName = TextEditingController(); | ||
|
||
Future<List<ThingModel>>? existingMatches; | ||
|
||
createThing() { | ||
ref | ||
.read(thingsRepositoryProvider.notifier) | ||
.createThing(name: _name.text, spanishName: _spanishName.text) | ||
.then((value) { | ||
Navigator.of(context).pop(); | ||
ScaffoldMessenger.of(context).showSnackBar( | ||
SnackBar( | ||
content: Text('${value.name} created'), | ||
), | ||
); | ||
}); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Dialog( | ||
child: Container( | ||
padding: const EdgeInsets.all(16), | ||
child: IntrinsicWidth( | ||
child: Form( | ||
key: _formKey, | ||
autovalidateMode: AutovalidateMode.onUserInteraction, | ||
child: Column( | ||
mainAxisSize: MainAxisSize.min, | ||
crossAxisAlignment: CrossAxisAlignment.start, | ||
children: [ | ||
Row( | ||
mainAxisAlignment: MainAxisAlignment.spaceBetween, | ||
children: [ | ||
Text( | ||
'New Thing', | ||
style: Theme.of(context).textTheme.headlineMedium, | ||
), | ||
IconButton( | ||
onPressed: () { | ||
Navigator.of(context).pop(); | ||
}, | ||
icon: const Icon(Icons.close), | ||
), | ||
], | ||
), | ||
const SizedBox(height: 16), | ||
TextFormField( | ||
controller: _name, | ||
validator: (value) { | ||
if (value == null || value.isEmpty) { | ||
return 'Name is required'; | ||
return AlertDialog( | ||
icon: const Icon(Icons.build), | ||
title: const Text('Create New Thing'), | ||
actions: [ | ||
OutlinedButton( | ||
onPressed: () { | ||
Navigator.of(context).pop(); | ||
}, | ||
child: const Text('Cancel'), | ||
), | ||
ListenableBuilder( | ||
listenable: _name, | ||
builder: (context, child) => FilledButton( | ||
onPressed: _name.text.isNotEmpty | ||
? () { | ||
if (_formKey.currentState!.validate()) { | ||
createThing(); | ||
} | ||
} | ||
: null, | ||
child: const Text('Create'), | ||
), | ||
), | ||
], | ||
contentPadding: const EdgeInsets.all(16), | ||
content: SizedBox( | ||
width: 500, | ||
child: Form( | ||
key: _formKey, | ||
autovalidateMode: AutovalidateMode.onUserInteraction, | ||
child: Column( | ||
mainAxisSize: MainAxisSize.min, | ||
crossAxisAlignment: CrossAxisAlignment.start, | ||
children: [ | ||
TextFormField( | ||
controller: _name, | ||
validator: (value) { | ||
if (value == null || value.isEmpty) { | ||
return 'Name is required'; | ||
} | ||
|
||
return null; | ||
}, | ||
decoration: inputDecoration.copyWith( | ||
labelText: 'Name', | ||
icon: const Icon(Icons.build), | ||
constraints: const BoxConstraints(minWidth: 500), | ||
), | ||
), | ||
const SizedBox(height: 16), | ||
TextFormField( | ||
controller: _spanishName, | ||
decoration: inputDecoration.copyWith( | ||
labelText: 'Name (Spanish)', | ||
icon: const Icon(Icons.build), | ||
constraints: const BoxConstraints(minWidth: 500), | ||
), | ||
return null; | ||
}, | ||
decoration: inputDecoration.copyWith( | ||
labelText: 'Name', | ||
constraints: const BoxConstraints(minWidth: 500), | ||
), | ||
const SizedBox(height: 32), | ||
Row( | ||
mainAxisAlignment: MainAxisAlignment.end, | ||
children: [ | ||
OutlinedButton( | ||
onPressed: () { | ||
Navigator.of(context).pop(); | ||
}, | ||
child: const Text('Cancel'), | ||
), | ||
const SizedBox(width: 16), | ||
FilledButton( | ||
onPressed: () { | ||
if (_formKey.currentState!.validate()) { | ||
onCreate?.call(_name.text, _spanishName.text); | ||
} | ||
}, | ||
child: const Text('Create'), | ||
), | ||
], | ||
onChanged: (value) { | ||
if (value.isEmpty || value.length < 4) { | ||
return; | ||
} | ||
|
||
setState(() { | ||
existingMatches = ref.read(findThingsByName(value)); | ||
}); | ||
}, | ||
), | ||
const SizedBox(height: 16), | ||
TextFormField( | ||
controller: _spanishName, | ||
decoration: inputDecoration.copyWith( | ||
labelText: 'Name (Spanish)', | ||
constraints: const BoxConstraints(minWidth: 500), | ||
), | ||
], | ||
), | ||
), | ||
const SizedBox(height: 16), | ||
FutureBuilder( | ||
future: existingMatches, | ||
builder: (context, snapshot) { | ||
final existingThingName = snapshot.data?.firstOrNull?.name; | ||
if (existingThingName != null) { | ||
return _ExistingThingWarning( | ||
thingName: _name.text, | ||
existingThingName: existingThingName, | ||
); | ||
} | ||
|
||
return const SizedBox.shrink(); | ||
}, | ||
), | ||
], | ||
), | ||
), | ||
), | ||
); | ||
} | ||
} | ||
|
||
class _ExistingThingWarning extends StatelessWidget { | ||
const _ExistingThingWarning({ | ||
required this.thingName, | ||
required this.existingThingName, | ||
}); | ||
|
||
final String thingName; | ||
final String existingThingName; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Card( | ||
clipBehavior: Clip.antiAlias, | ||
margin: EdgeInsets.zero, | ||
child: ListTile( | ||
leading: const Icon( | ||
Icons.warning_rounded, | ||
color: Colors.amber, | ||
), | ||
title: const Text('Thing Already Exists'), | ||
subtitle: Text.rich( | ||
TextSpan( | ||
children: [ | ||
const TextSpan(text: 'A thing named '), | ||
TextSpan(text: thingName, style: boldTextStyle), | ||
const TextSpan(text: ' might already exist.\n'), | ||
const TextSpan(text: 'Is '), | ||
TextSpan(text: existingThingName, style: boldTextStyle), | ||
const TextSpan(text: ' the same thing?'), | ||
], | ||
), | ||
), | ||
), | ||
); | ||
} | ||
} | ||
|
||
const boldTextStyle = TextStyle(fontWeight: FontWeight.bold); |