From f03461b78c015f96cf08d011e9f5a5f5afadb004 Mon Sep 17 00:00:00 2001 From: Niklas Merz Date: Fri, 21 Oct 2016 13:54:44 +0200 Subject: [PATCH] Add clearItemsForIdentifiers function --- README.md | 13 +++++++ src/ios/app/IndexAppContent.h | 1 + src/ios/app/IndexAppContent.m | 66 ++++++++++++++++++++++------------- www/IndexAppContent.js | 4 +++ 4 files changed, 59 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index cbf664c..b044eab 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,19 @@ window.plugins.indexAppContent.clearItemsForDomains(['com.my.domain', 'com.my.ot ``` +Call ``window.plugins.indexAppContent.clearItemsForIdentifiers(identifiers, success, error)`` to clear all items stored for a given array of identifiers. + +Example: + +``` +window.plugins.indexAppContent.clearItemsForIdentifiers(['id1', 'id2'], function() { + console.log('Items removed'); + }, function(error) { + // Handle error + }); + +``` + ### Set indexing interval Call ``window.plugins.indexAppContent.setIndexingInterval(interval, success, error)`` to configure the interval (in minutes) for how often indexing should be allowed. diff --git a/src/ios/app/IndexAppContent.h b/src/ios/app/IndexAppContent.h index 9df9b4f..c884d48 100644 --- a/src/ios/app/IndexAppContent.h +++ b/src/ios/app/IndexAppContent.h @@ -20,6 +20,7 @@ UIKIT_EXTERN NSString *kIndexAppContentExecutionDelayKey; - (void)deviceIsReady:(CDVInvokedUrlCommand *)command; - (void)setItems:(CDVInvokedUrlCommand *)command; - (void)clearItemsForDomains:(CDVInvokedUrlCommand *)command; +- (void)clearItemsForIdentifiers:(CDVInvokedUrlCommand *)command; - (void)setIndexingInterval:(CDVInvokedUrlCommand *)command; @end diff --git a/src/ios/app/IndexAppContent.m b/src/ios/app/IndexAppContent.m index 04574c2..efff5b5 100644 --- a/src/ios/app/IndexAppContent.m +++ b/src/ios/app/IndexAppContent.m @@ -39,7 +39,7 @@ - (void)deviceIsReady:(CDVInvokedUrlCommand *)command - (void)pluginInitialize { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleIndexAppContentDelayExecutionNotification:) name:kIndexAppContentDelayExecutionNotification object:nil]; - + self.ready = YES; } @@ -50,20 +50,20 @@ - (void)dealloc - (void)setItems:(CDVInvokedUrlCommand *)command { - + if (![self _shouldUpdateIndex]) { [self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Will not update index"] callbackId:command.callbackId]; - + return; } - + _group = dispatch_group_create(); _queue = dispatch_queue_create(kGROUP_IDENTIFIER, NULL); - + NSArray *items = [command.arguments firstObject]; - + __block NSMutableArray *searchableItems = [[NSMutableArray alloc] init]; - + for (NSDictionary *item in items) { dispatch_group_async(_group, _queue, ^{ NSString *domain = [item objectForKey:@"domain"]; @@ -71,30 +71,30 @@ - (void)setItems:(CDVInvokedUrlCommand *)command NSString *title = [item objectForKey:@"title"]; NSString *description = [item objectForKey:@"description"]; NSString *url = [item objectForKey:@"url"]; - + // Optional NSNumber *rating = [NSNumber numberWithInteger:[[item objectForKey:@"rating"] integerValue]]; NSArray *keywords = [item objectForKey:@"keywords"]; NSInteger lifetime = [[item objectForKey:@"lifetime"] integerValue]; - + NSLog(@"Indexing %@", title); - + CSSearchableItemAttributeSet *attributes = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:(__bridge NSString *)kUTTypeText]; - + attributes.title = title; attributes.contentDescription = description; attributes.thumbnailData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]]; attributes.displayName = title; attributes.rating = rating; attributes.keywords = keywords; - + CSSearchableItem *item = [[CSSearchableItem alloc] initWithUniqueIdentifier:identifier domainIdentifier:domain attributeSet:attributes]; item.expirationDate = lifetime ? [self _dateByMinuteOffset:lifetime] : nil; - + [searchableItems addObject:item]; }); } - + dispatch_group_notify(_group, _queue, ^{ [[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:searchableItems completionHandler:^(NSError * _Nullable error) { if (error) { @@ -111,7 +111,7 @@ - (void)setItems:(CDVInvokedUrlCommand *)command - (void)clearItemsForDomains:(CDVInvokedUrlCommand *)command { NSArray *domains = [command.arguments firstObject]; - + [[CSSearchableIndex defaultSearchableIndex] deleteSearchableItemsWithDomainIdentifiers:domains completionHandler:^(NSError * _Nullable error) { if (error) { NSLog(@"%@", error); @@ -124,10 +124,26 @@ - (void)clearItemsForDomains:(CDVInvokedUrlCommand *)command }]; } +- (void)clearItemsForIdentifiers:(CDVInvokedUrlCommand *)command +{ + NSArray *identifiers = [command.arguments firstObject]; + + [[CSSearchableIndex defaultSearchableIndex] deleteSearchableItemsWithIdentifiers:identifiers completionHandler:^(NSError * _Nullable error) { + if (error) { + NSLog(@"%@", error); + [self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR] callbackId:command.callbackId]; + } else { + NSLog(@"Items removed with identifiers %@", identifiers); + [self _clearTimestamp]; + [self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK] callbackId:command.callbackId]; + } + }]; +} + - (void)setIndexingInterval:(CDVInvokedUrlCommand *)command { NSInteger interval = [[command.arguments firstObject] integerValue]; - + if ([self _setIndexingInterval:interval]) { [self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK] callbackId:command.callbackId]; } else { @@ -140,9 +156,9 @@ - (void)setIndexingInterval:(CDVInvokedUrlCommand *)command - (void)handleIndexAppContentDelayExecutionNotification:(NSNotification *)notification { float delay = [notification.userInfo[kIndexAppContentExecutionDelayKey] floatValue]; - + self.ready = NO; - + dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delay * NSEC_PER_SEC); dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ self.ready = YES; @@ -155,21 +171,21 @@ - (BOOL)_shouldUpdateIndex { NSDate *updatedAt = [self _getTimestamp]; BOOL shouldUpdate = YES; - + if (updatedAt && [updatedAt compare:[NSDate date]] == NSOrderedDescending) { NSLog(@"Will not update index. Last update: %@", updatedAt); shouldUpdate = NO; } else { [self _setTimestamp]; } - + return shouldUpdate; } - (void)_setTimestamp { NSDate *nextDate = [self _dateByMinuteOffset:[self _getIndexingInterval]]; - + [[NSUserDefaults standardUserDefaults] setObject:nextDate forKey:kINDEX_TIMESTAMP_KEY]; [[NSUserDefaults standardUserDefaults] synchronize]; } @@ -178,7 +194,7 @@ - (NSDate *)_dateByMinuteOffset:(NSInteger)minuteOffset { NSDateComponents *minuteComponent = [[NSDateComponents alloc] init]; minuteComponent.minute = minuteOffset; - + return [[NSCalendar currentCalendar] dateByAddingComponents:minuteComponent toDate:[NSDate date] options:0]; } @@ -195,7 +211,7 @@ - (void)_clearTimestamp - (NSInteger)_getIndexingInterval { NSInteger interval = [[[NSUserDefaults standardUserDefaults] objectForKey:kINDEXING_INTERVAL_KEY] integerValue]; - + return interval ? interval : kDEFAULT_INDEXING_INTERVAL; } @@ -204,10 +220,10 @@ - (BOOL)_setIndexingInterval:(NSInteger)interval if (!interval) { return NO; } - + [[NSUserDefaults standardUserDefaults] setObject:@(interval) forKey:kINDEXING_INTERVAL_KEY]; [[NSUserDefaults standardUserDefaults] synchronize]; - + return YES; } diff --git a/www/IndexAppContent.js b/www/IndexAppContent.js index 140540f..ead153e 100644 --- a/www/IndexAppContent.js +++ b/www/IndexAppContent.js @@ -14,6 +14,10 @@ IndexAppContent.prototype.clearItemsForDomains = function (domains, onSuccess, o exec(onSuccess, onError, "IndexAppContent", "clearItemsForDomains", [domains]); }; +IndexAppContent.prototype.clearItemsForIdentifiers = function (identifiers, onSuccess, onError) { + exec(onSuccess, onError, "IndexAppContent", "clearItemsForIdentifiers", [identifiers]); +}; + IndexAppContent.prototype.setIndexingInterval = function (interval, onSuccess, onError) { exec(onSuccess, onError, "IndexAppContent", "setIndexingInterval", [interval]); };