Make work editor modular

The work editor holds all data for a work and its parts. The widgets for
editing work information are reusable now.
This commit is contained in:
Elias Projahn 2019-12-12 14:57:51 +01:00
parent 0d29a5f15d
commit 0594b00199

View file

@ -5,6 +5,84 @@ import '../database.dart';
import '../selectors/instruments.dart'; import '../selectors/instruments.dart';
import '../selectors/person.dart'; import '../selectors/person.dart';
class WorkData {
String title = '';
Person composer;
List<Instrument> instruments = [];
List<WorkData> parts = [];
}
class WorkProperties extends StatelessWidget {
final TextEditingController titleController;
final Person composer;
final List<Instrument> instruments;
final void Function(Person) onComposerChanged;
final void Function(List<Instrument>) onInstrumentsChanged;
WorkProperties({
@required this.titleController,
@required this.composer,
@required this.instruments,
@required this.onComposerChanged,
@required this.onInstrumentsChanged,
});
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(16.0),
child: TextField(
controller: titleController,
decoration: InputDecoration(
labelText: 'Title',
),
),
),
ListTile(
title: Text('Composer'),
subtitle: Text(composer != null
? '${composer.firstName} ${composer.lastName}'
: 'Select composer'),
onTap: () async {
final Person person = await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => PersonsSelector(),
fullscreenDialog: true,
));
if (person != null) {
onComposerChanged(person);
}
},
),
ListTile(
title: Text('Instruments'),
subtitle: Text(instruments.isNotEmpty
? instruments.map((i) => i.name).join(', ')
: 'Select instruments'),
onTap: () async {
final List<Instrument> selection = await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => InstrumentsSelector(
selection: instruments,
),
fullscreenDialog: true,
));
if (selection != null) {
onInstrumentsChanged(selection);
}
},
),
],
);
}
}
class WorkEditor extends StatefulWidget { class WorkEditor extends StatefulWidget {
final Work work; final Work work;
@ -20,8 +98,7 @@ class _WorkEditorState extends State<WorkEditor> {
final titleController = TextEditingController(); final titleController = TextEditingController();
Backend backend; Backend backend;
Person composer; final WorkData data = WorkData();
List<Instrument> instruments = [];
@override @override
void initState() { void initState() {
@ -45,9 +122,9 @@ class _WorkEditorState extends State<WorkEditor> {
await backend.db.personById(widget.work.composer).getSingle(); await backend.db.personById(widget.work.composer).getSingle();
// We don't want to override a newly selected composer. // We don't want to override a newly selected composer.
if (composer != null) { if (data.composer != null) {
setState(() { setState(() {
composer = person; data.composer = person;
}); });
} }
}(); }();
@ -58,9 +135,9 @@ class _WorkEditorState extends State<WorkEditor> {
await backend.db.instrumentsByWork(widget.work.id).get(); await backend.db.instrumentsByWork(widget.work.id).get();
// We don't want to override already selected instruments. // We don't want to override already selected instruments.
if (instruments.isEmpty) { if (data.instruments.isEmpty) {
setState(() { setState(() {
instruments = selection; data.instruments = selection;
}); });
} }
}(); }();
@ -81,55 +158,19 @@ class _WorkEditorState extends State<WorkEditor> {
), ),
body: ListView( body: ListView(
children: <Widget>[ children: <Widget>[
Padding( WorkProperties(
padding: const EdgeInsets.all(16.0), titleController: titleController,
child: TextField( composer: data.composer,
controller: titleController, instruments: data.instruments,
decoration: InputDecoration( onComposerChanged: (composer) {
labelText: 'Title', setState(() {
), data.composer = composer;
), });
),
ListTile(
title: Text('Composer'),
subtitle: Text(composer != null
? '${composer.firstName} ${composer.lastName}'
: 'Select composer'),
onTap: () async {
final Person person = await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => PersonsSelector(),
fullscreenDialog: true,
));
if (person != null) {
setState(() {
composer = person;
});
}
}, },
), onInstrumentsChanged: (instruments) {
ListTile( setState(() {
title: Text('Instruments'), data.instruments = instruments;
subtitle: Text(instruments.isNotEmpty });
? instruments.map((i) => i.name).join(', ')
: 'Select instruments'),
onTap: () async {
final List<Instrument> selection = await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => InstrumentsSelector(
selection: instruments,
),
fullscreenDialog: true,
));
if (selection != null) {
setState(() {
instruments = selection;
});
}
}, },
), ),
], ],