Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/providers/download_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@ class DownloadProvider with StreamSubscriber {
final _downloadsCleared = StreamController<bool>.broadcast();
final _downloadRemoved = StreamController<Song>.broadcast();
final _songDownloaded = StreamController<Download>.broadcast();
final _songDownloadStarted = StreamController<Song>.broadcast();

Stream<bool> get downloadsClearedStream => _downloadsCleared.stream;

Stream<Song> get downloadRemovedStream => _downloadRemoved.stream;

Stream<Download> get songDownloadedStream => _songDownloaded.stream;

Stream<Song> get downloadStartedStream => _songDownloadStarted.stream;

static const serializedSongContainer = 'Downloads';
static const downloadCacheKey = 'koel.downloaded.songs';
static final _songStorage = GetStorage(serializedSongContainer);
Expand Down Expand Up @@ -79,6 +82,8 @@ class DownloadProvider with StreamSubscriber {
get serializedSongKey => '${preferences.host}.${preferences.userEmail}.songs';

Future<void> download({required Song song}) async {
_songDownloadStarted.add(song);

final file = await _downloadManager.downloadFile(
song.sourceUrl,
key: song.cacheKey,
Expand Down
1 change: 1 addition & 0 deletions lib/ui/screens/album_details.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class _AlbumDetailsScreenState extends State<AlbumDetailsScreen> {
AppBar(
headingText: album.name,
actions: [
SongListCacheIcon(songs: songs),
SortButton(
fields: ['track', 'title', 'created_at'],
currentField: sortConfig.field,
Expand Down
1 change: 1 addition & 0 deletions lib/ui/screens/playlist_details.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class _PlaylistDetailsScreen extends State<PlaylistDetailsScreen> {
headingText: playlist.name,
coverImage: _cover,
actions: [
SongListCacheIcon(songs: songs),
SortButton(
fields: ['title', 'artist_name', 'created_at'],
currentField: sortConfig.field,
Expand Down
10 changes: 9 additions & 1 deletion lib/ui/widgets/song_cache_icon.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,15 @@ class _SongCacheIconState extends State<SongCacheIcon> with StreamSubscriber {
}));

subscribe(downloadProvider.songDownloadedStream.listen((event) {
if (event.song == widget.song) setState(() => _downloaded = true);
if (event.song == widget.song)
setState(() {
_downloaded = true;
_downloading = false;
});
}));

subscribe(downloadProvider.downloadStartedStream.listen((song) {
if (song == widget.song) setState(() => _downloading = true);
}));

setState(() => _downloaded = downloadProvider.has(song: widget.song));
Expand Down
128 changes: 128 additions & 0 deletions lib/ui/widgets/song_list_cache_icon.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import 'package:app/constants/constants.dart';
import 'package:app/mixins/stream_subscriber.dart';
import 'package:app/models/models.dart';
import 'package:app/providers/providers.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class SongListCacheIcon extends StatefulWidget {
final List<Song> songs;

const SongListCacheIcon({Key? key, required this.songs}) : super(key: key);

@override
_SongListCacheIconState createState() => _SongListCacheIconState();
}

class _SongListCacheIconState extends State<SongListCacheIcon>
with StreamSubscriber {
late DownloadProvider downloadProvider;
var _downloading = false;
bool? _downloaded = false;

static const downloadBatchSize = 3;

@override
void initState() {
super.initState();
downloadProvider = context.read();

subscribe(downloadProvider.downloadsClearedStream.listen((_) {
setState(() => _downloaded = false);
}));

subscribe(downloadProvider.downloadRemovedStream.listen((song) {
if (widget.songs.contains(song)) setState(() => _downloaded = false);
}));

subscribe(downloadProvider.songDownloadedStream.listen((event) {
if (widget.songs.contains(event.song)) _resolveDownloadStatus();
}));

_resolveDownloadStatus();
}

/// Since this widget is rendered inside NowPlayingScreen, change to current
/// song in the parent will not trigger initState() and as a result not
/// refresh the song's cache status.
/// For that, we hook into didUpdateWidget().
/// See https://stackoverflow.com/questions/54759920/flutter-why-is-child-widgets-initstate-is-not-called-on-every-rebuild-of-pa.
@override
void didUpdateWidget(covariant SongListCacheIcon oldWidget) {
super.didUpdateWidget(oldWidget);
_resolveDownloadStatus();
}

void _resolveDownloadStatus() {
setState(() => _downloaded =
!widget.songs.any((song) => !downloadProvider.has(song: song)));
}

@override
void dispose() {
unsubscribeAll();
super.dispose();
}

Future<void> _download() async {
setState(() => _downloading = true);

int indexLastStarted = 0;

/// Download songs in parallel.
/// Recursively iterates over the song list, downloading songs that haven't
/// been downloaded yet.
Future<void> downloadNextSong() async {
if (indexLastStarted >= widget.songs.length) return;

Song song;
do {
song = widget.songs[indexLastStarted++];
} while (downloadProvider.has(song: song) &&
indexLastStarted < widget.songs.length);

await downloadProvider.download(song: song);
await downloadNextSong();
}

await Future.wait(
List.generate(downloadBatchSize, (_) => downloadNextSong()));

setState(() {
_downloading = false;
_downloaded = true;
});
}

@override
Widget build(BuildContext context) {
if (_downloading)
return const Padding(
padding: EdgeInsets.only(right: 4.0),
child: CupertinoActivityIndicator(radius: 9, color: AppColors.white),
);

final downloaded = this._downloaded;

if (downloaded == null) return const SizedBox.shrink();

if (downloaded) {
return const Padding(
padding: EdgeInsets.only(right: 4.0),
child: Icon(
CupertinoIcons.checkmark_alt_circle_fill,
size: 18,
color: Color(0xFFFAD763),
),
);
}

return IconButton(
onPressed: _download,
constraints: const BoxConstraints(),
padding: const EdgeInsets.symmetric(horizontal: 0.0),
icon: const Icon(CupertinoIcons.cloud_download_fill, size: 16),
);
}
}
1 change: 1 addition & 0 deletions lib/ui/widgets/widgets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ export 'song_row.dart';
export 'song_thumbnail.dart';
export 'spinner.dart';
export 'typography.dart';
export 'song_list_cache_icon.dart';