Playlists menu for iOS

This commit is contained in:
Arkadiusz Fal 2022-12-10 21:47:38 +01:00
parent e9c219a76e
commit 7df397b662

View File

@ -56,41 +56,9 @@ struct PlaylistsView: View {
} }
var body: some View { var body: some View {
BrowserPlayerControls(toolbar: { BrowserPlayerControls {
HStack {
HStack {
newPlaylistButton
.offset(x: -10)
if currentPlaylist != nil {
editPlaylistButton
}
}
if !model.isEmpty {
Spacer()
}
HStack {
if model.isEmpty {
Text("No Playlists")
.foregroundColor(.secondary)
} else {
selectPlaylistButton
.transaction { t in t.animation = .none }
}
}
Spacer()
if currentPlaylist != nil {
playButton
.offset(x: 10)
}
}
.padding(.horizontal)
}) {
SignInRequiredView(title: "Playlists".localized()) { SignInRequiredView(title: "Playlists".localized()) {
ScrollView { Section {
VStack { VStack {
#if os(tvOS) #if os(tvOS)
toolbar toolbar
@ -145,7 +113,12 @@ struct PlaylistsView: View {
model.load(force: true) { model.reloadPlaylists.toggle() } model.load(force: true) { model.reloadPlaylists.toggle() }
} }
} }
.navigationBarTitleDisplayMode(RefreshControl.navigationBarTitleDisplayMode) .navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .principal) {
playlistsMenu
}
}
#endif #endif
#if os(tvOS) #if os(tvOS)
.fullScreenCover(isPresented: $showingNewPlaylist, onDismiss: selectCreatedPlaylist) { .fullScreenCover(isPresented: $showingNewPlaylist, onDismiss: selectCreatedPlaylist) {
@ -187,6 +160,37 @@ struct PlaylistsView: View {
#endif #endif
} }
#if os(iOS)
var playlistsMenu: some View {
Menu {
selectPlaylistButton
Section {
if let currentPlaylist {
playButton
editPlaylistButton
FavoriteButton(item: FavoriteItem(section: .playlist(currentPlaylist.id)))
.labelStyle(.iconOnly)
}
}
newPlaylistButton
} label: {
HStack(spacing: 12) {
Text(currentPlaylist?.title ?? "Playlists")
.font(.headline)
.foregroundColor(.primary)
Image(systemName: "chevron.down.circle.fill")
.foregroundColor(.accentColor)
.imageScale(.small)
}
.transaction { t in t.animation = nil }
}
}
#endif
#if os(tvOS) #if os(tvOS)
var toolbar: some View { var toolbar: some View {
HStack { HStack {
@ -214,6 +218,7 @@ struct PlaylistsView: View {
newPlaylistButton newPlaylistButton
.padding(.leading, 40) .padding(.leading, 40)
} }
.labelStyle(.iconOnly)
} }
#endif #endif
@ -277,19 +282,10 @@ struct PlaylistsView: View {
Button("Cancel", role: .cancel) {} Button("Cancel", role: .cancel) {}
} }
#else #else
Menu { Picker("Current Playlist", selection: $selectedPlaylistID) {
ForEach(model.all) { playlist in ForEach(model.all) { playlist in
Button(action: { selectedPlaylistID = playlist.id }) { Text(playlist.title).tag(playlist.id)
if playlist == currentPlaylist {
Label(playlist.title, systemImage: "checkmark")
} else {
Text(playlist.title)
}
}
} }
} label: {
Text(currentPlaylist?.title ?? "Select playlist")
.frame(maxWidth: 140, alignment: .center)
} }
#endif #endif
} }
@ -299,22 +295,13 @@ struct PlaylistsView: View {
self.editedPlaylist = self.currentPlaylist self.editedPlaylist = self.currentPlaylist
self.showingEditPlaylist = true self.showingEditPlaylist = true
}) { }) {
HStack(spacing: 8) { Label("Edit Playlist", systemImage: "rectangle.and.pencil.and.ellipsis")
Image(systemName: "rectangle.and.pencil.and.ellipsis")
}
} }
} }
var newPlaylistButton: some View { var newPlaylistButton: some View {
Button(action: { self.showingNewPlaylist = true }) { Button(action: { self.showingNewPlaylist = true }) {
HStack(spacing: 0) { Label("New Playlist", systemImage: "plus")
Image(systemName: "plus")
.padding(8)
.contentShape(Rectangle())
#if os(tvOS)
Text("New Playlist")
#endif
}
} }
} }
@ -322,29 +309,31 @@ struct PlaylistsView: View {
Button { Button {
player.play(items.compactMap(\.video)) player.play(items.compactMap(\.video))
} label: { } label: {
Image(systemName: "play") Label("Play", systemImage: "play")
.padding(8)
.contentShape(Rectangle())
} }
.contextMenu { .contextMenu {
Button { Button {
player.play(items.compactMap(\.video), shuffling: true) player.play(items.compactMap(\.video), shuffling: true)
} label: { } label: {
Label("Shuffle", systemImage: "shuffle") Label("Shuffle", systemImage: "shuffle")
.padding(8)
.contentShape(Rectangle())
} }
} }
} }
private var currentPlaylist: Playlist? { private var currentPlaylist: Playlist? {
model.find(id: selectedPlaylistID) ?? model.all.first if selectedPlaylistID.isEmpty {
DispatchQueue.main.async {
self.selectedPlaylistID = model.all.first?.id ?? ""
}
}
return model.find(id: selectedPlaylistID) ?? model.all.first
} }
} }
struct PlaylistsView_Provider: PreviewProvider { struct PlaylistsView_Provider: PreviewProvider {
static var previews: some View { static var previews: some View {
PlaylistsView() NavigationView {
.injectFixtureEnvironmentObjects() PlaylistsView()
}
} }
} }