2021-07-11 20:52:49 +00:00
|
|
|
import Foundation
|
|
|
|
import SwiftUI
|
|
|
|
|
2021-09-25 08:18:22 +00:00
|
|
|
final class NavigationModel: ObservableObject {
|
2021-08-29 21:36:18 +00:00
|
|
|
enum TabSelection: Hashable {
|
2021-11-01 21:56:18 +00:00
|
|
|
case favorites
|
2021-10-05 20:20:09 +00:00
|
|
|
case subscriptions
|
|
|
|
case popular
|
|
|
|
case trending
|
|
|
|
case playlists
|
|
|
|
case channel(String)
|
|
|
|
case playlist(String)
|
|
|
|
case recentlyOpened(String)
|
|
|
|
case nowPlaying
|
|
|
|
case search
|
2021-10-24 21:36:24 +00:00
|
|
|
|
2022-03-27 18:38:59 +00:00
|
|
|
var stringValue: String {
|
|
|
|
switch self {
|
|
|
|
case .favorites:
|
|
|
|
return "favorites"
|
|
|
|
case .subscriptions:
|
|
|
|
return "subscriptions"
|
|
|
|
case .popular:
|
|
|
|
return "popular"
|
|
|
|
case .trending:
|
|
|
|
return "trending"
|
|
|
|
case .playlists:
|
|
|
|
return "playlists"
|
|
|
|
case let .channel(string):
|
|
|
|
return "channel\(string)"
|
|
|
|
case let .playlist(string):
|
|
|
|
return "playlist\(string)"
|
|
|
|
case .recentlyOpened:
|
|
|
|
return "recentlyOpened"
|
|
|
|
case .search:
|
|
|
|
return "search"
|
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-24 21:36:24 +00:00
|
|
|
var playlistID: Playlist.ID? {
|
|
|
|
if case let .playlist(id) = self {
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-08-29 21:36:18 +00:00
|
|
|
}
|
|
|
|
|
2021-12-01 11:22:19 +00:00
|
|
|
@Published var tabSelection: TabSelection!
|
2021-07-11 20:52:49 +00:00
|
|
|
|
2021-09-28 18:06:05 +00:00
|
|
|
@Published var presentingAddToPlaylist = false
|
|
|
|
@Published var videoToAddToPlaylist: Video!
|
|
|
|
|
2021-08-29 21:36:18 +00:00
|
|
|
@Published var presentingPlaylistForm = false
|
|
|
|
@Published var editedPlaylist: Playlist!
|
|
|
|
|
|
|
|
@Published var presentingUnsubscribeAlert = false
|
|
|
|
@Published var channelToUnsubscribe: Channel!
|
|
|
|
|
2021-10-22 23:04:03 +00:00
|
|
|
@Published var presentingChannel = false
|
|
|
|
@Published var presentingPlaylist = false
|
2021-08-31 21:17:50 +00:00
|
|
|
@Published var sidebarSectionChanged = false
|
|
|
|
|
2021-09-25 08:18:22 +00:00
|
|
|
@Published var presentingSettings = false
|
2021-10-17 23:06:00 +00:00
|
|
|
@Published var presentingWelcomeScreen = false
|
2021-09-25 08:18:22 +00:00
|
|
|
|
2022-06-24 22:48:57 +00:00
|
|
|
@Published var alert = Alert(title: Text("Error"))
|
2022-06-18 12:39:49 +00:00
|
|
|
@Published var presentingAlert = false
|
2022-06-24 22:48:57 +00:00
|
|
|
#if os(macOS)
|
|
|
|
@Published var presentingAlertInVideoPlayer = false
|
|
|
|
#endif
|
2022-06-18 12:39:49 +00:00
|
|
|
|
2021-12-17 16:34:55 +00:00
|
|
|
static func openChannel(
|
|
|
|
_ channel: Channel,
|
|
|
|
player: PlayerModel,
|
|
|
|
recents: RecentsModel,
|
2022-06-24 22:48:57 +00:00
|
|
|
navigation: NavigationModel
|
2021-12-17 16:34:55 +00:00
|
|
|
) {
|
2022-04-16 17:44:07 +00:00
|
|
|
guard channel.id != Video.fixtureChannelID else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-24 22:48:57 +00:00
|
|
|
player.hide()
|
2022-05-29 18:26:56 +00:00
|
|
|
navigation.presentingChannel = false
|
|
|
|
|
2021-12-17 16:34:55 +00:00
|
|
|
let recent = RecentItem(from: channel)
|
2021-12-19 17:17:04 +00:00
|
|
|
#if os(macOS)
|
2022-01-06 15:35:45 +00:00
|
|
|
Windows.main.open()
|
2021-12-19 17:17:04 +00:00
|
|
|
#else
|
|
|
|
player.hide()
|
|
|
|
#endif
|
2021-12-17 16:34:55 +00:00
|
|
|
|
2022-06-24 22:48:57 +00:00
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
|
2021-12-17 16:34:55 +00:00
|
|
|
recents.add(recent)
|
2022-01-06 16:55:56 +00:00
|
|
|
navigation.sidebarSectionChanged.toggle()
|
|
|
|
navigation.tabSelection = .recentlyOpened(recent.tag)
|
2022-06-24 22:48:57 +00:00
|
|
|
navigation.presentingChannel = true
|
2022-01-06 16:55:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static func openChannelPlaylist(
|
|
|
|
_ playlist: ChannelPlaylist,
|
|
|
|
player: PlayerModel,
|
|
|
|
recents: RecentsModel,
|
2022-06-24 22:48:57 +00:00
|
|
|
navigation: NavigationModel
|
2022-01-06 16:55:56 +00:00
|
|
|
) {
|
2022-06-24 22:48:57 +00:00
|
|
|
navigation.presentingChannel = false
|
2022-05-29 19:09:57 +00:00
|
|
|
navigation.presentingPlaylist = false
|
|
|
|
|
2022-01-06 16:55:56 +00:00
|
|
|
let recent = RecentItem(from: playlist)
|
|
|
|
#if os(macOS)
|
|
|
|
Windows.main.open()
|
|
|
|
#else
|
|
|
|
player.hide()
|
|
|
|
#endif
|
|
|
|
|
2022-06-24 22:48:57 +00:00
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
|
2022-01-06 16:55:56 +00:00
|
|
|
recents.add(recent)
|
2022-06-24 22:48:57 +00:00
|
|
|
navigation.sidebarSectionChanged.toggle()
|
|
|
|
navigation.tabSelection = .recentlyOpened(recent.tag)
|
2022-01-06 16:55:56 +00:00
|
|
|
navigation.presentingPlaylist = true
|
|
|
|
}
|
2022-06-24 22:48:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static func openSearchQuery(
|
|
|
|
_ searchQuery: String?,
|
|
|
|
player: PlayerModel,
|
|
|
|
recents: RecentsModel,
|
|
|
|
navigation: NavigationModel,
|
|
|
|
search: SearchModel
|
|
|
|
) {
|
|
|
|
player.hide()
|
|
|
|
navigation.presentingChannel = false
|
|
|
|
navigation.presentingPlaylist = false
|
|
|
|
navigation.tabSelection = .search
|
|
|
|
|
|
|
|
if let searchQuery = searchQuery {
|
|
|
|
let recent = RecentItem(from: searchQuery)
|
|
|
|
recents.add(recent)
|
2022-01-06 16:55:56 +00:00
|
|
|
|
2022-06-24 22:48:57 +00:00
|
|
|
DispatchQueue.main.async {
|
|
|
|
search.queryText = searchQuery
|
|
|
|
search.changeQuery { query in query.query = searchQuery }
|
2021-12-17 16:34:55 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-24 22:48:57 +00:00
|
|
|
|
|
|
|
#if os(macOS)
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
|
|
|
|
Windows.main.focus()
|
|
|
|
}
|
|
|
|
#endif
|
2021-12-17 16:34:55 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 23:01:49 +00:00
|
|
|
var tabSelectionBinding: Binding<TabSelection> {
|
|
|
|
Binding<TabSelection>(
|
2021-07-11 20:52:49 +00:00
|
|
|
get: {
|
2021-12-19 23:36:12 +00:00
|
|
|
self.tabSelection ?? .search
|
2021-07-11 20:52:49 +00:00
|
|
|
},
|
2021-08-31 21:17:50 +00:00
|
|
|
set: { newValue in
|
2021-09-28 23:01:49 +00:00
|
|
|
self.tabSelection = newValue
|
2021-07-11 20:52:49 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
2021-08-29 21:36:18 +00:00
|
|
|
|
2021-09-28 18:06:05 +00:00
|
|
|
func presentAddToPlaylist(_ video: Video) {
|
|
|
|
videoToAddToPlaylist = video
|
|
|
|
presentingAddToPlaylist = true
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2022-03-26 14:22:29 +00:00
|
|
|
|
|
|
|
func hideKeyboard() {
|
|
|
|
#if os(iOS)
|
|
|
|
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
|
|
|
|
#endif
|
|
|
|
}
|
2022-06-18 12:39:49 +00:00
|
|
|
|
|
|
|
func presentAlert(title: String, message: String) {
|
2022-06-24 22:48:57 +00:00
|
|
|
alert = Alert(title: Text(title), message: Text(message))
|
2022-06-18 12:39:49 +00:00
|
|
|
presentingAlert = true
|
|
|
|
}
|
2021-07-11 20:52:49 +00:00
|
|
|
}
|
2021-08-29 21:36:18 +00:00
|
|
|
|
2021-09-25 08:18:22 +00:00
|
|
|
typealias TabSelection = NavigationModel.TabSelection
|