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

[WIP] Poster Buttons Context Menus #1234

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
80 changes: 80 additions & 0 deletions Swiftfin/Components/PosterButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,86 @@ extension PosterButton {

// MARK: Default Content

struct ContextMenuView: View {

let item: Item
var onPlay: (() -> Void)?
var onRestart: (() -> Void)?
var onShuffle: (() -> Void)?
var onPlayed: (() -> Void)?
var onUnplayed: (() -> Void)?
var onPlayedToggle: (() -> Void)?
var onFavoriteToggle: (() -> Void)?
var onDownload: (() -> Void)?

var body: some View {
if let baseItem = item as? BaseItemDto {

if let playedPercentage = baseItem.userData?.playedPercentage, playedPercentage > 0 && playedPercentage < 100 {
if let onRestart = onRestart {
Button(action: onRestart) {
Label("Play From Beginning", systemImage: "memories")
}
}
if let onPlay = onPlay {
Button(action: onPlay) {
Label("Resume", systemImage: "forward.end.fill")
}
}
} else if let onPlay = onPlay {
Button(action: onPlay) {
Label("Play", systemImage: "play.fill")
}
}

if let onShuffle = onShuffle {
Button(action: onShuffle) {
Label("Shuffle", systemImage: "shuffle")
}
}

if let onPlayedToggle = onPlayedToggle {
if let isPlayed = baseItem.userData?.isPlayed {
Button(action: onPlayedToggle) {
Label(
isPlayed ? L10n.unplayed : L10n.played,
systemImage: isPlayed ? "minus.circle" : "checkmark.circle"
)
}
}
} else {
if let onPlayed = onPlayed {
Button(action: onPlayed) {
Label(L10n.played, systemImage: "checkmark.circle")
}
}
if let onUnplayed = onUnplayed {
Button(action: onUnplayed) {
Label(L10n.unplayed, systemImage: "minus.circle")
}
}
}

if let isFavorite = baseItem.userData?.isFavorite,
let onFavoriteToggle = onFavoriteToggle
{
Button(action: onFavoriteToggle) {
Label(
isFavorite ? "Unfavorite" : "Favorite",
systemImage: isFavorite ? "heart.slash" : "heart"
)
}
}

if let onDownload = onDownload {
Button(action: onDownload) {
Label("Download", systemImage: "arrow.down.circle")
}
}
}
}
}

struct TitleContentView: View {

let item: Item
Expand Down
81 changes: 66 additions & 15 deletions Swiftfin/Views/PagingLibraryView/PagingLibraryView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ struct PagingLibraryView<Element: Poster>: View {

@EnvironmentObject
private var router: LibraryCoordinator<Element>.Router
@EnvironmentObject
private var mainRouter: MainCoordinator.Router

@State
private var layout: CollectionVGridLayout
Expand Down Expand Up @@ -182,22 +184,43 @@ struct PagingLibraryView<Element: Poster>: View {

@ViewBuilder
private func landscapeGridItemView(item: Element) -> some View {
PosterButton(item: item, type: .landscape)
.content {
if item.showTitle {
PosterButton.TitleContentView(item: item)
.backport
.lineLimit(1, reservesSpace: true)
} else if viewModel.parent?.libraryType == .folder {
PosterButton.TitleContentView(item: item)
.backport
.lineLimit(1, reservesSpace: true)
.hidden()
}
}
.onSelect {
onSelect(item)
PosterButton(
item: item,
type: .landscape
)
.content {
if item.showTitle {
PosterButton.TitleContentView(item: item)
.backport
.lineLimit(1, reservesSpace: true)
} else if viewModel.parent?.libraryType == .folder {
PosterButton.TitleContentView(item: item)
.backport
.lineLimit(1, reservesSpace: true)
.hidden()
}
}
.onSelect {
onSelect(item)
}
.contextMenu {
PosterButton.ContextMenuView(
item: item,
onPlay: {
if let baseItem = item as! BaseItemDto? {
let mediaViewModel = ItemViewModel(item: baseItem)
if let playButtonItem = mediaViewModel.playButtonItem,
let selectedMediaSource = mediaViewModel.selectedMediaSource
{
mainRouter.route(
to: \.videoPlayer,
OnlineVideoPlayerManager(item: playButtonItem, mediaSource: selectedMediaSource)
)
}
}
}
)
}
}

@ViewBuilder
Expand All @@ -218,6 +241,34 @@ struct PagingLibraryView<Element: Poster>: View {
.onSelect {
onSelect(item)
}
.contextMenu {
PosterButton.ContextMenuView(
item: item,
onPlay: {
if let baseItem = item as! BaseItemDto? {
if let selectedMediaSource = baseItem.mediaSources?.first {
mainRouter.route(
to: \.videoPlayer,
OnlineVideoPlayerManager(
item: baseItem,
mediaSource: selectedMediaSource
)
)
}
}
},
onPlayedToggle: {
if let baseItem = item as! BaseItemDto? {
ItemViewModel(item: baseItem).send(.toggleIsPlayed)
}
},
onFavoriteToggle: {
if let baseItem = item as! BaseItemDto? {
ItemViewModel(item: baseItem).send(.toggleIsFavorite)
}
}
)
}
}

@ViewBuilder
Expand Down