Skip to content

Commit

Permalink
update songs based on lastUpdated at bank
Browse files Browse the repository at this point in the history
  • Loading branch information
RedyAu committed Nov 25, 2024
1 parent f55731e commit 654d289
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
10 changes: 8 additions & 2 deletions lib/data/bank/bank.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:convert';
import 'package:intl/intl.dart';

import 'package:dio/dio.dart';
import 'package:drift/drift.dart';
Expand Down Expand Up @@ -37,8 +38,13 @@ class Bank extends Insertable<Bank> {
this.baseUrl,
);

Future<List<ProtoSong>> getProtoSongs() async {
final resp = await dio.get<String>('$baseUrl/songs');
Future<List<ProtoSong>> getProtoSongs({DateTime? since}) async {
String url = '$baseUrl/songs';
if (since != null) {
String formattedDate = DateFormat('yyyy-MM-dd+HH:mm').format(since);
url += '?c=$formattedDate';
}
final resp = await dio.get<String>(url);
final jsonList = jsonDecode(resp.data ?? "[]") as List;

return jsonList.map((e) => ProtoSong.fromJson(e as Map<String, dynamic>)).toList();
Expand Down
13 changes: 5 additions & 8 deletions lib/services/songs/update.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ double? getProgress(({int toUpdateCount, int updatedCount})? record) {
@riverpod
Stream<Map<Bank, ({int toUpdateCount, int updatedCount})?>> updateAllBanks(Ref ref) async* {
Map<Bank, ({int toUpdateCount, int updatedCount})?> bankStates = Map.fromEntries(
(await db.banks.select().get()).map(
(await (db.banks.select()..where((b) => b.isEnabled)).get()).map(
(e) => MapEntry(e, null),
),
);
Expand All @@ -43,13 +43,10 @@ Stream<Map<Bank, ({int toUpdateCount, int updatedCount})?>> updateAllBanks(Ref r
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> protoSongs = await bank.getProtoSongs();
Iterable<String> existingUuids = (await db.songs.select().get()).map((song) => song.uuid);
List<ProtoSong> toUpdate = await bank.getProtoSongs(since: bank.lastUpdated);

// todo instead supply last update date to api and get new/updated protosongs
Iterable<ProtoSong> toUpdate = protoSongs.where((protoSong) => !existingUuids.contains(protoSong.uuid));

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

if (toUpdate.isEmpty) {
print(' No new songs to update');
Expand Down Expand Up @@ -80,7 +77,7 @@ Stream<({int toUpdateCount, int updatedCount})> updateBank(Bank bank) async* {
queue.onComplete.then((v) => print(' Queue completed: $v'));

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

Expand Down

0 comments on commit 654d289

Please sign in to comment.