mirror of
https://github.com/johrpan/musicus_mobile.git
synced 2025-10-26 10:47:25 +01:00
database: Limit to one method parameter
This commit is contained in:
parent
ab5d07afdd
commit
92c4ec932a
4 changed files with 111 additions and 46 deletions
|
|
@ -7,26 +7,68 @@ part 'database.g.dart';
|
||||||
final _random = Random(DateTime.now().millisecondsSinceEpoch);
|
final _random = Random(DateTime.now().millisecondsSinceEpoch);
|
||||||
int generateId() => _random.nextInt(0xFFFFFFFF);
|
int generateId() => _random.nextInt(0xFFFFFFFF);
|
||||||
|
|
||||||
class WorkModel {
|
class WorkPartData {
|
||||||
final Work work;
|
final Work work;
|
||||||
final List<int> instrumentIds;
|
final List<int> instrumentIds;
|
||||||
|
|
||||||
WorkModel({
|
WorkPartData({
|
||||||
@required this.work,
|
this.work,
|
||||||
@required this.instrumentIds,
|
this.instrumentIds,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
factory WorkPartData.fromJson(Map<String, dynamic> json) => WorkPartData(
|
||||||
|
work: Work.fromJson(json['work']),
|
||||||
|
instrumentIds: List<int>.from(json['instrumentIds']),
|
||||||
|
);
|
||||||
|
|
||||||
|
Map<String, dynamic> toJson() => {
|
||||||
|
'work': work.toJson(),
|
||||||
|
'instrumentIds': instrumentIds,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
class PerformanceModel {
|
class WorkData {
|
||||||
final Person person;
|
final WorkPartData data;
|
||||||
final Ensemble ensemble;
|
final List<WorkPartData> partData;
|
||||||
final Instrument role;
|
|
||||||
|
|
||||||
PerformanceModel({
|
WorkData({
|
||||||
this.person,
|
this.data,
|
||||||
this.ensemble,
|
this.partData,
|
||||||
this.role,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
factory WorkData.fromJson(Map<String, dynamic> json) => WorkData(
|
||||||
|
data: WorkPartData.fromJson(json['data']),
|
||||||
|
partData: json['partData']
|
||||||
|
.map<WorkPartData>((j) => WorkPartData.fromJson(j))
|
||||||
|
.toList(),
|
||||||
|
);
|
||||||
|
|
||||||
|
Map<String, dynamic> toJson() => {
|
||||||
|
'data': data.toJson(),
|
||||||
|
'partData': partData.map((d) => d.toJson()).toList(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
class RecordingData {
|
||||||
|
final Recording recording;
|
||||||
|
final List<Performance> performances;
|
||||||
|
|
||||||
|
RecordingData({
|
||||||
|
this.recording,
|
||||||
|
this.performances,
|
||||||
|
});
|
||||||
|
|
||||||
|
factory RecordingData.fromJson(Map<String, dynamic> json) => RecordingData(
|
||||||
|
recording: Recording.fromJson(json['recording']),
|
||||||
|
performances: json['performances']
|
||||||
|
.map<Performance>((j) => Performance.fromJson(j))
|
||||||
|
.toList(),
|
||||||
|
);
|
||||||
|
|
||||||
|
Map<String, dynamic> toJson() => {
|
||||||
|
'recording': recording.toJson(),
|
||||||
|
'performances': performances.map((p) => p.toJson()).toList(),
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@UseMoor(
|
@UseMoor(
|
||||||
|
|
@ -57,25 +99,25 @@ class Database extends _$Database {
|
||||||
await into(instruments).insert(instrument, orReplace: true);
|
await into(instruments).insert(instrument, orReplace: true);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> updateWork(WorkModel model, List<WorkModel> parts) async {
|
Future<void> updateWork(WorkData data) async {
|
||||||
await transaction(() async {
|
await transaction(() async {
|
||||||
final workId = model.work.id;
|
final workId = data.data.work.id;
|
||||||
await (delete(works)..where((w) => w.id.equals(workId))).go();
|
await (delete(works)..where((w) => w.id.equals(workId))).go();
|
||||||
await (delete(works)..where((w) => w.partOf.equals(workId))).go();
|
await (delete(works)..where((w) => w.partOf.equals(workId))).go();
|
||||||
|
|
||||||
Future<void> insertWork(WorkModel model) async {
|
Future<void> insertWork(WorkPartData partData) async {
|
||||||
await into(works).insert(model.work);
|
await into(works).insert(partData.work);
|
||||||
await batch((b) => b.insertAll(
|
await batch((b) => b.insertAll(
|
||||||
instrumentations,
|
instrumentations,
|
||||||
model.instrumentIds
|
partData.instrumentIds
|
||||||
.map((id) =>
|
.map((id) =>
|
||||||
Instrumentation(work: model.work.id, instrument: id))
|
Instrumentation(work: partData.work.id, instrument: id))
|
||||||
.toList()));
|
.toList()));
|
||||||
}
|
}
|
||||||
|
|
||||||
await insertWork(model);
|
await insertWork(data.data);
|
||||||
for (final part in parts) {
|
for (final partData in data.partData) {
|
||||||
await insertWork(part);
|
await insertWork(partData);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -84,20 +126,14 @@ class Database extends _$Database {
|
||||||
await into(ensembles).insert(ensemble, orReplace: true);
|
await into(ensembles).insert(ensemble, orReplace: true);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> updateRecording(
|
Future<void> updateRecording(RecordingData data) async {
|
||||||
Recording recording, List<PerformanceModel> models) async {
|
|
||||||
await transaction(() async {
|
await transaction(() async {
|
||||||
await (delete(performances)
|
await (delete(performances)
|
||||||
..where((p) => p.recording.equals(recording.id)))
|
..where((p) => p.recording.equals(data.recording.id)))
|
||||||
.go();
|
.go();
|
||||||
await into(recordings).insert(recording, orReplace: true);
|
await into(recordings).insert(data.recording, orReplace: true);
|
||||||
for (final model in models) {
|
for (final performance in data.performances) {
|
||||||
await into(performances).insert(Performance(
|
await into(performances).insert(performance);
|
||||||
recording: recording.id,
|
|
||||||
person: model.person?.id,
|
|
||||||
ensemble: model.ensemble?.id,
|
|
||||||
role: model.role?.id,
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ class _RecordingEditorState extends State<RecordingEditor> {
|
||||||
final commentController = TextEditingController();
|
final commentController = TextEditingController();
|
||||||
|
|
||||||
Work work;
|
Work work;
|
||||||
List<PerformanceModel> performances = [];
|
List<PerformanceModel> performanceModels = [];
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
|
|
@ -64,7 +64,20 @@ class _RecordingEditorState extends State<RecordingEditor> {
|
||||||
comment: commentController.text,
|
comment: commentController.text,
|
||||||
);
|
);
|
||||||
|
|
||||||
await backend.db.updateRecording(recording, performances);
|
final performances = performanceModels
|
||||||
|
.map((m) => Performance(
|
||||||
|
recording: recording.id,
|
||||||
|
person: m.person?.id,
|
||||||
|
ensemble: m.ensemble?.id,
|
||||||
|
role: m.role?.id,
|
||||||
|
))
|
||||||
|
.toList();
|
||||||
|
|
||||||
|
await backend.db.updateRecording(RecordingData(
|
||||||
|
recording: recording,
|
||||||
|
performances: performances,
|
||||||
|
));
|
||||||
|
|
||||||
Navigator.pop(context, recording);
|
Navigator.pop(context, recording);
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
@ -111,13 +124,13 @@ class _RecordingEditorState extends State<RecordingEditor> {
|
||||||
|
|
||||||
if (model != null) {
|
if (model != null) {
|
||||||
setState(() {
|
setState(() {
|
||||||
performances.add(model);
|
performanceModels.add(model);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
for (final performance in performances)
|
for (final performance in performanceModels)
|
||||||
ListTile(
|
ListTile(
|
||||||
title: Text(performance.person != null
|
title: Text(performance.person != null
|
||||||
? '${performance.person.firstName} ${performance.person.lastName}'
|
? '${performance.person.firstName} ${performance.person.lastName}'
|
||||||
|
|
@ -128,7 +141,7 @@ class _RecordingEditorState extends State<RecordingEditor> {
|
||||||
icon: const Icon(Icons.delete),
|
icon: const Icon(Icons.delete),
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
setState(() {
|
setState(() {
|
||||||
performances.remove(performance);
|
performanceModels.remove(performance);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
|
|
||||||
|
|
@ -295,7 +295,7 @@ class _WorkEditorState extends State<WorkEditor> {
|
||||||
onPressed: () async {
|
onPressed: () async {
|
||||||
final workId = widget.work?.id ?? generateId();
|
final workId = widget.work?.id ?? generateId();
|
||||||
|
|
||||||
final model = WorkModel(
|
final data = WorkPartData(
|
||||||
work: Work(
|
work: Work(
|
||||||
id: workId,
|
id: workId,
|
||||||
title: titleController.text,
|
title: titleController.text,
|
||||||
|
|
@ -304,10 +304,10 @@ class _WorkEditorState extends State<WorkEditor> {
|
||||||
instrumentIds: instruments.map((i) => i.id).toList(),
|
instrumentIds: instruments.map((i) => i.id).toList(),
|
||||||
);
|
);
|
||||||
|
|
||||||
final List<WorkModel> partModels = [];
|
final List<WorkPartData> partData = [];
|
||||||
for (var i = 0; i < parts.length; i++) {
|
for (var i = 0; i < parts.length; i++) {
|
||||||
final part = parts[i];
|
final part = parts[i];
|
||||||
partModels.add(WorkModel(
|
partData.add(WorkPartData(
|
||||||
work: Work(
|
work: Work(
|
||||||
id: generateId(),
|
id: generateId(),
|
||||||
title: part.titleController.text,
|
title: part.titleController.text,
|
||||||
|
|
@ -319,8 +319,12 @@ class _WorkEditorState extends State<WorkEditor> {
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
await backend.db.updateWork(model, partModels);
|
await backend.db.updateWork(WorkData(
|
||||||
Navigator.pop(context, model.work);
|
data: data,
|
||||||
|
partData: partData,
|
||||||
|
));
|
||||||
|
|
||||||
|
Navigator.pop(context, data.work);
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,18 @@ import '../editors/person.dart';
|
||||||
|
|
||||||
import 'instruments.dart';
|
import 'instruments.dart';
|
||||||
|
|
||||||
|
class PerformanceModel {
|
||||||
|
final Person person;
|
||||||
|
final Ensemble ensemble;
|
||||||
|
final Instrument role;
|
||||||
|
|
||||||
|
PerformanceModel({
|
||||||
|
this.person,
|
||||||
|
this.ensemble,
|
||||||
|
this.role,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
class PerformerSelector extends StatefulWidget {
|
class PerformerSelector extends StatefulWidget {
|
||||||
@override
|
@override
|
||||||
_PerformerSelectorState createState() => _PerformerSelectorState();
|
_PerformerSelectorState createState() => _PerformerSelectorState();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue