Optimized IconHelper to only create internal structures once, not all the time.

This commit is contained in:
michael starke
2013-12-03 20:58:27 +01:00
parent 385a56c489
commit c59ce56f4d
2 changed files with 63 additions and 48 deletions

View File

@@ -27,13 +27,16 @@ typedef NS_ENUM(NSUInteger, MPIconType) {
MPIconCamera,
MPIconRemote,
MPIconKeys,
MPIconScanner = 15,
MPIconBattery,
MPIconScanner,
MPIconBrowser,
MPIconCDRom,
MPIconDisplay,
MPIconEmail,
MPIconMisc,
MPIconFileSave = 26,
MPIconTerminal = 30,
MPIconPrint = 31,
MPIconTrash = 43,
MPIconFolder = 48,
MPIconPhone = 68,

View File

@@ -14,7 +14,7 @@ static NSDictionary *icons;
+ (NSImage *)icon:(MPIconType)type {
if(!icons) {
icons = [MPIconHelper availableIconNames];
icons = [MPIconHelper availableIconNames];
}
if([[icons allKeys] containsObject:@(type)]) {
NSString *imageName = icons[@(type)];
@@ -24,57 +24,69 @@ static NSDictionary *icons;
}
+ (NSArray *)databaseIcons {
NSDictionary *imageNames = [MPIconHelper availableIconNames];
NSMutableArray *icons = [[NSMutableArray alloc] initWithCapacity:[imageNames count]];
for(NSNumber *iconNumber in [imageNames allKeys]) {
if([iconNumber integerValue] > MPCustomIconTypeBegin) {
continue; // Skip all non-db Keys
static NSArray *icons;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSDictionary *imageNames = [MPIconHelper availableIconNames];
NSMutableArray *mutableIcons = [[NSMutableArray alloc] initWithCapacity:[imageNames count]];
for(NSNumber *iconNumber in [imageNames allKeys]) {
if([iconNumber integerValue] > MPCustomIconTypeBegin) {
continue; // Skip all non-db Keys
}
MPIconType iconType = (MPIconType)[iconNumber integerValue];
[mutableIcons addObject:[MPIconHelper icon:iconType]];
}
MPIconType iconType = (MPIconType)[iconNumber integerValue];
[icons addObject:[MPIconHelper icon:iconType]];
}
icons = mutableIcons;
});
return icons;
}
+ (NSDictionary *)availableIconNames {
NSDictionary *imageNames = @{
@(MPIconPassword): @"00_PasswordTemplate",
@(MPIconPackageNetwork): @"01_PackageNetworkTemplate",
@(MPIconWarning): @"02_MessageBoxWarningTemplate",
@(MPIconServer): @"03_ServerTemplate",
@(MPIconKlipper): @"04_KlipperTemplate",
@(MPIconLanguages): @"05_LanguagesTemplate",
@(MPIconBlockDevice): @"06_BlockDeviceTemplate",
@(MPIconNotepad): @"07_NotepadTemplate",
@(MPIconSocket): @"08_SocketTemplate",
@(MPIconIdentity): @"09_IdentityTemplate",
@(MPIconContact): @"10_ContactTemplate",
@(MPIconCamera): @"11_CameraTemplate",
@(MPIconRemote): @"12_RemoteTemplate",
@(MPIconKeys): @"13_KeysTemplate",
@(MPIconScanner): @"15_ScannerTemplate",
@(MPIconBrowser): @"16_BrowserTemplate",
@(MPIconCDRom): @"17_CDRomTemplate",
@(MPIconDisplay): @"18_DisplayTemplate",
@(MPIconEmail): @"19_EmailTemplate",
@(MPIconMisc): @"20_MiscTemplate",
@(MPIconFileSave): @"26_FileSaveTemplate",
@(MPIconTrash): @"43_TrashTemplate",
@(MPIconFolder): @"48_FolderTemplate",
@(MPIconPhone): @"68_PhoneTemplate",
@(MPIconInfo): @"99_InfoTemplate",
@(MPIconAddFolder): @"99_AddFolderTemplate",
@(MPIconHardDisk): @"99_HarddiskTemplate",
@(MPIconCreated): @"99_CreatedTemplate",
@(MPIconAddEntry): @"addEntryTemplate",
@(MPIconContextTriangle): @"contextTriangleTemplate"
};
static NSDictionary *imageNames;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
imageNames = @{
@(MPIconPassword): @"00_PasswordTemplate",
@(MPIconPackageNetwork): @"01_PackageNetworkTemplate",
@(MPIconWarning): @"02_MessageBoxWarningTemplate",
@(MPIconServer): @"03_ServerTemplate",
@(MPIconKlipper): @"04_KlipperTemplate",
@(MPIconLanguages): @"05_LanguagesTemplate",
@(MPIconBlockDevice): @"06_BlockDeviceTemplate",
@(MPIconNotepad): @"07_NotepadTemplate",
@(MPIconSocket): @"08_SocketTemplate",
@(MPIconIdentity): @"09_IdentityTemplate",
@(MPIconContact): @"10_ContactTemplate",
@(MPIconCamera): @"11_CameraTemplate",
@(MPIconRemote): @"12_RemoteTemplate",
@(MPIconKeys): @"13_KeysTemplate",
@(MPIconBattery): @"14_BatteryTemplate",
@(MPIconScanner): @"15_ScannerTemplate",
@(MPIconBrowser): @"16_BrowserTemplate",
@(MPIconCDRom): @"17_CDRomTemplate",
@(MPIconDisplay): @"18_DisplayTemplate",
@(MPIconEmail): @"19_EmailTemplate",
@(MPIconMisc): @"20_MiscTemplate",
@(MPIconFileSave): @"26_FileSaveTemplate",
@(MPIconTerminal) : @"30_TerminalTemplate",
@(MPIconPrint) : @"31_PrintTemplate",
@(MPIconTrash): @"43_TrashTemplate",
@(MPIconFolder): @"48_FolderTemplate",
@(MPIconPhone): @"68_PhoneTemplate",
@(MPIconInfo): @"99_InfoTemplate",
@(MPIconAddFolder): @"99_AddFolderTemplate",
@(MPIconHardDisk): @"99_HarddiskTemplate",
@(MPIconCreated): @"99_CreatedTemplate",
@(MPIconAddEntry): @"addEntryTemplate",
@(MPIconContextTriangle): @"contextTriangleTemplate"
};
});
return imageNames;
}
@end