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/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 {
final Work work;
@ -20,8 +98,7 @@ class _WorkEditorState extends State<WorkEditor> {
final titleController = TextEditingController();
Backend backend;
Person composer;
List<Instrument> instruments = [];
final WorkData data = WorkData();
@override
void initState() {
@ -45,9 +122,9 @@ class _WorkEditorState extends State<WorkEditor> {
await backend.db.personById(widget.work.composer).getSingle();
// We don't want to override a newly selected composer.
if (composer != null) {
if (data.composer != null) {
setState(() {
composer = person;
data.composer = person;
});
}
}();
@ -58,9 +135,9 @@ class _WorkEditorState extends State<WorkEditor> {
await backend.db.instrumentsByWork(widget.work.id).get();
// We don't want to override already selected instruments.
if (instruments.isEmpty) {
if (data.instruments.isEmpty) {
setState(() {
instruments = selection;
data.instruments = selection;
});
}
}();
@ -81,55 +158,19 @@ class _WorkEditorState extends State<WorkEditor> {
),
body: ListView(
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) {
setState(() {
composer = person;
});
}
WorkProperties(
titleController: titleController,
composer: data.composer,
instruments: data.instruments,
onComposerChanged: (composer) {
setState(() {
data.composer = composer;
});
},
),
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) {
setState(() {
instruments = selection;
});
}
onInstrumentsChanged: (instruments) {
setState(() {
data.instruments = instruments;
});
},
),
],