From 0a5a220db037b6e28cb28e72768437f7bdf27a11 Mon Sep 17 00:00:00 2001
From: lint <47455468+lint@users.noreply.github.com>
Date: Thu, 5 Nov 2020 20:31:36 -0500
Subject: [PATCH] Add button to check ingest in prefs
---
prefs/Resources/Root.plist | 14 +++
prefs/TFDTSRootListController.h | 10 ++-
prefs/TFDTSRootListController.m | 155 +++++++++++++++++++++++++++++++-
3 files changed, 177 insertions(+), 2 deletions(-)
diff --git a/prefs/Resources/Root.plist b/prefs/Resources/Root.plist
index c8d87e2..07de813 100644
--- a/prefs/Resources/Root.plist
+++ b/prefs/Resources/Root.plist
@@ -248,6 +248,20 @@
showValue
+
+ cell
+ PSGroupCell
+ footerText
+ Displays how far behind the ingest is (i.e. how long ago the most recently archived comment and post were created).
+
+
+ cell
+ PSButtonCell
+ label
+ Check Latest Ingest Times
+ action
+ checkLatest:
+
title
TFDidThatSay?
diff --git a/prefs/TFDTSRootListController.h b/prefs/TFDTSRootListController.h
index e96e2b1..41ff97a 100644
--- a/prefs/TFDTSRootListController.h
+++ b/prefs/TFDTSRootListController.h
@@ -1,5 +1,13 @@
#import
+#import
+
+#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
diff --git a/prefs/TFDTSRootListController.m b/prefs/TFDTSRootListController.m
index 9bc92b2..bad3511 100644
--- a/prefs/TFDTSRootListController.m
+++ b/prefs/TFDTSRootListController.m
@@ -1,8 +1,18 @@
#include "TFDTSRootListController.h"
-#import
@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