1
0
mirror of https://github.com/opa334/TrollStore.git synced 2024-12-05 07:13:06 +08:00
TrollStore/Store/TSAppTableViewController.m

163 lines
6.2 KiB
Mathematica
Raw Normal View History

2022-09-02 23:19:48 +08:00
#import "TSAppTableViewController.h"
#import "TSApplicationsManager.h"
2022-09-12 15:06:07 +08:00
@interface UIImage ()
+ (UIImage *)_applicationIconImageForBundleIdentifier:(NSString *)id format:(NSInteger)format scale:(double)scale;
@end
2022-09-02 23:19:48 +08:00
@implementation TSAppTableViewController
- (void)reloadTable
{
2022-09-03 09:08:59 +08:00
dispatch_async(dispatch_get_main_queue(), ^
{
[self.tableView reloadData];
});
2022-09-02 23:19:48 +08:00
}
- (void)loadView
{
[super loadView];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reloadTable)
name:@"ApplicationsChanged"
object:nil];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.allowsMultipleSelectionDuringEditing = NO;
}
2022-09-10 02:22:34 +08:00
- (void)showError:(NSError*)error
{
UIAlertController* errorAlert = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"Error %ld", error.code] message:error.localizedDescription preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* closeAction = [UIAlertAction actionWithTitle:@"Close" style:UIAlertActionStyleDefault handler:nil];
[errorAlert addAction:closeAction];
[self presentViewController:errorAlert animated:YES completion:nil];
}
- (void)uninstallPressedForRowAtIndexPath:(NSIndexPath*)indexPath
{
TSApplicationsManager* appsManager = [TSApplicationsManager sharedInstance];
NSString* appPath = [appsManager installedAppPaths][indexPath.row];
NSString* appId = [appsManager appIdForAppPath: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];
UIAlertAction* uninstallAction = [UIAlertAction actionWithTitle:@"Uninstall" style:UIAlertActionStyleDestructive handler:^(UIAlertAction* action)
{
if(appId)
{
[appsManager uninstallApp:appId];
}
else
{
[appsManager uninstallAppByPath:appPath];
}
}];
[confirmAlert addAction:uninstallAction];
UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
[confirmAlert addAction:cancelAction];
[self presentViewController:confirmAlert animated:YES completion:nil];
}
- (void)deselectRow
{
[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
}
2022-09-02 23:19:48 +08:00
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[TSApplicationsManager sharedInstance] installedAppPaths].count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
2022-09-12 15:06:07 +08:00
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ApplicationCell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"ApplicationCell"];
}
2022-09-02 23:19:48 +08:00
NSString* appPath = [[TSApplicationsManager sharedInstance] installedAppPaths][indexPath.row];
2022-09-12 15:06:07 +08:00
NSString* appId = [[TSApplicationsManager sharedInstance] appIdForAppPath:appPath];
NSString* appVersion = [[TSApplicationsManager sharedInstance] versionStringForAppPath:appPath];
2022-09-02 23:19:48 +08:00
// Configure the cell...
cell.textLabel.text = [[TSApplicationsManager sharedInstance] displayNameForAppPath:appPath];
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ • %@", appVersion, appId];
cell.imageView.image = [UIImage _applicationIconImageForBundleIdentifier:appId format:10 scale:[UIScreen mainScreen].scale];
cell.imageView.layer.borderWidth = 0.2;
cell.imageView.layer.borderColor = [UIColor blackColor].CGColor;
cell.imageView.layer.cornerRadius = 13.8;
cell.preservesSuperviewLayoutMargins = NO;
cell.separatorInset = UIEdgeInsetsZero;
cell.layoutMargins = UIEdgeInsetsZero;
2022-09-12 15:06:07 +08:00
2022-09-02 23:19:48 +08:00
return cell;
}
2022-09-12 15:06:07 +08:00
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 80.0f;
}
2022-09-02 23:19:48 +08:00
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
2022-09-10 02:22:34 +08:00
if(editingStyle == UITableViewCellEditingStyleDelete)
2022-09-02 23:19:48 +08:00
{
2022-09-10 02:22:34 +08:00
[self uninstallPressedForRowAtIndexPath:indexPath];
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
TSApplicationsManager* appsManager = [TSApplicationsManager sharedInstance];
NSString* appPath = [appsManager installedAppPaths][indexPath.row];
NSString* appId = [appsManager appIdForAppPath:appPath];
NSString* appName = [appsManager displayNameForAppPath:appPath];
UIAlertController* appSelectAlert = [UIAlertController alertControllerWithTitle:appName message:appId preferredStyle:UIAlertControllerStyleActionSheet];
/*UIAlertAction* detachAction = [UIAlertAction actionWithTitle:@"Detach from TrollStore" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action)
{
int detachRet = [appsManager detachFromApp:appId];
if(detachRet != 0)
2022-09-04 21:37:49 +08:00
{
2022-09-10 02:22:34 +08:00
[self showError:[appsManager errorForCode:detachRet]];
2022-09-04 21:37:49 +08:00
}
2022-09-10 02:22:34 +08:00
[self deselectRow];
}];
[appSelectAlert addAction:detachAction];*/
UIAlertAction* uninstallAction = [UIAlertAction actionWithTitle:@"Uninstall App" style:UIAlertActionStyleDestructive handler:^(UIAlertAction* action)
{
[self uninstallPressedForRowAtIndexPath:indexPath];
[self deselectRow];
}];
[appSelectAlert addAction:uninstallAction];
UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction* action)
{
[self deselectRow];
}];
[appSelectAlert addAction:cancelAction];
appSelectAlert.popoverPresentationController.sourceView = tableView;
appSelectAlert.popoverPresentationController.sourceRect = [tableView rectForRowAtIndexPath:indexPath];
[self presentViewController:appSelectAlert animated:YES completion:nil];
2022-09-02 23:19:48 +08:00
}
@end