yattee/Model/NavigationState.swift

109 lines
2.7 KiB
Swift
Raw Normal View History

2021-07-11 20:52:49 +00:00
import Foundation
import SwiftUI
final class NavigationState: ObservableObject {
2021-08-29 21:36:18 +00:00
enum TabSelection: Hashable {
case subscriptions, popular, trending, playlists, channel(String), playlist(String), search
}
2021-07-11 20:52:49 +00:00
@Published var tabSelection: TabSelection = .subscriptions
@Published var showingVideoDetails = false
2021-07-18 22:32:46 +00:00
@Published var showingVideo = false
2021-07-11 20:52:49 +00:00
@Published var video: Video?
2021-07-18 22:32:46 +00:00
@Published var returnToDetails = false
2021-08-29 21:36:18 +00:00
@Published var presentingPlaylistForm = false
@Published var editedPlaylist: Playlist!
@Published var presentingUnsubscribeAlert = false
@Published var channelToUnsubscribe: Channel!
@Published var openChannels = Set<Channel>()
@Published var isChannelOpen = false
@Published var sidebarSectionChanged = false
2021-07-11 20:52:49 +00:00
func openChannel(_ channel: Channel) {
openChannels.insert(channel)
isChannelOpen = true
tabSelection = .channel(channel.id)
}
func closeChannel(_ channel: Channel) {
guard openChannels.remove(channel) != nil else {
return
}
isChannelOpen = !openChannels.isEmpty
if tabSelection == .channel(channel.id) {
tabSelection = .subscriptions
}
}
func closeAllChannels() {
isChannelOpen = false
openChannels.removeAll()
2021-07-11 20:52:49 +00:00
}
func showOpenChannel(_ id: Channel.ID) -> Bool {
if case .channel = tabSelection {
return false
} else {
return !openChannels.contains { $0.id == id }
}
2021-07-11 20:52:49 +00:00
}
func openVideoDetails(_ video: Video) {
self.video = video
showingVideoDetails = true
}
func closeVideoDetails() {
showingVideoDetails = false
video = nil
}
2021-07-18 22:32:46 +00:00
func playVideo(_ video: Video) {
self.video = video
showingVideo = true
}
func showVideoDetailsIfNeeded() {
showingVideoDetails = returnToDetails
returnToDetails = false
}
2021-07-11 20:52:49 +00:00
var tabSelectionOptionalBinding: Binding<TabSelection?> {
Binding<TabSelection?>(
get: {
self.tabSelection
},
set: { newValue in
if newValue != nil {
self.tabSelection = newValue!
}
2021-07-11 20:52:49 +00:00
}
)
}
2021-08-29 21:36:18 +00:00
func presentEditPlaylistForm(_ playlist: Playlist?) {
editedPlaylist = playlist
presentingPlaylistForm = editedPlaylist != nil
}
func presentNewPlaylistForm() {
editedPlaylist = nil
presentingPlaylistForm = true
}
func presentUnsubscribeAlert(_ channel: Channel?) {
channelToUnsubscribe = channel
presentingUnsubscribeAlert = channelToUnsubscribe != nil
}
2021-07-11 20:52:49 +00:00
}
2021-08-29 21:36:18 +00:00
typealias TabSelection = NavigationState.TabSelection