mirror of
https://github.com/austinried/subtracks.git
synced 2026-02-10 23:02:43 +01:00
active source switching and reactivity
This commit is contained in:
63
lib/database/dao/library_dao.dart
Normal file
63
lib/database/dao/library_dao.dart
Normal file
@@ -0,0 +1,63 @@
|
||||
import 'package:drift/drift.dart';
|
||||
|
||||
import '../../sources/models.dart' as models;
|
||||
import '../database.dart';
|
||||
|
||||
part 'library_dao.g.dart';
|
||||
|
||||
@DriftAccessor(include: {'../tables.drift'})
|
||||
class LibraryDao extends DatabaseAccessor<SubtracksDatabase>
|
||||
with _$LibraryDaoMixin {
|
||||
LibraryDao(super.db);
|
||||
|
||||
Future<List<models.Album>> listAlbums({
|
||||
required int limit,
|
||||
required int offset,
|
||||
}) {
|
||||
final query = albums.select()
|
||||
..where(
|
||||
(f) => f.sourceId.equalsExp(
|
||||
subqueryExpression(db.sourcesDao.activeSourceId()),
|
||||
),
|
||||
)
|
||||
..limit(limit, offset: offset);
|
||||
|
||||
return query.get();
|
||||
}
|
||||
|
||||
Future<List<({models.Artist artist, int albumCount})>> listArtists({
|
||||
required int limit,
|
||||
required int offset,
|
||||
}) async {
|
||||
final albumCount = albums.id.count();
|
||||
|
||||
final query =
|
||||
artists.select().join([
|
||||
leftOuterJoin(
|
||||
albums,
|
||||
albums.artistId.equalsExp(artists.id),
|
||||
),
|
||||
])
|
||||
..addColumns([albumCount])
|
||||
..where(
|
||||
artists.sourceId.equalsExp(
|
||||
subqueryExpression(db.sourcesDao.activeSourceId()),
|
||||
) &
|
||||
albums.sourceId.equalsExp(
|
||||
subqueryExpression(db.sourcesDao.activeSourceId()),
|
||||
),
|
||||
)
|
||||
..groupBy([artists.sourceId, artists.id])
|
||||
..orderBy([OrderingTerm.asc(artists.name)])
|
||||
..limit(limit, offset: offset);
|
||||
|
||||
return (await query.get())
|
||||
.map(
|
||||
(row) => (
|
||||
artist: row.readTable(artists),
|
||||
albumCount: row.read(albumCount) ?? 0,
|
||||
),
|
||||
)
|
||||
.toList();
|
||||
}
|
||||
}
|
||||
14
lib/database/dao/library_dao.g.dart
Normal file
14
lib/database/dao/library_dao.g.dart
Normal file
@@ -0,0 +1,14 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'library_dao.dart';
|
||||
|
||||
// ignore_for_file: type=lint
|
||||
mixin _$LibraryDaoMixin on DatabaseAccessor<SubtracksDatabase> {
|
||||
Sources get sources => attachedDatabase.sources;
|
||||
SubsonicSettings get subsonicSettings => attachedDatabase.subsonicSettings;
|
||||
Artists get artists => attachedDatabase.artists;
|
||||
Albums get albums => attachedDatabase.albums;
|
||||
Playlists get playlists => attachedDatabase.playlists;
|
||||
PlaylistSongs get playlistSongs => attachedDatabase.playlistSongs;
|
||||
Songs get songs => attachedDatabase.songs;
|
||||
}
|
||||
46
lib/database/dao/sources_dao.dart
Normal file
46
lib/database/dao/sources_dao.dart
Normal file
@@ -0,0 +1,46 @@
|
||||
import 'package:drift/drift.dart';
|
||||
|
||||
import '../database.dart';
|
||||
|
||||
part 'sources_dao.g.dart';
|
||||
|
||||
@DriftAccessor(include: {'../tables.drift'})
|
||||
class SourcesDao extends DatabaseAccessor<SubtracksDatabase>
|
||||
with _$SourcesDaoMixin {
|
||||
SourcesDao(super.db);
|
||||
|
||||
JoinedSelectStatement<Sources, Source> activeSourceId() {
|
||||
return selectOnly(sources)
|
||||
..addColumns([sources.id])
|
||||
..where(sources.isActive.equals(true));
|
||||
}
|
||||
|
||||
Stream<List<(Source, SubsonicSetting)>> listSources() {
|
||||
final query = select(sources).join([
|
||||
innerJoin(
|
||||
subsonicSettings,
|
||||
sources.id.equalsExp(subsonicSettings.sourceId),
|
||||
),
|
||||
]);
|
||||
|
||||
return query.watch().map(
|
||||
(rows) => rows
|
||||
.map(
|
||||
(row) => (
|
||||
row.readTable(sources),
|
||||
row.readTable(subsonicSettings),
|
||||
),
|
||||
)
|
||||
.toList(),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> setActiveSource(int id) async {
|
||||
await transaction(() async {
|
||||
await db.managers.sources.update((o) => o(isActive: Value(null)));
|
||||
await db.managers.sources
|
||||
.filter((f) => f.id.equals(id))
|
||||
.update((o) => o(isActive: Value(true)));
|
||||
});
|
||||
}
|
||||
}
|
||||
14
lib/database/dao/sources_dao.g.dart
Normal file
14
lib/database/dao/sources_dao.g.dart
Normal file
@@ -0,0 +1,14 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'sources_dao.dart';
|
||||
|
||||
// ignore_for_file: type=lint
|
||||
mixin _$SourcesDaoMixin on DatabaseAccessor<SubtracksDatabase> {
|
||||
Sources get sources => attachedDatabase.sources;
|
||||
SubsonicSettings get subsonicSettings => attachedDatabase.subsonicSettings;
|
||||
Artists get artists => attachedDatabase.artists;
|
||||
Albums get albums => attachedDatabase.albums;
|
||||
Playlists get playlists => attachedDatabase.playlists;
|
||||
PlaylistSongs get playlistSongs => attachedDatabase.playlistSongs;
|
||||
Songs get songs => attachedDatabase.songs;
|
||||
}
|
||||
Reference in New Issue
Block a user