Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Playlist Sort By Video Duration #5627

Open
wants to merge 12 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
16 changes: 15 additions & 1 deletion src/renderer/helpers/playlists.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export const SORT_BY_VALUES = {
AuthorDescending: 'author_descending',
VideoTitleAscending: 'video_title_ascending',
VideoTitleDescending: 'video_title_descending',
VideoDurationAscending: 'video_duration_ascending',
VideoDurationDescending: 'video_duration_descending',
Custom: 'custom'
}

Expand All @@ -19,7 +21,9 @@ export function getSortedPlaylistItems(playlistItems, sortOrder, locale, reverse
sortOrder === SORT_BY_VALUES.VideoTitleAscending ||
sortOrder === SORT_BY_VALUES.VideoTitleDescending ||
sortOrder === SORT_BY_VALUES.AuthorAscending ||
sortOrder === SORT_BY_VALUES.AuthorDescending
sortOrder === SORT_BY_VALUES.AuthorDescending ||
sortOrder === SORT_BY_VALUES.VideoDurationAscending ||
sortOrder === SORT_BY_VALUES.VideoDurationDescending
) {
collator = new Intl.Collator([locale, 'en'])
}
Expand All @@ -45,6 +49,16 @@ function compareTwoPlaylistItems(a, b, sortOrder, collator) {
return collator.compare(a.author, b.author)
case SORT_BY_VALUES.AuthorDescending:
return collator.compare(b.author, a.author)
case SORT_BY_VALUES.VideoDurationAscending: {
const aLengthForSort = (isNaN(a.lengthSeconds) || a.lengthSeconds === 0) ? 0 : a.lengthSeconds
const bLengthForSort = (isNaN(b.lengthSeconds) || b.lengthSeconds === 0) ? 0 : b.lengthSeconds
return aLengthForSort - bLengthForSort
}
case SORT_BY_VALUES.VideoDurationDescending: {
const aLengthForSort = (isNaN(a.lengthSeconds) || a.lengthSeconds === 0) ? 0 : a.lengthSeconds
const bLengthForSort = (isNaN(b.lengthSeconds) || b.lengthSeconds === 0) ? 0 : b.lengthSeconds
return bLengthForSort - aLengthForSort
}
default:
console.error(`Unknown sortOrder: ${sortOrder}`)
return 0
Expand Down
20 changes: 20 additions & 0 deletions src/renderer/views/Playlist/Playlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export default defineComponent({
getPlaylistInfoDebounce: function() {},
playlistInEditMode: false,
forceListView: false,
alreadyShownNotice: false,

videoSearchQuery: '',

Expand Down Expand Up @@ -180,6 +181,7 @@ export default defineComponent({
return this.sortOrder === SORT_BY_VALUES.Custom
},
sortedPlaylistItems: function () {
this.showNoticesSometimes()
return getSortedPlaylistItems(this.playlistItems, this.sortOrder, this.currentLocale)
},
visiblePlaylistItems: function () {
Expand Down Expand Up @@ -214,6 +216,10 @@ export default defineComponent({
return this.$t('Playlist.Sort By.AuthorAscending')
case SORT_BY_VALUES.AuthorDescending:
return this.$t('Playlist.Sort By.AuthorDescending')
case SORT_BY_VALUES.VideoDurationAscending:
return this.$t('Playlist.Sort By.VideoDurationAscending')
case SORT_BY_VALUES.VideoDurationDescending:
return this.$t('Playlist.Sort By.VideoDurationDescending')
default:
console.error(`Unknown sort: ${k}`)
return k
Expand Down Expand Up @@ -420,6 +426,20 @@ export default defineComponent({
showToast(this.$t('User Playlists.SinglePlaylistView.Toast.This playlist does not exist'))
},

showNoticesSometimes: function () {
if (this.alreadyShownNotice) return
Hoverth marked this conversation as resolved.
Show resolved Hide resolved
if (
this.sortOrder === SORT_BY_VALUES.VideoDurationAscending ||
this.sortOrder === SORT_BY_VALUES.VideoDurationDescending
) {
const anyVideoMissingDuration = this.playlistItems.some(v => isNaN(v.lengthSeconds) || v.lengthSeconds === 0)
if (anyVideoMissingDuration) {
showToast(this.$t('User Playlists.SinglePlaylistView.Toast.This playlist has a video with a duration error'), 5000)
this.alreadyShownNotice = true
}
}
},

getNextPage: function () {
switch (this.infoSource) {
case 'local':
Expand Down
4 changes: 4 additions & 0 deletions static/locales/en-GB.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ User Playlists:
This playlist is protected and cannot be removed.: This playlist is protected
and cannot be removed.
This playlist does not exist: This playlist does not exist

This playlist has a video with a duration error: This playlist contains at least one video that doesn't have a duration, it will be sorted as if their duration is zero.
Playlist {playlistName} has been deleted.: Playlist {playlistName} has been
deleted.
Video has been removed: Video has been removed
Expand Down Expand Up @@ -1006,6 +1008,8 @@ Playlist:
AuthorAscending: Author (A-Z)
AuthorDescending: Author (Z-A)
VideoTitleAscending: Title (A-Z)
VideoDurationAscending: Duration (Shortest first)
VideoDurationDescending: Duration (Longest first)
Toggle Theatre Mode: 'Toggle Theatre Mode'
Change Format:
Change Media Formats: 'Change Media Formats'
Expand Down
4 changes: 4 additions & 0 deletions static/locales/en-US.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ User Playlists:
Playlist {playlistName} is the new quick bookmark playlist.: Playlist {playlistName} is the new quick bookmark playlist.

This playlist does not exist: This playlist does not exist

This playlist has a video with a duration error: This playlist contains at least one video that doesn't have a duration, it will be sorted as if their duration is zero.
AddVideoPrompt:
Select a playlist to add your N videos to: 'Select a playlist to add your video to | Select a playlist to add your {videoCount} videos to'
N playlists selected: '{playlistCount} Selected'
Expand Down Expand Up @@ -928,6 +930,8 @@ Playlist:
AuthorDescending: Author (Z-A)
VideoTitleAscending: Title (A-Z)
VideoTitleDescending: Title (Z-A)
VideoDurationAscending: Duration (Shortest first)
VideoDurationDescending: Duration (Longest first)
Custom: Custom

# On Video Watch Page
Expand Down