Ver código fonte

Add button to check ingest in prefs

master
lint 4 anos atrás
pai
commit
0a5a220db0
3 arquivos alterados com 177 adições e 2 exclusões
  1. +14
    -0
      prefs/Resources/Root.plist
  2. +9
    -1
      prefs/TFDTSRootListController.h
  3. +154
    -1
      prefs/TFDTSRootListController.m

+ 14
- 0
prefs/Resources/Root.plist Ver arquivo

@@ -248,6 +248,20 @@
<key>showValue</key>
<true/>
</dict>
<dict>
<key>cell</key>
<string>PSGroupCell</string>
<key>footerText</key>
<string>Displays how far behind the ingest is (i.e. how long ago the most recently archived comment and post were created).</string>
</dict>
<dict>
<key>cell</key>
<string>PSButtonCell</string>
<key>label</key>
<string>Check Latest Ingest Times</string>
<key>action</key>
<string>checkLatest:</string>
</dict>
</array>
<key>title</key>
<string>TFDidThatSay?</string>

+ 9
- 1
prefs/TFDTSRootListController.h Ver arquivo

@@ -1,5 +1,13 @@
#import <Preferences/PSListController.h>
#import <Preferences/PSSpecifier.h>

@interface TFDTSRootListController : PSListController
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

@interface TFDTSRootListController : PSListController
@property(strong, nonatomic) PSSpecifier *latestPostSpecifier;
@property(strong, nonatomic) PSSpecifier *latestCommentSpecifier;
- (void)checkLatest:(PSSpecifier *)arg1;
- (void)performPushshiftRequest:(BOOL)isComment insertAfterSpecifier:(PSSpecifier *)arg2;
- (void)insertLatestTimeCell:(NSDictionary *)data;
- (void)possiblyBothChecksComplete:(PSSpecifier *)arg1;
@end

+ 154
- 1
prefs/TFDTSRootListController.m Ver arquivo

@@ -1,8 +1,18 @@
#include "TFDTSRootListController.h"
#import <Preferences/PSSpecifier.h>

@implementation TFDTSRootListController

- (instancetype)init {

self = [super init];

if (self) {

}

return self;
}

- (NSArray *)specifiers {
if (!_specifiers) {
_specifiers = [self loadSpecifiersFromPlistName:@"Root" target:self];
@@ -31,4 +41,147 @@
}
}

- (void)checkLatest:(PSSpecifier *)arg1 {

NSIndexPath *indexPath = [self indexPathForSpecifier:arg1];
UITableViewCell *cell = [self.table cellForRowAtIndexPath:indexPath];

cell.userInteractionEnabled = NO;

UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];

if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"13.0")) {
activityView.activityIndicatorViewStyle = UIScreen.mainScreen.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark ? UIActivityIndicatorViewStyleWhite : UIActivityIndicatorViewStyleGray;
}

[cell setAccessoryView:activityView];
[activityView startAnimating];

// removes latest time rows if they already exist
if (_latestPostSpecifier) {
[self removeSpecifier:_latestPostSpecifier animated:YES];
_latestPostSpecifier = nil;
}
if (_latestCommentSpecifier) {
[self removeSpecifier:_latestCommentSpecifier animated:YES];
_latestCommentSpecifier = nil;
}

// performs the requests for the most recent comment and post
[self performPushshiftRequest:YES insertAfterSpecifier:arg1];
[self performPushshiftRequest:NO insertAfterSpecifier:arg1];
}

- (void)performPushshiftRequest:(BOOL)isComment insertAfterSpecifier:(PSSpecifier *)arg2 {

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
//NSOperationQueue *queue = [[NSOperationQueue alloc] init];

if (isComment){
[request setURL:[NSURL URLWithString:@"https://api.pushshift.io/reddit/search/comment/?fields=created_utc&size=1"]];
} else {
[request setURL:[NSURL URLWithString:@"https://api.pushshift.io/reddit/search/submission/?fields=created_utc&size=1"]];
}

[request setHTTPMethod:@"GET"];
[request setTimeoutInterval:10];

NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
//[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

NSString *resultText;

if (data) {
id jsonData = [[NSJSONSerialization JSONObjectWithData:data options:0 error:&error] objectForKey:@"data"];
if (jsonData && [jsonData count] != 0) {

NSString *epochStr = jsonData[0][@"created_utc"];
NSDate *creationDate = [NSDate dateWithTimeIntervalSince1970:[epochStr intValue]];

// months and years? not really needed
unsigned int unitFlags = NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;

NSDateComponents *components = [[NSCalendar currentCalendar] components:unitFlags fromDate:creationDate toDate:[NSDate date] options:0];
NSInteger days = [components day];
NSInteger hours = [components hour];
NSInteger minutes = [components minute];
NSInteger seconds = [components second];

NSMutableString *timeSinceString = [NSMutableString string];

BOOL prevComponentUsed = NO;

if (days > 0) {
[timeSinceString appendFormat:@"%lid, ", (long)days];
prevComponentUsed = YES;
}
if (hours > 0 || prevComponentUsed) {
[timeSinceString appendFormat:@"%lih, ", (long)hours];
prevComponentUsed = YES;
}
if (minutes > 0 || prevComponentUsed) {
[timeSinceString appendFormat:@"%lim, ", (long)minutes];
prevComponentUsed = YES;
}
if (seconds > 0 || prevComponentUsed) {
[timeSinceString appendFormat:@"%lis ", (long)seconds];
}

[timeSinceString appendString:@"ago"];
resultText = timeSinceString;

} else {
resultText = @"no data returned";
}
}

if (error) {
resultText = [NSString stringWithFormat:@"an error occurred. HTTP Status Code: %li, Error Description: %@",
(long)((NSHTTPURLResponse *)response).statusCode, [error localizedDescription]];
}

NSString *labelText = [NSString stringWithFormat:@"Last %@: %@", isComment ? @"Comment" : @"Post", resultText];

// specifier to create new table row
PSSpecifier *customSpecifier = [PSSpecifier preferenceSpecifierNamed:labelText target:self set:NULL get:NULL detail:Nil cell:PSStaticTextCell edit:Nil];
[customSpecifier setProperty:labelText forKey:@"label"];
[customSpecifier setProperty: @"PSStaticTextCell" forKey:@"cell"];

if (isComment) {
_latestCommentSpecifier = customSpecifier;
} else {
_latestPostSpecifier = customSpecifier;
}

NSDictionary *dataDict = @{@"custom_specifier" : customSpecifier, @"after_specifier" : arg2};
[self performSelectorOnMainThread:@selector(insertLatestTimeCell:) withObject:dataDict waitUntilDone:NO];
[self performSelectorOnMainThread:@selector(possiblyBothChecksComplete:) withObject:arg2 waitUntilDone:NO];
}];
[dataTask resume];
}

- (void)insertLatestTimeCell:(NSDictionary *)data {
if (data[@"custom_specifier"] && data[@"after_specifier"]) {
[self insertSpecifier:data[@"custom_specifier"] afterSpecifier:data[@"after_specifier"] animated:YES];
}
}

- (void)possiblyBothChecksComplete:(PSSpecifier *)arg1 {

// only when both the comment and the post request have finished
if (_latestPostSpecifier && _latestCommentSpecifier) {

NSIndexPath *indexPath = [self indexPathForSpecifier:arg1];
UITableViewCell *cell = [self.table cellForRowAtIndexPath:indexPath];

if (cell) {
UIActivityIndicatorView *activityView = (UIActivityIndicatorView *)[cell accessoryView];
[activityView stopAnimating];
[cell setAccessoryView:nil];

cell.userInteractionEnabled = YES;
}
}
}

@end

Carregando…
Cancelar
Salvar