From ea0f7f86aad738744dd3505e2e1908e9a12a9005 Mon Sep 17 00:00:00 2001 From: Elias Projahn Date: Sun, 26 Apr 2020 17:26:19 +0200 Subject: [PATCH] Work editor: Initialize from WorkInfo --- mobile/lib/editors/work.dart | 83 ++++++++-------------------------- mobile/lib/screens/person.dart | 20 ++++---- mobile/lib/screens/work.dart | 10 ++-- 3 files changed, 36 insertions(+), 77 deletions(-) diff --git a/mobile/lib/editors/work.dart b/mobile/lib/editors/work.dart index 1810f4d..8396ec1 100644 --- a/mobile/lib/editors/work.dart +++ b/mobile/lib/editors/work.dart @@ -157,10 +157,13 @@ class _PartTileState extends State { /// If the user is finished editing, the result will be returned as a [WorkInfo] /// object. class WorkEditor extends StatefulWidget { - final Work work; + /// The work to edit. + /// + /// If this is null, a new work will be created. + final WorkInfo workInfo; WorkEditor({ - this.work, + this.workInfo, }); @override @@ -170,9 +173,6 @@ class WorkEditor extends StatefulWidget { class _WorkEditorState extends State { final titleController = TextEditingController(); - BackendState backend; - - String title = ''; Person composer; List instruments = []; List parts = []; @@ -181,71 +181,26 @@ class _WorkEditorState extends State { void initState() { super.initState(); - if (widget.work != null) { - titleController.text = widget.work.title; - } - } + if (widget.workInfo != null) { + titleController.text = widget.workInfo.work.title; + // TODO: Theoretically this includes the composers of all parts. + composer = widget.workInfo.composers.first; + instruments = List.from(widget.workInfo.instruments); - @override - void didChangeDependencies() { - super.didChangeDependencies(); - - backend = Backend.of(context); - - if (widget.work != null) { - if (widget.work.composer != null) { - () async { - final person = - await backend.db.personById(widget.work.composer).getSingle(); - - // We don't want to override a newly selected composer. - if (composer == null) { - setState(() { - composer = person; - }); - } - }(); + for (final partInfo in widget.workInfo.parts) { + parts.add(PartData( + title: partInfo.work.title, + composer: partInfo.composer, + instruments: List.from(partInfo.instruments), + )); } - - () async { - final selection = - await backend.db.instrumentsByWork(widget.work.id).get(); - - // We don't want to override already selected instruments. - if (instruments.isEmpty) { - setState(() { - instruments = selection; - }); - } - }(); - - () async { - final dbParts = await backend.db.workParts(widget.work.id).get(); - for (final dbPart in dbParts) { - final partInstruments = - await backend.db.instrumentsByWork(dbPart.id).get(); - - Person partComposer; - - if (dbPart.composer != null) { - partComposer = - await backend.db.personById(dbPart.composer).getSingle(); - } - - setState(() { - parts.add(PartData( - title: dbPart.title, - composer: partComposer, - instruments: partInstruments, - )); - }); - } - }(); } } @override Widget build(BuildContext context) { + final backend = Backend.of(context); + final List partTiles = []; for (var i = 0; i < parts.length; i++) { final part = parts[i]; @@ -297,7 +252,7 @@ class _WorkEditorState extends State { FlatButton( child: Text('DONE'), onPressed: () async { - final workId = widget.work?.id ?? generateId(); + final workId = widget?.workInfo?.work?.id ?? generateId(); List partInfos = []; for (var i = 0; i < parts.length; i++) { diff --git a/mobile/lib/screens/person.dart b/mobile/lib/screens/person.dart index c8d68b5..0abf05c 100644 --- a/mobile/lib/screens/person.dart +++ b/mobile/lib/screens/person.dart @@ -16,7 +16,7 @@ class PersonScreen extends StatelessWidget { @override Widget build(BuildContext context) { final backend = Backend.of(context); - + return Scaffold( appBar: AppBar( title: Text('${person.firstName} ${person.lastName}'), @@ -47,14 +47,18 @@ class PersonScreen extends StatelessWidget { final work = snapshot.data[index]; return ListTile( title: Text(work.title), - onTap: () => Navigator.push( - context, - MaterialPageRoute( - builder: (context) => WorkScreen( - work: work, + onTap: () async { + final workInfo = await backend.db.getWorkInfo(work); + + Navigator.push( + context, + MaterialPageRoute( + builder: (context) => WorkScreen( + workInfo: workInfo, + ), ), - ), - ), + ); + }, ); }, ); diff --git a/mobile/lib/screens/work.dart b/mobile/lib/screens/work.dart index e8aed33..46d1d69 100644 --- a/mobile/lib/screens/work.dart +++ b/mobile/lib/screens/work.dart @@ -6,10 +6,10 @@ import '../editors/work.dart'; import '../widgets/texts.dart'; class WorkScreen extends StatelessWidget { - final Work work; + final WorkInfo workInfo; WorkScreen({ - this.work, + this.workInfo, }); @override @@ -18,7 +18,7 @@ class WorkScreen extends StatelessWidget { return Scaffold( appBar: AppBar( - title: Text(work.title), + title: Text(workInfo.work.title), actions: [ IconButton( icon: const Icon(Icons.edit), @@ -27,7 +27,7 @@ class WorkScreen extends StatelessWidget { context, MaterialPageRoute( builder: (context) => WorkEditor( - work: work, + workInfo: workInfo, ), fullscreenDialog: true, ), @@ -37,7 +37,7 @@ class WorkScreen extends StatelessWidget { ], ), body: StreamBuilder>( - stream: backend.db.recordingsByWork(work.id).watch(), + stream: backend.db.recordingsByWork(workInfo.work.id).watch(), builder: (context, snapshot) { if (snapshot.hasData) { return ListView.builder(