Skip to content

Commit

Permalink
Add My List functions to movie details
Browse files Browse the repository at this point in the history
  • Loading branch information
1hitsong committed Dec 27, 2024
1 parent 7bf3a66 commit 723ec66
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 69 deletions.
38 changes: 38 additions & 0 deletions components/movies/MovieDetails.bs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import "pkg:/source/enums/KeyCode.bs"
import "pkg:/source/enums/MediaStreamType.bs"
import "pkg:/source/enums/PersonType.bs"
import "pkg:/source/enums/String.bs"
import "pkg:/source/enums/TaskControl.bs"
import "pkg:/source/enums/VideoType.bs"
import "pkg:/source/enums/ViewLoadStatus.bs"

Expand Down Expand Up @@ -48,6 +49,40 @@ sub init()
m.global.queueManager.callFunc("setLastKnownItemID", string.empty)

m.top.observeField("itemContent", "itemContentChanged")

m.loadItemsTask = createObject("roSGNode", "LoadItemsTask")
m.loadItemsTask.observeField("content", "onMyListLoaded")
m.loadItemsTask.itemsToLoad = "isInMyList"
end sub

sub onIsInMyListChanged()
myListButton = m.top.findNode("mylist-button")
if not isValid(myListButton) then return

if m.top.isInMyList
myListButton.text = tr("In My List")
myListButton.iconBlendColor = ColorPalette.RED
else
myListButton.text = tr("Add To My List")
myListButton.iconBlendColor = ColorPalette.WHITE
end if
end sub

sub onMyListLoaded()
isInMyListData = m.loadItemsTask.content
m.loadItemsTask.unobserveField("content")
m.loadItemsTask.content = []

myListButton = m.top.findNode("mylist-button")
if not isValid(myListButton) then return

' Invalid data returned, remove button to prevent issues
if not isValidAndNotEmpty(isInMyListData)
m.buttonGrp.content.removeChild(myListButton)
return
end if

m.top.isInMyList = isInMyListData[0]
end sub

sub onMovieExtrasHasItems()
Expand Down Expand Up @@ -259,6 +294,9 @@ sub itemContentChanged()
itemData = item.json
m.top.id = itemData.id

m.loadItemsTask.itemId = m.top.id
m.loadItemsTask.control = TaskControl.RUN

if itemData.UserData.PlaybackPositionTicks > 0
playButton = m.top.findNode("play-button")
playButton.text = `Play / Resume from ${ticksToHuman(itemData.UserData.PlaybackPositionTicks)}`
Expand Down
2 changes: 2 additions & 0 deletions components/movies/MovieDetails.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
<ButtonData id="part-button" iconSide="left" fontSize="35" padding="35" icon="pkg:/images/icons/puzzle.png" height="75" width="600" />
<ButtonData id="options-button" text="Options" iconSide="left" fontSize="35" padding="35" icon="pkg:/images/icons/options.png" height="75" width="600" />
<ButtonData id="trailer-button" text="Play Trailer" iconSide="left" fontSize="35" padding="35" icon="pkg:/images/icons/play.png" height="75" width="600" />
<ButtonData id="mylist-button" text="Add To My List" iconSide="left" fontSize="35" padding="35" icon="pkg:/images/icons/plus.png" height="75" width="600" />
<ButtonData id="watched-button" text="Watched" iconSide="left" fontSize="35" padding="35" icon="pkg:/images/icons/check.png" height="75" width="600" />
<ButtonData id="favorite-button" text="Favorite" iconSide="left" fontSize="35" padding="35" icon="pkg:/images/icons/heart.png" height="75" width="600" />
<ButtonData id="editSubtitlesButton" text="Manage Subtitles" iconSide="left" fontSize="35" padding="35" icon="pkg:/images/icons/cc.png" height="75" width="600" />
Expand All @@ -71,6 +72,7 @@
<field id="itemContent" type="node" />
<field id="logoImageURI" type="string" onChange="onLogoImageURIChange" />
<field id="trailerAvailable" type="bool" onChange="trailerAvailableChanged" value="true" />
<field id="isInMyList" type="bool" onChange="onIsInMyListChanged" />
<field id="additionalParts" type="assocarray" onChange="additionalPartsChanged" />
<field id="selectedPart" type="assocarray" onChange="onSelectedPartChanged" />
<field id="selectedAudioStreamIndex" type="integer" />
Expand Down
Binary file added images/icons/plus.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
85 changes: 85 additions & 0 deletions source/MainActions.bs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,19 @@ namespace MainAction
movie.watched = not movie.watched
end sub

sub onMyListButtonClicked(activeScene as object)
movie = chainLookup(activeScene, "itemContent")
if not isValid(movie.id) then return

if activeScene.isInMyList
MainAction.removeItemFromMyList(movie.id)
else
MainAction.addItemToMyList(movie.id)
end if

activeScene.isInMyList = not activeScene.isInMyList
end sub

sub onFavoriteButtonClicked(activeScene as object)
movie = chainLookup(activeScene, "itemContent")
if not isChainValid(movie, "favorite") or not isValid(movie.id) then return
Expand Down Expand Up @@ -181,4 +194,76 @@ namespace MainAction
dialog.close = true
end if
end sub

' Add item to user's My List playlist
sub addItemToMyList(itemID as string)
data = api.GetUserViews({ "userId": m.global.session.user.id })
if not isChainValid(data, "items") then return

myListPlaylist = invalid

for each item in data.LookupCI("items")
if isStringEqual(item.LookupCI("CollectionType"), "playlists")
myListPlaylist = api.items.Get({
"userid": m.global.session.user.id,
"includeItemTypes": "Playlist",
"nameStartsWith": "|My List|",
"parentId": item.LookupCI("id")
})
exit for
end if
end for

' My list playlist exists. Add item to it
if isValid(myListPlaylist) and isValidAndNotEmpty(myListPlaylist.items)
api.playlists.Add(myListPlaylist.items[0].LookupCI("id"), {
ids: itemID,
userid: m.global.session.user.id
})
return
end if

' My list playlist does not exist. Create it with this item
api.playlists.Create({
name: "|My List|",
ids: [itemID],
userid: m.global.session.user.id,
mediatype: "Unknown",
users: [
{
userid: m.global.session.user.id,
canedit: true
}
],
ispublic: false
})
end sub

' Remove item from user's My List playlist
sub removeItemFromMyList(itemID as string)

data = api.GetUserViews({ "userId": m.global.session.user.id })
if not isChainValid(data, "items") then return

myListPlaylist = invalid

for each item in data.LookupCI("items")
if isStringEqual(item.LookupCI("CollectionType"), "playlists")
myListPlaylist = api.items.Get({
"userid": m.global.session.user.id,
"includeItemTypes": "Playlist",
"nameStartsWith": "|My List|",
"parentId": item.LookupCI("id")
})
exit for
end if
end for

' My list playlist exists. Remove item from it
if isValid(myListPlaylist) and isValidAndNotEmpty(myListPlaylist.items)
api.playlists.Remove(myListPlaylist.items[0].LookupCI("id"), {
entryIds: itemID
})
end if
end sub
end namespace
85 changes: 16 additions & 69 deletions source/MainEventHandlers.bs
Original file line number Diff line number Diff line change
Expand Up @@ -1079,6 +1079,12 @@ sub onButtonSelectedEvent(msg)
return
end if

' Toggle item in My List
if isStringEqual(btn, "mylist-button")
MainAction.onMyListButtonClicked(activeScene)
return
end if

' Toggle favorite state
if isStringEqual(btn, "favorite-button")
MainAction.onFavoriteButtonClicked(activeScene)
Expand Down Expand Up @@ -1313,82 +1319,23 @@ end sub
sub processLibraryItemPopup(selectedPopupButton as string, itemID as string)
' Add item to user's list
if isStringEqual(selectedPopupButton, "Add To My List")
data = api.GetUserViews({ "userId": m.global.session.user.id })
if not isChainValid(data, "items") then return

myListPlaylist = invalid

for each item in data.LookupCI("items")
if isStringEqual(item.LookupCI("CollectionType"), "playlists")
myListPlaylist = api.items.Get({
"userid": m.global.session.user.id,
"includeItemTypes": "Playlist",
"nameStartsWith": "|My List|",
"parentId": item.LookupCI("id")
})
exit for
end if
end for

' My list playlist exists. Add item to it
if isValid(myListPlaylist) and isValidAndNotEmpty(myListPlaylist.items)
api.playlists.Add(myListPlaylist.items[0].LookupCI("id"), {
ids: itemID,
userid: m.global.session.user.id
})
return
end if

' My list playlist does not exist. Create it with this item
api.playlists.Create({
name: "|My List|",
ids: [itemID],
userid: m.global.session.user.id,
mediatype: "Unknown",
users: [
{
userid: m.global.session.user.id,
canedit: true
}
],
ispublic: false
})
MainAction.addItemToMyList(itemID)
return
end if

' Remove item from user's list
if isStringEqual(selectedPopupButton, "Remove From My List")
data = api.GetUserViews({ "userId": m.global.session.user.id })
if not isChainValid(data, "items") then return

myListPlaylist = invalid

for each item in data.LookupCI("items")
if isStringEqual(item.LookupCI("CollectionType"), "playlists")
myListPlaylist = api.items.Get({
"userid": m.global.session.user.id,
"includeItemTypes": "Playlist",
"nameStartsWith": "|My List|",
"parentId": item.LookupCI("id")
})
exit for
end if
end for
MainAction.removeItemFromMyList(itemID)

' My list playlist exists. Remove item from it
if isValid(myListPlaylist) and isValidAndNotEmpty(myListPlaylist.items)
api.playlists.Remove(myListPlaylist.items[0].LookupCI("id"), {
entryIds: itemID
})

activeScene = m.global.sceneManager.callFunc("getActiveScene")
if not isValid(activeScene) then return
' If we're in My List, remove item from the item grid
activeScene = m.global.sceneManager.callFunc("getActiveScene")
if not isValid(activeScene) then return

if isStringEqual(activeScene.parentItem.collectionType, CollectionType.MYLIST)
itemGrid = activeScene.findNode("itemGrid")
if not isValid(itemGrid) then return
if isStringEqual(activeScene.parentItem.collectionType, CollectionType.MYLIST)
itemGrid = activeScene.findNode("itemGrid")
if not isValid(itemGrid) then return

itemGrid.content.removeChildIndex(itemGrid.itemFocused)
end if
itemGrid.content.removeChildIndex(itemGrid.itemFocused)
end if

return
Expand Down

0 comments on commit 723ec66

Please sign in to comment.