1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-13 13:50:32 +05:30
yattee/Model/NavigationModel.swift

237 lines
6.6 KiB
Swift
Raw Normal View History

2021-07-12 02:22:49 +05:30
import Foundation
import SwiftUI
2021-09-25 13:48:22 +05:30
final class NavigationModel: ObservableObject {
2021-08-30 03:06:18 +05:30
enum TabSelection: Hashable {
2021-11-02 03:26:18 +05:30
case favorites
case subscriptions
case popular
case trending
case playlists
case channel(String)
case playlist(String)
case recentlyOpened(String)
case nowPlaying
case search
#if os(tvOS)
case settings
#endif
2021-10-25 03:06:24 +05:30
2022-03-28 00:08:59 +05:30
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"
#if os(tvOS)
case .settings: // swiftlint:disable:this switch_case_alignment
return "settings"
#endif
2022-03-28 00:08:59 +05:30
default:
return ""
}
}
2021-10-25 03:06:24 +05:30
var playlistID: Playlist.ID? {
if case let .playlist(id) = self {
return id
}
return nil
}
2021-08-30 03:06:18 +05:30
}
@Published var tabSelection: TabSelection!
2021-07-12 02:22:49 +05:30
@Published var presentingAddToPlaylist = false
@Published var videoToAddToPlaylist: Video!
2021-08-30 03:06:18 +05:30
@Published var presentingPlaylistForm = false
@Published var editedPlaylist: Playlist!
@Published var presentingUnsubscribeAlert = false
@Published var channelToUnsubscribe: Channel!
2021-10-23 04:34:03 +05:30
@Published var presentingChannel = false
@Published var presentingPlaylist = false
@Published var sidebarSectionChanged = false
2021-09-25 13:48:22 +05:30
@Published var presentingSettings = false
2021-10-18 04:36:00 +05:30
@Published var presentingWelcomeScreen = false
2021-09-25 13:48:22 +05:30
@Published var presentingShareSheet = false
@Published var shareURL: URL?
2022-06-25 04:18:57 +05:30
@Published var alert = Alert(title: Text("Error"))
@Published var presentingAlert = false
2022-06-25 04:18:57 +05:30
#if os(macOS)
@Published var presentingAlertInVideoPlayer = false
#endif
2021-12-17 22:04:55 +05:30
static func openChannel(
_ channel: Channel,
player: PlayerModel,
recents: RecentsModel,
2022-06-30 13:35:32 +05:30
navigation: NavigationModel,
navigationStyle: NavigationStyle
2021-12-17 22:04:55 +05:30
) {
2022-04-16 23:14:07 +05:30
guard channel.id != Video.fixtureChannelID else {
return
}
2022-06-25 04:18:57 +05:30
player.hide()
2022-05-29 23:56:56 +05:30
navigation.presentingChannel = false
2021-12-17 22:04:55 +05:30
let recent = RecentItem(from: channel)
#if os(macOS)
2022-01-06 21:05:45 +05:30
Windows.main.open()
#else
player.hide()
#endif
2021-12-17 22:04:55 +05:30
2022-06-25 04:18:57 +05:30
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
2021-12-17 22:04:55 +05:30
recents.add(recent)
2022-06-30 13:35:32 +05:30
if navigationStyle == .sidebar {
navigation.sidebarSectionChanged.toggle()
navigation.tabSelection = .recentlyOpened(recent.tag)
} else {
navigation.presentingChannel = true
}
}
}
static func openChannelPlaylist(
_ playlist: ChannelPlaylist,
player: PlayerModel,
recents: RecentsModel,
2022-06-30 13:35:32 +05:30
navigation: NavigationModel,
navigationStyle: NavigationStyle
) {
2022-06-25 04:18:57 +05:30
navigation.presentingChannel = false
2022-05-30 00:39:57 +05:30
navigation.presentingPlaylist = false
let recent = RecentItem(from: playlist)
#if os(macOS)
Windows.main.open()
#else
player.hide()
#endif
2022-06-25 04:18:57 +05:30
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
recents.add(recent)
2022-06-30 13:35:32 +05:30
if navigationStyle == .sidebar {
navigation.sidebarSectionChanged.toggle()
navigation.tabSelection = .recentlyOpened(recent.tag)
} else {
navigation.presentingPlaylist = true
}
}
2022-06-25 04:18:57 +05:30
}
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-06-25 04:18:57 +05:30
DispatchQueue.main.async {
search.queryText = searchQuery
search.changeQuery { query in query.query = searchQuery }
2021-12-17 22:04:55 +05:30
}
}
2022-06-25 04:18:57 +05:30
#if os(macOS)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
Windows.main.focus()
}
#endif
2021-12-17 22:04:55 +05:30
}
2021-09-29 04:31:49 +05:30
var tabSelectionBinding: Binding<TabSelection> {
Binding<TabSelection>(
2021-07-12 02:22:49 +05:30
get: {
self.tabSelection ?? .search
2021-07-12 02:22:49 +05:30
},
set: { newValue in
2021-09-29 04:31:49 +05:30
self.tabSelection = newValue
2021-07-12 02:22:49 +05:30
}
)
}
2021-08-30 03:06:18 +05:30
func presentAddToPlaylist(_ video: Video) {
videoToAddToPlaylist = video
presentingAddToPlaylist = true
}
2021-08-30 03:06:18 +05:30
func presentEditPlaylistForm(_ playlist: Playlist?) {
editedPlaylist = playlist
presentingPlaylistForm = editedPlaylist != nil
}
func presentNewPlaylistForm() {
editedPlaylist = nil
presentingPlaylistForm = true
}
2022-06-25 04:51:05 +05:30
func presentUnsubscribeAlert(_ channel: Channel, subscriptions: SubscriptionsModel) {
2021-08-30 03:06:18 +05:30
channelToUnsubscribe = channel
2022-06-25 04:51:05 +05:30
alert = Alert(
title: Text(
"Are you sure you want to unsubscribe from \(channelToUnsubscribe.name)?"
),
primaryButton: .destructive(Text("Unsubscribe")) { [weak self] in
if let id = self?.channelToUnsubscribe.id {
subscriptions.unsubscribe(id)
}
},
secondaryButton: .cancel()
)
presentingAlert = true
2021-08-30 03:06:18 +05:30
}
2022-03-26 19:52:29 +05:30
func hideKeyboard() {
#if os(iOS)
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
#endif
}
func presentAlert(title: String, message: String? = nil) {
let message = message.isNil ? nil : Text(message!)
alert = Alert(title: Text(title), message: message)
presentingAlert = true
}
func presentShareSheet(_ url: URL) {
shareURL = url
presentingShareSheet = true
}
2021-07-12 02:22:49 +05:30
}
2021-08-30 03:06:18 +05:30
2021-09-25 13:48:22 +05:30
typealias TabSelection = NavigationModel.TabSelection