From 5083abe3ed02e2ffe2b0b8d01d1c57d5877c9f21 Mon Sep 17 00:00:00 2001 From: Damiano Galassi Date: Thu, 13 Oct 2016 10:54:17 +0200 Subject: [PATCH] Fixed some deprecated api warnings. --- Classes/MetadataImporters/SBChapterDB.m | 56 ++++++++++++------- Classes/MetadataImporters/SBChapterImporter.h | 10 +--- Classes/MetadataImporters/SBChapterImporter.m | 24 +------- Classes/MetadataImporters/SBMetadataHelper.h | 2 +- Classes/MetadataImporters/SBMetadataHelper.m | 41 ++++++-------- Classes/PropertyView/Base.lproj/MovieView.xib | 10 ++-- .../PropertyView/SBChapterViewController.m | 2 +- Classes/SBAppDelegate.m | 4 +- Classes/SBChapterSearchController.m | 5 +- Classes/SBDocument.m | 2 +- Classes/SBFileImport.m | 4 +- Classes/SBMetadataSearchController.m | 2 +- 12 files changed, 74 insertions(+), 88 deletions(-) diff --git a/Classes/MetadataImporters/SBChapterDB.m b/Classes/MetadataImporters/SBChapterDB.m index 45764e0..b0e159e 100644 --- a/Classes/MetadataImporters/SBChapterDB.m +++ b/Classes/MetadataImporters/SBChapterDB.m @@ -14,41 +14,55 @@ #define API_KEY @"ETET7TXFJH45YNYW0I4A" +@interface SBChapterDB () + +@property (nonatomic, readwrite) NSURLSessionTask *task; + +@end + @implementation SBChapterDB -- (NSArray *)searchTitle:(NSString *)title language:(nullable NSString *)language duration:(NSUInteger)duration +- (void)searchTitle:(NSString *)title language:(nullable NSString *)language duration:(NSUInteger)duration completionHandler:(void(^)(NSArray *results))handler { NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.chapterdb.org/chapters/search?title=%@", [SBMetadataHelper urlEncoded:title]]]; NSDictionary *headerOptions = @{@"ApiKey" : API_KEY}; - NSData *xmlData = xmlData = [SBMetadataHelper dataFromUrl:url withHTTPMethod:@"GET" headerOptions:headerOptions error:nil]; + self.task = [SBMetadataHelper sessionTaskFromUrl:url withHTTPMethod:@"GET" headerOptions:headerOptions completionHandler:^(NSData * _Nullable data) { + NSXMLDocument *xml = [[NSXMLDocument alloc] initWithData:data + options:0 + error:NULL]; - NSXMLDocument *xml = [[NSXMLDocument alloc] initWithData:xmlData - options:0 - error:NULL]; + NSMutableArray *resultsArray = [[NSMutableArray alloc] init]; - NSMutableArray *resultsArray = [[NSMutableArray alloc] init]; + if (xml) { + NSArray *children = [xml nodesForXPath:@"//*:chapterInfo" error:nil]; - if (xml) { - NSArray *children = [xml nodesForXPath:@"//*:chapterInfo" error:nil]; - - for (NSXMLNode *child in children) { - SBChapterResult *result = [self parseResult:child]; - if (result) { - [resultsArray addObject:result]; + for (NSXMLNode *child in children) { + SBChapterResult *result = [self parseResult:child]; + if (result) { + [resultsArray addObject:result]; + } } } - } - - if (duration > 0) { - NSArray *filteredArray = [self filterArray:resultsArray byDuration:duration delta:10000]; - if (filteredArray.count) { - return filteredArray; + if (duration > 0) { + NSArray *filteredArray = [self filterArray:resultsArray byDuration:duration delta:10000]; + if (filteredArray.count) { + handler(filteredArray); + return; + } } - } + + handler(resultsArray); - return resultsArray; + }]; + + [self.task resume]; +} + +- (void)cancel; +{ + [self.task cancel]; } /** diff --git a/Classes/MetadataImporters/SBChapterImporter.h b/Classes/MetadataImporters/SBChapterImporter.h index 91a2304..3e9945b 100644 --- a/Classes/MetadataImporters/SBChapterImporter.h +++ b/Classes/MetadataImporters/SBChapterImporter.h @@ -13,20 +13,16 @@ NS_ASSUME_NONNULL_BEGIN -@interface SBChapterImporter : NSObject { -@private - BOOL _isCancelled; -} +@interface SBChapterImporter : NSObject + (NSString *)defaultProvider; + (nullable instancetype)importerForProvider:(NSString *)providerName; -- (void) searchTitle:(NSString *)title language:(nullable NSString *)language duration:(NSUInteger)duration completionHandler:(void(^)(NSArray *results))handler; -- (void) cancel; #pragma mark Methods to be overridden -- (NSArray *)searchTitle:(NSString *)title language:(nullable NSString *)language duration:(NSUInteger)duration; +- (void)searchTitle:(NSString *)title language:(nullable NSString *)language duration:(NSUInteger)duration completionHandler:(void(^)(NSArray *results))handler; +- (void)cancel; @end diff --git a/Classes/MetadataImporters/SBChapterImporter.m b/Classes/MetadataImporters/SBChapterImporter.m index a4b0605..0851611 100644 --- a/Classes/MetadataImporters/SBChapterImporter.m +++ b/Classes/MetadataImporters/SBChapterImporter.m @@ -11,16 +11,8 @@ #import "SBChapterDB.h" -@interface SBChapterImporter () - -@property (atomic, readwrite) BOOL isCancelled; - -@end - @implementation SBChapterImporter -@synthesize isCancelled = _isCancelled; - + (NSString *)defaultProvider { return @"ChapterDB"; @@ -36,22 +28,12 @@ - (void)searchTitle:(NSString *)title language:(nullable NSString *)language duration:(NSUInteger)duration completionHandler:(void(^)(NSArray *results))handler { - dispatch_async(dispatch_get_global_queue(0, 0), ^{ - NSArray *results = [self searchTitle:title language:language duration:duration]; - dispatch_async(dispatch_get_main_queue(), ^{ - if (!self.isCancelled) { - handler(results); - } - }); - }); + @throw [NSException exceptionWithName:NSInternalInconsistencyException + reason:[NSString stringWithFormat:@"You must override %@ in a subclass", NSStringFromSelector(_cmd)] + userInfo:nil]; } - (void)cancel -{ - self.isCancelled = YES; -} - -- (NSArray *)searchTitle:(NSString *)title language:(nullable NSString *)language duration:(NSUInteger)duration { @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:[NSString stringWithFormat:@"You must override %@ in a subclass", NSStringFromSelector(_cmd)] diff --git a/Classes/MetadataImporters/SBMetadataHelper.h b/Classes/MetadataImporters/SBMetadataHelper.h index b06a546..d6d27b8 100644 --- a/Classes/MetadataImporters/SBMetadataHelper.h +++ b/Classes/MetadataImporters/SBMetadataHelper.h @@ -23,7 +23,7 @@ typedef NS_ENUM(NSUInteger, SBCachePolicy) { + (NSString *)urlEncoded:(NSString *)string; + (nullable NSData *)downloadDataFromURL:(NSURL *)url withCachePolicy:(SBCachePolicy)policy; -+ (nullable NSData *)dataFromUrl:(NSURL *)url withHTTPMethod:(NSString *)method headerOptions:(nullable NSDictionary *)header error:(NSError **)outError; ++ (NSURLSessionTask *)sessionTaskFromUrl:(NSURL *)url withHTTPMethod:(NSString *)method headerOptions:(nullable NSDictionary *)header completionHandler:(void(^)(NSData * _Nullable data))completionHandler; @end diff --git a/Classes/MetadataImporters/SBMetadataHelper.m b/Classes/MetadataImporters/SBMetadataHelper.m index eb3963a..fdafc3c 100644 --- a/Classes/MetadataImporters/SBMetadataHelper.m +++ b/Classes/MetadataImporters/SBMetadataHelper.m @@ -121,14 +121,7 @@ } + (NSString *)urlEncoded:(NSString *)string { - string = string.precomposedStringWithCompatibilityMapping; - CFStringRef urlString = CFURLCreateStringByAddingPercentEscapes( - NULL, - (CFStringRef) string, - NULL, - (CFStringRef) @"!*'\"();:@&=+$,/?%#[]% ", - kCFStringEncodingUTF8); - return CFAutorelease(urlString); + return [string.precomposedStringWithCompatibilityMapping stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; } + (NSString *)md5String:(NSString *)s { @@ -189,7 +182,7 @@ } #pragma mark NSURLRequest -+ (nullable NSData *)dataFromUrl:(NSURL *)url withHTTPMethod:(NSString *)method headerOptions:(nullable NSDictionary *)header error:(NSError * __autoreleasing *)outError ++ (NSURLSessionTask *)sessionTaskFromUrl:(NSURL *)url withHTTPMethod:(NSString *)method headerOptions:(nullable NSDictionary *)header completionHandler:(void(^)(NSData * _Nullable data))completionHandler { NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy @@ -202,24 +195,24 @@ } } - NSURLResponse *response = nil; - NSError *error = nil; - NSData *data = [NSURLConnection sendSynchronousRequest:request - returningResponse:&response - error:&error]; + NSURLSession *defaultSession = [NSURLSession sharedSession]; + NSURLSessionTask *task = [defaultSession dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { + if (data) { + NSUInteger statusCode = 0; + if ([response isKindOfClass:[NSHTTPURLResponse class]]) { + statusCode = (long)((NSHTTPURLResponse *)response).statusCode; + } - if (data) { - NSUInteger statusCode = 0; - if ([response isKindOfClass:[NSHTTPURLResponse class]]) { - statusCode = (long)((NSHTTPURLResponse *)response).statusCode; + if (statusCode == 200) { + completionHandler(data); + } + else { + completionHandler(nil); + } } - if (statusCode == 200) { - return data; - } - } - - return nil; + }]; + return task; } @end diff --git a/Classes/PropertyView/Base.lproj/MovieView.xib b/Classes/PropertyView/Base.lproj/MovieView.xib index 321c1a2..e78c737 100755 --- a/Classes/PropertyView/Base.lproj/MovieView.xib +++ b/Classes/PropertyView/Base.lproj/MovieView.xib @@ -348,9 +348,9 @@