Add list views for channels, playlists and placeholders

This commit is contained in:
Arkadiusz Fal
2022-12-13 11:40:08 +01:00
parent d38a507be5
commit 1cd2dbe5f7
11 changed files with 332 additions and 43 deletions

View File

@@ -1,63 +0,0 @@
import SwiftUI
struct ChannelAvatarView: View {
var channel: Channel?
var video: Video?
var subscribedBadge = true
@ObservedObject private var accounts = AccountsModel.shared
@ObservedObject private var subscribedChannels = SubscribedChannelsModel.shared
var body: some View {
ZStack(alignment: .bottomTrailing) {
Group {
Group {
if let url = channel?.thumbnailURL {
ThumbnailView(url: url)
} else {
ZStack {
Color(white: 0.6)
.opacity(0.5)
Group {
if let video, video.isLocal {
Image(systemName: video.localStreamImageSystemName)
} else {
Image(systemName: "play.rectangle")
}
}
.foregroundColor(.accentColor)
.font(.system(size: 20))
.contentShape(Rectangle())
}
}
}
.clipShape(Circle())
if subscribedBadge,
accounts.app.supportsSubscriptions,
accounts.signedIn,
let channel,
subscribedChannels.isSubscribing(channel.id)
{
Image(systemName: "star.circle.fill")
#if os(tvOS)
.background(Color.black)
#else
.background(Color.background)
#endif
.clipShape(Circle())
.foregroundColor(.secondary)
}
}
}
.imageScale(.small)
}
}
struct ChannelAvatarView_Previews: PreviewProvider {
static var previews: some View {
ChannelAvatarView(channel: Video.fixture.channel)
}
}

View File

@@ -1,88 +0,0 @@
import Foundation
import SDWebImageSwiftUI
import SwiftUI
struct ChannelCell: View {
let channel: Channel
@Environment(\.navigationStyle) private var navigationStyle
var body: some View {
#if os(tvOS)
button
#else
if navigationStyle == .tab {
navigationLink
} else {
button
}
#endif
}
var navigationLink: some View {
NavigationLink(destination: ChannelVideosView(channel: channel).modifier(PlayerOverlayModifier())) {
labelContent
}
}
var button: some View {
Button {
NavigationModel.shared.openChannel(
channel,
navigationStyle: navigationStyle
)
} label: {
labelContent
}
.buttonStyle(.plain)
}
var label: some View {
labelContent
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
.contentShape(RoundedRectangle(cornerRadius: 12))
}
var labelContent: some View {
VStack {
HStack(alignment: .top, spacing: 3) {
Image(systemName: "person.crop.rectangle")
Text("Channel".localized().uppercased())
.fontWeight(.light)
.opacity(0.6)
}
.foregroundColor(.secondary)
WebImage(url: channel.thumbnailURL, options: [.lowPriority])
.resizable()
.placeholder {
Rectangle().fill(Color("PlaceholderColor"))
}
.indicator(.activity)
.frame(width: 88, height: 88)
.clipShape(Circle())
DetailBadge(text: channel.name, style: .prominent)
Group {
if let subscriptions = channel.subscriptionsString {
Text("\(subscriptions) subscribers")
.foregroundColor(.secondary)
} else {
Text("")
}
}
.frame(height: 20)
}
}
}
struct ChannelSearchItem_Preview: PreviewProvider {
static var previews: some View {
Group {
ChannelCell(channel: Video.fixture.channel)
}
.frame(maxWidth: 300, maxHeight: 200)
.injectFixtureEnvironmentObjects()
}
}

View File

@@ -1,67 +0,0 @@
import SDWebImageSwiftUI
import SwiftUI
struct ChannelPlaylistCell: View {
let playlist: ChannelPlaylist
@Environment(\.navigationStyle) private var navigationStyle
var navigation = NavigationModel.shared
var body: some View {
if navigationStyle == .tab {
NavigationLink(destination: ChannelPlaylistView(playlist: playlist)) { cell }
} else {
Button {
NavigationModel.shared.openChannelPlaylist(playlist, navigationStyle: navigationStyle)
} label: {
cell
}
.buttonStyle(.plain)
}
}
var cell: some View {
content
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
.contentShape(RoundedRectangle(cornerRadius: 12))
}
var content: some View {
VStack {
HStack(alignment: .top, spacing: 3) {
Image(systemName: "list.and.film")
Text("Playlist".localized().uppercased())
.fontWeight(.light)
.opacity(0.6)
}
.foregroundColor(.secondary)
WebImage(url: playlist.thumbnailURL, options: [.lowPriority])
.resizable()
.placeholder {
Rectangle().fill(Color("PlaceholderColor"))
}
.indicator(.activity)
.frame(width: 165, height: 88)
.clipShape(RoundedRectangle(cornerRadius: 10))
Group {
DetailBadge(text: playlist.title, style: .prominent)
.lineLimit(2)
Text("\(playlist.videosCount ?? playlist.videos.count) videos")
.foregroundColor(.secondary)
.frame(height: 20)
}
}
}
}
struct ChannelPlaylistCell_Previews: PreviewProvider {
static var previews: some View {
ChannelPlaylistCell(playlist: ChannelPlaylist.fixture)
.frame(maxWidth: 320)
.injectFixtureEnvironmentObjects()
}
}

View File

@@ -1,188 +0,0 @@
import Defaults
import Siesta
import SwiftUI
struct ChannelPlaylistView: View {
var playlist: ChannelPlaylist?
var showCloseButton = false
@State private var presentingShareSheet = false
@State private var shareURL: URL?
@StateObject private var store = Store<ChannelPlaylist>()
@Environment(\.colorScheme) private var colorScheme
@Environment(\.navigationStyle) private var navigationStyle
@Default(.channelPlaylistListingStyle) private var channelPlaylistListingStyle
@ObservedObject private var accounts = AccountsModel.shared
var player = PlayerModel.shared
@ObservedObject private var recents = RecentsModel.shared
private var items: [ContentItem] {
ContentItem.array(of: store.item?.videos ?? [])
}
private var presentedPlaylist: ChannelPlaylist? {
playlist ?? recents.presentedPlaylist
}
private var resource: Resource? {
guard let playlist = presentedPlaylist else {
return nil
}
let resource = accounts.api.channelPlaylist(playlist.id)
resource?.addObserver(store)
return resource
}
var body: some View {
VStack(alignment: .leading) {
#if os(tvOS)
HStack {
if let playlist = presentedPlaylist {
Text(playlist.title)
.font(.title2)
.frame(alignment: .leading)
Spacer()
FavoriteButton(item: FavoriteItem(section: .channelPlaylist(accounts.app.appType.rawValue, playlist.id, playlist.title)))
.labelStyle(.iconOnly)
}
playButton
.labelStyle(.iconOnly)
}
#endif
VerticalCells(items: items)
.environment(\.inChannelPlaylistView, true)
}
.environment(\.listingStyle, channelPlaylistListingStyle)
.onAppear {
if navigationStyle == .tab {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
resource?.loadIfNeeded()
}
} else {
resource?.loadIfNeeded()
}
}
#if os(tvOS)
.background(Color.background(scheme: colorScheme))
#endif
#if os(iOS)
.toolbar {
ToolbarItem(placement: .principal) {
playlistMenu
}
}
#endif
#if os(macOS)
.toolbar {
ToolbarItem(placement: .cancellationAction) {
if showCloseButton {
Button {
NavigationModel.shared.presentingPlaylist = false
} label: {
Label("Close", systemImage: "xmark")
}
.buttonStyle(.plain)
}
}
ToolbarItem(placement: playlistButtonsPlacement) {
HStack {
ListingStyleButtons(listingStyle: $channelPlaylistListingStyle)
ShareButton(contentItem: contentItem)
favoriteButton
playButton
}
}
}
.navigationTitle(label)
#endif
}
@ViewBuilder private var favoriteButton: some View {
if let playlist = presentedPlaylist {
FavoriteButton(item: FavoriteItem(section: .channelPlaylist(accounts.app.appType.rawValue, playlist.id, playlist.title)))
}
}
#if os(iOS)
private var playlistMenu: some View {
Menu {
favoriteButton
ListingStyleButtons(listingStyle: $channelPlaylistListingStyle)
Section {
SettingsButtons()
}
} label: {
HStack(spacing: 12) {
ThumbnailView(url: store.item?.thumbnailURL ?? playlist?.thumbnailURL)
.frame(width: 60, height: 30)
.clipShape(RoundedRectangle(cornerRadius: 2))
Text(label)
.font(.headline)
.foregroundColor(.primary)
Image(systemName: "chevron.down.circle.fill")
.foregroundColor(.accentColor)
.imageScale(.small)
}
.frame(maxWidth: 320)
.transaction { t in t.animation = nil }
}
}
#endif
private var label: String {
presentedPlaylist?.title ?? ""
}
private var playlistButtonsPlacement: ToolbarItemPlacement {
#if os(iOS)
.navigationBarTrailing
#else
.automatic
#endif
}
private var playButton: some View {
Button {
player.play(videos)
} label: {
Label("Play All", systemImage: "play")
}
.contextMenu {
Button {
player.play(videos, shuffling: true)
} label: {
Label("Shuffle All", systemImage: "shuffle")
}
}
}
private var videos: [Video] {
items.compactMap(\.video)
}
private var contentItem: ContentItem {
ContentItem(playlist: playlist)
}
}
struct ChannelPlaylistView_Previews: PreviewProvider {
static var previews: some View {
NavigationView {
ChannelPlaylistView(playlist: ChannelPlaylist.fixture)
}
}
}

View File

@@ -1,347 +0,0 @@
import Defaults
import SDWebImageSwiftUI
import Siesta
import SwiftUI
struct ChannelVideosView: View {
var channel: Channel?
var showCloseButton = false
@State private var presentingShareSheet = false
@State private var shareURL: URL?
@State private var subscriptionToggleButtonDisabled = false
@State private var contentType = Channel.ContentType.videos
@StateObject private var contentTypeItems = Store<[ContentItem]>()
@StateObject private var store = Store<Channel>()
@Environment(\.colorScheme) private var colorScheme
#if os(iOS)
@Environment(\.horizontalSizeClass) private var horizontalSizeClass
#endif
@ObservedObject private var accounts = AccountsModel.shared
@ObservedObject private var navigation = NavigationModel.shared
@ObservedObject private var recents = RecentsModel.shared
@ObservedObject private var subscriptions = SubscribedChannelsModel.shared
@Namespace private var focusNamespace
@Default(.channelPlaylistListingStyle) private var channelPlaylistListingStyle
var presentedChannel: Channel? {
store.item ?? channel ?? recents.presentedChannel
}
var contentItems: [ContentItem] {
guard contentType != .videos else {
return ContentItem.array(of: presentedChannel?.videos ?? [])
}
return contentTypeItems.collection
}
var body: some View {
let content = VStack {
#if os(tvOS)
VStack {
HStack(spacing: 24) {
thumbnail
Text(navigationTitle)
.font(.title2)
.frame(alignment: .leading)
Spacer()
subscriptionsLabel
viewsLabel
subscriptionToggleButton
}
contentTypePicker
.pickerStyle(.automatic)
}
.frame(maxWidth: .infinity)
#endif
VerticalCells(items: contentItems) {
banner
}
.environment(\.inChannelView, true)
.environment(\.listingStyle, channelPlaylistListingStyle)
#if os(tvOS)
.prefersDefaultFocus(in: focusNamespace)
#endif
}
#if !os(tvOS)
.toolbar {
#if os(iOS)
ToolbarItem(placement: .principal) {
channelMenu
}
#endif
ToolbarItem(placement: .cancellationAction) {
if showCloseButton {
Button {
withAnimation(Constants.overlayAnimation) {
navigation.presentingChannel = false
}
} label: {
Label("Close", systemImage: "xmark")
}
.buttonStyle(.plain)
}
}
#if !os(iOS)
ToolbarItem(placement: .navigation) {
thumbnail
}
ToolbarItem {
ListingStyleButtons(listingStyle: $channelPlaylistListingStyle)
}
ToolbarItem {
contentTypePicker
}
ToolbarItem {
HStack(spacing: 3) {
subscriptionsLabel
viewsLabel
}
}
ToolbarItem {
if let contentItem = presentedChannel?.contentItem {
ShareButton(contentItem: contentItem)
}
}
ToolbarItem {
subscriptionToggleButton
.layoutPriority(2)
}
ToolbarItem {
if let presentedChannel {
FavoriteButton(item: FavoriteItem(section: .channel(accounts.app.appType.rawValue, presentedChannel.id, presentedChannel.name)))
}
}
#endif
}
#endif
.onAppear {
resource?.loadIfNeeded()
}
.onChange(of: contentType) { _ in
resource?.load()
}
#if os(iOS)
.navigationBarTitleDisplayMode(.inline)
#endif
#if !os(tvOS)
.navigationTitle(navigationTitle)
#endif
return Group {
if #available(macOS 12.0, *) {
content
#if os(tvOS)
.background(Color.background(scheme: colorScheme))
#endif
#if !os(iOS)
.focusScope(focusNamespace)
#endif
} else {
content
}
}
}
var thumbnail: some View {
ChannelAvatarView(channel: store.item)
#if os(tvOS)
.frame(width: 80, height: 80, alignment: .trailing)
#else
.frame(width: 30, height: 30, alignment: .trailing)
#endif
}
@ViewBuilder var banner: some View {
if let banner = presentedChannel?.bannerURL {
WebImage(url: banner)
.resizable()
.placeholder { Color.clear.frame(height: 0) }
.scaledToFit()
.clipShape(RoundedRectangle(cornerRadius: 3))
}
}
var subscriptionsLabel: some View {
Group {
if let subscribers = store.item?.subscriptionsString {
HStack(spacing: 0) {
Text(subscribers)
Image(systemName: "person.2.fill")
}
} else if store.item.isNil {
HStack(spacing: 0) {
Text("1234")
.redacted(reason: .placeholder)
Image(systemName: "person.2.fill")
}
}
}
.imageScale(.small)
.foregroundColor(.secondary)
}
var viewsLabel: some View {
HStack(spacing: 0) {
if let views = store.item?.totalViewsString {
Text(views)
Image(systemName: "eye.fill")
.imageScale(.small)
}
}
.foregroundColor(.secondary)
}
#if !os(tvOS)
var channelMenu: some View {
Menu {
if let channel = presentedChannel {
contentTypePicker
Section {
subscriptionToggleButton
FavoriteButton(item: FavoriteItem(section: .channel(accounts.app.appType.rawValue, channel.id, channel.name)))
}
ListingStyleButtons(listingStyle: $channelPlaylistListingStyle)
}
} label: {
HStack(spacing: 12) {
thumbnail
VStack(alignment: .leading) {
Text(presentedChannel?.name ?? "Channel")
.font(.headline)
.foregroundColor(.primary)
.layoutPriority(1)
.frame(minWidth: 160, alignment: .leading)
Group {
HStack(spacing: 12) {
subscriptionsLabel
if presentedChannel?.verified ?? false {
Image(systemName: "checkmark.seal.fill")
.imageScale(.small)
}
viewsLabel
}
.frame(minWidth: 160, alignment: .leading)
}
.font(.caption2.bold())
.foregroundColor(.secondary)
}
Image(systemName: "chevron.down.circle.fill")
.foregroundColor(.accentColor)
.imageScale(.small)
}
.frame(maxWidth: 320)
}
}
#endif
private var contentTypePicker: some View {
Picker("Content type", selection: $contentType) {
if let channel = presentedChannel {
ForEach(Channel.ContentType.allCases, id: \.self) { type in
if channel.hasData(for: type) {
Label(type.description, systemImage: type.systemImage).tag(type)
}
}
}
}
}
private var resource: Resource? {
guard let channel = presentedChannel else { return nil }
let data = contentType != .videos ? channel.tabs.first(where: { $0.contentType == contentType })?.data : nil
let resource = accounts.api.channel(channel.id, contentType: contentType, data: data)
if contentType == .videos {
resource.addObserver(store)
} else {
resource.addObserver(contentTypeItems)
}
return resource
}
@ViewBuilder private var subscriptionToggleButton: some View {
if let channel = presentedChannel {
Group {
if accounts.app.supportsSubscriptions && accounts.signedIn {
if subscriptions.isSubscribing(channel.id) {
Button {
subscriptionToggleButtonDisabled = true
subscriptions.unsubscribe(channel.id) {
subscriptionToggleButtonDisabled = false
}
} label: {
Label("Unsubscribe", systemImage: "xmark.circle")
#if os(iOS)
.labelStyle(.automatic)
#else
.labelStyle(.titleOnly)
#endif
}
} else {
Button {
subscriptionToggleButtonDisabled = true
subscriptions.subscribe(channel.id) {
subscriptionToggleButtonDisabled = false
navigation.sidebarSectionChanged.toggle()
}
} label: {
Label("Subscribe", systemImage: "circle")
#if os(iOS)
.labelStyle(.automatic)
#else
.labelStyle(.titleOnly)
#endif
}
}
}
}
.disabled(subscriptionToggleButtonDisabled)
}
}
private var navigationTitle: String {
presentedChannel?.name ?? "No channel"
}
}
struct ChannelVideosView_Previews: PreviewProvider {
static var previews: some View {
ChannelVideosView(channel: Video.fixture.channel)
.environment(\.navigationStyle, .tab)
.injectFixtureEnvironmentObjects()
NavigationView {
Spacer()
ChannelVideosView(channel: Video.fixture.channel)
.environment(\.navigationStyle, .sidebar)
}
}
}

View File

@@ -9,28 +9,77 @@ struct ContentItemView: View {
Group {
switch item.contentType {
case .video:
if listingStyle == .cells {
VideoCell(video: item.video)
} else {
PlayerQueueRow(item: .init(item.video))
.contextMenu {
VideoContextMenuView(video: item.video)
}
#if os(tvOS)
.padding(.horizontal, 30)
#endif
#if !os(tvOS)
Divider()
#endif
}
case .playlist:
ChannelPlaylistCell(playlist: item.playlist)
videoItem(item.video)
case .channel:
ChannelCell(channel: item.channel)
channelItem(item.channel)
case .playlist:
playlistItem(item.playlist)
default:
PlaceholderCell()
placeholderItem()
}
}
}
@ViewBuilder func videoItem(_ video: Video) -> some View {
if listingStyle == .cells {
VideoCell(video: video)
} else {
PlayerQueueRow(item: .init(video))
.contextMenu {
VideoContextMenuView(video: video)
}
#if os(tvOS)
.padding(.horizontal, 30)
#endif
#if !os(tvOS)
Divider()
#endif
}
}
@ViewBuilder func playlistItem(_ playlist: ChannelPlaylist) -> some View {
if listingStyle == .cells {
ChannelPlaylistCell(playlist: playlist)
} else {
ChannelPlaylistListItem(playlist: playlist)
#if os(tvOS)
.padding(.horizontal, 30)
#endif
#if !os(tvOS)
Divider()
#endif
}
}
@ViewBuilder func channelItem(_ channel: Channel) -> some View {
if listingStyle == .cells {
ChannelCell(channel: channel)
} else {
ChannelListItem(channel: channel)
#if os(tvOS)
.padding(.horizontal, 30)
#endif
#if !os(tvOS)
Divider()
#endif
}
}
@ViewBuilder func placeholderItem() -> some View {
if listingStyle == .cells {
PlaceholderCell()
} else {
PlaceholderListItem()
#if os(tvOS)
.padding(.horizontal, 30)
#endif
#if !os(tvOS)
Divider()
#endif
}
}
}

View File

@@ -0,0 +1,15 @@
import Defaults
import SwiftUI
struct PlaceholderListItem: View {
var body: some View {
VideoBanner(id: UUID().uuidString, video: .fixture)
.redacted(reason: .placeholder)
}
}
struct PlaceholderListItem_Previews: PreviewProvider {
static var previews: some View {
PlaceholderListItem()
}
}

View File

@@ -1,113 +0,0 @@
import Siesta
import SwiftUI
struct PlaylistVideosView: View {
var playlist: Playlist
@ObservedObject private var accounts = AccountsModel.shared
var player = PlayerModel.shared
@ObservedObject private var model = PlaylistsModel.shared
@StateObject private var channelPlaylist = Store<ChannelPlaylist>()
@StateObject private var userPlaylist = Store<Playlist>()
var contentItems: [ContentItem] {
var videos = playlist.videos
if videos.isEmpty {
videos = userPlaylist.item?.videos ?? channelPlaylist.item?.videos ?? []
if !accounts.app.userPlaylistsEndpointIncludesVideos {
var i = 0
for index in videos.indices {
var video = videos[index]
video.indexID = "\(i)"
i += 1
videos[index] = video
}
}
}
return ContentItem.array(of: videos)
}
private var resource: Resource? {
let resource = accounts.api.playlist(playlist.id)
if accounts.app.userPlaylistsUseChannelPlaylistEndpoint {
resource?.addObserver(channelPlaylist)
} else {
resource?.addObserver(userPlaylist)
}
return resource
}
func loadResource() {
loadCachedResource()
resource?.load()
.onSuccess { response in
if let playlist: Playlist = response.typedContent() {
ChannelPlaylistsCacheModel.shared.storePlaylist(playlist: playlist.channelPlaylist)
}
}
}
func loadCachedResource() {
if let cache = ChannelPlaylistsCacheModel.shared.retrievePlaylist(playlist.id) {
DispatchQueue.main.async {
self.channelPlaylist.replace(cache)
}
}
}
var videos: [Video] {
contentItems.compactMap(\.video)
}
init(_ playlist: Playlist) {
self.playlist = playlist
}
var body: some View {
VerticalCells(items: contentItems)
.onAppear {
guard contentItems.isEmpty else { return }
loadResource()
}
.onChange(of: model.reloadPlaylists) { _ in
loadResource()
}
#if !os(tvOS)
.navigationTitle("\(playlist.title) Playlist")
#endif
.toolbar {
ToolbarItem(placement: playlistButtonsPlacement) {
HStack {
FavoriteButton(item: FavoriteItem(section: .channelPlaylist(accounts.app.appType.rawValue, playlist.id, playlist.title)))
Button {
player.play(videos)
} label: {
Label("Play All", systemImage: "play")
}
.contextMenu {
Button {
player.play(videos, shuffling: true)
} label: {
Label("Shuffle All", systemImage: "shuffle")
}
}
}
}
}
}
private var playlistButtonsPlacement: ToolbarItemPlacement {
#if os(iOS)
.navigationBarTrailing
#else
.automatic
#endif
}
}