Permalink
Browse files

finished refactoring utility methods

  • Loading branch information...
1 parent 173a6a7 commit 13fd2c86ed77641a3166b1f27c7c5fba521c32b0 @calebd calebd committed Dec 16, 2010
@@ -15,7 +15,6 @@
3B20D890128A321E000CE777 /* HTNotifier.strings in Resources */ = {isa = PBXBuildFile; fileRef = 3B20D888128A321E000CE777 /* HTNotifier.strings */; };
3B20D891128A321E000CE777 /* HTNotice.m in Sources */ = {isa = PBXBuildFile; fileRef = 3B20D88B128A321E000CE777 /* HTNotice.m */; };
3B20D892128A321E000CE777 /* HTNotifier.m in Sources */ = {isa = PBXBuildFile; fileRef = 3B20D88D128A321E000CE777 /* HTNotifier.m */; };
- 3B20D893128A321E000CE777 /* HTUtilities.m in Sources */ = {isa = PBXBuildFile; fileRef = 3B20D88F128A321E000CE777 /* HTUtilities.m */; };
3B29FC3112B97F3C00B7A6F6 /* HTFunctions.m in Sources */ = {isa = PBXBuildFile; fileRef = 3B29FC3012B97F3C00B7A6F6 /* HTFunctions.m */; };
3B54C28D1257D6D600089639 /* DDXMLDocument.m in Sources */ = {isa = PBXBuildFile; fileRef = 3B54C27A1257D6D600089639 /* DDXMLDocument.m */; };
3B54C28E1257D6D600089639 /* DDXMLElement.m in Sources */ = {isa = PBXBuildFile; fileRef = 3B54C27C1257D6D600089639 /* DDXMLElement.m */; };
@@ -40,8 +39,6 @@
3B20D88B128A321E000CE777 /* HTNotice.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HTNotice.m; sourceTree = "<group>"; };
3B20D88C128A321E000CE777 /* HTNotifier.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HTNotifier.h; sourceTree = "<group>"; };
3B20D88D128A321E000CE777 /* HTNotifier.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HTNotifier.m; sourceTree = "<group>"; };
- 3B20D88E128A321E000CE777 /* HTUtilities.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HTUtilities.h; sourceTree = "<group>"; };
- 3B20D88F128A321E000CE777 /* HTUtilities.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HTUtilities.m; sourceTree = "<group>"; };
3B29FC1312B974DA00B7A6F6 /* HTNotifierDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HTNotifierDelegate.h; sourceTree = "<group>"; };
3B29FC2F12B97F3C00B7A6F6 /* HTFunctions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HTFunctions.h; sourceTree = "<group>"; };
3B29FC3012B97F3C00B7A6F6 /* HTFunctions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HTFunctions.m; sourceTree = "<group>"; };
@@ -156,8 +153,6 @@
3B29FC3012B97F3C00B7A6F6 /* HTFunctions.m */,
3B20D88A128A321E000CE777 /* HTNotice.h */,
3B20D88B128A321E000CE777 /* HTNotice.m */,
- 3B20D88E128A321E000CE777 /* HTUtilities.h */,
- 3B20D88F128A321E000CE777 /* HTUtilities.m */,
);
name = hoptoadnotifier;
path = ../hoptoadnotifier;
@@ -258,7 +253,6 @@
3B54C2AD1257D7A800089639 /* CAAppDelegate.m in Sources */,
3B20D891128A321E000CE777 /* HTNotice.m in Sources */,
3B20D892128A321E000CE777 /* HTNotifier.m in Sources */,
- 3B20D893128A321E000CE777 /* HTUtilities.m in Sources */,
3B29FC3112B97F3C00B7A6F6 /* HTFunctions.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
@@ -59,6 +59,50 @@ NSString * HTBundleDisplayName();
*/
NSString * HTPlatform();
+/*
+ get the path to a notice given a file name
+
+ the file name should not contain a path extension
+ */
+NSString * HTPathForNewNoticeWithName(NSString *);
+
+/*
+ returns a string with all of the hoptoad variables
+ replaced by their appropriate values
+ */
+NSString * HTStringByReplacingHoptoadVariablesInString(NSString *);
+
+#if TARGET_OS_IPHONE
+/*
+ return the class name of the on screen view controller.
+
+ this does not indicate the controller where the crash
+ occured, simply the one that has a view on screen
+
+ if the HTNotifier delegate implements
+ - rootViewControllerForNotice
+ the heirarchy of the returned controller will be inspected
+
+ if not, the rootViewController of the key window will be
+ inspected (if it exists)
+ */
+NSString * HTCurrentViewController();
+
+/*
+ return the name of the visible view controller given a
+ starting view controller.
+
+ this method makes assumptions about tab bar and navigation
+ controllers and will traverse the view heirarchy until an
+ unknown controller class is encountered. this is often the
+ onscreen controller
+
+ this method is recursive and is called by
+ + currentViewController
+ */
+NSString * HTVisibleViewControllerWithViewController(UIViewController *);
+#endif
+
// library logging methods
void HTLog(NSString *fmt, ...);
NSString * HTLogStringWithFormat(NSString *fmt, ...);
@@ -23,10 +23,10 @@ static void HTHandleSignal(int signal) {
static void HTHandleException(NSException *e) {
HTStopHandler();
- NSString *noticeName = [NSString stringWithFormat:@"%d", time(NULL)];
- NSString *noticePath = [HTUtilities noticePathWithName:noticeName];
+ NSString *name = [NSString stringWithFormat:@"%d", time(NULL)];
+ NSString *path = HTPathForNewNoticeWithName(name);
HTNotice *notice = [HTNotice noticeWithException:e];
- [notice writeToFile:noticePath];
+ [notice writeToFile:path];
id<HTNotifierDelegate> delegate = [[HTNotifier sharedNotifier] delegate];
if ([delegate respondsToSelector:@selector(notifierDidHandleException:)]) {
[delegate notifierDidHandleException:e];
@@ -175,6 +175,86 @@ void HTStopHandler() {
#endif
}
+NSString * HTPathForNewNoticeWithName(NSString *name) {
+ NSString *path = [HTNoticesDirectory() stringByAppendingPathComponent:name];
+ return [path stringByAppendingPathExtension:HTNotifierPathExtension];
+}
+
+NSString * HTStringByReplacingHoptoadVariablesInString(NSString *string) {
+ NSMutableString *mutable = [string mutableCopy];
+
+ [mutable replaceOccurrencesOfString:HTNotifierBundleName
+ withString:HTBundleDisplayName()
+ options:0
+ range:NSMakeRange(0, [mutable length])];
+
+ [mutable replaceOccurrencesOfString:HTNotifierBundleVersion
+ withString:HTApplicationVersion()
+ options:0
+ range:NSMakeRange(0, [mutable length])];
+
+ [mutable replaceOccurrencesOfString:HTNotifierBuildDate
+ withString:[NSString stringWithFormat:@"%s", __DATE__]
+ options:0
+ range:NSMakeRange(0, [mutable length])];
+
+ [mutable replaceOccurrencesOfString:HTNotifierBuildTime
+ withString:[NSString stringWithFormat:@"%s", __TIME__]
+ options:0
+ range:NSMakeRange(0, [mutable length])];
+
+ NSString *toReturn = [NSString stringWithString:mutable];
+ [mutable release];
+ return toReturn;
+}
+
+#if TARGET_OS_IPHONE
+NSString * HTCurrentViewController() {
+ // view controller to inspect
+ UIViewController *rootController = nil;
+
+ // try getting view controller from notifier delegate
+ id<HTNotifierDelegate> notifierDelegate = [[HTNotifier sharedNotifier] delegate];
+ if ([notifierDelegate respondsToSelector:@selector(rootViewControllerForNotice)]) {
+ rootController = [notifierDelegate rootViewControllerForNotice];
+ }
+
+ // try getting view controller from window
+ UIApplication *app = [UIApplication sharedApplication];
+ UIWindow *keyWindow = [app keyWindow];
+ if (rootController == nil && [keyWindow respondsToSelector:@selector(rootViewController)]) {
+ rootController = [keyWindow rootViewController];
+ }
+
+ // if we don't have a controller yet, give up
+ if (rootController == nil) {
+ return nil;
+ }
+
+ // call method to get class name
+ return HTVisibleViewControllerWithViewController(rootController);
+}
+
+NSString * HTVisibleViewControllerWithViewController(UIViewController *controller) {
+
+ // tab bar controller
+ if ([controller isKindOfClass:[UITabBarController class]]) {
+ UIViewController *visibleController = [(UITabBarController *)controller selectedViewController];
+ return HTVisibleViewControllerWithViewController(visibleController);
+ }
+ // navigation controller
+ else if ([controller isKindOfClass:[UINavigationController class]]) {
+ UIViewController *visibleController = [(UINavigationController *)controller visibleViewController];
+ return HTVisibleViewControllerWithViewController(visibleController);
+ }
+ // other type
+ else {
+ return NSStringFromClass([controller class]);
+ }
+
+}
+#endif
+
void HTLog(NSString *frmt, ...) {
va_list list;
va_start(list, frmt);
@@ -62,10 +62,10 @@ + (HTNotice *)notice {
notice.platform = HTPlatform();
notice.applicationVersion = HTApplicationVersion();
#if TARGET_OS_IPHONE
- notice.viewControllerName = [HTUtilities currentViewController];
+ notice.viewControllerName = HTCurrentViewController();
#endif
NSString *envName = [[HTNotifier sharedNotifier] environmentName];
- notice.environmentName = [HTUtilities stringByReplacingHoptoadVariablesInString:envName];
+ notice.environmentName = HTStringByReplacingHoptoadVariablesInString(envName);
notice.environmentInfo = [[HTNotifier sharedNotifier] environmentInfo];
return [notice autorelease];
}
@@ -13,7 +13,6 @@
#import "HTNotifierDelegate.h"
#import "HTFunctions.h"
#import "HTNotice.h"
-#import "HTUtilities.h"
// notifier version
extern NSString * const HTNotifierVersion;
@@ -54,7 +54,7 @@ - (id)initWithAPIKey:(NSString *)key environmentName:(NSString *)name {
// setup values
apiKey = [key copy];
- environmentName = [[HTUtilities stringByReplacingHoptoadVariablesInString:name] retain];
+ environmentName = [HTStringByReplacingHoptoadVariablesInString(name) retain];
self.useSSL = NO;
self.environmentInfo = [NSMutableDictionary dictionary];
@@ -124,8 +124,8 @@ - (void)showNoticeAlert {
}
}
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[HTUtilities stringByReplacingHoptoadVariablesInString:title]
- message:[HTUtilities stringByReplacingHoptoadVariablesInString:body]
+ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:HTStringByReplacingHoptoadVariablesInString(title)
+ message:HTStringByReplacingHoptoadVariablesInString(body)
delegate:self
cancelButtonTitle:HTLocalizedString(@"DONT_SEND")
otherButtonTitles:HTLocalizedString(@"ALWAYS_SEND"), HTLocalizedString(@"SEND"), nil];
@@ -272,7 +272,7 @@ - (void)dealloc {
[super dealloc];
}
- (void)writeTestNotice {
- NSString *noticePath = [HTUtilities noticePathWithName:@"TEST"];
+ NSString *noticePath = HTPathForNewNoticeWithName(@"TEST");
if ([[NSFileManager defaultManager] fileExistsAtPath:noticePath]) {
return;
@@ -1,67 +0,0 @@
-//
-// HTUtilities.h
-// HoptoadNotifier
-//
-// Created by Caleb Davenport on 10/13/10.
-// Copyright 2010 GUI Cocoa, LLC. All rights reserved.
-//
-
-#import <Foundation/Foundation.h>
-#if TARGET_OS_IPHONE
-#import <UIKit/UIKit.h>
-#endif
-
-/*
- The HTUtilities class contains a set of utility methods
- that perform various tasks that assist the notifier.
- */
-@interface HTUtilities : NSObject {
-
-}
-
-
-/*
- returns a path for a notice given a name with the correct
- extension
- */
-+ (NSString *)noticePathWithName:(NSString *)name;
-
-/*
- substitute common variables into string
- */
-+ (NSString *)stringByReplacingHoptoadVariablesInString:(NSString *)string;
-
-
-
-
-#if TARGET_OS_IPHONE
-/*
- return the class name of the on screen view controller.
-
- this does not indicate the controller where the crash
- occured, simply the one that has a view on screen
-
- if the HTNotifier delegate implements
- -rootViewControllerForNotice, the heirarchy of the returned
- controller will be inspected
-
- if not, the rootViewController of the key window will be
- inspected (if it exists)
- */
-+ (NSString *)currentViewController;
-
-/*
- return the name of the visible view controller given a
- starting view controller.
-
- this method makes assumptions about tab and nav controllers
- and will traverse the view heirarchy until an unknown
- controller is encountered - this is often the onscreen
- controller
-
- this method is called by +currentViewController
- */
-+ (NSString *)visibleViewControllerWithViewController:(UIViewController *)controller;
-#endif
-
-@end
@@ -1,90 +0,0 @@
-//
-// HTUtilities.m
-// HoptoadNotifier
-//
-// Created by Caleb Davenport on 10/13/10.
-// Copyright 2010 GUI Cocoa, LLC. All rights reserved.
-//
-
-#import "HTNotifier.h"
-
-
-
-@implementation HTUtilities
-
-+ (NSString *)noticePathWithName:(NSString *)name {
- NSString *path = [[self noticesDirectory] stringByAppendingPathComponent:name];
- return [path stringByAppendingPathExtension:HTNotifierPathExtension];
-}
-+ (NSString *)stringByReplacingHoptoadVariablesInString:(NSString *)string {
- NSMutableString *mutable = [string mutableCopy];
-
- [mutable replaceOccurrencesOfString:HTNotifierBundleName
- withString:[self bundleDisplayName]
- options:0
- range:NSMakeRange(0, [mutable length])];
-
- [mutable replaceOccurrencesOfString:HTNotifierBundleVersion
- withString:[self applicationVersion]
- options:0
- range:NSMakeRange(0, [mutable length])];
-
- [mutable replaceOccurrencesOfString:HTNotifierBuildDate
- withString:[NSString stringWithFormat:@"%s", __DATE__]
- options:0
- range:NSMakeRange(0, [mutable length])];
-
- [mutable replaceOccurrencesOfString:HTNotifierBuildTime
- withString:[NSString stringWithFormat:@"%s", __TIME__]
- options:0
- range:NSMakeRange(0, [mutable length])];
-
- NSString *toReturn = [NSString stringWithString:mutable];
- [mutable release];
- return toReturn;
-}
-#if TARGET_OS_IPHONE
-+ (NSString *)currentViewController {
- // view controller to inspect
- UIViewController *rootController = nil;
-
- // try getting view controller from notifier delegate
- id<HTNotifierDelegate> notifierDelegate = [[HTNotifier sharedNotifier] delegate];
- if ([notifierDelegate respondsToSelector:@selector(rootViewControllerForNotice)]) {
- rootController = [notifierDelegate rootViewControllerForNotice];
- }
-
- // try getting view controller from window
- UIApplication *app = [UIApplication sharedApplication];
- UIWindow *keyWindow = [app keyWindow];
- if (rootController == nil && [keyWindow respondsToSelector:@selector(rootViewController)]) {
- rootController = [keyWindow rootViewController];
- }
-
- // if we don't have a controller yet, give up
- if (rootController == nil) {
- return nil;
- }
-
- // call method to get class name
- return [self visibleViewControllerWithViewController:rootController];
-}
-+ (NSString *)visibleViewControllerWithViewController:(UIViewController *)controller {
- // tab bar controller
- if ([controller isKindOfClass:[UITabBarController class]]) {
- UIViewController *visibleController = [(UITabBarController *)controller selectedViewController];
- return [self visibleViewControllerWithViewController:visibleController];
- }
- // navigation controller
- else if ([controller isKindOfClass:[UINavigationController class]]) {
- UIViewController *visibleController = [(UINavigationController *)controller visibleViewController];
- return [self visibleViewControllerWithViewController:visibleController];
- }
- // other type
- else {
- return NSStringFromClass([controller class]);
- }
-}
-#endif
-
-@end

0 comments on commit 13fd2c8

Please sign in to comment.