Skip to content

Commit

Permalink
save lastUpdated into bank
Browse files Browse the repository at this point in the history
  • Loading branch information
RedyAu committed Nov 25, 2024
1 parent ebc9af8 commit 4e72403
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 21 deletions.
10 changes: 10 additions & 0 deletions lib/services/bank/bank_updated.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'package:drift/drift.dart';
import 'package:lyric/data/database.dart';

import '../../data/bank/bank.dart';

Future setAsUpdatedNow(Bank bank) {
return (db.banks.update()..where((b) => b.id.equals(bank.id))).write(BanksCompanion(
lastUpdated: Value(DateTime.now()),
));
}
50 changes: 29 additions & 21 deletions lib/services/songs/update.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:drift/drift.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:lyric/data/database.dart';
import 'package:lyric/services/bank/bank_updated.dart';
import 'package:queue/queue.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

Expand Down Expand Up @@ -44,44 +45,51 @@ Stream<({int toUpdateCount, int updatedCount})> updateBank(Bank bank) async* {
// stay in indefinite loading state until we know protosong count
// return protosong count for display
List<ProtoSong> toUpdate = await bank.getProtoSongs(since: bank.lastUpdated);
bool hadErrors = false; // todo proper logging across app

print(
' Updating bank ${bank.name} with ${toUpdate.length} new or updated songs since ${bank.lastUpdated}');

if (toUpdate.isEmpty) {
/*if (toUpdate.isEmpty) {
print(' No new songs to update');
yield (toUpdateCount: 0, updatedCount: 0);
return;
}
}*/

yield (toUpdateCount: toUpdate.length, updatedCount: 0);

final Queue queue = Queue(parallel: 10);
for (var protoSong in toUpdate) {
queue.add(() async {
try {
Song song = await bank.getSongDetails(protoSong.uuid);
if (toUpdate.isNotEmpty) {
final Queue queue = Queue(parallel: 10);
for (var protoSong in toUpdate) {
queue.add(() async {
try {
db
.into(db.songs)
.insert(song, mode: InsertMode.insertOrReplace); // todo handle user modified data, etc
} catch (f) {
print('Error while writing song ${protoSong.uuid} to database:\n$f');
Song song = await bank.getSongDetails(protoSong.uuid);
try {
db
.into(db.songs)
.insert(song, mode: InsertMode.insertOrReplace); // todo handle user modified data, etc
} catch (f) {
print('Error while writing song ${protoSong.uuid} to database:\n$f');
hadErrors = true;
}
} catch (e) {
print('Error while fetching details for song ${protoSong.uuid}:\n$e'); // todo log/ui
hadErrors = true;
}
} catch (e) {
print('Error while fetching details for song ${protoSong.uuid}:\n$e'); // todo log/ui
}
});
}
});
}

queue.onComplete.then((v) => print(' Queue completed: $v'));
queue.onComplete.then((v) => print(' Queue completed: $v'));

await for (int remaining in queue.remainingItems) {
yield (toUpdateCount: toUpdate.length, updatedCount: toUpdate.length - remaining);
if (remaining == 0) break;
await for (int remaining in queue.remainingItems) {
yield (toUpdateCount: toUpdate.length, updatedCount: toUpdate.length - remaining);
if (remaining == 0) break;
}
}

print(' All songs updated');

if (!hadErrors) await setAsUpdatedNow(bank);

return;
}

0 comments on commit 4e72403

Please sign in to comment.