Performance fixes

This commit is contained in:
opa334 2022-09-12 19:23:09 +02:00
parent b766a8586f
commit 55437694e3
3 changed files with 142 additions and 106 deletions

View File

@ -1,5 +1,9 @@
#import <UIKit/UIKit.h> #import <UIKit/UIKit.h>
@interface TSAppTableViewController : UITableViewController @interface TSAppTableViewController : UITableViewController
{
UIImage* _placeholderIcon;
NSArray* _cachedAppPaths;
}
@end @end

View File

@ -8,182 +8,212 @@
@implementation TSAppTableViewController @implementation TSAppTableViewController
- (void)loadCachedAppPaths
{
_cachedAppPaths = [[TSApplicationsManager sharedInstance] installedAppPaths];
}
- (instancetype)init
{
self = [super init];
if(self)
{
[self loadCachedAppPaths];
_placeholderIcon = [UIImage _applicationIconImageForBundleIdentifier:@"com.apple.WebSheet" format:10 scale:[UIScreen mainScreen].scale];
}
return self;
}
- (void)reloadTable - (void)reloadTable
{ {
dispatch_async(dispatch_get_main_queue(), ^ [self loadCachedAppPaths];
{ dispatch_async(dispatch_get_main_queue(), ^
[self.tableView reloadData]; {
}); [self.tableView reloadData];
});
} }
- (void)loadView - (void)loadView
{ {
[super loadView]; [super loadView];
[[NSNotificationCenter defaultCenter] addObserver:self [[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reloadTable) selector:@selector(reloadTable)
name:@"ApplicationsChanged" name:@"ApplicationsChanged"
object:nil]; object:nil];
} }
- (void)viewDidLoad { - (void)viewDidLoad {
[super viewDidLoad]; [super viewDidLoad];
self.tableView.allowsMultipleSelectionDuringEditing = NO; self.tableView.allowsMultipleSelectionDuringEditing = NO;
} }
- (void)showError:(NSError*)error - (void)showError:(NSError*)error
{ {
UIAlertController* errorAlert = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"Error %ld", error.code] message:error.localizedDescription preferredStyle:UIAlertControllerStyleAlert]; UIAlertController* errorAlert = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"Error %ld", error.code] message:error.localizedDescription preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* closeAction = [UIAlertAction actionWithTitle:@"Close" style:UIAlertActionStyleDefault handler:nil]; UIAlertAction* closeAction = [UIAlertAction actionWithTitle:@"Close" style:UIAlertActionStyleDefault handler:nil];
[errorAlert addAction:closeAction]; [errorAlert addAction:closeAction];
[self presentViewController:errorAlert animated:YES completion:nil]; [self presentViewController:errorAlert animated:YES completion:nil];
} }
- (void)openAppPressedForRowAtIndexPath:(NSIndexPath *)indexPath - (void)openAppPressedForRowAtIndexPath:(NSIndexPath *)indexPath
{ {
TSApplicationsManager* appsManager = [TSApplicationsManager sharedInstance]; TSApplicationsManager* appsManager = [TSApplicationsManager sharedInstance];
NSString* appPath = [appsManager installedAppPaths][indexPath.row]; NSString* appPath = _cachedAppPaths[indexPath.row];
NSString* appId = [appsManager appIdForAppPath:appPath]; NSString* appId = [appsManager appIdForAppPath:appPath];
BOOL didOpen = [appsManager openApplicationWithBundleID:appId]; BOOL didOpen = [appsManager openApplicationWithBundleID:appId];
// if we failed to open the app, show an alert // if we failed to open the app, show an alert
if (!didOpen) { if (!didOpen) {
NSString *failMessage = [NSString stringWithFormat: @"Failed to open %@", appId]; NSString *failMessage = [NSString stringWithFormat: @"Failed to open %@", appId];
UIAlertController* didFailController = [UIAlertController alertControllerWithTitle:failMessage message: nil preferredStyle:UIAlertControllerStyleAlert]; UIAlertController* didFailController = [UIAlertController alertControllerWithTitle:failMessage message: nil preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]; UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
[didFailController addAction: cancelAction]; [didFailController addAction: cancelAction];
[self presentViewController:didFailController animated:YES completion:nil]; [self presentViewController:didFailController animated:YES completion:nil];
} }
} }
- (void)uninstallPressedForRowAtIndexPath:(NSIndexPath*)indexPath - (void)uninstallPressedForRowAtIndexPath:(NSIndexPath*)indexPath
{ {
TSApplicationsManager* appsManager = [TSApplicationsManager sharedInstance]; TSApplicationsManager* appsManager = [TSApplicationsManager sharedInstance];
NSString* appPath = [appsManager installedAppPaths][indexPath.row]; NSString* appPath = _cachedAppPaths[indexPath.row];
NSString* appId = [appsManager appIdForAppPath:appPath]; NSString* appId = [appsManager appIdForAppPath:appPath];
NSString* appName = [appsManager displayNameForAppPath:appPath]; NSString* appName = [appsManager displayNameForAppPath:appPath];
UIAlertController* confirmAlert = [UIAlertController alertControllerWithTitle:@"Confirm Uninstallation" message:[NSString stringWithFormat:@"Uninstalling the app '%@' will delete the app and all data associated to it.", appName] preferredStyle:UIAlertControllerStyleAlert]; UIAlertController* confirmAlert = [UIAlertController alertControllerWithTitle:@"Confirm Uninstallation" message:[NSString stringWithFormat:@"Uninstalling the app '%@' will delete the app and all data associated to it.", appName] preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* uninstallAction = [UIAlertAction actionWithTitle:@"Uninstall" style:UIAlertActionStyleDestructive handler:^(UIAlertAction* action) UIAlertAction* uninstallAction = [UIAlertAction actionWithTitle:@"Uninstall" style:UIAlertActionStyleDestructive handler:^(UIAlertAction* action)
{ {
if(appId) if(appId)
{ {
[appsManager uninstallApp:appId]; [appsManager uninstallApp:appId];
} }
else else
{ {
[appsManager uninstallAppByPath:appPath]; [appsManager uninstallAppByPath:appPath];
} }
}]; }];
[confirmAlert addAction:uninstallAction]; [confirmAlert addAction:uninstallAction];
UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]; UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
[confirmAlert addAction:cancelAction]; [confirmAlert addAction:cancelAction];
[self presentViewController:confirmAlert animated:YES completion:nil]; [self presentViewController:confirmAlert animated:YES completion:nil];
} }
- (void)deselectRow - (void)deselectRow
{ {
[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES]; [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
} }
#pragma mark - Table view data source #pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1; return 1;
} }
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[TSApplicationsManager sharedInstance] installedAppPaths].count; return _cachedAppPaths.count;
} }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ApplicationCell"]; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ApplicationCell"];
if (!cell) { if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"ApplicationCell"]; cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"ApplicationCell"];
} }
NSString* appPath = [[TSApplicationsManager sharedInstance] installedAppPaths][indexPath.row]; NSString* appPath = _cachedAppPaths[indexPath.row];
NSString* appId = [[TSApplicationsManager sharedInstance] appIdForAppPath:appPath]; NSString* appId = [[TSApplicationsManager sharedInstance] appIdForAppPath:appPath];
NSString* appVersion = [[TSApplicationsManager sharedInstance] versionStringForAppPath:appPath]; NSString* appVersion = [[TSApplicationsManager sharedInstance] versionStringForAppPath:appPath];
// Configure the cell... // Configure the cell...
cell.textLabel.text = [[TSApplicationsManager sharedInstance] displayNameForAppPath:appPath]; cell.textLabel.text = [[TSApplicationsManager sharedInstance] displayNameForAppPath:appPath];
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ • %@", appVersion, appId]; cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ • %@", appVersion, appId];
cell.imageView.image = [UIImage _applicationIconImageForBundleIdentifier:appId format:10 scale:[UIScreen mainScreen].scale]; cell.imageView.image = _placeholderIcon;
cell.imageView.layer.borderWidth = 0.2; cell.imageView.layer.borderWidth = 0.2;
cell.imageView.layer.borderColor = [UIColor blackColor].CGColor; cell.imageView.layer.borderColor = [UIColor blackColor].CGColor;
cell.imageView.layer.cornerRadius = 13.8; cell.imageView.layer.cornerRadius = 13.8;
cell.preservesSuperviewLayoutMargins = NO; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^
cell.separatorInset = UIEdgeInsetsZero; {
cell.layoutMargins = UIEdgeInsetsZero; //usleep(1000 * 5000); // (test delay for debugging)
UIImage* iconImage = [UIImage _applicationIconImageForBundleIdentifier:appId format:10 scale:[UIScreen mainScreen].scale];
dispatch_async(dispatch_get_main_queue(), ^{
if([tableView.indexPathsForVisibleRows containsObject:indexPath])
{
cell.imageView.image = iconImage;
}
});
});
return cell; cell.preservesSuperviewLayoutMargins = NO;
cell.separatorInset = UIEdgeInsetsZero;
cell.layoutMargins = UIEdgeInsetsZero;
return cell;
} }
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 80.0f; return 80.0f;
} }
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{ {
if(editingStyle == UITableViewCellEditingStyleDelete) if(editingStyle == UITableViewCellEditingStyleDelete)
{ {
[self uninstallPressedForRowAtIndexPath:indexPath]; [self uninstallPressedForRowAtIndexPath:indexPath];
} }
} }
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{ {
TSApplicationsManager* appsManager = [TSApplicationsManager sharedInstance]; TSApplicationsManager* appsManager = [TSApplicationsManager sharedInstance];
NSString* appPath = [appsManager installedAppPaths][indexPath.row]; NSString* appPath = _cachedAppPaths[indexPath.row];
NSString* appId = [appsManager appIdForAppPath:appPath]; NSString* appId = [appsManager appIdForAppPath:appPath];
NSString* appName = [appsManager displayNameForAppPath:appPath]; NSString* appName = [appsManager displayNameForAppPath:appPath];
UIAlertController* appSelectAlert = [UIAlertController alertControllerWithTitle:appName message:appId preferredStyle:UIAlertControllerStyleActionSheet]; UIAlertController* appSelectAlert = [UIAlertController alertControllerWithTitle:appName message:appId preferredStyle:UIAlertControllerStyleActionSheet];
/*UIAlertAction* detachAction = [UIAlertAction actionWithTitle:@"Detach from TrollStore" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action) /*UIAlertAction* detachAction = [UIAlertAction actionWithTitle:@"Detach from TrollStore" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action)
{ {
int detachRet = [appsManager detachFromApp:appId]; int detachRet = [appsManager detachFromApp:appId];
if(detachRet != 0) if(detachRet != 0)
{ {
[self showError:[appsManager errorForCode:detachRet]]; [self showError:[appsManager errorForCode:detachRet]];
} }
[self deselectRow]; [self deselectRow];
}]; }];
[appSelectAlert addAction:detachAction];*/ [appSelectAlert addAction:detachAction];*/
UIAlertAction* openAction = [UIAlertAction actionWithTitle: @"Open" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action) UIAlertAction* openAction = [UIAlertAction actionWithTitle: @"Open" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action)
{ {
[self openAppPressedForRowAtIndexPath:indexPath]; [self openAppPressedForRowAtIndexPath:indexPath];
}]; [self deselectRow];
[appSelectAlert addAction: openAction]; }];
[appSelectAlert addAction: openAction];
UIAlertAction* uninstallAction = [UIAlertAction actionWithTitle:@"Uninstall App" style:UIAlertActionStyleDestructive handler:^(UIAlertAction* action) UIAlertAction* uninstallAction = [UIAlertAction actionWithTitle:@"Uninstall App" style:UIAlertActionStyleDestructive handler:^(UIAlertAction* action)
{ {
[self uninstallPressedForRowAtIndexPath:indexPath]; [self uninstallPressedForRowAtIndexPath:indexPath];
[self deselectRow]; [self deselectRow];
}]; }];
[appSelectAlert addAction:uninstallAction]; [appSelectAlert addAction:uninstallAction];
UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction* action) UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction* action)
{ {
[self deselectRow]; [self deselectRow];
}]; }];
[appSelectAlert addAction:cancelAction]; [appSelectAlert addAction:cancelAction];
appSelectAlert.popoverPresentationController.sourceView = tableView; appSelectAlert.popoverPresentationController.sourceView = tableView;
appSelectAlert.popoverPresentationController.sourceRect = [tableView rectForRowAtIndexPath:indexPath]; appSelectAlert.popoverPresentationController.sourceRect = [tableView rectForRowAtIndexPath:indexPath];
[self presentViewController:appSelectAlert animated:YES completion:nil]; [self presentViewController:appSelectAlert animated:YES completion:nil];
} }
@end @end

View File

@ -42,8 +42,10 @@
dispatch_async(dispatch_get_main_queue(), ^ dispatch_async(dispatch_get_main_queue(), ^
{ {
NSLog(@"1");
[infoAlert dismissViewControllerAnimated:YES completion:^ [infoAlert dismissViewControllerAnimated:YES completion:^
{ {
NSLog(@"2");
if(ret != 0) if(ret != 0)
{ {
UIAlertController* errorAlert = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"Install Error %d", ret] message:[error localizedDescription] preferredStyle:UIAlertControllerStyleAlert]; UIAlertController* errorAlert = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"Install Error %d", ret] message:[error localizedDescription] preferredStyle:UIAlertControllerStyleAlert];