Skip to content

Commit

Permalink
fix: possible wrong indices while downloading playlists
Browse files Browse the repository at this point in the history
  • Loading branch information
MSOB7YY committed Oct 19, 2024
1 parent f4d92ec commit 0266cd5
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 40 deletions.
2 changes: 1 addition & 1 deletion lib/core/namida_converter_ext.dart
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ extension OnYoutubeLinkOpenActionUtils on OnYoutubeLinkOpenAction {
switch (this) {
case OnYoutubeLinkOpenAction.showDownload:
if (ids.length == 1) {
showDownloadVideoBottomSheet(videoId: ids.first, index: null, totalLength: null, playlistId: null, streamInfoItem: null);
showDownloadVideoBottomSheet(videoId: ids.first, originalIndex: null, totalLength: null, playlistId: null, streamInfoItem: null);
} else {
final ptitle = 'External - ${DateTime.now().millisecondsSinceEpoch.dateAndClockFormattedOriginal}';
YTPlaylistDownloadPage(
Expand Down
8 changes: 4 additions & 4 deletions lib/youtube/class/youtube_item_download_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class YoutubeItemDownloadConfig {
final String? prefferedAudioQualityID;
final bool? fetchMissingAudio;
final bool? fetchMissingVideo;
final int? index;
final int? originalIndex;
final int? totalLength;
final String? playlistId;

Expand All @@ -41,7 +41,7 @@ class YoutubeItemDownloadConfig {
required this.prefferedAudioQualityID,
required this.fetchMissingAudio,
required this.fetchMissingVideo,
required this.index,
required this.originalIndex,
required this.totalLength,
required this.playlistId,
}) : _filename = filename.obs;
Expand Down Expand Up @@ -79,7 +79,7 @@ class YoutubeItemDownloadConfig {
prefferedAudioQualityID: map['prefferedAudioQualityID'],
fetchMissingAudio: map['fetchMissingAudio'],
fetchMissingVideo: map['fetchMissingVideo'],
index: map['index'],
originalIndex: map['index'],
totalLength: map['totalLength'],
playlistId: map['playlistId'],
);
Expand All @@ -99,7 +99,7 @@ class YoutubeItemDownloadConfig {
'prefferedAudioQualityID': prefferedAudioQualityID,
'fetchMissingAudio': fetchMissingAudio,
'fetchMissingVideo': fetchMissingVideo,
'index': index,
'index': originalIndex,
'totalLength': totalLength,
'playlistId': playlistId,
};
Expand Down
4 changes: 2 additions & 2 deletions lib/youtube/controller/youtube_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ class YoutubeController {
streams,
playlistInfo,
playlistInfo?.id ?? config.playlistId,
config.index,
config.originalIndex,
config.totalLength,
autoExtract: autoExtractTitleAndArtist,
initialBuilding: config.ffmpegTags,
Expand Down Expand Up @@ -847,7 +847,7 @@ class YoutubeController {
bool requiresRenaming = false;

final finalFilenameTempRebuilt = filenameBuilder.rebuildFilenameWithDecodedParams(
finalFilenameTemp, id.videoId, streams, pageResult, config.streamInfoItem, playlistInfo, videoStream, audioStream, config.index, config.totalLength);
finalFilenameTemp, id.videoId, streams, pageResult, config.streamInfoItem, playlistInfo, videoStream, audioStream, config.originalIndex, config.totalLength);
if (finalFilenameTempRebuilt != null && finalFilenameTempRebuilt.isNotEmpty) {
finalFilenameTemp = finalFilenameTempRebuilt;
requiresRenaming = true;
Expand Down
12 changes: 6 additions & 6 deletions lib/youtube/controller/yt_filename_rebuilder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class _YtFilenameRebuilder {
PlaylistBasicInfo? playlistInfo,
VideoStream? videoStream,
AudioStream? audioStream,
int? index,
int? originalIndex,
int? totalLength,
) {
bool didConvertSmth = false;
Expand All @@ -33,7 +33,7 @@ class _YtFilenameRebuilder {
playlistInfo,
videoStream,
audioStream,
index,
originalIndex,
totalLength,
);
if (converted != null) {
Expand Down Expand Up @@ -98,7 +98,7 @@ class _YtFilenameRebuilder {
String buildParamForFilename(String e) => '%($e)s';

String? _keywordToInfo(String keyword, String videoId, VideoStreamsResult? streams, YoutiPieVideoPageResult? pageResult, StreamInfoItem? videoItem,
PlaylistBasicInfo? playlistInfo, VideoStream? videoStream, AudioStream? audioStream, int? index, int? totalLength) {
PlaylistBasicInfo? playlistInfo, VideoStream? videoStream, AudioStream? audioStream, int? originalIndex, int? totalLength) {
return switch (keyword) {
'none' => '',
'id' || 'video_id' => videoId,
Expand Down Expand Up @@ -161,10 +161,10 @@ class _YtFilenameRebuilder {
return finalTitle;
}(),
'playlist_count' => (playlistInfo?.videosCount ?? totalLength)?.toString(),
'playlist_index' => (videoItem?.indexInPlaylist ?? index)?.toString().padLeft((playlistInfo?.videosCount ?? totalLength)?.toString().length ?? 0, '0'),
'playlist_autonumber' => index == null && videoItem?.indexInPlaylist == null
'playlist_index' => (videoItem?.indexInPlaylist ?? originalIndex)?.toString().padLeft((playlistInfo?.videosCount ?? totalLength)?.toString().length ?? 0, '0'),
'playlist_autonumber' => originalIndex == null && videoItem?.indexInPlaylist == null
? null
: ((videoItem?.indexInPlaylist ?? index)! + 1).toString().padLeft((playlistInfo?.videosCount ?? totalLength)?.toString().length ?? 0, '0'),
: ((videoItem?.indexInPlaylist ?? originalIndex)! + 1).toString().padLeft((playlistInfo?.videosCount ?? totalLength)?.toString().length ?? 0, '0'),
_ => throw const _NonMatched(),
};
}
Expand Down
8 changes: 4 additions & 4 deletions lib/youtube/functions/download_sheet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Future<void> showDownloadVideoBottomSheet({
YoutubeItemDownloadConfig? initialItemConfig,
PlaylistBasicInfo? playlistInfo,
required String? playlistId,
required int? index,
required int? originalIndex,
required int? totalLength,
required StreamInfoItem? streamInfoItem,
bool preferAudioOnly = false,
Expand Down Expand Up @@ -99,7 +99,7 @@ Future<void> showDownloadVideoBottomSheet({
playlistInfo,
selectedVideoOnlyStream.value,
selectedAudioOnlyStream.value,
initialItemConfig?.index,
initialItemConfig?.originalIndex,
initialItemConfig?.totalLength,
);
if (finalFilenameTempRebuilt != null && finalFilenameTempRebuilt.isNotEmpty) {
Expand Down Expand Up @@ -189,7 +189,7 @@ Future<void> showDownloadVideoBottomSheet({
streamResult.value,
playlistInfo,
playlistId,
index,
originalIndex,
totalLength,
autoExtract: settings.youtube.autoExtractVideoTagsFromInfo.value,
initialBuilding: initialItemConfig?.ffmpegTags,
Expand Down Expand Up @@ -717,7 +717,7 @@ Future<void> showDownloadVideoBottomSheet({
: const BoxDecoration(),
onTap: () async {
final itemConfig = YoutubeItemDownloadConfig(
index: index,
originalIndex: originalIndex,
totalLength: totalLength,
playlistId: playlistId,
id: DownloadTaskVideoId(videoId: videoId),
Expand Down
39 changes: 26 additions & 13 deletions lib/youtube/pages/yt_playlist_download_subpage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class _YTPlaylistDownloadPageState extends State<YTPlaylistDownloadPage> {
void initState() {
_groupName.value = widget.playlistName;
_addAllYTIDsToSelected();
onRenameAllTasks(settings.youtube.downloadFilenameBuilder.value); // needed to provide initial data specially original indices
super.initState();
}

Expand All @@ -90,16 +91,16 @@ class _YTPlaylistDownloadPageState extends State<YTPlaylistDownloadPage> {
}

void onRenameAllTasks(String? defaultFilename) {
widget.ids.mapIndexed((ytid, index) => _configMap[ytid.id] = _getDummyDownloadConfig(ytid.id, index, defaultFilename: defaultFilename)).toList();
widget.ids.mapIndexed((ytid, originalIndex) => _configMap[ytid.id] = _getDummyDownloadConfig(ytid.id, originalIndex, defaultFilename: defaultFilename)).toList();
}

YoutubeItemDownloadConfig _getDummyDownloadConfig(String id, int index, {String? defaultFilename}) {
YoutubeItemDownloadConfig _getDummyDownloadConfig(String id, int originalIndex, {String? defaultFilename}) {
final streamInfoItem = widget.infoLookup[id];
final filenameBuilderSettings = settings.youtube.downloadFilenameBuilder.value;
final filename =
filenameBuilderSettings.isNotEmpty ? filenameBuilderSettings : (defaultFilename ?? streamInfoItem?.title ?? YoutubeInfoController.utils.getVideoName(id) ?? id);
return YoutubeItemDownloadConfig(
index: index,
originalIndex: originalIndex,
totalLength: widget.playlistInfo?.videosCount ?? widget.ids.length,
playlistId: widget.playlistInfo?.id,
id: DownloadTaskVideoId(videoId: id),
Expand All @@ -123,10 +124,10 @@ class _YTPlaylistDownloadPageState extends State<YTPlaylistDownloadPage> {

Future<void> _onEditIconTap({
required String id,
required int index,
required int originalIndex,
}) async {
await showDownloadVideoBottomSheet(
index: _configMap[id]?.index,
originalIndex: _configMap[id]?.originalIndex,
totalLength: _configMap[id]?.totalLength,
streamInfoItem: widget.infoLookup[id],
playlistInfo: widget.playlistInfo,
Expand Down Expand Up @@ -454,8 +455,8 @@ class _YTPlaylistDownloadPageState extends State<YTPlaylistDownloadPage> {
SliverFixedExtentList.builder(
itemExtent: Dimensions.youtubeCardItemExtent * _hmultiplier,
itemCount: widget.ids.length,
itemBuilder: (context, index) {
final id = widget.ids[index].id;
itemBuilder: (context, originalIndex) {
final id = widget.ids[originalIndex].id;
final info = widget.infoLookup[id] ?? YoutubeInfoController.utils.getStreamInfoSync(id);
final duration = info?.durSeconds?.secondsLabel;

Expand Down Expand Up @@ -488,8 +489,8 @@ class _YTPlaylistDownloadPageState extends State<YTPlaylistDownloadPage> {
break;
}
}
if (latestIndex != null && index > latestIndex) {
final selectedRange = widget.ids.getRange(latestIndex + 1, index + 1);
if (latestIndex != null && originalIndex > latestIndex) {
final selectedRange = widget.ids.getRange(latestIndex + 1, originalIndex + 1);
selectedRange.toList().loop((e) {
if (!_selectedList.contains(e.id)) _selectedList.add(e.id);
});
Expand Down Expand Up @@ -553,7 +554,7 @@ class _YTPlaylistDownloadPageState extends State<YTPlaylistDownloadPage> {
horizontalPadding: 4.0,
icon: Broken.edit_2,
iconSize: 20.0,
onPressed: () => _onEditIconTap(id: id, index: index),
onPressed: () => _onEditIconTap(id: id, originalIndex: originalIndex),
),
Checkbox.adaptive(
visualDensity: VisualDensity.compact,
Expand All @@ -572,7 +573,7 @@ class _YTPlaylistDownloadPageState extends State<YTPlaylistDownloadPage> {
borderRadius: BorderRadius.only(bottomLeft: Radius.circular(6.0.multipliedRadius)),
padding: const EdgeInsets.only(top: 2.0, right: 8.0, left: 6.0, bottom: 2.0),
child: Text(
'${index + 1}',
'${originalIndex + 1}',
style: context.textTheme.displaySmall,
),
),
Expand Down Expand Up @@ -627,10 +628,22 @@ class _YTPlaylistDownloadPageState extends State<YTPlaylistDownloadPage> {
onPressed: () async {
if (_selectedList.isEmpty) return;
if (!await requestManageStoragePermission()) return;
final itemsConfig = _selectedList.value
.map(
(id) =>
_configMap[id] ??

// -- this is not really used since initState() calls onRenameAllTasks() which fills _configMap
_getDummyDownloadConfig(
id,
widget.ids.indexWhere((element) => element.id == id),
),
)
.toList();
NamidaNavigator.inst.popPage();
YoutubeController.inst.downloadYoutubeVideos(
groupName: DownloadTaskGroupName(groupName: widget.playlistName),
itemsConfig: _selectedList.value.mapIndexed((id, index) => _configMap[id] ?? _getDummyDownloadConfig(id, index)).toList(),
groupName: DownloadTaskGroupName(groupName: _groupName.value),
itemsConfig: itemsConfig,
useCachedVersionsIfAvailable: useCachedVersionsIfAvailable,
autoExtractTitleAndArtist: autoExtractTitleAndArtist,
keepCachedVersionsIfDownloaded: keepCachedVersionsIfDownloaded,
Expand Down
4 changes: 2 additions & 2 deletions lib/youtube/widgets/yt_download_task_item_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ class YTDownloadTaskItemCard extends StatelessWidget {

void _onEditIconTap({required YoutubeItemDownloadConfig config, required BuildContext context}) async {
await showDownloadVideoBottomSheet(
index: config.index,
originalIndex: config.originalIndex,
totalLength: config.totalLength,
streamInfoItem: config.streamInfoItem,
playlistId: config.playlistId,
Expand Down Expand Up @@ -479,7 +479,7 @@ class YTDownloadTaskItemCard extends StatelessWidget {
openOnTap: true,
openOnLongPress: true,
childrenDefault: () => YTUtils.getVideoCardMenuItems(
downloadIndex: item.index,
downloadIndex: item.originalIndex,
totalLength: item.totalLength,
playlistId: item.playlistId,
streamInfoItem: infoFinal,
Expand Down
4 changes: 2 additions & 2 deletions lib/youtube/youtube_miniplayer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ class YoutubeMiniPlayerState extends State<YoutubeMiniPlayer> {
title: videoPercText ?? audioPercText ?? lang.DOWNLOAD,
icon: icon,
onLongPress: () async => await showDownloadVideoBottomSheet(
videoId: currentId, index: null, totalLength: null, playlistId: null, streamInfoItem: null),
videoId: currentId, originalIndex: null, totalLength: null, playlistId: null, streamInfoItem: null),
onPressed: () async {
if (isDownloading) {
YoutubeController.inst.pauseDownloadTask(
Expand All @@ -619,7 +619,7 @@ class YoutubeMiniPlayerState extends State<YoutubeMiniPlayer> {
);
} else {
await showDownloadVideoBottomSheet(
videoId: currentId, index: null, totalLength: null, playlistId: null, streamInfoItem: null);
videoId: currentId, originalIndex: null, totalLength: null, playlistId: null, streamInfoItem: null);
}
},
);
Expand Down
10 changes: 5 additions & 5 deletions lib/youtube/yt_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ class YTUtils {
onTap: () {
showDownloadVideoBottomSheet(
videoId: videoId,
index: downloadIndex,
originalIndex: downloadIndex,
playlistId: playlistId,
totalLength: totalLength,
streamInfoItem: streamInfoItem,
Expand Down Expand Up @@ -559,7 +559,7 @@ class YTUtils {
VideoStreamsResult? streams,
PlaylistBasicInfo? playlistInfo,
String? playlistId,
int? index,
int? originalIndex,
int? totalLength, {
bool autoExtract = true,
Map<String, String?>? initialBuilding,
Expand All @@ -577,7 +577,7 @@ class YTUtils {
final userText = ib.value;
if (userText != null) {
infoMap[ib.key] = YoutubeController.filenameBuilder
.rebuildFilenameWithDecodedParams(userText, id, streams, videoPage, streamInfoItem, playlistInfo, videoStream, audioStream, index, totalLength) ??
.rebuildFilenameWithDecodedParams(userText, id, streams, videoPage, streamInfoItem, playlistInfo, videoStream, audioStream, originalIndex, totalLength) ??
userText;
}
}
Expand All @@ -587,15 +587,15 @@ class YTUtils {
for (final di in defaultInfoSett.entries) {
final defaultText = di.value;
infoMap[di.key] ??= YoutubeController.filenameBuilder
.rebuildFilenameWithDecodedParams(defaultText, id, streams, videoPage, streamInfoItem, playlistInfo, videoStream, audioStream, index, totalLength) ??
.rebuildFilenameWithDecodedParams(defaultText, id, streams, videoPage, streamInfoItem, playlistInfo, videoStream, audioStream, originalIndex, totalLength) ??
defaultText;
}

final defaultInfo = getDefaultTagsFieldsBuilders(autoExtract);
for (final di in defaultInfo.entries) {
final defaultText = di.value;
infoMap[di.key] ??= YoutubeController.filenameBuilder
.rebuildFilenameWithDecodedParams(defaultText, id, streams, videoPage, streamInfoItem, playlistInfo, videoStream, audioStream, index, totalLength) ??
.rebuildFilenameWithDecodedParams(defaultText, id, streams, videoPage, streamInfoItem, playlistInfo, videoStream, audioStream, originalIndex, totalLength) ??
'';
}

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: namida
description: A Beautiful and Feature-rich Music Player, With YouTube & Video Support Built in Flutter
publish_to: "none"
version: 4.5.58-beta+241019202
version: 4.5.6-beta+241019204

environment:
sdk: ">=3.4.0 <4.0.0"
Expand Down

0 comments on commit 0266cd5

Please sign in to comment.