7a0437087a
Closes the Flutter half of Fable #416. Web got a generalized system-playlist refresh kebab in d12afda; this brings Flutter to parity instead of leaving the affordance web-only. - PlaylistsApi.refreshSystem(variant): POST /api/playlists/system/{for-you|discover}/refresh, maps the underscore model variant to the hyphenated route segment, returns the rotated playlist id. - PlaylistCard: top-right PopupMenuButton on system playlists with a context-labelled "Refresh For You" / "Refresh Discover" item. Calls refreshSystem, invalidates playlistsListProvider (which reconciles the rotated UUID + new tracks), snackbars the result. ScaffoldMessenger captured pre-await. - Tests: kebab present for system, absent for user playlists. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
73 lines
2.7 KiB
Dart
73 lines
2.7 KiB
Dart
import 'package:dio/dio.dart';
|
|
|
|
import '../../models/playlist.dart';
|
|
|
|
/// Wire shape for GET /api/playlists. Server splits owned vs. public so
|
|
/// the UI can present them as different sections; we preserve that split
|
|
/// to give the integrations page room to grow.
|
|
class PlaylistsList {
|
|
const PlaylistsList({required this.owned, required this.public});
|
|
final List<Playlist> owned;
|
|
final List<Playlist> public;
|
|
|
|
factory PlaylistsList.empty() =>
|
|
const PlaylistsList(owned: [], public: []);
|
|
|
|
/// Concatenated view for callers that don't care about the split.
|
|
List<Playlist> get all => [...owned, ...public];
|
|
}
|
|
|
|
class PlaylistsApi {
|
|
PlaylistsApi(this._dio);
|
|
final Dio _dio;
|
|
|
|
/// GET /api/playlists?kind=user|system|all. Server returns
|
|
/// `{"owned": [...], "public": [...]}`. Owned is the caller's own
|
|
/// playlists, filtered by the kind param (default "user"). Public
|
|
/// is other users' shared playlists; not filtered by kind.
|
|
Future<PlaylistsList> list({String kind = 'user'}) async {
|
|
final r = await _dio.get<Map<String, dynamic>>(
|
|
'/api/playlists',
|
|
queryParameters: {'kind': kind},
|
|
);
|
|
final body = r.data ?? const <String, dynamic>{};
|
|
List<Playlist> parse(String key) {
|
|
final raw = (body[key] as List?) ?? const [];
|
|
return raw
|
|
.map((e) => Playlist.fromJson((e as Map).cast<String, dynamic>()))
|
|
.toList(growable: false);
|
|
}
|
|
return PlaylistsList(owned: parse('owned'), public: parse('public'));
|
|
}
|
|
|
|
Future<PlaylistDetail> get(String id) async {
|
|
final r = await _dio.get<Map<String, dynamic>>('/api/playlists/$id');
|
|
return PlaylistDetail.fromJson(r.data ?? const {});
|
|
}
|
|
|
|
/// POST /api/playlists/{id}/tracks. Owner only; server returns the
|
|
/// playlist detail with the new rows.
|
|
Future<void> appendTracks(String playlistId, List<String> trackIds) async {
|
|
await _dio.post<void>(
|
|
'/api/playlists/$playlistId/tracks',
|
|
data: {'track_ids': trackIds},
|
|
);
|
|
}
|
|
|
|
/// POST /api/playlists/system/{variant}/refresh. Synchronously
|
|
/// rebuilds the caller's system playlist for the given variant
|
|
/// ("for_you" | "discover") and returns the new playlist id, or
|
|
/// null when the library is empty so there's nothing to build.
|
|
///
|
|
/// The variant uses underscores in the model (system_variant) but
|
|
/// the route segment is hyphenated ("for-you"), so map here.
|
|
Future<String?> refreshSystem(String variant) async {
|
|
final segment = variant == 'for_you' ? 'for-you' : variant;
|
|
final r = await _dio.post<Map<String, dynamic>>(
|
|
'/api/playlists/system/$segment/refresh',
|
|
data: const <String, dynamic>{},
|
|
);
|
|
return (r.data ?? const {})['playlist_id'] as String?;
|
|
}
|
|
}
|