Skip to content

Commit

Permalink
Merge branch 'Y0ngg4n-download-added-to-playlist'
Browse files Browse the repository at this point in the history
  • Loading branch information
jmshrv committed Oct 8, 2023
2 parents 028b251 + 7126c3f commit ece715c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
62 changes: 62 additions & 0 deletions lib/components/DownloadsScreen/sync_downloaded_playlists.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import 'package:finamp/models/finamp_models.dart';
import 'package:finamp/models/jellyfin_models.dart';
import 'package:finamp/services/downloads_helper.dart';
import 'package:finamp/services/jellyfin_api_helper.dart';
import 'package:flutter/material.dart';
import 'package:flutter_downloader/flutter_downloader.dart';
import 'package:get_it/get_it.dart';
import 'package:logging/logging.dart';

class SyncDownloadedPlaylistsButton extends StatefulWidget {
const SyncDownloadedPlaylistsButton({super.key});

@override
State<SyncDownloadedPlaylistsButton> createState() =>
_SyncDownloadedPlaylistsButtonState();
}

class _SyncDownloadedPlaylistsButtonState
extends State<SyncDownloadedPlaylistsButton> {
final _syncLogger = Logger("SyncDownloadedPlaylistsButton");
DownloadsHelper downloadsHelper = GetIt.instance<DownloadsHelper>();
final _jellyfinApiData = GetIt.instance<JellyfinApiHelper>();

void syncPlaylists() async {
_syncLogger.info("Syncing downloaded playlists");
List<DownloadedParent> parents = downloadsHelper.downloadedParents.toList();
List<DownloadedSong> songs = downloadsHelper.downloadedItems.toList();

for (DownloadedParent parent in parents) {
Set<String> children =
parent.downloadedChildren.values.map((e) => e.id).toSet();
DownloadedSong firstSong = songs.first;
List<BaseItemDto>? items = await _jellyfinApiData.getItems(
isGenres: false, parentItem: parent.item);

if (items == null) continue;

for (BaseItemDto item in items) {
_syncLogger.info(item.id.toString());
bool isInChildren = children.contains(item.id);

if (isInChildren) {
_syncLogger.info("Need sync download of ${item.id}");

await downloadsHelper.addDownloads(
items: [item],
parent: parent.item,
useHumanReadableNames: firstSong.useHumanReadableNames,
downloadLocation: firstSong.downloadLocation!,
viewId: firstSong.viewId,
);
}
}
}
}

@override
Widget build(BuildContext context) {
return IconButton(
onPressed: () => syncPlaylists(), icon: const Icon(Icons.sync));
}
}
2 changes: 2 additions & 0 deletions lib/screens/downloads_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import '../components/DownloadsScreen/downloads_overview.dart';
import '../components/DownloadsScreen/downloaded_albums_list.dart';
import '../components/DownloadsScreen/download_error_screen_button.dart';
import '../components/DownloadsScreen/download_missing_images_button.dart';
import '../components/DownloadsScreen/sync_downloaded_playlists.dart';

class DownloadsScreen extends StatelessWidget {
const DownloadsScreen({Key? key}) : super(key: key);
Expand All @@ -17,6 +18,7 @@ class DownloadsScreen extends StatelessWidget {
appBar: AppBar(
title: Text(AppLocalizations.of(context)!.downloads),
actions: const [
SyncDownloadedPlaylistsButton(),
DownloadMissingImagesButton(),
DownloadErrorScreenButton()
],
Expand Down
1 change: 1 addition & 0 deletions lib/services/downloads_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,7 @@ class DownloadsHelper {
{List<String>? keys}) =>
_downloadedItemsBox.listenable(keys: keys);


/// Converts a dart list to a string with the correct SQL syntax
String _dartListToSqlList(List dartList) {
try {
Expand Down

0 comments on commit ece715c

Please sign in to comment.