Files
minstrel/flutter_client/lib/discover/discover_screen.dart
T
bvandeusen fc0350cb96 feat(flutter): adopt MainAppBarActions in Search, Discover, Playlists, Settings
Search keeps its conditional clear-text button and gains the shared
nav widget after it. Discover/Playlists/Settings get an actions row
they didn't have before. The kebab is now reachable from every
top-level screen, which is the prerequisite for the Admin entry.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-08 21:50:29 -04:00

264 lines
8.9 KiB
Dart

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import '../api/endpoints/discover.dart';
import '../api/errors.dart';
import '../library/library_providers.dart' show dioProvider;
import '../models/lidarr.dart';
import '../shared/widgets/main_app_bar_actions.dart';
import '../theme/theme_extension.dart';
final _discoverApiProvider = FutureProvider<DiscoverApi>((ref) async {
return DiscoverApi(await ref.watch(dioProvider.future));
});
class DiscoverScreen extends ConsumerStatefulWidget {
const DiscoverScreen({super.key});
@override
ConsumerState<DiscoverScreen> createState() => _DiscoverScreenState();
}
class _DiscoverScreenState extends ConsumerState<DiscoverScreen> {
final _ctrl = TextEditingController();
LidarrRequestKind _kind = LidarrRequestKind.artist;
Future<List<LidarrSearchResult>>? _resultsFuture;
@override
void dispose() {
_ctrl.dispose();
super.dispose();
}
void _runSearch() {
final q = _ctrl.text.trim();
if (q.isEmpty) {
setState(() => _resultsFuture = null);
return;
}
setState(() {
_resultsFuture = ref
.read(_discoverApiProvider.future)
.then((api) => api.search(q, _kind));
});
}
Future<void> _request(LidarrSearchResult row) async {
final fs = Theme.of(context).extension<FabledSwordTheme>()!;
try {
final api = await ref.read(_discoverApiProvider.future);
await api.createRequest(
kind: _kind,
artistMbid: _kind == LidarrRequestKind.artist ? row.mbid : row.artistMbid,
artistName: _kind == LidarrRequestKind.artist ? row.name : row.secondaryText,
albumMbid: _kind == LidarrRequestKind.album ? row.mbid : null,
albumTitle: _kind == LidarrRequestKind.album ? row.name : null,
);
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text('Requested: ${row.name}'),
backgroundColor: fs.iron,
));
// Re-run search to refresh the `requested` flag on the row.
_runSearch();
}
} on DioException catch (e) {
if (mounted) {
final code = ApiError.fromDio(e).code;
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text('Request failed: $code'),
backgroundColor: fs.error,
));
}
}
}
@override
Widget build(BuildContext context) {
final fs = Theme.of(context).extension<FabledSwordTheme>()!;
return Scaffold(
backgroundColor: fs.obsidian,
appBar: AppBar(
backgroundColor: fs.obsidian,
elevation: 0,
leading: IconButton(
icon: Icon(Icons.arrow_back, color: fs.parchment),
onPressed: () => context.pop(),
),
title: Text('Discover', style: TextStyle(color: fs.parchment)),
actions: const [MainAppBarActions(currentRoute: '/discover')],
),
body: Column(children: [
Padding(
padding: const EdgeInsets.fromLTRB(16, 8, 16, 4),
child: Row(children: [
Expanded(
child: TextField(
controller: _ctrl,
style: TextStyle(color: fs.parchment),
cursorColor: fs.accent,
onSubmitted: (_) => _runSearch(),
decoration: InputDecoration(
hintText: 'Search Lidarr for new music',
hintStyle: TextStyle(color: fs.ash),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: fs.iron),
),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: fs.accent),
),
suffixIcon: IconButton(
icon: Icon(Icons.search, color: fs.ash),
onPressed: _runSearch,
),
),
),
),
]),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 4),
child: SegmentedButton<LidarrRequestKind>(
segments: const [
ButtonSegment(value: LidarrRequestKind.artist, label: Text('Artists')),
ButtonSegment(value: LidarrRequestKind.album, label: Text('Albums')),
],
selected: {_kind},
onSelectionChanged: (s) {
setState(() {
_kind = s.first;
if (_ctrl.text.trim().isNotEmpty) _runSearch();
});
},
),
),
Expanded(
child: _resultsFuture == null
? Center(
child: Text(
'Type to search, then tap Request to send to Lidarr.',
textAlign: TextAlign.center,
style: TextStyle(color: fs.ash),
),
)
: FutureBuilder<List<LidarrSearchResult>>(
future: _resultsFuture,
builder: (ctx, snap) {
if (snap.connectionState != ConnectionState.done) {
return const Center(child: CircularProgressIndicator());
}
if (snap.hasError) {
final err = snap.error;
final code = err is DioException
? ApiError.fromDio(err).code
: 'unknown';
return Center(
child: Text('Search failed: $code',
style: TextStyle(color: fs.error)),
);
}
final rows = snap.data ?? const [];
if (rows.isEmpty) {
return Center(
child: Text('No matches.',
style: TextStyle(color: fs.ash)),
);
}
return ListView.separated(
itemCount: rows.length,
separatorBuilder: (_, __) =>
Divider(height: 1, color: fs.iron),
itemBuilder: (ctx, i) => _ResultTile(
row: rows[i],
onRequest: () => _request(rows[i]),
),
);
},
),
),
]),
);
}
}
class _ResultTile extends StatelessWidget {
const _ResultTile({required this.row, required this.onRequest});
final LidarrSearchResult row;
final VoidCallback onRequest;
@override
Widget build(BuildContext context) {
final fs = Theme.of(context).extension<FabledSwordTheme>()!;
final disabled = row.inLibrary || row.requested;
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 10),
child: Row(children: [
ClipRRect(
borderRadius: BorderRadius.circular(6),
child: Container(
width: 56,
height: 56,
color: fs.slate,
child: row.imageUrl.isEmpty
? Icon(Icons.album, color: fs.ash)
: Image.network(row.imageUrl, fit: BoxFit.cover,
errorBuilder: (_, __, ___) =>
Icon(Icons.album, color: fs.ash)),
),
),
const SizedBox(width: 12),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(row.name,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(color: fs.parchment, fontSize: 14)),
if (row.secondaryText.isNotEmpty)
Text(row.secondaryText,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(color: fs.ash, fontSize: 12)),
],
),
),
const SizedBox(width: 8),
if (row.inLibrary)
_Pill(label: 'In library', color: fs.ash)
else if (row.requested)
_Pill(label: 'Requested', color: fs.ash)
else
FilledButton(
onPressed: disabled ? null : onRequest,
style: FilledButton.styleFrom(
backgroundColor: fs.accent,
foregroundColor: fs.parchment,
),
child: const Text('Request'),
),
]),
);
}
}
class _Pill extends StatelessWidget {
const _Pill({required this.label, required this.color});
final String label;
final Color color;
@override
Widget build(BuildContext context) {
final fs = Theme.of(context).extension<FabledSwordTheme>()!;
return Container(
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
decoration: BoxDecoration(
color: fs.iron,
borderRadius: BorderRadius.circular(4),
),
child: Text(label, style: TextStyle(color: color, fontSize: 11)),
);
}
}