Skip to content

Commit

Permalink
Add new categories, GCDTimer and PersistentStack.
Browse files Browse the repository at this point in the history
  • Loading branch information
pk committed Sep 15, 2014
1 parent a3ce4ff commit bb40292
Show file tree
Hide file tree
Showing 10 changed files with 654 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Source/Categories/NSObject+PKNotificationHandlingAdditions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// NSObject+PKNotificationHandlingAdditions.h
// PKToolBox
//
// Created by Pavel Kunc on 08/04/2014.
// Copyright (c) 2014 Pavel Kunc. All rights reserved.
//

@import Foundation;
#import "PKNotificationHandlingProtocol.h"

@interface NSObject (PKNotificationHandlingAdditions) <PKNotificationHandlingProtocol>
- (void)pk_registerNotificationObservers;
- (void)pk_removeNotificationObservers;
- (void)pk_handleNotification:(NSNotification *)notification;
@end
38 changes: 38 additions & 0 deletions Source/Categories/NSObject+PKNotificationHandlingAdditions.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// NSString+PKNotificationHandlingAdditions.m
// PKToolBox
//
// Created by Pavel Kunc on 08/04/2014.
// Copyright (c) 2014 Pavel Kunc. All rights reserved.
//

#import "NSObject+PKNotificationHandlingAdditions.h"

@implementation NSObject (PKNotificationHandlingAdditions)

- (void)pk_registerNotificationObservers {
SEL selector = @selector(pk_handleNotification:);
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
NSSet *notifications = [[self class] pk_observedNotifications];
for (NSString *notification in notifications) {
[nc addObserver:self selector:selector name:notification object:nil];
}
}

- (void)pk_removeNotificationObservers {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)pk_handleNotification:(NSNotification *)aNotification {
if ([NSThread isMainThread]) {
[self pk_handleNotificationOnMainThread:aNotification];
} else {
__block __typeof(self) __weak weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
__typeof(weakSelf) __strong blockSelf = weakSelf;
[blockSelf pk_handleNotificationOnMainThread:aNotification];
});
}
}

@end
17 changes: 17 additions & 0 deletions Source/Categories/UIButton+PKAdditions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// UIButton+PKAdditions.h
// PKToolBox
//
// Created by Pavel Kunc on 17/01/2014.
// Copyright (c) 2014 Pavel Kunc. All rights reserved.
//

@import Foundation;

@interface UIButton (PKAdditions)

+ (instancetype)pk_buttonWithImage:(UIImage *)image
target:(id)target
action:(SEL)action;

@end
25 changes: 25 additions & 0 deletions Source/Categories/UIButton+PKAdditions.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// UIButton+PKAdditions.h
// PKToolBox
//
// Created by Pavel Kunc on 17/01/2014.
// Copyright (c) 2014 Pavel Kunc. All rights reserved.
//

#import "UIButton+PKAdditions.h"

@implementation UIButton (PKAdditions)

+ (instancetype)pk_buttonWithImage:(UIImage *)aImage
target:(id)aTarget
action:(SEL)aAction {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:aImage forState:UIControlStateNormal];
[button addTarget:aTarget
action:aAction
forControlEvents:UIControlEventTouchUpInside];
[button sizeToFit];
return button;
}

@end
20 changes: 20 additions & 0 deletions Source/Categories/UIDevice+PKAdditions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// UIDevice+PKAdditions.h
// PKToolBox
//
// Created by Pavel Kunc on 19/02/2014.
// Copyright (c) 2014 Pavel Kunc. All rights reserved.
//

@import UIKit;
@import AdSupport;

@interface UIDevice (PKAdditions)

- (NSString *)pk_uniqueDeviceIdentifierSaltedWith:(NSString *)salt;
- (NSString *)pk_uniqueGlobalDeviceIdentifierSaltedWith:(NSString *)salt;

- (unsigned long long)pk_availableDiskSpace;
- (NSString *)pk_ipAddress;

@end
104 changes: 104 additions & 0 deletions Source/Categories/UIDevice+PKAdditions.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
//
// UIDevice+PKAdditions.m
// PKToolBox
//
// Created by Pavel Kunc on 19/02/2014.
// Copyright (c) 2014 Pavel Kunc. All rights reserved.
//

#import "UIDevice+PKAdditions.h"
#import "NSString+PKAdditions.h"

#import <ifaddrs.h>
#import <arpa/inet.h>


@implementation UIDevice (PKAdditions)


#pragma mark - Device identifier generation

- (NSString *)pk_uniqueDeviceIdentifierSaltedWith:(NSString *)aSalt {
NSString *identifier =
[[ASIdentifierManager sharedManager].advertisingIdentifier UUIDString];

identifier =
[identifier stringByAppendingString:[[NSBundle mainBundle] bundleIdentifier]];

if (aSalt != nil && [aSalt length] > 0) {
identifier = [identifier stringByAppendingString:aSalt];
}

return [identifier pk_SHA1_Base64];
}

- (NSString *)pk_uniqueGlobalDeviceIdentifierSaltedWith:(NSString *)aSalt {
NSString *identifier =
[[ASIdentifierManager sharedManager].advertisingIdentifier UUIDString];

if (aSalt != nil && [aSalt length] > 0) {
identifier = [identifier stringByAppendingString:aSalt];
}

return [identifier pk_SHA1_Base64];
}

- (NSString *)pk_ipAddress {
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
NSString *wifiAddress = nil;
NSString *cellAddress = nil;

// retrieve the current interfaces - returns 0 on success
if(!getifaddrs(&interfaces)) {
// Loop through linked list of interfaces
temp_addr = interfaces;
while(temp_addr != NULL) {
sa_family_t sa_type = temp_addr->ifa_addr->sa_family;
if(sa_type == AF_INET || sa_type == AF_INET6) {
NSString *name = [NSString stringWithUTF8String:temp_addr->ifa_name];
NSString *addr = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)]; // pdp_ip0

if([name isEqualToString:@"en0"]) {
wifiAddress = addr;
} else
if([name isEqualToString:@"pdp_ip0"]) {
cellAddress = addr;
}
}
temp_addr = temp_addr->ifa_next;
}

// Free memory
freeifaddrs(interfaces);
}

NSString *addr = wifiAddress ? wifiAddress : cellAddress;
return addr ? addr : @"0.0.0.0";
}

- (unsigned long long)pk_availableDiskSpace {
unsigned long long totalSpace = 0;
unsigned long long totalFreeSpace = 0;

NSError * __autoreleasing error = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSDictionary *dictionary =
[[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject]
error:&error];
if (dictionary) {
totalSpace = [dictionary[NSFileSystemSize] unsignedLongLongValue];
totalFreeSpace = [dictionary[NSFileSystemFreeSize] unsignedLongLongValue];
NSLog(@"Memory Capacity of %llu MiB with %llu MiB Free memory available.",
((totalSpace/1024ll)/1024ll),
((totalFreeSpace/1024ll)/1024ll));
} else {
NSLog(@"Error Obtaining System Memory Info: Domain = %@, Code = %d",
[error domain],
[error code]);
}

return totalFreeSpace;
}

@end
47 changes: 47 additions & 0 deletions Source/Foundation/PKGCDTimer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// PKGCDTimer.h
// PKToolBox
//
// Created by Pavel Kunc on 14/01/2014.
// Copyright (c) 2014 Fry-it, Limited. All rights reserved.
//

@import Foundation;

@class PKGCDTimer;

typedef void (^PKGCDTimerTaskBlock)();
typedef void (^PKGCDTimerTickBlock)(PKGCDTimer *timer, NSTimeInterval delay, NSTimeInterval remaining);
typedef void (^PKGCDTimerTimeoutBlock)();

@interface PKGCDTimer : NSObject

@property (nonatomic, copy, readwrite) PKGCDTimerTimeoutBlock timeoutHandler;
@property (nonatomic, copy, readwrite) PKGCDTimerTickBlock tickHandler;

- (instancetype)initWithTickHandler:(PKGCDTimerTickBlock)tickHanlder;

- (instancetype)initWithTimeoutHandler:(PKGCDTimerTimeoutBlock)timeoutHandler;

- (instancetype)initWithTickHandler:(PKGCDTimerTickBlock)tickHanlder
timeoutHandler:(PKGCDTimerTimeoutBlock)timeoutHandler;

- (void)performBlock:(PKGCDTimerTaskBlock)task
withDelay:(NSTimeInterval)delay;

- (void)performBlock:(PKGCDTimerTaskBlock)task
every:(NSTimeInterval)interval;

- (void)performBlock:(PKGCDTimerTaskBlock)task
every:(NSTimeInterval)interval
timeout:(NSTimeInterval)timeout;

- (void)performBlock:(PKGCDTimerTaskBlock)task
every:(NSTimeInterval)interval
timeout:(NSTimeInterval)timeout
retries:(NSUInteger)retries;

- (void)markTaskFinished;
- (void)invalidate;

@end
Loading

0 comments on commit bb40292

Please sign in to comment.