Skip to content

Commit

Permalink
Automatically download playlist metadata on first launch (#905)
Browse files Browse the repository at this point in the history
* automatically add playlist metadata download item during migration

* download playlist metadata only after logging in

- also reworded the download item name for it

* don't require viewId for downloads, move queue restore into postLaunchHook

* provide download location for playlist metadata download

* fix playlist metadata status check

* reword playlist metadata setting

* review findings

* remove check for existing playlist metadata
  • Loading branch information
Chaphasilor authored Nov 9, 2024
1 parent eaab425 commit 672e7ac
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 23 deletions.
6 changes: 3 additions & 3 deletions lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -1369,7 +1369,7 @@
},
"description": "Tooltip for downloadbutton on incidental downloads, which show a lock icon. It says one of the requiring downloads."
},
"finampCollectionNames": "{itemType, select, favorites{Favorites} allPlaylists{All Playlists} fiveLatestAlbums{5 Latest Albums} allPlaylistsMetadata{Info for All Playlists} other{{itemType}} }",
"finampCollectionNames": "{itemType, select, favorites{Favorites} allPlaylists{All Playlists} fiveLatestAlbums{5 Latest Albums} allPlaylistsMetadata{Playlist Metadata} other{{itemType}} }",
"@finampCollectionNames": {
"placeholders": {
"itemType": {
Expand Down Expand Up @@ -1601,8 +1601,8 @@
},
"trackOfflineFavorites": "Sync all favorite statuses",
"trackOfflineFavoritesSubtitle": "This allows showing more up-to-date favorite statuses while offline. Does not download any additional files.",
"allPlaylistsInfoSetting": "Show all playlists offline",
"allPlaylistsInfoSettingSubtitle": "Sync metadata for all playlists to show partially downloaded playlists offline",
"allPlaylistsInfoSetting": "Download Playlist Metadata",
"allPlaylistsInfoSettingSubtitle": "Sync metadata for all playlists to improve your playlist experience",
"downloadFavoritesSetting": "Download all favorites",
"downloadAllPlaylistsSetting": "Download all playlists",
"fiveLatestAlbumsSetting": "Download 5 latest albums",
Expand Down
2 changes: 1 addition & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ Future<void> _setupDownloadsHelper() async {
final downloadsService = GetIt.instance<DownloadsService>();

if (!FinampSettingsHelper
.finampSettings.hasCompleteddownloadsServiceMigration) {
.finampSettings.hasCompletedDownloadsServiceMigration) {
await downloadsService.migrateFromHive();
FinampSettingsHelper.setHasCompleteddownloadsServiceMigration(true);
}
Expand Down
13 changes: 9 additions & 4 deletions lib/models/finamp_models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ const _showStopButtonOnMediaNotificationDefault = false;
const _showSeekControlsOnMediaNotificationDefault = true;
const _keepScreenOnOption = KeepScreenOnOption.whileLyrics;
const _keepScreenOnWhilePluggedIn = true;
const _hasDownloadedPlaylistInfoDefault = false;
const _defaultTranscodingSegmentContainer =
FinampSegmentContainer.fragmentedMp4;
const _featureChipsConfigurationDefault =
Expand Down Expand Up @@ -171,7 +172,7 @@ class FinampSettings {
this.autoloadLastQueueOnStartup = _autoLoadLastQueueOnStartup,
this.hasCompletedBlurhashImageMigration = true,
this.hasCompletedBlurhashImageMigrationIdFix = true,
this.hasCompleteddownloadsServiceMigration = true,
this.hasCompletedDownloadsServiceMigration = true,
this.requireWifiForDownloads = false,
this.onlyShowFullyDownloaded = false,
this.showDownloadsWithUnknownLibrary = true,
Expand Down Expand Up @@ -211,6 +212,7 @@ class FinampSettings {
_showSeekControlsOnMediaNotificationDefault,
this.keepScreenOnOption = _keepScreenOnOption,
this.keepScreenOnWhilePluggedIn = _keepScreenOnWhilePluggedIn,
this.hasDownloadedPlaylistInfo = _hasDownloadedPlaylistInfoDefault,
this.transcodingSegmentContainer = _defaultTranscodingSegmentContainer,
this.featureChipsConfiguration = _featureChipsConfigurationDefault});

Expand Down Expand Up @@ -326,7 +328,7 @@ class FinampSettings {
VolumeNormalizationMode volumeNormalizationMode;

@HiveField(34, defaultValue: false)
bool hasCompleteddownloadsServiceMigration;
bool hasCompletedDownloadsServiceMigration;

@HiveField(35, defaultValue: false)
bool requireWifiForDownloads;
Expand Down Expand Up @@ -449,10 +451,13 @@ class FinampSettings {
@HiveField(73, defaultValue: _keepScreenOnWhilePluggedIn)
bool keepScreenOnWhilePluggedIn;

@HiveField(74, defaultValue: _defaultTranscodingSegmentContainer)
@HiveField(74, defaultValue: _hasDownloadedPlaylistInfoDefault)
bool hasDownloadedPlaylistInfo;

@HiveField(75, defaultValue: _defaultTranscodingSegmentContainer)
FinampSegmentContainer transcodingSegmentContainer;

@HiveField(75, defaultValue: _featureChipsConfigurationDefault)
@HiveField(76, defaultValue: _featureChipsConfigurationDefault)
FinampFeatureChipsConfiguration featureChipsConfiguration;

static Future<FinampSettings> create() async {
Expand Down
20 changes: 12 additions & 8 deletions lib/models/finamp_models.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 23 additions & 5 deletions lib/screens/music_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:get_it/get_it.dart';
import 'package:hive/hive.dart';
import 'package:logging/logging.dart';

import 'package:finamp/services/downloads_service.dart';
import '../components/MusicScreen/music_screen_drawer.dart';
import '../components/MusicScreen/music_screen_tab_view.dart';
import '../components/MusicScreen/sort_by_menu_button.dart';
Expand All @@ -22,6 +23,27 @@ import '../services/finamp_settings_helper.dart';
import '../services/finamp_user_helper.dart';
import '../services/jellyfin_api_helper.dart';

final _musicScreenLogger = Logger("MusicScreen");

void postLaunchHook(WidgetRef ref) async {
final downloadsService = GetIt.instance<DownloadsService>();
final queueService = GetIt.instance<QueueService>();

// make sure playlist info is downloaded for users upgrading from older versions and new installations AFTER logging in and selecting their libraries/views
if (!FinampSettingsHelper.finampSettings.hasDownloadedPlaylistInfo) {
await downloadsService.addDefaultPlaylistInfoDownload().catchError((e) {
// log error without snackbar, we don't want users to be greeted with errors on first launch
_musicScreenLogger.severe("Failed to download playlist metadata: $e");
});
FinampSettingsHelper.setHasDownloadedPlaylistInfo(true);
}

// Restore queue
unawaited(queueService
.performInitialQueueLoad()
.catchError((x) => GlobalSnackbar.error(x)));
}

class MusicScreen extends ConsumerStatefulWidget {
const MusicScreen({super.key});

Expand All @@ -37,15 +59,13 @@ class _MusicScreenState extends ConsumerState<MusicScreen>
bool _showShuffleFab = false;
TextEditingController textEditingController = TextEditingController();
String? searchQuery;
final _musicScreenLogger = Logger("MusicScreen");
final Map<TabContentType, MusicRefreshCallback> refreshMap = {};

TabController? _tabController;

final _audioServiceHelper = GetIt.instance<AudioServiceHelper>();
final _finampUserHelper = GetIt.instance<FinampUserHelper>();
final _jellyfinApiHelper = GetIt.instance<JellyfinApiHelper>();
final _queueService = GetIt.instance<QueueService>();

void _stopSearching() {
setState(() {
Expand Down Expand Up @@ -93,6 +113,7 @@ class _MusicScreenState extends ConsumerState<MusicScreen>
@override
void initState() {
super.initState();
postLaunchHook(ref);
}

@override
Expand Down Expand Up @@ -179,9 +200,6 @@ class _MusicScreenState extends ConsumerState<MusicScreen>

@override
Widget build(BuildContext context) {
_queueService
.performInitialQueueLoad()
.catchError((x) => GlobalSnackbar.error(x));
if (_tabController == null) {
_buildTabController();
}
Expand Down
25 changes: 24 additions & 1 deletion lib/services/downloads_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'dart:io';
import 'package:background_downloader/background_downloader.dart';
import 'package:collection/collection.dart';
import 'package:finamp/components/global_snackbar.dart';
import 'package:finamp/services/finamp_user_helper.dart';
import 'package:finamp/services/jellyfin_api_helper.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
Expand Down Expand Up @@ -434,8 +435,8 @@ class DownloadsService {
/// it to the anchor as required and then syncing.
Future<void> addDownload({
required DownloadStub stub,
required String viewId,
required DownloadProfile transcodeProfile,
String? viewId,
}) async {
// Comment https://github.com/jmshrv/finamp/issues/134#issuecomment-1563441355
// suggests this does not make a request and always returns failure
Expand Down Expand Up @@ -1225,6 +1226,28 @@ class DownloadsService {
}
}

Future<void> addDefaultPlaylistInfoDownload() async {

String? downloadLocation =
FinampSettingsHelper.finampSettings.defaultDownloadLocation;
if (!FinampSettingsHelper.finampSettings.downloadLocationsMap
.containsKey(downloadLocation)) {
downloadLocation = null;
}
downloadLocation ??= FinampSettingsHelper.finampSettings.internalSongDir.id;

// Automatically download playlist metadata (to enhance the playlist actions dialog and offline mode)
await addDownload(
stub: DownloadStub.fromFinampCollection(
FinampCollection(type: FinampCollectionType.allPlaylistsMetadata)),
transcodeProfile:
DownloadProfile(
transcodeCodec: FinampTranscodingCodec.original,
downloadLocationId: downloadLocation,
),
);
}

/// Get all user-downloaded items. Used to show items on downloads screen.
List<DownloadStub> getUserDownloaded() => getVisibleChildren(_anchor);

Expand Down
9 changes: 8 additions & 1 deletion lib/services/finamp_settings_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ class FinampSettingsHelper {
static void setHasCompleteddownloadsServiceMigration(
bool hasCompleteddownloadsServiceMigration) {
FinampSettings finampSettingsTemp = finampSettings;
finampSettingsTemp.hasCompleteddownloadsServiceMigration =
finampSettingsTemp.hasCompletedDownloadsServiceMigration =
hasCompleteddownloadsServiceMigration;
Hive.box<FinampSettings>("FinampSettings")
.put("FinampSettings", finampSettingsTemp);
Expand All @@ -305,6 +305,13 @@ class FinampSettingsHelper {
.put("FinampSettings", finampSettingsTemp);
}

static void setHasDownloadedPlaylistInfo(bool hasDownloadedPlaylistInfo) {
FinampSettings finampSettingsTemp = finampSettings;
finampSettingsTemp.hasDownloadedPlaylistInfo = hasDownloadedPlaylistInfo;
Hive.box<FinampSettings>("FinampSettings")
.put("FinampSettings", finampSettingsTemp);
}

static void setTabOrder(List<TabContentType> newTabOrder) {
FinampSettings finampSettingsTemp = finampSettings;
finampSettingsTemp.tabOrder = newTabOrder;
Expand Down

0 comments on commit 672e7ac

Please sign in to comment.