🎉 Initial Commit
This commit is contained in:
557
lib/ui/screens/library_screen.dart
Normal file
557
lib/ui/screens/library_screen.dart
Normal file
@@ -0,0 +1,557 @@
|
||||
import 'dart:io';
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_hooks/flutter_hooks.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
|
||||
import '../../data/track_repository.dart';
|
||||
import '../../providers/audio_provider.dart';
|
||||
import '../../data/playlist_repository.dart';
|
||||
import '../../data/db.dart';
|
||||
import '../tabs/albums_tab.dart';
|
||||
import '../tabs/playlists_tab.dart';
|
||||
|
||||
class LibraryScreen extends HookConsumerWidget {
|
||||
const LibraryScreen({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
// We can define a stream provider locally or in repository file.
|
||||
// For now, using StreamBuilder is easiest since `watchAllTracks` returns a Stream.
|
||||
// Or better: `ref.watch(trackListStreamProvider)`.
|
||||
|
||||
// Let's assume we use StreamBuilder for now to avoid creating another file/provider on the fly.
|
||||
final repo = ref.watch(trackRepositoryProvider.notifier);
|
||||
final selectedTrackIds = useState<Set<int>>({});
|
||||
final isSelectionMode = selectedTrackIds.value.isNotEmpty;
|
||||
|
||||
void toggleSelection(int id) {
|
||||
final newSet = Set<int>.from(selectedTrackIds.value);
|
||||
if (newSet.contains(id)) {
|
||||
newSet.remove(id);
|
||||
} else {
|
||||
newSet.add(id);
|
||||
}
|
||||
selectedTrackIds.value = newSet;
|
||||
}
|
||||
|
||||
void clearSelection() {
|
||||
selectedTrackIds.value = {};
|
||||
}
|
||||
|
||||
return DefaultTabController(
|
||||
length: 3,
|
||||
child: Scaffold(
|
||||
appBar: isSelectionMode
|
||||
? AppBar(
|
||||
leading: IconButton(
|
||||
icon: const Icon(Icons.close),
|
||||
onPressed: clearSelection,
|
||||
),
|
||||
title: Text('${selectedTrackIds.value.length} selected'),
|
||||
backgroundColor: Theme.of(context).primaryColorDark,
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.playlist_add),
|
||||
tooltip: 'Add to Playlist',
|
||||
onPressed: () {
|
||||
_batchAddToPlaylist(
|
||||
context,
|
||||
ref,
|
||||
selectedTrackIds.value.toList(),
|
||||
clearSelection,
|
||||
);
|
||||
},
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.delete),
|
||||
tooltip: 'Delete',
|
||||
onPressed: () {
|
||||
_batchDelete(
|
||||
context,
|
||||
ref,
|
||||
selectedTrackIds.value.toList(),
|
||||
clearSelection,
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
)
|
||||
: AppBar(
|
||||
title: const Text('Library'),
|
||||
bottom: const TabBar(
|
||||
tabs: [
|
||||
Tab(text: 'Tracks', icon: Icon(Icons.audiotrack)),
|
||||
Tab(text: 'Albums', icon: Icon(Icons.album)),
|
||||
Tab(text: 'Playlists', icon: Icon(Icons.queue_music)),
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.add_circle_outline),
|
||||
tooltip: 'Add Tracks',
|
||||
onPressed: () async {
|
||||
final result = await FilePicker.platform.pickFiles(
|
||||
type: FileType.audio,
|
||||
allowMultiple: true,
|
||||
);
|
||||
if (result != null) {
|
||||
// Collect paths
|
||||
final paths = result.files
|
||||
.map((f) => f.path)
|
||||
.whereType<String>()
|
||||
.toList();
|
||||
if (paths.isNotEmpty) {
|
||||
await repo.importFiles(paths);
|
||||
}
|
||||
}
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
body: TabBarView(
|
||||
children: [
|
||||
// Tracks Tab (Existing Logic)
|
||||
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());
|
||||
}
|
||||
final tracks = snapshot.data!;
|
||||
if (tracks.isEmpty) {
|
||||
return const Center(child: Text('No tracks yet. Add some!'));
|
||||
}
|
||||
|
||||
return ListView.builder(
|
||||
itemCount: tracks.length,
|
||||
itemBuilder: (context, index) {
|
||||
final track = tracks[index];
|
||||
final isSelected = selectedTrackIds.value.contains(
|
||||
track.id,
|
||||
);
|
||||
|
||||
if (isSelectionMode) {
|
||||
return ListTile(
|
||||
selected: isSelected,
|
||||
selectedTileColor: Colors.white10,
|
||||
leading: Checkbox(
|
||||
value: isSelected,
|
||||
onChanged: (_) => toggleSelection(track.id),
|
||||
),
|
||||
title: Text(
|
||||
track.title,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
subtitle: Text(
|
||||
'${track.artist ?? 'Unknown Artist'} • ${_formatDuration(track.duration)}',
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
onTap: () => toggleSelection(track.id),
|
||||
);
|
||||
}
|
||||
|
||||
return Dismissible(
|
||||
key: Key('track_${track.id}'),
|
||||
direction: DismissDirection.endToStart,
|
||||
background: Container(
|
||||
color: Colors.red,
|
||||
alignment: Alignment.centerRight,
|
||||
padding: const EdgeInsets.only(right: 20),
|
||||
child: const Icon(Icons.delete, color: Colors.white),
|
||||
),
|
||||
confirmDismiss: (direction) async {
|
||||
return await showDialog(
|
||||
context: context,
|
||||
builder: (context) {
|
||||
return AlertDialog(
|
||||
title: const Text('Delete Track?'),
|
||||
content: Text(
|
||||
'Are you sure you want to delete "${track.title}"? This cannot be undone.',
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () =>
|
||||
Navigator.of(context).pop(false),
|
||||
child: const Text('Cancel'),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () =>
|
||||
Navigator.of(context).pop(true),
|
||||
style: TextButton.styleFrom(
|
||||
foregroundColor: Colors.red,
|
||||
),
|
||||
child: const Text('Delete'),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
onDismissed: (direction) {
|
||||
ref
|
||||
.read(trackRepositoryProvider.notifier)
|
||||
.deleteTrack(track.id);
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('Deleted "${track.title}"')),
|
||||
);
|
||||
},
|
||||
child: ListTile(
|
||||
leading: AspectRatio(
|
||||
aspectRatio: 1,
|
||||
child: Container(
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.grey[800],
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
image: track.artUri != null
|
||||
? DecorationImage(
|
||||
image: FileImage(File(track.artUri!)),
|
||||
fit: BoxFit.cover,
|
||||
)
|
||||
: null,
|
||||
),
|
||||
child: track.artUri == null
|
||||
? const Icon(
|
||||
Icons.music_note,
|
||||
color: Colors.white54,
|
||||
)
|
||||
: null,
|
||||
),
|
||||
),
|
||||
title: Text(
|
||||
track.title,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
subtitle: Text(
|
||||
'${track.artist ?? 'Unknown Artist'} • ${_formatDuration(track.duration)}',
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
trailing: isSelectionMode
|
||||
? null
|
||||
: IconButton(
|
||||
icon: const Icon(Icons.more_vert),
|
||||
onPressed: () {
|
||||
_showTrackOptions(context, ref, track);
|
||||
},
|
||||
),
|
||||
onTap: () {
|
||||
final audio = ref.read(audioHandlerProvider);
|
||||
audio.setSource(track.path);
|
||||
audio.play();
|
||||
},
|
||||
onLongPress: () {
|
||||
// Enter selection mode
|
||||
toggleSelection(track.id);
|
||||
},
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
),
|
||||
|
||||
// Albums Tab
|
||||
const AlbumsTab(),
|
||||
|
||||
// Playlists Tab
|
||||
const PlaylistsTab(),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _showTrackOptions(BuildContext context, WidgetRef ref, Track track) {
|
||||
showModalBottomSheet(
|
||||
context: context,
|
||||
builder: (context) {
|
||||
return SafeArea(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
ListTile(
|
||||
leading: const Icon(Icons.playlist_add),
|
||||
title: const Text('Add to Playlist'),
|
||||
onTap: () {
|
||||
Navigator.pop(context);
|
||||
_showAddToPlaylistDialog(context, ref, track);
|
||||
},
|
||||
),
|
||||
ListTile(
|
||||
leading: const Icon(Icons.edit),
|
||||
title: const Text('Edit Metadata'),
|
||||
onTap: () {
|
||||
Navigator.pop(context);
|
||||
_showEditDialog(context, ref, track);
|
||||
},
|
||||
),
|
||||
ListTile(
|
||||
leading: const Icon(Icons.delete, color: Colors.red),
|
||||
title: const Text(
|
||||
'Delete Track',
|
||||
style: TextStyle(color: Colors.red),
|
||||
),
|
||||
onTap: () {
|
||||
Navigator.pop(context);
|
||||
ref
|
||||
.read(trackRepositoryProvider.notifier)
|
||||
.deleteTrack(track.id);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
void _showAddToPlaylistDialog(
|
||||
BuildContext context,
|
||||
WidgetRef ref,
|
||||
Track track,
|
||||
) {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) {
|
||||
// Fetch playlists
|
||||
// Note: Using a hook/provider inside dialog builder might need a Consumer or similar if stream updates.
|
||||
// For simplicity, we'll assume the user wants to pick from *current* playlists.
|
||||
// Or we can use a Consumer widget inside the dialog.
|
||||
return AlertDialog(
|
||||
title: const Text('Add to Playlist'),
|
||||
content: SizedBox(
|
||||
width: double.maxFinite,
|
||||
child: Consumer(
|
||||
builder: (context, ref, child) {
|
||||
final playlistsAsync = ref
|
||||
.watch(playlistRepositoryProvider.notifier)
|
||||
.watchAllPlaylists();
|
||||
return StreamBuilder<List<Playlist>>(
|
||||
stream: playlistsAsync,
|
||||
builder: (context, snapshot) {
|
||||
if (!snapshot.hasData) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
final playlists = snapshot.data!;
|
||||
if (playlists.isEmpty) {
|
||||
return const Text(
|
||||
'No playlists available. Create one first!',
|
||||
);
|
||||
}
|
||||
|
||||
return ListView.builder(
|
||||
shrinkWrap: true,
|
||||
itemCount: playlists.length,
|
||||
itemBuilder: (context, index) {
|
||||
final playlist = playlists[index];
|
||||
return ListTile(
|
||||
title: Text(playlist.name),
|
||||
onTap: () {
|
||||
ref
|
||||
.read(playlistRepositoryProvider.notifier)
|
||||
.addToPlaylist(playlist.id, track.id);
|
||||
Navigator.pop(context);
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Added to ${playlist.name}'),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: const Text('Cancel'),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
void _showEditDialog(BuildContext context, WidgetRef ref, Track track) {
|
||||
final titleController = TextEditingController(text: track.title);
|
||||
final artistController = TextEditingController(text: track.artist);
|
||||
final albumController = TextEditingController(text: track.album);
|
||||
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text('Edit Track'),
|
||||
content: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
TextField(
|
||||
controller: titleController,
|
||||
decoration: const InputDecoration(labelText: 'Title'),
|
||||
),
|
||||
TextField(
|
||||
controller: artistController,
|
||||
decoration: const InputDecoration(labelText: 'Artist'),
|
||||
),
|
||||
TextField(
|
||||
controller: albumController,
|
||||
decoration: const InputDecoration(labelText: 'Album'),
|
||||
),
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: const Text('Cancel'),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
ref
|
||||
.read(trackRepositoryProvider.notifier)
|
||||
.updateMetadata(
|
||||
trackId: track.id,
|
||||
title: titleController.text,
|
||||
artist: artistController.text,
|
||||
album: albumController.text,
|
||||
);
|
||||
Navigator.pop(context);
|
||||
},
|
||||
child: const Text('Save'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
String _formatDuration(int? durationMs) {
|
||||
if (durationMs == null) return '--:--';
|
||||
final d = Duration(milliseconds: durationMs);
|
||||
final minutes = d.inMinutes;
|
||||
final seconds = d.inSeconds % 60;
|
||||
return '$minutes:${seconds.toString().padLeft(2, '0')}';
|
||||
}
|
||||
|
||||
void _batchAddToPlaylist(
|
||||
BuildContext context,
|
||||
WidgetRef ref,
|
||||
List<int> trackIds,
|
||||
VoidCallback onSuccess,
|
||||
) {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) {
|
||||
return AlertDialog(
|
||||
title: const Text('Add to Playlist'),
|
||||
content: SizedBox(
|
||||
width: double.maxFinite,
|
||||
child: Consumer(
|
||||
builder: (context, ref, child) {
|
||||
final playlistsAsync = ref
|
||||
.watch(playlistRepositoryProvider.notifier)
|
||||
.watchAllPlaylists();
|
||||
return StreamBuilder<List<Playlist>>(
|
||||
stream: playlistsAsync,
|
||||
builder: (context, snapshot) {
|
||||
if (!snapshot.hasData) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
final playlists = snapshot.data!;
|
||||
if (playlists.isEmpty) {
|
||||
return const Text('No playlists available.');
|
||||
}
|
||||
|
||||
return ListView.builder(
|
||||
shrinkWrap: true,
|
||||
itemCount: playlists.length,
|
||||
itemBuilder: (context, index) {
|
||||
final playlist = playlists[index];
|
||||
return ListTile(
|
||||
title: Text(playlist.name),
|
||||
onTap: () async {
|
||||
final repo = ref.read(
|
||||
playlistRepositoryProvider.notifier,
|
||||
);
|
||||
for (final id in trackIds) {
|
||||
await repo.addToPlaylist(playlist.id, id);
|
||||
}
|
||||
if (!context.mounted) return;
|
||||
Navigator.pop(context);
|
||||
onSuccess();
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
'Added ${trackIds.length} tracks to ${playlist.name}',
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: const Text('Cancel'),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
void _batchDelete(
|
||||
BuildContext context,
|
||||
WidgetRef ref,
|
||||
List<int> trackIds,
|
||||
VoidCallback onSuccess,
|
||||
) async {
|
||||
final confirm = await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text('Delete Tracks?'),
|
||||
content: Text(
|
||||
'Are you sure you want to delete ${trackIds.length} tracks? '
|
||||
'This will remove them from your device.',
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context, false),
|
||||
child: const Text('Cancel'),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context, true),
|
||||
style: TextButton.styleFrom(foregroundColor: Colors.red),
|
||||
child: const Text('Delete'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
if (confirm == true) {
|
||||
final repo = ref.read(trackRepositoryProvider.notifier);
|
||||
for (final id in trackIds) {
|
||||
await repo.deleteTrack(id);
|
||||
}
|
||||
onSuccess();
|
||||
if (!context.mounted) return;
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('Deleted ${trackIds.length} tracks')),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user