Skip to content

Commit

Permalink
Enhance caching related behavior (#19908)
Browse files Browse the repository at this point in the history
* When an item is selected and the last played position is closed to
  the end of the file, don't set the last played position to current time.
  The user just may just want to play it again.

* When pressing previous item button, rewind the video to the start
  first.

* Implement "Remove played contents" context menu item of folders
* Implement "Cache for offline play" context menu item of folders
  • Loading branch information
sangwoo108 authored Aug 29, 2023
1 parent a5a01d5 commit 7685a58
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 27 deletions.
68 changes: 43 additions & 25 deletions components/playlist/browser/resources/components/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,32 +122,50 @@ function BackButton ({

function PlaylistHeader ({ playlistId }: { playlistId: string }) {
const playlist = usePlaylist(playlistId)
const contextualMenuItems = playlist?.items.length
? [
{
name: getLocalizedString('bravePlaylistContextMenuEdit'),
iconName: 'list-bullet-default',
onClick: () =>
getPlaylistActions().setPlaylistEditMode(PlaylistEditMode.BULK_EDIT)
},
// TODO(sko) We don't support this yet.
// { name: 'Share', iconName: 'share-macos', onClick: () => {} },
{
name: getLocalizedString(
'bravePlaylistContextMenuKeepForOfflinePlaying'
),
iconName: 'cloud-download',
onClick: () => {}
},
{
name: getLocalizedString(
'bravePlaylistContextMenuRemovePlayedContents'
),
iconName: 'list-checks',
onClick: () => {}
const contextualMenuItems = []
if (playlist?.items.length) {
contextualMenuItems.push({
name: getLocalizedString('bravePlaylistContextMenuEdit'),
iconName: 'list-bullet-default',
onClick: () =>
getPlaylistActions().setPlaylistEditMode(PlaylistEditMode.BULK_EDIT)
})

// TODO(sko) We don't support this yet.
// contextualMenuItems.push({ name: 'Share', iconName: 'share-macos', onClick: () => {} })

const uncachedItems = playlist.items.filter(item => !item.cached)
if (uncachedItems.length) {
contextualMenuItems.push({
name: getLocalizedString(
'bravePlaylistContextMenuKeepForOfflinePlaying'
),
iconName: 'cloud-download',
onClick: () => {
uncachedItems.forEach(item =>
getPlaylistAPI().recoverLocalData(item.id)
)
}
]
: []
})
}

const playedItems = playlist.items.filter(
item => item.lastPlayedPosition >= Math.floor(+item.duration / 1e6)
)
if (playedItems.length) {
contextualMenuItems.push({
name: getLocalizedString(
'bravePlaylistContextMenuRemovePlayedContents'
),
iconName: 'list-checks',
onClick: () => {
playedItems.forEach(item =>
getPlaylistAPI().removeItemFromPlaylist(playlistId, item.id)
)
}
})
}
}

const isDefaultPlaylist = playlist?.id === 'default'
if (contextualMenuItems && !isDefaultPlaylist) {
Expand Down
6 changes: 5 additions & 1 deletion components/playlist/browser/resources/components/player.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ export default function Player () {
videoElement.src !== currentItem?.mediaPath.url
) {
videoElement.src = currentItem.mediaPath.url
videoElement.currentTime = currentItem.lastPlayedPosition
if (currentItem.lastPlayedPosition < +currentItem.duration / 1e6 - 5) {
// When the last played position is close to the end(5sec), don't
// set last position to the current time.
videoElement.currentTime = currentItem.lastPlayedPosition
}
}
}
}, [currentItem, videoElement])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,15 @@ export default function PlayerControls ({ videoElement, className }: Props) {
<Control
iconName='start-outline'
size='jumbo'
onClick={() => getPlayerActions().playPreviousItem()}
onClick={() => {
if (!videoElement) return

if (videoElement.currentTime > 5) {
videoElement.currentTime = 0
} else {
getPlayerActions().playPreviousItem()
}
}}
></Control>
<Control
iconName='rewind-15'
Expand Down

0 comments on commit 7685a58

Please sign in to comment.