Skip to content

Commit

Permalink
feat: show loading indicator on play track
Browse files Browse the repository at this point in the history
  • Loading branch information
KRTirtho committed Sep 11, 2023
1 parent 1540999 commit d12ea48
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 47 deletions.
10 changes: 5 additions & 5 deletions lib/components/library/user_local_tracks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ final localTracksProvider = FutureProvider<List<LocalTrack>>((ref) async {
class UserLocalTracks extends HookConsumerWidget {
const UserLocalTracks({Key? key}) : super(key: key);

void playLocalTracks(
Future<void> playLocalTracks(
WidgetRef ref,
List<LocalTrack> tracks, {
LocalTrack? currentTrack,
Expand Down Expand Up @@ -203,10 +203,10 @@ class UserLocalTracks extends HookConsumerWidget {
const SizedBox(width: 10),
FilledButton(
onPressed: trackSnapshot.value != null
? () {
? () async {
if (trackSnapshot.value?.isNotEmpty == true) {
if (!isPlaylistPlaying) {
playLocalTracks(
await playLocalTracks(
ref,
trackSnapshot.value!,
);
Expand Down Expand Up @@ -295,8 +295,8 @@ class UserLocalTracks extends HookConsumerWidget {
index: index,
track: track,
userPlaylist: false,
onTap: () {
playLocalTracks(
onTap: () async {
await playLocalTracks(
ref,
sortedTracks,
currentTrack: track,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:fl_query/fl_query.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:go_router/go_router.dart';
Expand Down Expand Up @@ -27,7 +29,7 @@ class TrackCollectionView<T> extends HookConsumerWidget {
final Query<List<TrackSimple>, T> tracksSnapshot;
final String titleImage;
final PlayButtonState playingState;
final void Function([Track? currentTrack]) onPlay;
final Future<void> Function([Track? currentTrack]) onPlay;
final void Function([Track? currentTrack]) onShuffledPlay;
final void Function() onAddToQueue;
final void Function() onShare;
Expand Down
52 changes: 33 additions & 19 deletions lib/components/shared/track_table/track_tile.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
Expand All @@ -21,7 +23,7 @@ class TrackTile extends HookConsumerWidget {
final Track track;
final bool selected;
final ValueChanged<bool?>? onChanged;
final VoidCallback? onTap;
final Future<void> Function()? onTap;
final VoidCallback? onLongPress;
final bool userPlaylist;
final String? playlistId;
Expand Down Expand Up @@ -62,6 +64,10 @@ class TrackTile extends HookConsumerWidget {

final isPlaying = track.id == playlist.activeTrack?.id;

final isLoading = useState(false);

final isSelected = isPlaying || isLoading.value;

return LayoutBuilder(builder: (context, constrains) {
return Listener(
onPointerDown: (event) {
Expand All @@ -76,11 +82,18 @@ class TrackTile extends HookConsumerWidget {
);
},
child: HoverBuilder(
permanentState: isPlaying || constrains.smAndDown ? true : null,
permanentState: isSelected || constrains.smAndDown ? true : null,
builder: (context, isHovering) {
return ListTile(
selected: isPlaying,
onTap: onTap,
selected: isSelected,
onTap: () async {
try {
isLoading.value = true;
await onTap?.call();
} finally {
isLoading.value = false;
}
},
onLongPress: onLongPress,
enabled: !isBlackListed,
contentPadding: EdgeInsets.zero,
Expand Down Expand Up @@ -145,22 +158,23 @@ class TrackTile extends HookConsumerWidget {
.copyWith(size: 26, color: Colors.white),
child: AnimatedSwitcher(
duration: const Duration(milliseconds: 300),
child: !isHovering
? const SizedBox.shrink()
: isPlaying && playlist.isFetching
? const SizedBox(
width: 26,
height: 26,
child: CircularProgressIndicator(
strokeWidth: 1.5,
color: Colors.white,
),
child: (isPlaying && playlist.isFetching) ||
isLoading.value
? const SizedBox(
width: 26,
height: 26,
child: CircularProgressIndicator(
strokeWidth: 1.5,
color: Colors.white,
),
)
: isPlaying
? Icon(
SpotubeIcons.pause,
color: theme.colorScheme.primary,
)
: isPlaying
? Icon(
SpotubeIcons.pause,
color: theme.colorScheme.primary,
)
: !isHovering
? const SizedBox.shrink()
: const Icon(SpotubeIcons.play),
),
),
Expand Down
14 changes: 7 additions & 7 deletions lib/components/shared/track_table/tracks_table_view.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
Expand Down Expand Up @@ -27,7 +29,7 @@ final trackCollectionSortState =
StateProvider.family<SortBy, String>((ref, _) => SortBy.none);

class TracksTableView extends HookConsumerWidget {
final void Function(Track currentTrack)? onTrackPlayButtonPressed;
final Future<void> Function(Track currentTrack)? onTrackPlayButtonPressed;
final List<Track> tracks;
final bool userPlaylist;
final String? playlistId;
Expand Down Expand Up @@ -58,8 +60,7 @@ class TracksTableView extends HookConsumerWidget {
final downloader = ref.watch(downloadManagerProvider.notifier);
final apiType =
ref.watch(userPreferencesProvider.select((s) => s.youtubeApiType));
final tableHeadStyle =
const TextStyle(fontWeight: FontWeight.bold, fontSize: 16);
const tableHeadStyle = TextStyle(fontWeight: FontWeight.bold, fontSize: 16);

final selected = useState<List<String>>([]);
final showCheck = useState<bool>(false);
Expand Down Expand Up @@ -297,7 +298,7 @@ class TracksTableView extends HookConsumerWidget {
selected: selected.value.contains(track.id),
userPlaylist: userPlaylist,
playlistId: playlistId,
onTap: () {
onTap: () async {
if (showCheck.value) {
final alreadyChecked = selected.value.contains(track.id);
if (alreadyChecked) {
Expand All @@ -314,9 +315,8 @@ class TracksTableView extends HookConsumerWidget {
),
),
);
if (!isBlackListed) {
onTrackPlayButtonPressed?.call(track);
}
if (isBlackListed) return;
await onTrackPlayButtonPressed?.call(track);
}
},
onLongPress: () {
Expand Down
8 changes: 4 additions & 4 deletions lib/pages/album/album.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,18 @@ class AlbumPage extends HookConsumerWidget {
album: album,
routePath: "/album/${album.id}",
bottomSpace: mediaQuery.mdAndDown,
onPlay: ([track]) {
onPlay: ([track]) async {
if (tracksSnapshot.hasData) {
if (!isAlbumPlaying) {
playPlaylist(
await playPlaylist(
tracksSnapshot.data!
.map((track) =>
TypeConversionUtils.simpleTrack_X_Track(track, album))
.toList(),
ref,
);
} else if (isAlbumPlaying && track != null) {
playPlaylist(
await playPlaylist(
tracksSnapshot.data!
.map((track) =>
TypeConversionUtils.simpleTrack_X_Track(track, album))
Expand All @@ -105,7 +105,7 @@ class AlbumPage extends HookConsumerWidget {
ref,
);
} else {
playback
await playback
.removeTracks(tracksSnapshot.data!.map((track) => track.id!));
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/artist/artist.dart
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ class ArtistPage extends HookConsumerWidget {
return TrackTile(
index: i,
track: track,
onTap: () {
onTap: () async {
playPlaylist(
topTracks.toList(),
currentTrack: track,
Expand Down
14 changes: 4 additions & 10 deletions lib/pages/playlist/playlist.dart
Original file line number Diff line number Diff line change
Expand Up @@ -104,22 +104,16 @@ class PlaylistView extends HookConsumerWidget {
tracksSnapshot: tracksSnapshot,
description: playlist.description,
isOwned: ownPlaylist,
onPlay: ([track]) {
onPlay: ([track]) async {
if (tracksSnapshot.hasData) {
if (!isPlaylistPlaying) {
playPlaylist(
tracksSnapshot.data!,
ref,
currentTrack: track,
);
} else if (isPlaylistPlaying && track != null) {
playPlaylist(
if (!isPlaylistPlaying || (isPlaylistPlaying && track != null)) {
await playPlaylist(
tracksSnapshot.data!,
ref,
currentTrack: track,
);
} else {
playlistNotifier
await playlistNotifier
.removeTracks(tracksSnapshot.data!.map((e) => e.id!));
}
}
Expand Down

0 comments on commit d12ea48

Please sign in to comment.