Skip to content

Commit

Permalink
Merge pull request #11 from NiklasMerz/delete-with-identifiers
Browse files Browse the repository at this point in the history
Add clearItemsForIdentifiers function
  • Loading branch information
Johan Blomgren authored Nov 15, 2016
2 parents 8d26092 + f03461b commit f098b21
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 25 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions src/ios/app/IndexAppContent.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
66 changes: 41 additions & 25 deletions src/ios/app/IndexAppContent.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ - (void)deviceIsReady:(CDVInvokedUrlCommand *)command
- (void)pluginInitialize
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleIndexAppContentDelayExecutionNotification:) name:kIndexAppContentDelayExecutionNotification object:nil];

self.ready = YES;
}

Expand All @@ -50,51 +50,51 @@ - (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"];
NSString *identifier = [item objectForKey:@"identifier"];
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) {
Expand All @@ -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);
Expand All @@ -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 {
Expand All @@ -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;
Expand All @@ -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];
}
Expand All @@ -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];
}

Expand All @@ -195,7 +211,7 @@ - (void)_clearTimestamp
- (NSInteger)_getIndexingInterval
{
NSInteger interval = [[[NSUserDefaults standardUserDefaults] objectForKey:kINDEXING_INTERVAL_KEY] integerValue];

return interval ? interval : kDEFAULT_INDEXING_INTERVAL;
}

Expand All @@ -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;
}

Expand Down
4 changes: 4 additions & 0 deletions www/IndexAppContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
};
Expand Down

0 comments on commit f098b21

Please sign in to comment.