ExtensionDelegate.swift 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*****************************************************************************
  2. * ExtensionDelegate.swift
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2015 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Tobias Conradi <videolan # tobias-conradi.de>
  9. *
  10. * Refer to the COPYING file of the official project for license.
  11. *****************************************************************************/
  12. import WatchKit
  13. import WatchConnectivity
  14. import CoreData
  15. import MediaLibraryKit
  16. class ExtensionDelegate: NSObject, WKExtensionDelegate, WCSessionDelegate, NSFileManagerDelegate {
  17. func applicationDidFinishLaunching() {
  18. // Perform any final initialization of your application.
  19. let additionalOptions = [NSReadOnlyPersistentStoreOption : NSNumber(bool: true)]
  20. let library = MLMediaLibrary.sharedMediaLibrary() as! MLMediaLibrary
  21. library.additionalPersitentStoreOptions = additionalOptions
  22. WCSession.defaultSession().delegate = self;
  23. WCSession.defaultSession().activateSession()
  24. }
  25. func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) {
  26. let msg = VLCWatchMessage(dictionary: message)
  27. if msg.name == VLCWatchMessageNameNotification, let payloadDict = msg.payload as? [String : AnyObject] {
  28. if let name = payloadDict["name"] as? String {
  29. handleRemoteNotification(name, userInfo: payloadDict["userInfo"] as? [String : AnyObject])
  30. }
  31. }
  32. }
  33. func handleRemoteNotification(name:String, userInfo: [String: AnyObject]?) {
  34. NSNotificationCenter.defaultCenter().postNotificationName(name, object: self, userInfo: userInfo)
  35. }
  36. func session(session: WCSession, didReceiveFile file: WCSessionFile) {
  37. let fileType = file.metadata?["filetype"] as? String ?? ""
  38. switch (fileType) {
  39. case "coredata":
  40. dispatch_sync(dispatch_get_main_queue())
  41. {
  42. self.copyUpdatedCoreDataDBFromURL(file.fileURL)
  43. }
  44. case "thumbnail":
  45. if let data = NSData(contentsOfURL: file.fileURL),
  46. let image = UIImage(data: data),
  47. let URIRepresentation = file.metadata?["URIRepresentation"] as? String,
  48. let objectIDURL = NSURL(string: URIRepresentation) {
  49. setImage(image, forObjectIDURL: objectIDURL)
  50. }
  51. default:
  52. NSLog("unandled file with meta data \(file.metadata)")
  53. }
  54. }
  55. func copyUpdatedCoreDataDBFromURL(url:NSURL) {
  56. let library = MLMediaLibrary.sharedMediaLibrary() as! MLMediaLibrary
  57. library.overrideLibraryWithLibraryFromURL(url)
  58. NSNotificationCenter.defaultCenter().postNotificationName(VLCDBUpdateNotification, object: self)
  59. }
  60. func setImage(image: UIImage, forObjectIDURL objectIDURL: NSURL) {
  61. let library = MLMediaLibrary.sharedMediaLibrary()
  62. if let file = library.objectForURIRepresentation(objectIDURL) as? MLFile {
  63. file.managedObjectContext?.performBlock({ () -> Void in
  64. file.computedThumbnail = image
  65. })
  66. }
  67. }
  68. }