Skip to content

Commit

Permalink
Timer screen figures out its own units (#497)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
krugerk authored Nov 6, 2024
1 parent 34cd7e5 commit c45b64a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
12 changes: 0 additions & 12 deletions BeeSwift/GoalViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
36 changes: 32 additions & 4 deletions BeeSwift/TimerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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"

Expand Down Expand Up @@ -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
}
}

0 comments on commit c45b64a

Please sign in to comment.