Permalink
Browse files

Improved IPA loading

  • Loading branch information...
0xced committed Jan 4, 2012
1 parent ac2de6a commit 3d63f0408d8afcfe46cd125c5dd400ab32dc2492
@@ -8,6 +8,8 @@
#import <UIKit/UIKit.h>
+@class IPAArchive;
+
@interface ArtworkViewController : UITableViewController
{
IBOutlet UIProgressView *progressView;
@@ -28,8 +30,8 @@
@property (nonatomic, retain) NSIndexPath *firstCellIndexPath;
@property (nonatomic, assign) NSUInteger saveCounter;
-- (id) initWithIPAPath:(NSString *)ipaPath;
-@property (nonatomic, retain) NSString *ipaPath;
+- (id) initWithArchive:(IPAArchive *)archive;
+@property (nonatomic, retain) IPAArchive *archive;
- (IBAction) saveAll;
@@ -92,15 +92,15 @@ @implementation ArtworkViewController
@synthesize bundles = _bundles;
@synthesize firstCellIndexPath = _firstCellIndexPath;
@synthesize saveCounter = _saveCounter;
-@synthesize ipaPath = _ipaPath;
+@synthesize archive = _archive;
-- (id) initWithIPAPath:(NSString *)ipaPath
+- (id) initWithArchive:(IPAArchive *)archive
{
if (!(self = [super initWithNibName:@"ArtworkViewController" bundle:nil]))
return nil;
- self.ipaPath = ipaPath;
- self.title = [[ipaPath lastPathComponent] stringByDeletingPathExtension];
+ self.archive = archive;
+ self.title = archive.appName;
return self;
}
@@ -111,7 +111,8 @@ - (void) dealloc
self.saveAllButton = nil;
self.bundles = nil;
self.firstCellIndexPath = nil;
- self.ipaPath = nil;
+ [self.archive unload];
+ self.archive = nil;
[artwork release];
}
@@ -128,7 +129,7 @@ - (BOOL) isEmoji
- (BOOL) isIPA
{
- return self.ipaPath != nil;
+ return self.archive != nil;
}
- (NSDictionary *) artwork
@@ -341,15 +342,14 @@ - (void) loadImages
}
else if ([self isIPA])
{
- IPAArchive *archive = [[[IPAArchive alloc] initWithPath:self.ipaPath] autorelease];
- for (NSString *imageName in archive.imageNames)
+ for (NSString *imageName in self.archive.imageNames)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
BOOL scale1 = [UIScreen mainScreen].scale == 1 && [[imageName lowercaseString] rangeOfString:@"@2x"].location == NSNotFound;
BOOL scale2 = [UIScreen mainScreen].scale == 2 && [[imageName lowercaseString] rangeOfString:@"@2x"].location != NSNotFound;
if ([imageName hasSuffix:@"png"] && (scale1 || scale2))
{
- [self addImage:[archive imageNamed:imageName] filePath:imageName];
+ [self addImage:[self.archive imageNamed:imageName] filePath:imageName];
}
[pool drain];
}
View
@@ -18,4 +18,6 @@
- (UIImage *) imageNamed:(NSString *)imageName;
+- (void) unload;
+
@end
View
@@ -15,6 +15,7 @@ @interface IPAArchive ()
@property (nonatomic, readwrite, retain) UIImage *appIcon;
@property (nonatomic, readwrite, retain) NSArray *imageNames;
+@property (nonatomic, retain) NSString *path;
@property (nonatomic, retain) ZKDataArchive *ipa;
@property (nonatomic, retain) NSDictionary *metadata;
@property (nonatomic, retain) NSDictionary *infoPlist;
@@ -23,6 +24,7 @@ @interface IPAArchive ()
@implementation IPAArchive
+@synthesize path = _path;
@synthesize ipa = _ipa;
@synthesize metadata = _metadata;
@synthesize infoPlist = _infoPlist;
@@ -34,13 +36,14 @@ - (id) initWithPath:(NSString *)ipaPath;
if (!(self = [super init]))
return nil;
- self.ipa = [ZKDataArchive archiveWithArchivePath:ipaPath];
+ self.path = ipaPath;
return self;
}
- (void) dealloc
{
+ self.path = nil;
self.imageNames = nil;
self.appIcon = nil;
self.ipa = nil;
@@ -172,4 +175,20 @@ - (UIImage *) imageNamed:(NSString *)imageName
return image;
}
+- (ZKDataArchive *) ipa
+{
+ if (!_ipa)
+ _ipa = [[ZKDataArchive archiveWithArchivePath:self.path] retain];
+
+ return _ipa;
+}
+
+- (void) unload
+{
+ [self appIcon];
+ [self metadata];
+ [self infoPlist];
+ self.ipa = nil;
+}
+
@end
@@ -10,4 +10,8 @@
@interface IPAViewController : UITableViewController
+@property (nonatomic, retain) IBOutlet UIView *archiveLoadingView;
+@property (nonatomic, retain) IBOutlet UILabel *appNameLabel;
+@property (nonatomic, retain) IBOutlet UIProgressView *progressView;
+
@end
@@ -15,45 +15,94 @@
@interface IPAViewController ()
-@property (nonatomic, retain) NSMutableArray *paths;
-@property (nonatomic, retain) NSMutableArray *icons;
+@property (nonatomic, retain) NSMutableArray *archives;
@end
@implementation IPAViewController
-@synthesize paths = _paths;
-@synthesize icons = _icons;
+@synthesize archiveLoadingView = _archiveLoadingView;
+@synthesize appNameLabel = _appNameLabel;
+@synthesize progressView = _progressView;
-- (void) viewDidLoad
+@synthesize archives = _archives;
+
+- (void) loadArchives
{
- self.tableView.rowHeight = 57;
-
- self.paths = [NSMutableArray array];
- self.icons = [NSMutableArray array];
+ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSString *mobileApplicationsPath = [[appDelegate homeDirectory] stringByAppendingPathComponent:@"/Music/iTunes/Mobile Applications"];
- for (NSString *relativePath in [[NSFileManager defaultManager] enumeratorAtPath:mobileApplicationsPath])
+ CGFloat i = 0.0f;
+ NSFileManager *fileManager = [[[NSFileManager alloc] init] autorelease];
+ NSArray *mobileApplications = [fileManager contentsOfDirectoryAtPath:mobileApplicationsPath error:NULL];
+ for (NSString *relativePath in mobileApplications)
{
+ NSAutoreleasePool *archivePool = [[NSAutoreleasePool alloc] init];
NSString *ipaPath = [mobileApplicationsPath stringByAppendingPathComponent:relativePath];
- [self.paths addObject:ipaPath];
- [self.icons addObject:[NSNull null]];
+ NSDictionary *attributes = [fileManager attributesOfItemAtPath:ipaPath error:NULL];
+ if ([attributes fileSize] > 500 * 1024 * 1024)
+ {
+ NSLog(@"Skipped %@ (too big)", [ipaPath lastPathComponent]);
+ continue;
+ }
+ IPAArchive *archive = [[[IPAArchive alloc] initWithPath:ipaPath] autorelease];
+ if ([archive.imageNames count] == 0)
+ {
+ NSLog(@"Skipped %@ (no images)", [ipaPath lastPathComponent]);
+ continue;
+ }
+ [archive unload];
+ [self.archives addObject:archive];
+
+ NSDictionary *progressInfo = [NSDictionary dictionaryWithObjectsAndKeys:archive.appName, @"title", [NSNumber numberWithFloat:i / [mobileApplications count]], @"progress", nil];
+ [self performSelectorOnMainThread:@selector(loadingDidProgress:) withObject:progressInfo waitUntilDone:NO];
+ i = i + 1.0f;
+
+ [archivePool drain];
}
+
+ [self performSelectorOnMainThread:@selector(archivesDidLoad) withObject:nil waitUntilDone:NO];
+
+ [pool drain];
+}
+
+- (void) loadingDidProgress:(NSDictionary *)progressInfo
+{
+ self.appNameLabel.text = [progressInfo objectForKey:@"title"];
+ self.progressView.progress = [[progressInfo objectForKey:@"progress"] floatValue];
+}
+
+- (void) archivesDidLoad
+{
+ [self.archiveLoadingView removeFromSuperview];
+ [self.tableView reloadData];
+}
+
+- (void) viewDidLoad
+{
+ self.archiveLoadingView.frame = self.view.frame;
+ [self.view addSubview:self.archiveLoadingView];
+
+ self.title = self.tabBarItem.title;
+ self.tableView.rowHeight = 57;
+
+ self.archives = [NSMutableArray array];
+
+ [self performSelectorInBackground:@selector(loadArchives) withObject:nil];
}
- (void) viewDidUnload
{
- self.paths = nil;
- self.icons = nil;
+ self.archives = nil;
}
// MARK: - UITableView data source
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
- return [self.paths count];
+ return [self.archives count];
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
@@ -69,24 +118,16 @@ - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
- UIImage *icon = [self.icons objectAtIndex:indexPath.row];
- NSString *ipaPath = [self.paths objectAtIndex:indexPath.row];
- if (![icon isKindOfClass:[UIImage class]])
- {
- IPAArchive *archive = [[IPAArchive alloc] initWithPath:ipaPath];
- icon = archive.appIcon;
- [self.icons replaceObjectAtIndex:indexPath.row withObject:icon];
- [archive release];
- }
+ IPAArchive *archive = [self.archives objectAtIndex:indexPath.row];
- cell.textLabel.text = [[ipaPath lastPathComponent] stringByDeletingPathExtension];
- cell.imageView.image = icon;
+ cell.textLabel.text = archive.appName;
+ cell.imageView.image = archive.appIcon;
}
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
- NSString *ipaPath = [self.paths objectAtIndex:indexPath.row];
- ArtworkViewController *artworkViewController = [[[ArtworkViewController alloc] initWithIPAPath:ipaPath] autorelease];
+ IPAArchive *archive = [self.archives objectAtIndex:indexPath.row];
+ ArtworkViewController *artworkViewController = [[[ArtworkViewController alloc] initWithArchive:archive] autorelease];
[self.navigationController pushViewController:artworkViewController animated:YES];
}
Oops, something went wrong.

0 comments on commit 3d63f04

Please sign in to comment.