mirror of
https://github.com/zarzet/SpotiFLAC-Mobile.git
synced 2026-08-05 02:28:36 +02:00
refactor(dart): consolidate duplicated lookup, filename, and search-provider helpers
This commit is contained in:
@@ -760,30 +760,16 @@ class HistoryDatabase {
|
||||
String column,
|
||||
Iterable<String> rawValues,
|
||||
Map<String, Map<String, dynamic>> destination,
|
||||
) async {
|
||||
final values = rawValues
|
||||
.where((value) => value.isNotEmpty)
|
||||
.toSet()
|
||||
.toList();
|
||||
const chunkSize = 450;
|
||||
for (var start = 0; start < values.length; start += chunkSize) {
|
||||
final end = start + chunkSize < values.length
|
||||
? start + chunkSize
|
||||
: values.length;
|
||||
final chunk = values.sublist(start, end);
|
||||
final placeholders = List.filled(chunk.length, '?').join(',');
|
||||
final rows = await db.rawQuery(
|
||||
'SELECT * FROM history WHERE $column IN ($placeholders) '
|
||||
'ORDER BY downloaded_at DESC',
|
||||
chunk,
|
||||
);
|
||||
for (final row in rows) {
|
||||
final key = row[column] as String?;
|
||||
if (key != null && key.isNotEmpty) {
|
||||
destination.putIfAbsent(key, () => _dbRowToJson(row));
|
||||
}
|
||||
}
|
||||
}
|
||||
) {
|
||||
return sqlite.loadRowsByColumn(
|
||||
db,
|
||||
table: 'history',
|
||||
column: column,
|
||||
rawValues: rawValues,
|
||||
destination: destination,
|
||||
mapRow: _dbRowToJson,
|
||||
orderBy: 'downloaded_at DESC',
|
||||
);
|
||||
}
|
||||
|
||||
final spotifyCandidates = requests.expand(
|
||||
|
||||
@@ -1437,27 +1437,15 @@ class LibraryDatabase {
|
||||
String column,
|
||||
Iterable<String> rawValues,
|
||||
Map<String, Map<String, dynamic>> destination,
|
||||
) async {
|
||||
final values = rawValues
|
||||
.where((value) => value.isNotEmpty)
|
||||
.toSet()
|
||||
.toList();
|
||||
const chunkSize = 450;
|
||||
for (var start = 0; start < values.length; start += chunkSize) {
|
||||
final end = (start + chunkSize).clamp(0, values.length);
|
||||
final chunk = values.sublist(start, end);
|
||||
final placeholders = List.filled(chunk.length, '?').join(',');
|
||||
final rows = await db.rawQuery(
|
||||
'SELECT * FROM library WHERE $column IN ($placeholders)',
|
||||
chunk,
|
||||
);
|
||||
for (final row in rows) {
|
||||
final key = row[column] as String?;
|
||||
if (key != null && key.isNotEmpty) {
|
||||
destination.putIfAbsent(key, () => _dbRowToJson(row));
|
||||
}
|
||||
}
|
||||
}
|
||||
) {
|
||||
return sqlite.loadRowsByColumn(
|
||||
db,
|
||||
table: 'library',
|
||||
column: column,
|
||||
rawValues: rawValues,
|
||||
destination: destination,
|
||||
mapRow: _dbRowToJson,
|
||||
);
|
||||
}
|
||||
|
||||
await Future.wait([
|
||||
|
||||
@@ -59,6 +59,37 @@ Future<void> addColumnIfMissing(
|
||||
}
|
||||
}
|
||||
|
||||
/// Loads rows whose [column] matches any of [rawValues] (chunked IN clauses)
|
||||
/// into [destination], keeping the first row seen per value.
|
||||
Future<void> loadRowsByColumn(
|
||||
DatabaseExecutor db, {
|
||||
required String table,
|
||||
required String column,
|
||||
required Iterable<String> rawValues,
|
||||
required Map<String, Map<String, dynamic>> destination,
|
||||
required Map<String, dynamic> Function(Map<String, Object?> row) mapRow,
|
||||
String? orderBy,
|
||||
}) async {
|
||||
final values = rawValues.where((value) => value.isNotEmpty).toSet().toList();
|
||||
const chunkSize = 450;
|
||||
for (var start = 0; start < values.length; start += chunkSize) {
|
||||
final end = (start + chunkSize).clamp(0, values.length);
|
||||
final chunk = values.sublist(start, end);
|
||||
final placeholders = List.filled(chunk.length, '?').join(',');
|
||||
final rows = await db.rawQuery(
|
||||
'SELECT * FROM $table WHERE $column IN ($placeholders)'
|
||||
'${orderBy == null ? '' : ' ORDER BY $orderBy'}',
|
||||
chunk,
|
||||
);
|
||||
for (final row in rows) {
|
||||
final key = row[column] as String?;
|
||||
if (key != null && key.isNotEmpty) {
|
||||
destination.putIfAbsent(key, () => mapRow(row));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> createPathKeyTable(DatabaseExecutor db, String table) async {
|
||||
await db.execute('''
|
||||
CREATE TABLE IF NOT EXISTS $table (
|
||||
|
||||
Reference in New Issue
Block a user