🐛 Bug fixes on library screen

This commit is contained in:
2025-12-20 01:40:52 +08:00
parent f3af2cf241
commit 9ce0a04fc5

View File

@@ -362,17 +362,53 @@ class LibraryScreen extends HookConsumerWidget {
return StreamBuilder<List<Track>>(
stream: repo.watchAllTracks(),
builder: (context, snapshot) {
if (snapshot.hasError) {
return Center(child: Text('Error: ${snapshot.error}'));
}
if (!snapshot.hasData) {
return const Center(child: CircularProgressIndicator());
}
// Calculate hintText
String hintText;
if (!snapshot.hasData || snapshot.hasError) {
hintText = 'Search tracks...';
} else {
final tracks = snapshot.data!;
if (tracks.isEmpty) {
return const Center(child: Text('No tracks yet. Add some!'));
final totalTracks = tracks.length;
if (searchQuery.value.isEmpty) {
hintText = 'Search tracks... ($totalTracks tracks)';
} else {
final query = searchQuery.value.toLowerCase();
final filteredCount = tracks.where((track) {
if (track.title.toLowerCase().contains(query)) return true;
if (track.artist?.toLowerCase().contains(query) ?? false) {
return true;
}
if (track.album?.toLowerCase().contains(query) ?? false) {
return true;
}
if (track.lyrics != null) {
try {
final lyricsData = LyricsData.fromJsonString(track.lyrics!);
for (final line in lyricsData.lines) {
if (line.text.toLowerCase().contains(query)) return true;
}
} catch (e) {
// Ignore parsing errors
}
}
return false;
}).length;
hintText =
'Search tracks... ($filteredCount of $totalTracks tracks)';
}
}
// Determine main content
Widget mainContent;
if (snapshot.hasError) {
mainContent = Center(child: Text('Error: ${snapshot.error}'));
} else if (!snapshot.hasData) {
mainContent = const Center(child: CircularProgressIndicator());
} else {
final tracks = snapshot.data!;
if (tracks.isEmpty) {
mainContent = const Center(child: Text('No tracks yet. Add some!'));
} else {
List<Track> filteredTracks;
if (searchQuery.value.isEmpty) {
filteredTracks = tracks;
@@ -401,12 +437,11 @@ class LibraryScreen extends HookConsumerWidget {
}
if (filteredTracks.isEmpty && searchQuery.value.isNotEmpty) {
return const Center(child: Text('No tracks match your search.'));
}
return Stack(
children: [
ListView.builder(
mainContent = const Center(
child: Text('No tracks match your search.'),
);
} else {
mainContent = ListView.builder(
padding: EdgeInsets.only(
bottom: 72 + MediaQuery.paddingOf(context).bottom,
top: 80,
@@ -458,11 +493,13 @@ class LibraryScreen extends HookConsumerWidget {
),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(false),
onPressed: () =>
Navigator.of(context).pop(false),
child: const Text('Cancel'),
),
TextButton(
onPressed: () => Navigator.of(context).pop(true),
onPressed: () =>
Navigator.of(context).pop(true),
style: TextButton.styleFrom(
foregroundColor: Colors.red,
),
@@ -497,7 +534,14 @@ class LibraryScreen extends HookConsumerWidget {
),
);
},
),
);
}
}
}
return Stack(
children: [
mainContent,
Positioned(
top: 0,
left: 0,
@@ -506,7 +550,7 @@ class LibraryScreen extends HookConsumerWidget {
padding: EdgeInsets.symmetric(horizontal: 24, vertical: 16),
child: SearchBar(
onChanged: (value) => searchQuery.value = value,
hintText: 'Search tracks...',
hintText: hintText,
leading: const Icon(Icons.search),
padding: WidgetStatePropertyAll(
EdgeInsets.symmetric(horizontal: 24),