🐛 Bug fixes

This commit is contained in:
2024-08-30 01:56:21 +08:00
parent be977f10d1
commit d25ebbf6bd
15 changed files with 616 additions and 2 deletions

View File

@@ -24,6 +24,7 @@ import 'package:rhythm_box/services/server/active_sourced_track.dart';
import 'package:rhythm_box/services/server/routes/playback.dart';
import 'package:rhythm_box/services/server/server.dart';
import 'package:rhythm_box/services/server/sourced_track.dart';
import 'package:rhythm_box/shells/system_shell.dart';
import 'package:rhythm_box/translations.dart';
import 'package:rhythm_box/widgets/tracks/querying_track_info.dart';
import 'package:smtc_windows/smtc_windows.dart';
@@ -41,6 +42,7 @@ Future<void> main(List<String> rawArgs) async {
WidgetsFlutterBinding.ensureInitialized();
if (PlatformInfo.isDesktop) {
await windowManager.ensureInitialized();
await windowManager.setPreventClose(true);
}
if (PlatformInfo.isWindows) {
@@ -82,6 +84,13 @@ class MyApp extends StatelessWidget {
themeMode: ThemeMode.system,
translations: AppTranslations(),
onInit: () => _initializeProviders(context),
builder: (context, child) {
return SystemShell(
child: ScaffoldMessenger(
child: child ?? const SizedBox(),
),
);
},
);
}

View File

@@ -1,6 +1,7 @@
import 'package:animations/animations.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:rhythm_box/screens/album/view.dart';
import 'package:rhythm_box/screens/auth/mobile_login.dart';
import 'package:rhythm_box/screens/explore.dart';
import 'package:rhythm_box/screens/library/view.dart';
@@ -37,6 +38,13 @@ final router = GoRouter(routes: [
playlistId: state.pathParameters['id']!,
),
),
GoRoute(
path: '/albums/:id',
name: 'albumView',
builder: (context, state) => AlbumViewScreen(
albumId: state.pathParameters['id']!,
),
),
GoRoute(
path: '/settings',
name: 'settings',

229
lib/screens/album/view.dart Normal file
View File

@@ -0,0 +1,229 @@
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:gap/gap.dart';
import 'package:get/get.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:intl/intl.dart';
import 'package:rhythm_box/providers/audio_player.dart';
import 'package:rhythm_box/providers/history.dart';
import 'package:rhythm_box/providers/spotify.dart';
import 'package:rhythm_box/services/artist.dart';
import 'package:rhythm_box/services/audio_player/audio_player.dart';
import 'package:rhythm_box/services/track.dart';
import 'package:rhythm_box/widgets/auto_cache_image.dart';
import 'package:rhythm_box/widgets/sized_container.dart';
import 'package:rhythm_box/widgets/tracks/playlist_track_list.dart';
import 'package:spotify/spotify.dart';
class AlbumViewScreen extends StatefulWidget {
final String albumId;
final Playlist? playlist;
const AlbumViewScreen({
super.key,
required this.albumId,
this.playlist,
});
@override
State<AlbumViewScreen> createState() => _AlbumViewScreenState();
}
class _AlbumViewScreenState extends State<AlbumViewScreen> {
late final SpotifyProvider _spotify = Get.find();
late final AudioPlayerProvider _playback = Get.find();
bool get _isCurrentPlaylist => _album != null
? _playback.state.value.containsCollection(_album!.id!)
: false;
bool _isLoading = true;
bool _isLoadingTracks = true;
bool _isUpdating = false;
Album? _album;
List<Track>? _tracks;
Future<void> _pullPlaylist() async {
_album = await _spotify.api.albums.get(widget.albumId);
setState(() => _isLoading = false);
}
Future<void> _pullTracks() async {
_tracks = (await _spotify.api.albums.tracks(widget.albumId).all())
.map((x) => x.asTrack(_album!))
.toList();
setState(() => _isLoadingTracks = false);
}
@override
void initState() {
super.initState();
_pullPlaylist();
_pullTracks();
}
@override
Widget build(BuildContext context) {
const radius = BorderRadius.all(Radius.circular(8));
return Material(
color: Theme.of(context).colorScheme.surface,
child: Scaffold(
appBar: AppBar(
title: const Text('Playlist'),
centerTitle: MediaQuery.of(context).size.width >= 720,
),
body: Builder(
builder: (context) {
if (_isLoading) {
return const Center(
child: CircularProgressIndicator(),
);
}
return CenteredContainer(
child: CustomScrollView(
slivers: [
SliverToBoxAdapter(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Material(
borderRadius: radius,
elevation: 2,
child: ClipRRect(
borderRadius: radius,
child: (_album?.images?.isNotEmpty ?? false)
? AutoCacheImage(
_album!.images!.first.url!,
width: 160.0,
height: 160.0,
)
: const SizedBox(
width: 160,
height: 160,
child: Icon(Icons.image),
),
),
),
const Gap(24),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
_album!.name ?? 'Playlist',
style: Theme.of(context)
.textTheme
.headlineSmall,
maxLines: 2,
overflow: TextOverflow.fade,
),
Text(
_album!.artists?.asString() ?? 'A Playlist',
maxLines: 3,
overflow: TextOverflow.ellipsis,
),
const Gap(8),
Text(
"${NumberFormat.compactCurrency(symbol: '', decimalDigits: 2).format(_album!.popularity ?? 0)} views",
),
Text(
'#${_album!.id}',
style: GoogleFonts.robotoMono(fontSize: 10),
),
],
),
),
],
).paddingOnly(left: 24, right: 24, top: 24),
const Gap(8),
Wrap(
spacing: 8,
children: [
Obx(
() => ElevatedButton.icon(
icon: (_isCurrentPlaylist &&
_playback.isPlaying.value)
? const Icon(Icons.pause_outlined)
: const Icon(Icons.play_arrow),
label: const Text('Play'),
onPressed: _isUpdating
? null
: () async {
if (_isCurrentPlaylist &&
_playback.isPlaying.value) {
audioPlayer.pause();
return;
} else if (_isCurrentPlaylist &&
!_playback.isPlaying.value) {
audioPlayer.resume();
return;
}
setState(() => _isUpdating = true);
await _playback.load(_tracks!,
autoPlay: true);
_playback.addCollection(_album!.id!);
Get.find<PlaybackHistoryProvider>()
.addAlbums([_album!]);
setState(() => _isUpdating = false);
},
),
),
TextButton.icon(
icon: const Icon(Icons.shuffle),
label: const Text('Shuffle'),
onPressed: _isUpdating
? null
: () async {
setState(() => _isUpdating = true);
audioPlayer.setShuffle(true);
await _playback.load(
_tracks!,
autoPlay: true,
initialIndex:
Random().nextInt(_tracks!.length),
);
_playback.addCollection(_album!.id!);
Get.find<PlaybackHistoryProvider>()
.addAlbums([_album!]);
setState(() => _isUpdating = false);
},
),
],
).paddingSymmetric(horizontal: 24),
const Gap(24),
],
),
),
SliverToBoxAdapter(
child: Text(
'Songs (${_tracks?.length ?? 0})',
style: Theme.of(context).textTheme.titleLarge,
).paddingOnly(left: 28, right: 28, bottom: 4),
),
PlaylistTrackList(
isLoading: _isLoadingTracks,
playlistId: widget.albumId,
tracks: _tracks,
),
],
),
);
},
),
),
);
}
}

70
lib/services/track.dart Normal file
View File

@@ -0,0 +1,70 @@
import 'dart:io';
import 'package:metadata_god/metadata_god.dart';
import 'package:path/path.dart';
import 'package:rhythm_box/services/audio_player/audio_player.dart';
import 'package:spotify/spotify.dart';
extension TrackExtensions on Track {
Track fromFile(
File file, {
Metadata? metadata,
String? art,
}) {
album = Album()
..name = metadata?.album ?? 'Unknown'
..images = [if (art != null) Image()..url = art]
..genres = [if (metadata?.genre != null) metadata!.genre!]
..artists = [
Artist()
..name = metadata?.albumArtist ?? 'Unknown'
..id = metadata?.albumArtist ?? 'Unknown'
..type = 'artist',
]
..id = metadata?.album
..releaseDate = metadata?.year?.toString();
artists = [
Artist()
..name = metadata?.artist ?? 'Unknown'
..id = metadata?.artist ?? 'Unknown'
];
id = metadata?.title ?? basenameWithoutExtension(file.path);
name = metadata?.title ?? basenameWithoutExtension(file.path);
type = 'track';
uri = file.path;
durationMs = (metadata?.durationMs?.toInt() ?? 0);
return this;
}
}
extension TrackSimpleExtensions on TrackSimple {
Track asTrack(AlbumSimple album) {
Track track = Track();
track.name = name;
track.album = album;
track.artists = artists;
track.availableMarkets = availableMarkets;
track.discNumber = discNumber;
track.durationMs = durationMs;
track.explicit = explicit;
track.externalUrls = externalUrls;
track.href = href;
track.id = id;
track.isPlayable = isPlayable;
track.linkedFrom = linkedFrom;
track.name = name;
track.previewUrl = previewUrl;
track.trackNumber = trackNumber;
track.type = type;
track.uri = uri;
return track;
}
}
extension TracksToMediaExtension on Iterable<Track> {
List<RhythmMedia> asMediaList() {
return map((track) => RhythmMedia(track)).toList();
}
}

View File

@@ -0,0 +1,29 @@
import 'package:flutter/material.dart';
import 'package:rhythm_box/platform.dart';
class SystemShell extends StatelessWidget {
final Widget child;
const SystemShell({super.key, required this.child});
@override
Widget build(BuildContext context) {
if (PlatformInfo.isMacOS) {
return Column(
children: [
Container(
height: 28,
color: Theme.of(context).colorScheme.surface,
),
const Divider(
thickness: 0.3,
height: 0.3,
),
Expanded(child: child),
],
);
}
return child;
}
}

View File

@@ -47,7 +47,7 @@ class PlaylistSection extends StatelessWidget {
onTap: () {
if (item == null) return;
GoRouter.of(context).pushNamed(
'playlistView',
'albumView',
pathParameters: {'id': item.id!},
);
},