diff --git a/Aware.xcodeproj/project.pbxproj b/Aware.xcodeproj/project.pbxproj index 988be03..aeb7d3c 100644 --- a/Aware.xcodeproj/project.pbxproj +++ b/Aware.xcodeproj/project.pbxproj @@ -11,6 +11,9 @@ 036EBD191C1408C200121D0B /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 036EBD181C1408C200121D0B /* AppDelegate.swift */; }; 036EBD1B1C1408C200121D0B /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 036EBD1A1C1408C200121D0B /* Assets.xcassets */; }; 036EBD1E1C1408C200121D0B /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 036EBD1C1C1408C200121D0B /* MainMenu.xib */; }; + 03F9E2311C24CCA8001DBE86 /* NSTimeIntervalFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03F9E2301C24CCA8001DBE86 /* NSTimeIntervalFormatter.swift */; }; + 03F9E2331C24CD2E001DBE86 /* NSTimeIntervalFormatterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03F9E2321C24CD2E001DBE86 /* NSTimeIntervalFormatterTests.swift */; }; + 03F9E2341C24CDAB001DBE86 /* NSTimeIntervalFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03F9E2301C24CCA8001DBE86 /* NSTimeIntervalFormatter.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -32,6 +35,8 @@ 036EBD1F1C1408C200121D0B /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 03F9E2261C24CAD3001DBE86 /* AwareTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = AwareTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 03F9E22A1C24CAD3001DBE86 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + 03F9E2301C24CCA8001DBE86 /* NSTimeIntervalFormatter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSTimeIntervalFormatter.swift; sourceTree = ""; }; + 03F9E2321C24CD2E001DBE86 /* NSTimeIntervalFormatterTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSTimeIntervalFormatterTests.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -74,6 +79,7 @@ isa = PBXGroup; children = ( 036EBD181C1408C200121D0B /* AppDelegate.swift */, + 03F9E2301C24CCA8001DBE86 /* NSTimeIntervalFormatter.swift */, 0337E7861C14E37B003A8150 /* NSTimer+Block.swift */, 036EBD1A1C1408C200121D0B /* Assets.xcassets */, 036EBD1C1C1408C200121D0B /* MainMenu.xib */, @@ -85,6 +91,7 @@ 03F9E2271C24CAD3001DBE86 /* AwareTests */ = { isa = PBXGroup; children = ( + 03F9E2321C24CD2E001DBE86 /* NSTimeIntervalFormatterTests.swift */, 03F9E22A1C24CAD3001DBE86 /* Info.plist */, ); path = AwareTests; @@ -190,6 +197,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 03F9E2311C24CCA8001DBE86 /* NSTimeIntervalFormatter.swift in Sources */, 036EBD191C1408C200121D0B /* AppDelegate.swift in Sources */, 0337E7871C14E37B003A8150 /* NSTimer+Block.swift in Sources */, ); @@ -199,6 +207,8 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 03F9E2331C24CD2E001DBE86 /* NSTimeIntervalFormatterTests.swift in Sources */, + 03F9E2341C24CDAB001DBE86 /* NSTimeIntervalFormatter.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Aware/AppDelegate.swift b/Aware/AppDelegate.swift index 08ed3d1..4a27a2f 100644 --- a/Aware/AppDelegate.swift +++ b/Aware/AppDelegate.swift @@ -49,15 +49,6 @@ class AppDelegate: NSObject, NSApplicationDelegate { } let duration = NSDate().timeIntervalSinceDate(timerStart) - statusItem.button!.title = formatDuration(duration) - } - - func formatDuration(duration: NSTimeInterval) -> String { - let minutes = NSInteger(duration) / 60 - if minutes < 60 { - return "\(minutes)m" - } else { - return "\(minutes / 60)h \(minutes % 60)m" - } + statusItem.button!.title = NSTimeIntervalFormatter().stringFromTimeInterval(duration) } } diff --git a/Aware/NSTimeIntervalFormatter.swift b/Aware/NSTimeIntervalFormatter.swift new file mode 100644 index 0000000..6c2b17b --- /dev/null +++ b/Aware/NSTimeIntervalFormatter.swift @@ -0,0 +1,20 @@ +import Foundation + +class NSTimeIntervalFormatter { + /** + Formats time interval as a human readable duration string. + + - Parameters: + - interval: The time interval in seconds. + + - Returns: A `String`. + */ + func stringFromTimeInterval(interval: NSTimeInterval) -> String { + let minutes = NSInteger(interval) / 60 + if minutes < 60 { + return "\(minutes)m" + } else { + return "\(minutes / 60)h \(minutes % 60)m" + } + } +} diff --git a/AwareTests/NSTimeIntervalFormatterTests.swift b/AwareTests/NSTimeIntervalFormatterTests.swift new file mode 100644 index 0000000..975e971 --- /dev/null +++ b/AwareTests/NSTimeIntervalFormatterTests.swift @@ -0,0 +1,33 @@ +import XCTest + +class NSTimeIntervalFormatterTests: XCTestCase { + func testStringFromTimeInterval() { + let formatter = NSTimeIntervalFormatter() + + XCTAssertEqual(formatter.stringFromTimeInterval(0), "0m") + XCTAssertEqual(formatter.stringFromTimeInterval(1), "0m") + XCTAssertEqual(formatter.stringFromTimeInterval(30), "0m") + XCTAssertEqual(formatter.stringFromTimeInterval(59), "0m") + + XCTAssertEqual(formatter.stringFromTimeInterval(60), "1m") + XCTAssertEqual(formatter.stringFromTimeInterval(61), "1m") + XCTAssertEqual(formatter.stringFromTimeInterval(119), "1m") + + XCTAssertEqual(formatter.stringFromTimeInterval(120), "2m") + XCTAssertEqual(formatter.stringFromTimeInterval(300), "5m") + XCTAssertEqual(formatter.stringFromTimeInterval(900), "15m") + XCTAssertEqual(formatter.stringFromTimeInterval(1800), "30m") + XCTAssertEqual(formatter.stringFromTimeInterval(2700), "45m") + XCTAssertEqual(formatter.stringFromTimeInterval(3540), "59m") + XCTAssertEqual(formatter.stringFromTimeInterval(3599), "59m") + + XCTAssertEqual(formatter.stringFromTimeInterval(3600), "1h 0m") + XCTAssertEqual(formatter.stringFromTimeInterval(3601), "1h 0m") + XCTAssertEqual(formatter.stringFromTimeInterval(3660), "1h 1m") + XCTAssertEqual(formatter.stringFromTimeInterval(4500), "1h 15m") + XCTAssertEqual(formatter.stringFromTimeInterval(5400), "1h 30m") + XCTAssertEqual(formatter.stringFromTimeInterval(6300), "1h 45m") + + XCTAssertEqual(formatter.stringFromTimeInterval(7200), "2h 0m") + } +}