From ffeb412a8426bc35399f43a75476b895b6e39e15 Mon Sep 17 00:00:00 2001 From: dankito Date: Sat, 17 Oct 2020 01:00:14 +0200 Subject: [PATCH] Implemented Stopwatch --- .../BankingiOSApp.xcodeproj/project.pbxproj | 11 ++++ .../BankingiOSApp/util/Stopwatch.swift | 65 +++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 ui/BankingiOSApp/BankingiOSApp/util/Stopwatch.swift diff --git a/ui/BankingiOSApp/BankingiOSApp.xcodeproj/project.pbxproj b/ui/BankingiOSApp/BankingiOSApp.xcodeproj/project.pbxproj index 3f4cc39a..71c3cf11 100644 --- a/ui/BankingiOSApp/BankingiOSApp.xcodeproj/project.pbxproj +++ b/ui/BankingiOSApp/BankingiOSApp.xcodeproj/project.pbxproj @@ -213,6 +213,7 @@ 3684EB8E250B7F3C0001139E /* BankingUiCommon.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = BankingUiCommon.framework; path = "../BankingUiCommon/build/xcode-frameworks/BankingUiCommon.framework"; sourceTree = ""; }; 3684EB91250FD4AF0001139E /* LabelledValue.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LabelledValue.swift; sourceTree = ""; }; 3684EB93250FD75B0001139E /* CustomUITextField.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomUITextField.swift; sourceTree = ""; }; + 36B70FA92536695800734588 /* Stopwatch.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Stopwatch.swift; sourceTree = ""; }; 36B8A4472503D12100C15359 /* ProtectAppSettingsDialog.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProtectAppSettingsDialog.swift; sourceTree = ""; }; 36B8A44A2503D1E800C15359 /* BiometricAuthenticationService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BiometricAuthenticationService.swift; sourceTree = ""; }; 36B8A44C2503D96D00C15359 /* AuthenticationService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthenticationService.swift; sourceTree = ""; }; @@ -412,6 +413,14 @@ path = extensions; sourceTree = ""; }; + 36B70FA82536693E00734588 /* util */ = { + isa = PBXGroup; + children = ( + 36B70FA92536695800734588 /* Stopwatch.swift */, + ); + path = util; + sourceTree = ""; + }; 36B8A4492503D15300C15359 /* Security */ = { isa = PBXGroup; children = ( @@ -498,6 +507,7 @@ 36BCF87924BFA679005BEC29 /* persistence */, 36B8A4492503D15300C15359 /* Security */, 36BE06B624D077B400CBBB68 /* BankIconFinder */, + 36B70FA82536693E00734588 /* util */, 36078B792506A50F0025C776 /* extensions */, 36FC92D924B3A479002B12E9 /* ui */, 36FC929B24B39A05002B12E9 /* AppDelegate.swift */, @@ -982,6 +992,7 @@ 36E21ED124DC540400649DC8 /* SettingsDialog.swift in Sources */, 3684EB8B2508F6F00001139E /* SearchBarWithLabel.swift in Sources */, 366FA4DC24C479120094F009 /* BankInfoListItem.swift in Sources */, + 36B70FAA2536695800734588 /* Stopwatch.swift in Sources */, 3675052C251FFE98006B13A0 /* AccountTransactionDetailsDialog.swift in Sources */, 36B8A44B2503D1E800C15359 /* BiometricAuthenticationService.swift in Sources */, 36FC929E24B39A05002B12E9 /* SceneDelegate.swift in Sources */, diff --git a/ui/BankingiOSApp/BankingiOSApp/util/Stopwatch.swift b/ui/BankingiOSApp/BankingiOSApp/util/Stopwatch.swift new file mode 100644 index 00000000..39e8b071 --- /dev/null +++ b/ui/BankingiOSApp/BankingiOSApp/util/Stopwatch.swift @@ -0,0 +1,65 @@ +import SwiftUI + + +class Stopwatch { + + static func logDuration(_ action: String, block: () -> T) -> T { + let stopwatch = Stopwatch() + + let result = block() + + stopwatch.stopAndPrint(action) + + return result + } + + + private var startTime: DispatchTime? = nil + + var elapsed: UInt64? = nil + + + init(_ createStarted: Bool = true) { + if createStarted { + start() + } + } + + + func start() { + startTime = now() + } + + @discardableResult + func stop() -> UInt64 { + let endTime = now() + + let elapsed = endTime.uptimeNanoseconds - (startTime ?? endTime).uptimeNanoseconds + + self.elapsed = elapsed + + return elapsed + } + + func stopAndPrint(_ action: String) { + stop() + + self.print(action) + } + + func print(_ action: String) { + if let elapsed = elapsed { + let totalMillis = elapsed / 1_000_000 + let millis = totalMillis % 1000 + let seconds = totalMillis / 1000 + + Swift.print(String(format: "\(action) took %02d:%03d", seconds, millis)) + } + } + + + private func now() -> DispatchTime { + return .now() + } + +}