feat(flutter/models): User, ArtistRef, AlbumRef, TrackRef, HomeData
Field names mirror web/src/lib/api/types.ts and the server JSON shape (internal/api/types.go's HomePayload). Hand-written DTOs per spec section 10 -- codegen revisits at ~30 models. Adopts server contract over plan-body mock: - cover_url (not cover_art_url) for artist/album refs - duration_sec (not duration_ms) for albums and tracks - stream_url, disc_number on TrackRef - sort_name/album_count on ArtistRef; sort_title/year/track_count on AlbumRef HomeData section keys match server: recently_added_albums, rediscover_albums, rediscover_artists, most_played_tracks, last_played_artists.
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
// Mirrors internal/api/types.go AlbumRef and web/src/lib/api/types.ts.
|
||||
//
|
||||
// `cover_url` (NOT cover_art_url) and `duration_sec` (NOT duration_ms) match
|
||||
// the server contract. `year` is omitempty on the server so we keep it
|
||||
// nullable. CoverURL is non-null but may be "" — UI branches on isEmpty.
|
||||
class AlbumRef {
|
||||
const AlbumRef({
|
||||
required this.id,
|
||||
required this.title,
|
||||
required this.artistId,
|
||||
this.sortTitle = '',
|
||||
this.artistName = '',
|
||||
this.year,
|
||||
this.trackCount = 0,
|
||||
this.durationSec = 0,
|
||||
this.coverUrl = '',
|
||||
});
|
||||
|
||||
final String id;
|
||||
final String title;
|
||||
final String sortTitle;
|
||||
final String artistId;
|
||||
final String artistName;
|
||||
final int? year;
|
||||
final int trackCount;
|
||||
final int durationSec;
|
||||
final String coverUrl;
|
||||
|
||||
factory AlbumRef.fromJson(Map<String, dynamic> j) => AlbumRef(
|
||||
id: j['id'] as String,
|
||||
title: j['title'] as String,
|
||||
sortTitle: j['sort_title'] as String? ?? '',
|
||||
artistId: j['artist_id'] as String,
|
||||
artistName: j['artist_name'] as String? ?? '',
|
||||
year: (j['year'] as num?)?.toInt(),
|
||||
trackCount: (j['track_count'] as num?)?.toInt() ?? 0,
|
||||
durationSec: (j['duration_sec'] as num?)?.toInt() ?? 0,
|
||||
coverUrl: j['cover_url'] as String? ?? '',
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
// Mirrors internal/api/types.go ArtistRef and web/src/lib/api/types.ts.
|
||||
//
|
||||
// `cover_url` is the server's field name (NOT cover_art_url). Server emits
|
||||
// "" when the artist has no representative album cover, so we keep it
|
||||
// non-null here and let UI code branch on isEmpty. `sort_name` and
|
||||
// `album_count` are exposed by the server contract; we accept their
|
||||
// absence defensively (older fixtures, partial mocks).
|
||||
class ArtistRef {
|
||||
const ArtistRef({
|
||||
required this.id,
|
||||
required this.name,
|
||||
this.sortName = '',
|
||||
this.albumCount = 0,
|
||||
this.coverUrl = '',
|
||||
});
|
||||
|
||||
final String id;
|
||||
final String name;
|
||||
final String sortName;
|
||||
final int albumCount;
|
||||
final String coverUrl;
|
||||
|
||||
factory ArtistRef.fromJson(Map<String, dynamic> j) => ArtistRef(
|
||||
id: j['id'] as String,
|
||||
name: j['name'] as String,
|
||||
sortName: j['sort_name'] as String? ?? '',
|
||||
albumCount: (j['album_count'] as num?)?.toInt() ?? 0,
|
||||
coverUrl: j['cover_url'] as String? ?? '',
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
import 'album.dart';
|
||||
import 'artist.dart';
|
||||
import 'track.dart';
|
||||
|
||||
// Mirrors internal/api/types.go HomePayload and web/src/lib/api/types.ts.
|
||||
// Section keys MUST stay in sync with the server's JSON tags:
|
||||
// recently_added_albums / rediscover_albums / rediscover_artists /
|
||||
// most_played_tracks / last_played_artists.
|
||||
//
|
||||
// Server contract guarantees all five slices are non-nil at JSON encode
|
||||
// time (empty sections render as []), but we still tolerate missing keys
|
||||
// so partial fixtures and offline-degraded responses can be parsed.
|
||||
class HomeData {
|
||||
const HomeData({
|
||||
required this.recentlyAddedAlbums,
|
||||
required this.rediscoverAlbums,
|
||||
required this.rediscoverArtists,
|
||||
required this.mostPlayedTracks,
|
||||
required this.lastPlayedArtists,
|
||||
});
|
||||
|
||||
final List<AlbumRef> recentlyAddedAlbums;
|
||||
final List<AlbumRef> rediscoverAlbums;
|
||||
final List<ArtistRef> rediscoverArtists;
|
||||
final List<TrackRef> mostPlayedTracks;
|
||||
final List<ArtistRef> lastPlayedArtists;
|
||||
|
||||
factory HomeData.fromJson(Map<String, dynamic> j) => HomeData(
|
||||
recentlyAddedAlbums: _list(j, 'recently_added_albums', AlbumRef.fromJson),
|
||||
rediscoverAlbums: _list(j, 'rediscover_albums', AlbumRef.fromJson),
|
||||
rediscoverArtists: _list(j, 'rediscover_artists', ArtistRef.fromJson),
|
||||
mostPlayedTracks: _list(j, 'most_played_tracks', TrackRef.fromJson),
|
||||
lastPlayedArtists: _list(j, 'last_played_artists', ArtistRef.fromJson),
|
||||
);
|
||||
|
||||
static List<T> _list<T>(
|
||||
Map<String, dynamic> j,
|
||||
String key,
|
||||
T Function(Map<String, dynamic>) parse,
|
||||
) {
|
||||
final raw = j[key] as List? ?? const [];
|
||||
return raw
|
||||
.map((e) => parse((e as Map).cast<String, dynamic>()))
|
||||
.toList(growable: false);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
// Mirrors internal/api/types.go TrackRef and web/src/lib/api/types.ts.
|
||||
//
|
||||
// Server uses `duration_sec` (NOT duration_ms). `track_number` and
|
||||
// `disc_number` are omitempty server-side so they're nullable here.
|
||||
// `stream_url` points at /api/tracks/{id}/stream.
|
||||
class TrackRef {
|
||||
const TrackRef({
|
||||
required this.id,
|
||||
required this.title,
|
||||
required this.albumId,
|
||||
required this.artistId,
|
||||
this.albumTitle = '',
|
||||
this.artistName = '',
|
||||
this.trackNumber,
|
||||
this.discNumber,
|
||||
this.durationSec = 0,
|
||||
this.streamUrl = '',
|
||||
});
|
||||
|
||||
final String id;
|
||||
final String title;
|
||||
final String albumId;
|
||||
final String albumTitle;
|
||||
final String artistId;
|
||||
final String artistName;
|
||||
final int? trackNumber;
|
||||
final int? discNumber;
|
||||
final int durationSec;
|
||||
final String streamUrl;
|
||||
|
||||
factory TrackRef.fromJson(Map<String, dynamic> j) => TrackRef(
|
||||
id: j['id'] as String,
|
||||
title: j['title'] as String,
|
||||
albumId: j['album_id'] as String,
|
||||
albumTitle: j['album_title'] as String? ?? '',
|
||||
artistId: j['artist_id'] as String,
|
||||
artistName: j['artist_name'] as String? ?? '',
|
||||
trackNumber: (j['track_number'] as num?)?.toInt(),
|
||||
discNumber: (j['disc_number'] as num?)?.toInt(),
|
||||
durationSec: (j['duration_sec'] as num?)?.toInt() ?? 0,
|
||||
streamUrl: j['stream_url'] as String? ?? '',
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
// Mirrors internal/api/types.go UserView. The /api/* user shape — narrower
|
||||
// than dbq.User (no password hash, no api_token, no subsonic_password).
|
||||
//
|
||||
// `id` is a pgtype.UUID server-side which marshals to a plain string when
|
||||
// Valid, so we accept it as String here. `is_admin` is always present in
|
||||
// the server response but we tolerate its absence (default false) to keep
|
||||
// older fixtures and partial mocks loadable.
|
||||
class User {
|
||||
const User({
|
||||
required this.id,
|
||||
required this.username,
|
||||
required this.isAdmin,
|
||||
});
|
||||
|
||||
final String id;
|
||||
final String username;
|
||||
final bool isAdmin;
|
||||
|
||||
factory User.fromJson(Map<String, dynamic> j) => User(
|
||||
id: j['id'] as String,
|
||||
username: j['username'] as String,
|
||||
isAdmin: j['is_admin'] as bool? ?? false,
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user