diff --git a/client/lib/src/client.dart b/client/lib/src/client.dart index b3200c2..e8288c0 100644 --- a/client/lib/src/client.dart +++ b/client/lib/src/client.dart @@ -436,7 +436,7 @@ class MusicusClient { )); final json = jsonDecode(response.body); - return json.map((j) => WorkInfo.fromJson(j)).toList(); + return json.map((j) => WorkInfo.fromJson(j, sync: true)).toList(); } /// Get a work by ID. @@ -446,7 +446,7 @@ class MusicusClient { )); final json = jsonDecode(response.body); - return WorkInfo.fromJson(json); + return WorkInfo.fromJson(json, sync: true); } /// Delete a work by ID. @@ -475,7 +475,9 @@ class MusicusClient { )); final json = jsonDecode(response.body); - return json.map((j) => RecordingInfo.fromJson(j)).toList(); + return json + .map((j) => RecordingInfo.fromJson(j, sync: true)) + .toList(); } /// Create or update a work. @@ -570,7 +572,7 @@ class MusicusClient { )); final json = jsonDecode(response.body); - return RecordingInfo.fromJson(json); + return RecordingInfo.fromJson(json, sync: true); } /// Create or update a recording. diff --git a/client/lib/src/database.dart b/client/lib/src/database.dart index 7283f2f..8293adf 100644 --- a/client/lib/src/database.dart +++ b/client/lib/src/database.dart @@ -120,7 +120,7 @@ class MusicusClientDatabase extends _$MusicusClientDatabase { mode: InsertMode.insertOrReplace, ); - if (person.sync) { + if (person.sync && !person.synced) { await client.putPerson(person); await updatePerson(person.copyWith(synced: true)); } @@ -164,7 +164,7 @@ class MusicusClientDatabase extends _$MusicusClientDatabase { mode: InsertMode.insertOrReplace, ); - if (instrument.sync) { + if (instrument.sync && !instrument.synced) { await client.putInstrument(instrument); await updateInstrument(instrument.copyWith(synced: true)); } @@ -296,7 +296,7 @@ class MusicusClientDatabase extends _$MusicusClientDatabase { } }); - if (workInfo.work.sync) { + if (workInfo.work.sync && !workInfo.work.synced) { await client.putWork(workInfo); await into(works) .insertOnConflictUpdate(workInfo.work.copyWith(synced: true)); @@ -343,7 +343,7 @@ class MusicusClientDatabase extends _$MusicusClientDatabase { mode: InsertMode.insertOrReplace, ); - if (ensemble.sync) { + if (ensemble.sync && !ensemble.synced) { await client.putEnsemble(ensemble); await updateEnsemble(ensemble.copyWith(synced: true)); } @@ -397,7 +397,7 @@ class MusicusClientDatabase extends _$MusicusClientDatabase { } }); - if (recordingInfo.recording.sync) { + if (recordingInfo.recording.sync && !recordingInfo.recording.synced) { await client.putRecording(recordingInfo); await into(recordings).insertOnConflictUpdate( recordingInfo.recording.copyWith(synced: true)); diff --git a/client/lib/src/info.dart b/client/lib/src/info.dart index d337b3d..c9bd3bf 100644 --- a/client/lib/src/info.dart +++ b/client/lib/src/info.dart @@ -67,13 +67,30 @@ class WorkInfo { this.sections, }); - factory WorkInfo.fromJson(Map json) => WorkInfo( - work: Work.fromJson(json['work']), + /// Deserialize work info from JSON. + /// + /// If [sync] is set to true, all contained items will have their sync + /// property set to true. + // TODO: Local versions should not be overriden, if their sync property is + // set to false. + factory WorkInfo.fromJson(Map json, {bool sync = false}) => + WorkInfo( + work: Work.fromJson(json['work']).copyWith( + sync: sync, + synced: sync, + ), instruments: json['instruments'] - .map((j) => Instrument.fromJson(j)) + .map((j) => Instrument.fromJson(j).copyWith( + sync: sync, + synced: sync, + )) + .toList(), + composers: json['composers'] + .map((j) => Person.fromJson(j).copyWith( + sync: sync, + synced: sync, + )) .toList(), - composers: - json['composers'].map((j) => Person.fromJson(j)).toList(), parts: json['parts'].map((j) => PartInfo.fromJson(j)).toList(), sections: json['sections'] @@ -111,13 +128,27 @@ class PerformanceInfo { this.role, }); - factory PerformanceInfo.fromJson(Map json) => + factory PerformanceInfo.fromJson(Map json, + {bool sync = false}) => PerformanceInfo( - person: json['person'] != null ? Person.fromJson(json['person']) : null, - ensemble: json['ensemble'] != null - ? Ensemble.fromJson(json['ensemble']) + person: json['person'] != null + ? Person.fromJson(json['person']).copyWith( + sync: sync, + synced: sync, + ) + : null, + ensemble: json['ensemble'] != null + ? Ensemble.fromJson(json['ensemble']).copyWith( + sync: sync, + synced: sync, + ) + : null, + role: json['role'] != null + ? Instrument.fromJson(json['role']).copyWith( + sync: sync, + synced: sync, + ) : null, - role: json['role'] != null ? Instrument.fromJson(json['role']) : null, ); Map toJson() => { @@ -143,10 +174,22 @@ class RecordingInfo { this.performances, }); - factory RecordingInfo.fromJson(Map json) => RecordingInfo( - recording: Recording.fromJson(json['recording']), + /// Deserialize recording info from JSON. + /// + /// If [sync] is set to true, all contained items will have their sync + /// property set to true. + // TODO: Local versions should not be overriden, if their sync property is + // set to false. + factory RecordingInfo.fromJson(Map json, + {bool sync = false}) => + RecordingInfo( + recording: Recording.fromJson(json['recording']).copyWith( + sync: sync, + synced: sync, + ), performances: json['performances'] - .map((j) => PerformanceInfo.fromJson(j)) + .map( + (j) => PerformanceInfo.fromJson(j, sync: sync)) .toList(), );