From c2510652f0e896f087c08564187bc8d8bfdafd5e Mon Sep 17 00:00:00 2001 From: arichornlover <78001398+arichornlover@users.noreply.github.com> Date: Sun, 11 Feb 2024 10:24:39 -0600 Subject: [PATCH] =?UTF-8?q?Added=20=E2=80=9CClear=20Cache=E2=80=9D=20Butto?= =?UTF-8?q?n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sources/uYouPlusSettings.xm | 42 +++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/Sources/uYouPlusSettings.xm b/Sources/uYouPlusSettings.xm index 165b490..dac600e 100644 --- a/Sources/uYouPlusSettings.xm +++ b/Sources/uYouPlusSettings.xm @@ -81,6 +81,22 @@ extern NSBundle *uYouPlusBundle(); %end %hook YTSettingsSectionItemManager +- (NSString *)getCacheSize { + NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject; + NSArray *filesArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:cachePath error:nil]; + + unsigned long long int folderSize = 0; + for (NSString *fileName in filesArray) { + NSString *filePath = [cachePath stringByAppendingPathComponent:fileName]; + NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil]; + folderSize += [fileAttributes fileSize]; + } + + NSByteCountFormatter *formatter = [[NSByteCountFormatter alloc] init]; + formatter.countStyle = NSByteCountFormatterCountStyleFile; + + return [formatter stringFromByteCount:folderSize]; +} %new(v@:@) - (void)updateTweakSectionWithEntry:(id)entry { NSMutableArray *sectionItems = [NSMutableArray array]; @@ -131,6 +147,32 @@ extern NSBundle *uYouPlusBundle(); ]; [sectionItems addObject:exitYT]; +# pragma mark - Cache + SECTION_HEADER(LOC(@"Cache")); + YTSettingsSectionItem *clearCache = [%c(YTSettingsSectionItem) + itemWithTitle:LOC(@"Clear Cache") + titleDescription:[self getCacheSize] + accessibilityIdentifier:nil + selectBlock:^BOOL (YTSettingsCell *cell, NSUInteger arg1) { + UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleMedium]; + activityIndicator.color = [UIColor labelColor]; + [activityIndicator startAnimating]; + cell.accessoryView = activityIndicator; + + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ + NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject; + [[NSFileManager defaultManager] removeItemAtPath:cachePath error:nil]; + + dispatch_async(dispatch_get_main_queue(), ^{ + [settingsViewController reloadData]; + }); + }); + + return YES; + } + ]; + [sectionItems addObject:clearCache]; + # pragma mark - App theme SECTION_HEADER(LOC(@"THEME_OPTIONS")); // COLOR_BUTTON_ITEM2(@"Custom Theme Color", @"", ColourOptionsController2);