2021-09-25 08:18:22 +00:00
|
|
|
import Defaults
|
2022-06-18 12:39:49 +00:00
|
|
|
import MediaPlayer
|
|
|
|
import PINCache
|
|
|
|
import SDWebImage
|
|
|
|
import SDWebImageWebPCoder
|
|
|
|
import Siesta
|
2021-06-09 20:51:23 +00:00
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
@main
|
2021-11-07 13:32:01 +00:00
|
|
|
struct YatteeApp: App {
|
2022-01-06 15:02:53 +00:00
|
|
|
static var version: String {
|
|
|
|
Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "unknown"
|
|
|
|
}
|
|
|
|
|
|
|
|
static var build: String {
|
|
|
|
Bundle.main.infoDictionary?["CFBundleVersion"] as? String ?? "unknown"
|
|
|
|
}
|
|
|
|
|
2022-06-18 12:39:49 +00:00
|
|
|
static var isForPreviews: Bool {
|
|
|
|
ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1"
|
|
|
|
}
|
|
|
|
|
2022-07-06 22:08:38 +00:00
|
|
|
static var logsDirectory: URL {
|
|
|
|
URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
|
|
|
|
}
|
|
|
|
|
2021-10-24 09:16:04 +00:00
|
|
|
#if os(macOS)
|
|
|
|
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
|
2022-01-02 19:41:04 +00:00
|
|
|
#elseif os(iOS)
|
|
|
|
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
|
2021-10-24 09:16:04 +00:00
|
|
|
#endif
|
|
|
|
|
2022-06-18 12:39:49 +00:00
|
|
|
@State private var configured = false
|
|
|
|
|
2021-12-19 17:17:04 +00:00
|
|
|
@StateObject private var accounts = AccountsModel()
|
|
|
|
@StateObject private var comments = CommentsModel()
|
|
|
|
@StateObject private var instances = InstancesModel()
|
2021-11-08 23:14:28 +00:00
|
|
|
@StateObject private var menu = MenuModel()
|
2021-12-19 17:17:04 +00:00
|
|
|
@StateObject private var navigation = NavigationModel()
|
2022-06-18 12:39:49 +00:00
|
|
|
@StateObject private var networkState = NetworkStateModel()
|
2021-12-19 17:17:04 +00:00
|
|
|
@StateObject private var player = PlayerModel()
|
2022-02-16 20:23:11 +00:00
|
|
|
@StateObject private var playerControls = PlayerControlsModel()
|
2021-12-19 17:17:04 +00:00
|
|
|
@StateObject private var playlists = PlaylistsModel()
|
|
|
|
@StateObject private var recents = RecentsModel()
|
|
|
|
@StateObject private var search = SearchModel()
|
2022-08-29 11:55:23 +00:00
|
|
|
@StateObject private var seek = SeekModel()
|
2022-07-01 21:28:32 +00:00
|
|
|
@StateObject private var settings = SettingsModel()
|
2021-12-19 17:17:04 +00:00
|
|
|
@StateObject private var subscriptions = SubscriptionsModel()
|
|
|
|
@StateObject private var thumbnails = ThumbnailsModel()
|
2021-11-08 23:14:28 +00:00
|
|
|
|
2021-12-26 21:14:46 +00:00
|
|
|
let persistenceController = PersistenceController.shared
|
|
|
|
|
2021-06-09 20:51:23 +00:00
|
|
|
var body: some Scene {
|
|
|
|
WindowGroup {
|
|
|
|
ContentView()
|
2022-06-18 12:39:49 +00:00
|
|
|
.onAppear(perform: configure)
|
2021-12-26 21:14:46 +00:00
|
|
|
.environment(\.managedObjectContext, persistenceController.container.viewContext)
|
2021-12-19 17:17:04 +00:00
|
|
|
.environmentObject(accounts)
|
|
|
|
.environmentObject(comments)
|
|
|
|
.environmentObject(instances)
|
|
|
|
.environmentObject(navigation)
|
2022-06-18 12:39:49 +00:00
|
|
|
.environmentObject(networkState)
|
2021-12-19 17:17:04 +00:00
|
|
|
.environmentObject(player)
|
2022-02-16 20:23:11 +00:00
|
|
|
.environmentObject(playerControls)
|
2021-12-19 17:17:04 +00:00
|
|
|
.environmentObject(playlists)
|
|
|
|
.environmentObject(recents)
|
2022-08-29 11:55:23 +00:00
|
|
|
.environmentObject(seek)
|
2022-07-01 21:28:32 +00:00
|
|
|
.environmentObject(settings)
|
2021-12-19 17:17:04 +00:00
|
|
|
.environmentObject(subscriptions)
|
|
|
|
.environmentObject(thumbnails)
|
2021-11-08 23:14:28 +00:00
|
|
|
.environmentObject(menu)
|
2021-12-19 17:17:04 +00:00
|
|
|
.environmentObject(search)
|
2022-01-06 15:35:45 +00:00
|
|
|
#if os(macOS)
|
|
|
|
.background(
|
|
|
|
HostingWindowFinder { window in
|
|
|
|
Windows.mainWindow = window
|
|
|
|
}
|
|
|
|
)
|
|
|
|
#else
|
|
|
|
.onReceive(
|
|
|
|
NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)
|
|
|
|
) { _ in
|
|
|
|
player.handleEnterForeground()
|
|
|
|
}
|
2022-06-05 17:12:00 +00:00
|
|
|
.onReceive(
|
|
|
|
NotificationCenter.default.publisher(for: UIApplication.didEnterBackgroundNotification)
|
|
|
|
) { _ in
|
|
|
|
player.handleEnterBackground()
|
|
|
|
}
|
2021-12-19 17:17:04 +00:00
|
|
|
#endif
|
2021-12-24 19:20:05 +00:00
|
|
|
#if os(iOS)
|
|
|
|
.handlesExternalEvents(preferring: Set(["*"]), allowing: Set(["*"]))
|
2021-12-19 17:17:04 +00:00
|
|
|
#endif
|
2021-06-09 20:51:23 +00:00
|
|
|
}
|
2021-12-24 19:20:05 +00:00
|
|
|
#if os(iOS)
|
|
|
|
.handlesExternalEvents(matching: Set(["*"]))
|
|
|
|
#endif
|
2021-07-27 22:40:04 +00:00
|
|
|
#if !os(tvOS)
|
2021-11-08 16:29:35 +00:00
|
|
|
.commands {
|
|
|
|
SidebarCommands()
|
2021-12-07 23:09:49 +00:00
|
|
|
|
2021-11-08 16:29:35 +00:00
|
|
|
CommandGroup(replacing: .newItem, addition: {})
|
2021-12-07 23:09:49 +00:00
|
|
|
|
2021-11-08 23:14:28 +00:00
|
|
|
MenuCommands(model: Binding<MenuModel>(get: { menu }, set: { _ in }))
|
2021-11-08 16:29:35 +00:00
|
|
|
}
|
2021-07-27 22:40:04 +00:00
|
|
|
#endif
|
2021-09-25 08:18:22 +00:00
|
|
|
|
|
|
|
#if os(macOS)
|
2021-12-19 17:17:04 +00:00
|
|
|
WindowGroup(player.windowTitle) {
|
|
|
|
VideoPlayerView()
|
2022-06-18 12:39:49 +00:00
|
|
|
.onAppear(perform: configure)
|
2022-01-06 15:35:45 +00:00
|
|
|
.background(
|
|
|
|
HostingWindowFinder { window in
|
|
|
|
Windows.playerWindow = window
|
2022-04-03 12:23:42 +00:00
|
|
|
|
|
|
|
NotificationCenter.default.addObserver(
|
|
|
|
forName: NSWindow.willExitFullScreenNotification,
|
|
|
|
object: window,
|
|
|
|
queue: OperationQueue.main
|
|
|
|
) { _ in
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
|
2022-06-18 12:39:49 +00:00
|
|
|
self.player.playingFullScreen = false
|
2022-04-03 12:23:42 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-06 15:35:45 +00:00
|
|
|
}
|
|
|
|
)
|
2021-12-19 17:17:04 +00:00
|
|
|
.onAppear { player.presentingPlayer = true }
|
|
|
|
.onDisappear { player.presentingPlayer = false }
|
2021-12-26 21:14:46 +00:00
|
|
|
.environment(\.managedObjectContext, persistenceController.container.viewContext)
|
2021-12-19 17:17:04 +00:00
|
|
|
.environment(\.navigationStyle, .sidebar)
|
|
|
|
.environmentObject(accounts)
|
|
|
|
.environmentObject(comments)
|
|
|
|
.environmentObject(instances)
|
|
|
|
.environmentObject(navigation)
|
2022-06-18 12:39:49 +00:00
|
|
|
.environmentObject(networkState)
|
2021-12-19 17:17:04 +00:00
|
|
|
.environmentObject(player)
|
2022-02-16 20:23:11 +00:00
|
|
|
.environmentObject(playerControls)
|
2021-12-19 17:17:04 +00:00
|
|
|
.environmentObject(playlists)
|
|
|
|
.environmentObject(recents)
|
2022-06-24 22:48:57 +00:00
|
|
|
.environmentObject(search)
|
2022-08-29 11:55:23 +00:00
|
|
|
.environmentObject(seek)
|
2021-12-19 17:17:04 +00:00
|
|
|
.environmentObject(subscriptions)
|
|
|
|
.environmentObject(thumbnails)
|
2021-12-24 19:20:05 +00:00
|
|
|
.handlesExternalEvents(preferring: Set(["player", "*"]), allowing: Set(["player", "*"]))
|
2021-12-19 17:17:04 +00:00
|
|
|
}
|
2021-12-24 19:20:05 +00:00
|
|
|
.handlesExternalEvents(matching: Set(["player", "*"]))
|
2021-12-19 17:17:04 +00:00
|
|
|
|
2021-09-25 08:18:22 +00:00
|
|
|
Settings {
|
|
|
|
SettingsView()
|
2021-12-26 21:14:46 +00:00
|
|
|
.environment(\.managedObjectContext, persistenceController.container.viewContext)
|
2021-12-24 19:20:05 +00:00
|
|
|
.environmentObject(accounts)
|
|
|
|
.environmentObject(instances)
|
2022-07-01 21:28:32 +00:00
|
|
|
.environmentObject(navigation)
|
2021-12-26 21:14:46 +00:00
|
|
|
.environmentObject(player)
|
2022-02-16 20:23:11 +00:00
|
|
|
.environmentObject(playerControls)
|
2022-07-01 21:28:32 +00:00
|
|
|
.environmentObject(settings)
|
2021-09-25 08:18:22 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2022-06-18 12:39:49 +00:00
|
|
|
|
|
|
|
func configure() {
|
|
|
|
guard !Self.isForPreviews, !configured else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
configured = true
|
|
|
|
|
2022-08-08 17:26:04 +00:00
|
|
|
#if DEBUG
|
|
|
|
SiestaLog.Category.enabled = .common
|
|
|
|
#endif
|
2022-06-18 12:39:49 +00:00
|
|
|
SDImageCodersManager.shared.addCoder(SDImageWebPCoder.shared)
|
|
|
|
SDWebImageManager.defaultImageCache = PINCache(name: "stream.yattee.app")
|
|
|
|
|
2022-08-25 23:36:46 +00:00
|
|
|
migrateAccounts()
|
|
|
|
|
2022-07-04 09:35:27 +00:00
|
|
|
if !Defaults[.lastAccountIsPublic] {
|
2022-07-03 20:39:10 +00:00
|
|
|
accounts.configureAccount()
|
2022-06-18 12:39:49 +00:00
|
|
|
}
|
|
|
|
|
2022-07-01 21:28:32 +00:00
|
|
|
let countryOfPublicInstances = Defaults[.countryOfPublicInstances]
|
|
|
|
if accounts.current.isNil, countryOfPublicInstances.isNil {
|
2022-06-18 12:39:49 +00:00
|
|
|
navigation.presentingWelcomeScreen = true
|
|
|
|
}
|
|
|
|
|
2022-07-01 21:28:32 +00:00
|
|
|
if !countryOfPublicInstances.isNil {
|
|
|
|
InstancesManifest.shared.setPublicAccount(countryOfPublicInstances!, accounts: accounts, asCurrent: accounts.current.isNil)
|
|
|
|
}
|
|
|
|
|
2022-08-31 22:00:28 +00:00
|
|
|
PlayerModel.shared = player
|
|
|
|
|
2022-06-18 12:39:49 +00:00
|
|
|
playlists.accounts = accounts
|
|
|
|
search.accounts = accounts
|
|
|
|
subscriptions.accounts = accounts
|
|
|
|
|
|
|
|
comments.player = player
|
|
|
|
|
|
|
|
menu.accounts = accounts
|
|
|
|
menu.navigation = navigation
|
|
|
|
menu.player = player
|
|
|
|
|
|
|
|
playerControls.player = player
|
|
|
|
|
|
|
|
player.accounts = accounts
|
|
|
|
player.comments = comments
|
|
|
|
player.controls = playerControls
|
2022-06-29 22:44:32 +00:00
|
|
|
player.navigation = navigation
|
2022-06-18 12:39:49 +00:00
|
|
|
player.networkState = networkState
|
2022-08-29 11:55:23 +00:00
|
|
|
player.seek = seek
|
2022-06-18 12:39:49 +00:00
|
|
|
|
2022-08-31 19:24:46 +00:00
|
|
|
PlayerTimeModel.shared.player = player
|
|
|
|
|
2022-06-18 12:39:49 +00:00
|
|
|
if !accounts.current.isNil {
|
|
|
|
player.restoreQueue()
|
|
|
|
}
|
|
|
|
|
|
|
|
if !Defaults[.saveRecents] {
|
|
|
|
recents.clear()
|
|
|
|
}
|
|
|
|
|
|
|
|
var section = Defaults[.visibleSections].min()?.tabSelection
|
|
|
|
|
|
|
|
#if os(macOS)
|
|
|
|
if section == .playlists {
|
|
|
|
section = .search
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
navigation.tabSelection = section ?? .search
|
|
|
|
|
2022-08-15 12:49:12 +00:00
|
|
|
NavigationModel.shared = navigation
|
|
|
|
|
2022-06-18 12:39:49 +00:00
|
|
|
subscriptions.load()
|
|
|
|
playlists.load()
|
|
|
|
|
2022-08-29 13:31:36 +00:00
|
|
|
#if !os(macOS)
|
2022-07-11 16:10:51 +00:00
|
|
|
player.updateRemoteCommandCenter()
|
2022-06-18 12:39:49 +00:00
|
|
|
#endif
|
2022-08-08 18:02:46 +00:00
|
|
|
|
2022-08-14 16:59:04 +00:00
|
|
|
if player.presentingPlayer {
|
|
|
|
player.presentingPlayer = false
|
|
|
|
}
|
2022-08-21 14:53:46 +00:00
|
|
|
|
|
|
|
#if os(iOS)
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
|
|
|
|
if Defaults[.lockPortraitWhenBrowsing] {
|
|
|
|
Orientation.lockOrientation(.portrait, andRotateTo: .portrait)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2022-08-26 20:44:02 +00:00
|
|
|
|
|
|
|
#if !os(macOS)
|
|
|
|
try? AVAudioSession.sharedInstance().setCategory(.playback)
|
|
|
|
try? AVAudioSession.sharedInstance().setActive(true)
|
|
|
|
#endif
|
2022-06-18 12:39:49 +00:00
|
|
|
}
|
2022-08-25 23:36:46 +00:00
|
|
|
|
|
|
|
func migrateAccounts() {
|
|
|
|
Defaults[.accounts].forEach { account in
|
|
|
|
if !account.username.isEmpty || !(account.password?.isEmpty ?? true) || !(account.name?.isEmpty ?? true) {
|
|
|
|
print("Account needs migration: \(account.description)")
|
|
|
|
if account.app == .invidious {
|
|
|
|
if let name = account.name, !name.isEmpty {
|
|
|
|
AccountsModel.setCredentials(account, username: name, password: "")
|
|
|
|
}
|
|
|
|
if !account.username.isEmpty {
|
|
|
|
AccountsModel.setToken(account, account.username)
|
|
|
|
}
|
|
|
|
} else if account.app == .piped,
|
|
|
|
!account.username.isEmpty,
|
|
|
|
let password = account.password,
|
|
|
|
!password.isEmpty
|
|
|
|
{
|
|
|
|
AccountsModel.setCredentials(account, username: account.username, password: password)
|
|
|
|
}
|
|
|
|
|
|
|
|
AccountsModel.removeDefaultsCredentials(account)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-09 20:51:23 +00:00
|
|
|
}
|