Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

Commit

Permalink
Align TimeInterval formatter with modern FormatStyle protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed Feb 12, 2024
1 parent a8061b5 commit f31af86
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 72 deletions.
16 changes: 8 additions & 8 deletions Aware.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
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 */; };
03F9E2311C24CCA8001DBE86 /* CustomTimeIntervalFormatStyle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03F9E2301C24CCA8001DBE86 /* CustomTimeIntervalFormatStyle.swift */; };
03F9E2331C24CD2E001DBE86 /* CustomTimeIntervalFormatStyleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03F9E2321C24CD2E001DBE86 /* CustomTimeIntervalFormatStyleTests.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand All @@ -31,8 +31,8 @@
036EBD1D1C1408C200121D0B /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/MainMenu.xib; sourceTree = "<group>"; };
038D0B381C4DDD5600040C44 /* Aware.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.xml; path = Aware.entitlements; sourceTree = "<group>"; };
03F9E2261C24CAD3001DBE86 /* AwareTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = AwareTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
03F9E2301C24CCA8001DBE86 /* NSTimeIntervalFormatter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSTimeIntervalFormatter.swift; sourceTree = "<group>"; };
03F9E2321C24CD2E001DBE86 /* NSTimeIntervalFormatterTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSTimeIntervalFormatterTests.swift; sourceTree = "<group>"; };
03F9E2301C24CCA8001DBE86 /* CustomTimeIntervalFormatStyle.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CustomTimeIntervalFormatStyle.swift; sourceTree = "<group>"; };
03F9E2321C24CD2E001DBE86 /* CustomTimeIntervalFormatStyleTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CustomTimeIntervalFormatStyleTests.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -75,7 +75,7 @@
isa = PBXGroup;
children = (
036EBD181C1408C200121D0B /* AppDelegate.swift */,
03F9E2301C24CCA8001DBE86 /* NSTimeIntervalFormatter.swift */,
03F9E2301C24CCA8001DBE86 /* CustomTimeIntervalFormatStyle.swift */,
036EBD1A1C1408C200121D0B /* Assets.xcassets */,
036EBD1C1C1408C200121D0B /* MainMenu.xib */,
038D0B381C4DDD5600040C44 /* Aware.entitlements */,
Expand All @@ -86,7 +86,7 @@
03F9E2271C24CAD3001DBE86 /* AwareTests */ = {
isa = PBXGroup;
children = (
03F9E2321C24CD2E001DBE86 /* NSTimeIntervalFormatterTests.swift */,
03F9E2321C24CD2E001DBE86 /* CustomTimeIntervalFormatStyleTests.swift */,
);
path = AwareTests;
sourceTree = "<group>";
Expand Down Expand Up @@ -202,7 +202,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
03F9E2311C24CCA8001DBE86 /* NSTimeIntervalFormatter.swift in Sources */,
03F9E2311C24CCA8001DBE86 /* CustomTimeIntervalFormatStyle.swift in Sources */,
036EBD191C1408C200121D0B /* AppDelegate.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -211,7 +211,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
03F9E2331C24CD2E001DBE86 /* NSTimeIntervalFormatterTests.swift in Sources */,
03F9E2331C24CD2E001DBE86 /* CustomTimeIntervalFormatStyleTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
2 changes: 1 addition & 1 deletion Aware/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {

if let statusButton = statusItem.button {
let duration = Date().timeIntervalSince(timerStart)
let title = NSTimeIntervalFormatter().stringFromTimeInterval(duration)
let title = CustomTimeIntervalFormatStyle().format(duration)

statusButton.title = title
statusButton.appearsDisabled = idle
Expand Down
28 changes: 28 additions & 0 deletions Aware/CustomTimeIntervalFormatStyle.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// CustomTimeIntervalFormatStyle.swift
// Aware
//
// Created by Joshua Peek on 02/12/24.
// Copyright © 2024 Joshua Peek. All rights reserved.
//

import Foundation

struct CustomTimeIntervalFormatStyle: FormatStyle {
/// Formats a time interval as a human readable duration string.
/// - Parameter value: The time interval to format.
/// - Returns: A string representation of the time interval.
func format(_ value: TimeInterval) -> String {
let minutes = Int(value) / 60
if minutes < 60 {
return "\(minutes)m"
} else {
return "\(minutes / 60)h \(minutes % 60)m"
}
}
}

@available(macOS 12.0, *)
extension FormatStyle where Self == CustomTimeIntervalFormatStyle {
static var custom: CustomTimeIntervalFormatStyle { .init() }
}
28 changes: 0 additions & 28 deletions Aware/NSTimeIntervalFormatter.swift

This file was deleted.

35 changes: 35 additions & 0 deletions AwareTests/CustomTimeIntervalFormatStyleTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import XCTest

@testable import Aware

class CustomTimeIntervalFormatStyleTests: XCTestCase {
func testFormat() {
let formatter = CustomTimeIntervalFormatStyle()

XCTAssertEqual(formatter.format(0), "0m")
XCTAssertEqual(formatter.format(1), "0m")
XCTAssertEqual(formatter.format(30), "0m")
XCTAssertEqual(formatter.format(59), "0m")

XCTAssertEqual(formatter.format(60), "1m")
XCTAssertEqual(formatter.format(61), "1m")
XCTAssertEqual(formatter.format(119), "1m")

XCTAssertEqual(formatter.format(120), "2m")
XCTAssertEqual(formatter.format(300), "5m")
XCTAssertEqual(formatter.format(900), "15m")
XCTAssertEqual(formatter.format(1800), "30m")
XCTAssertEqual(formatter.format(2700), "45m")
XCTAssertEqual(formatter.format(3540), "59m")
XCTAssertEqual(formatter.format(3599), "59m")

XCTAssertEqual(formatter.format(3600), "1h 0m")
XCTAssertEqual(formatter.format(3601), "1h 0m")
XCTAssertEqual(formatter.format(3660), "1h 1m")
XCTAssertEqual(formatter.format(4500), "1h 15m")
XCTAssertEqual(formatter.format(5400), "1h 30m")
XCTAssertEqual(formatter.format(6300), "1h 45m")

XCTAssertEqual(formatter.format(7200), "2h 0m")
}
}
35 changes: 0 additions & 35 deletions AwareTests/NSTimeIntervalFormatterTests.swift

This file was deleted.

0 comments on commit f31af86

Please sign in to comment.