Skip to content

Instantly share code, notes, and snippets.

@NSExceptional
Created November 2, 2015 06:57
Show Gist options
  • Save NSExceptional/19e649457894e1303a9f to your computer and use it in GitHub Desktop.
Save NSExceptional/19e649457894e1303a9f to your computer and use it in GitHub Desktop.
Modify the "News" cells of the iOS 9 Spotlight.
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
@interface SearchUISingleResultTableViewCell : NSObject
- (id)initWithResult:(id)result style:(unsigned long)style;
- (void)updateWithResult:(id)result;
@property (assign) id result;
@end
@interface SPSearchResult : NSObject
@property (assign) NSString *title;
@property (assign) NSString *summary;
@property (assign) NSString *subtitle;
@property (assign) NSString *footnote;
@property (assign) NSString *url;
@property (assign) NSUInteger score;
@property (assign, readonly) NSMutableDictionary *additionalPropertyDict;
@end
@interface SPSearchResult (TB)
- (NSString *)body;
- (void)setBody:(id)body;
@end
@implementation SPSearchResult (TB)
- (NSString *)body {
return [self additionalPropertyDict][@"descriptions"][0][@"formatted_text"][0][@"text"];
}
- (void)setBody:(id)body {
if ([self additionalPropertyDict][@"descriptions"][0][@"formatted_text"][0])
[self additionalPropertyDict][@"descriptions"][0][@"formatted_text"][0][@"text"] = body;
else {
NSMutableDictionary *dict = [@{@"descriptions": @[@{@"formatted_text": @[[NSMutableDictionary new]]}]} mutableCopy];
dict[@"descriptions"][0][@"formatted_text"][0][@"text"] = body;
[self setValue:dict forKey:@"additionalPropertyDict"];
}
}
@end
@interface SPUISearchViewController : NSObject
- (id)searchTableView;
- (BOOL)_isPullDownSpotlight;
@end
@interface SPUISearchViewController (TB)
- (NSArray *)links;
- (void)setLinks:(NSArray *)links;
@end
@implementation SPUISearchViewController (TB)
- (NSArray *)links {
return objc_getAssociatedObject(self, @selector(links));
}
- (void)setLinks:(NSArray *)links {
objc_setAssociatedObject(self, @selector(links), links, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (void)loadRedditData {
[self setLinks:@[[SPSearchResult new], [SPSearchResult new], [SPSearchResult new], [SPSearchResult new], [SPSearchResult new], [SPSearchResult new]]];
for (SPSearchResult *link in [self links]) {
[link setTitle:@"This is the title"];
[link setBody:@"This is the summary\nline 2\nline 3"];
[link setSubtitle:@"subtitle"];
[link setFootnote:@"footnote - https://www.google.com"];
}
[[self searchTableView] reloadData];
}
@end
%hook SPUISearchViewController
- (void)setTableViewShown:(BOOL)v {
%orig(v);
if (v) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self loadRedditData];
});
});
}
}
- (NSArray *)resultsForRow:(NSInteger)row inSection:(NSInteger)section {
if (section != 2) return %orig(row, section);
return @[[self links][row]];
}
- (NSInteger)numberOfSectionsInTableView:(id)tv {
return [self _isPullDownSpotlight] && [[self links] count] > 0 ? %orig(tv) : 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 2) return [[self links] count];
return %orig(tableView, section);
}
%end
%hook SearchUITextAreaView
- (BOOL)updateWithResult:(SPSearchResult *)result formatter:(id)f {
BOOL ret = %orig(result, f);
UILabel *body = [[self valueForKey:@"secondToLastView"] valueForKey:@"textLabel"];
[body setText:[result body]];
return ret;
}
%end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment