From c45b64ae6b2845894d2975c5e770f6e385bb5e71 Mon Sep 17 00:00:00 2001 From: krugerk <4656811+krugerk@users.noreply.github.com> Date: Wed, 6 Nov 2024 22:07:54 +0100 Subject: [PATCH] Timer screen figures out its own units (#497) The Timer Screen has special logic for interpreting the accumulated seconds, dividing the value by 60 when the goal's unit is (perhaps, probably) in minutes Previously the goal screen included the logic for figuring out which units the timer screen should use. --- BeeSwift/GoalViewController.swift | 12 ---------- BeeSwift/TimerViewController.swift | 36 ++++++++++++++++++++++++++---- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/BeeSwift/GoalViewController.swift b/BeeSwift/GoalViewController.swift index 598b0072..2da1f19d 100644 --- a/BeeSwift/GoalViewController.swift +++ b/BeeSwift/GoalViewController.swift @@ -332,18 +332,6 @@ class GoalViewController: UIViewController, UIScrollViewDelegate, DatapointTabl @objc func timerButtonPressed() { let controller = TimerViewController(goal: self.goal) controller.modalPresentationStyle = .fullScreen - do { - let hoursRegex = try NSRegularExpression(pattern: "(hr|hour)s?") - let minutesRegex = try NSRegularExpression(pattern: "(min|minute)s?") - if hoursRegex.firstMatch(in: self.goal.yAxis, options: [], range: NSMakeRange(0, self.goal.yAxis.count)) != nil { - controller.units = "hours" - } - if minutesRegex.firstMatch(in: self.goal.yAxis, options: [], range: NSMakeRange(0, self.goal.yAxis.count)) != nil { - controller.units = "minutes" - } - } catch { - // - } self.present(controller, animated: true, completion: nil) } diff --git a/BeeSwift/TimerViewController.swift b/BeeSwift/TimerViewController.swift index 90375833..aa824b55 100644 --- a/BeeSwift/TimerViewController.swift +++ b/BeeSwift/TimerViewController.swift @@ -13,16 +13,22 @@ import MBProgressHUD import BeeKit class TimerViewController: UIViewController { + private enum TimerUnit { + case hours, minutes + } + let timerLabel = BSLabel() let startStopButton = BSButton(type: .system) let goal: Goal var timingSince: Date? var timer: Timer? - var units: String? - var accumulatedSeconds = 0 + private let units: TimerUnit + var accumulatedSeconds = 0 + init(goal: Goal) { self.goal = goal + self.units = Self.timerUnit(goal: goal) ?? .hours super.init(nibName: nil, bundle: nil) } @@ -165,8 +171,14 @@ class TimerViewController: UIViewController { formatter.locale = Locale(identifier: "en_US") formatter.dateFormat = "d" - var value = self.totalSeconds()/3600.0 - if self.units == "minutes" { value = self.totalSeconds()/60.0 } + let value: Double + + switch self.units { + case .minutes: + value = self.totalSeconds()/60.0 + case .hours: + value = self.totalSeconds()/3600.0 + } let comment = "Automatically entered from iOS timer interface" @@ -199,3 +211,19 @@ class TimerViewController: UIViewController { } } } + +private extension TimerViewController { + static private func timerUnit(goal: Goal) -> TimerUnit? { + guard let hoursRegex = try? NSRegularExpression(pattern: "(hr|hour)s?") else { return nil } + if hoursRegex.firstMatch(in: goal.yAxis, options: [], range: NSMakeRange(0, goal.yAxis.count)) != nil { + return .hours + } + + guard let minutesRegex = try? NSRegularExpression(pattern: "(min|minute)s?") else { return nil } + if minutesRegex.firstMatch(in: goal.yAxis, options: [], range: NSMakeRange(0, goal.yAxis.count)) != nil { + return .minutes + } + + return nil + } +}