VLCExtensionDelegate.m 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*****************************************************************************
  2. * VLCExtensionDelegate.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2015-2016 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Tobias Conradi <videolan # tobias-conradi.de>
  9. * Felix Paul Kühne <fkuehne # videolan.org>
  10. *
  11. * Refer to the COPYING file of the official project for license.
  12. *****************************************************************************/
  13. #import "VLCExtensionDelegate.h"
  14. #import <WatchConnectivity/WatchConnectivity.h>
  15. #import <CoreData/CoreData.h>
  16. #import <MediaLibraryKit/MediaLibraryKit.h>
  17. #import "VLCWatchMessage.h"
  18. #import "VLCBaseInterfaceController.h"
  19. @interface VLCExtensionDelegate() <NSFileManagerDelegate, WCSessionDelegate>
  20. @end
  21. @implementation VLCExtensionDelegate
  22. - (void)applicationDidFinishLaunching
  23. {
  24. MLMediaLibrary *library = [MLMediaLibrary sharedMediaLibrary];
  25. library.additionalPersitentStoreOptions = @{ NSReadOnlyPersistentStoreOption : @(YES) };
  26. WCSession *wcsession = [WCSession defaultSession];
  27. wcsession.delegate = self;
  28. [wcsession activateSession];
  29. }
  30. - (void)session:(WCSession *)session didReceiveMessage:(NSDictionary<NSString *,id> *)message
  31. {
  32. VLCWatchMessage *msg = [[VLCWatchMessage alloc] initWithDictionary:message];
  33. if ([msg.name isEqualToString: VLCWatchMessageNameNotification]) {
  34. NSDictionary *payloadDict = (NSDictionary *)msg.payload;
  35. NSString *name = payloadDict[@"name"];
  36. if (name) {
  37. [[NSNotificationCenter defaultCenter] postNotificationName:name
  38. object:self
  39. userInfo:payloadDict[@"userInfo"]];
  40. }
  41. }
  42. }
  43. - (void)session:(WCSession *)session didReceiveFile:(WCSessionFile *)file
  44. {
  45. NSString *fileType = file.metadata[@"filetype"];
  46. if ([fileType isEqualToString:@"coredata"]) {
  47. dispatch_sync(dispatch_get_main_queue(), ^{
  48. [self copyUpdatedCoreDataDBFromURL:file.fileURL];
  49. });
  50. }
  51. if ([fileType isEqualToString:@"thumbnail"]) {
  52. [self copyThumbnailToDatabase:file];
  53. }
  54. }
  55. - (void)copyUpdatedCoreDataDBFromURL:(NSURL *)url
  56. {
  57. MLMediaLibrary *library = [MLMediaLibrary sharedMediaLibrary];
  58. [library overrideLibraryWithLibraryFromURL:url];
  59. [[NSNotificationCenter defaultCenter] postNotificationName:VLCDBUpdateNotification
  60. object:self];
  61. }
  62. - (void)copyThumbnailToDatabase:(WCSessionFile *)file
  63. {
  64. NSData *data = [NSData dataWithContentsOfURL:file.fileURL];
  65. if (!data) {
  66. return;
  67. }
  68. UIImage *image = [UIImage imageWithData:data];
  69. if (!image) {
  70. return;
  71. }
  72. NSString *uriRepresentation = file.metadata[@"URIRepresentation"];
  73. NSURL *objectIDURL = [NSURL URLWithString:uriRepresentation];
  74. if (objectIDURL) {
  75. MLMediaLibrary *library = [MLMediaLibrary sharedMediaLibrary];
  76. MLFile *file = (MLFile *)[library objectForURIRepresentation:objectIDURL];
  77. file.computedThumbnail = image;
  78. }
  79. }
  80. @end