Swift Media Player
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

201 lines
11 KiB

//
// AppDelegate.swift
// kplayer
//
// Created by Marco Schmickler on 24.05.15.
// Copyright (c) 2015 Marco Schmickler. All rights reserved.
//
import UIKit
import CoreData
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let splitViewController = self.window!.rootViewController as! UISplitViewController
let navigationController = splitViewController.viewControllers[splitViewController.viewControllers.count-1] as! UINavigationController
navigationController.topViewController!.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem
splitViewController.delegate = self
let masterNavigationController = splitViewController.viewControllers[0] as! UINavigationController
let controller = masterNavigationController.topViewController as! MasterViewController
controller.delegate = NetworkDelegate()
let url = URL(string: NetworkManager.sharedInstance.vidurl)?.appendingPathComponent("ren").appendingPathComponent("kplayer.txt")
var roots = [MediaItem]()
do {
let remoteroots = try String(contentsOf: url!)
let components = remoteroots.split(separator: "\n")
for c in components {
let line = c.split(separator: " ")
let name = String(line[0])
let root = String(line[1])
let item = MediaItem(name: name, path:"", root: root, type: ItemType.REMOTEROOT)
roots.append(item)
}
} catch {
}
let web = MediaItem(name: "web", path:"", root: "/srv/samba/ren/web", type: ItemType.WEBROOT)
// let google = MediaItem(name: "google", path:"", root: "www.google.de", type: ItemType.FOLDER)
// let a = MediaItem(name: "google", path:"", root: "www.google.de", type: ItemType.WEB)
// google.children = [ a ]
// web.children = [
//google
// ]
roots.append(NetworkManager.sharedInstance.favorites)
roots.append(web)
controller.model.items = roots
let neues = [
MediaItem(name: "sp", path:"", root: "/srv/samba/ren/sp/video", type: ItemType.REMOTEROOT),
MediaItem(name: "fav", path:"", root: "/srv/samba/ren/fav", type: ItemType.REMOTEROOT),
MediaItem(name: "knk", path:"", root: "/srv/samba/ren/knk", type: ItemType.REMOTEROOT),
MediaItem(name: "knk_archiv", path:"", root: "/srv/samba/ren/knk_archiv", type: ItemType.REMOTEROOT),
MediaItem(name: "knk_archiv2", path:"", root: "/srv/samba/ren/knk_archiv2", type: ItemType.REMOTEROOT),
MediaItem(name: "knk_archiv3", path:"", root: "/srv/samba/ren/knk_archiv3", type: ItemType.REMOTEROOT),
MediaItem(name: "knk_archiv4", path:"", root: "/srv/samba/ren/knk_archiv4", type: ItemType.REMOTEROOT),
MediaItem(name: "real", path:"", root: "/srv/samba/ren/real", type: ItemType.REMOTEROOT),
MediaItem(name: "fetish", path:"", root: "/srv/samba/ren/fetish", type: ItemType.REMOTEROOT),
MediaItem(name: "fjoy", path:"", root: "/srv/samba/ren/fjoy", type: ItemType.REMOTEROOT),
MediaItem(name: "heg", path:"", root: "/srv/samba/ren/heg", type: ItemType.REMOTEROOT),
MediaItem(name: "ten", path:"", root: "/srv/samba/ren/ten", type: ItemType.REMOTEROOT),
MediaItem(name: "leg", path:"", root: "/srv/samba/ren/leg", type: ItemType.REMOTEROOT),
MediaItem(name: "ang", path:"", root: "/srv/samba/ren/ang", type: ItemType.REMOTEROOT),
MediaItem(name: "series", path:"", root: "/srv/samba/ren/series", type: ItemType.REMOTEROOT),
MediaItem(name: "bm", path:"", root: "/srv/samba/ren/bm", type: ItemType.REMOTEROOT),
MediaItem(name: "medieval", path:"", root: "/srv/samba/ren/medieval", type: ItemType.REMOTEROOT),
NetworkManager.sharedInstance.favorites,
web,
]
NetworkManager.sharedInstance.alive()
Timer.scheduledTimer(withTimeInterval: 60, repeats: true) { (t) in
NetworkManager.sharedInstance.alive()
}
return true
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(_ application: UIApplication) {
NetworkManager.sharedInstance.alive()
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
// Saves changes in the application's managed object context before the application terminates.
self.saveContext()
}
// MARK: - Split view
func splitViewController(_ splitViewController: UISplitViewController, collapseSecondary secondaryViewController:UIViewController, onto primaryViewController:UIViewController) -> Bool {
if let secondaryAsNavController = secondaryViewController as? UINavigationController {
if let topAsDetailController = secondaryAsNavController.topViewController as? DetailViewController {
if topAsDetailController.detailItem == nil {
// Return true to indicate that we have handled the collapse by doing nothing; the secondary controller will be discarded.
return true
}
}
}
return false
}
// MARK: - Core Data stack
lazy var applicationDocumentsDirectory: URL = {
// The directory the application uses to store the Core Data store file. This code uses a directory named "schmickler.kplayer" in the application's documents Application Support directory.
let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
return urls[urls.count-1]
}()
lazy var managedObjectModel: NSManagedObjectModel = {
// The managed object model for the application. This property is not optional. It is a fatal error for the application not to be able to find and load its model.
let modelURL = Bundle.main.url(forResource: "kplayer", withExtension: "momd")!
return NSManagedObjectModel(contentsOf: modelURL)!
}()
lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? = {
// The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it. This property is optional since there are legitimate error conditions that could cause the creation of the store to fail.
// Create the coordinator and store
var coordinator: NSPersistentStoreCoordinator? = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
let url = self.applicationDocumentsDirectory.appendingPathComponent("kplayer.sqlite")
var error: NSError? = nil
var failureReason = "There was an error creating or loading the application's saved data."
do {
try coordinator!.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url, options: nil)
} catch var error1 as NSError {
error = error1
coordinator = nil
// Report any error we got.
var dict = [String: AnyObject]()
dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data" as AnyObject?
dict[NSLocalizedFailureReasonErrorKey] = failureReason as AnyObject?
dict[NSUnderlyingErrorKey] = error
error = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
// Replace this with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog("Unresolved error \(String(describing: error)), \(error!.userInfo)")
abort()
} catch {
fatalError()
}
return coordinator
}()
lazy var managedObjectContext: NSManagedObjectContext? = {
// Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) This property is optional since there are legitimate error conditions that could cause the creation of the context to fail.
let coordinator = self.persistentStoreCoordinator
if coordinator == nil {
return nil
}
var managedObjectContext = NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.mainQueueConcurrencyType)
managedObjectContext.persistentStoreCoordinator = coordinator
return managedObjectContext
}()
// MARK: - Core Data Saving support
func saveContext () {
if let moc = self.managedObjectContext {
if moc.hasChanges {
do {
try moc.save()
} catch let error as NSError {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog("Unresolved error \(error), \(error.userInfo)")
abort()
}
}
}
}
}