Skip to content

Commit

Permalink
set seconds, session view
Browse files Browse the repository at this point in the history
  • Loading branch information
aduryagin committed Dec 27, 2022
1 parent 0ff7b61 commit 5740cfc
Showing 1 changed file with 66 additions and 25 deletions.
91 changes: 66 additions & 25 deletions Work Timer/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ struct ContentView: View {
let workSessionSeconds: Double = 25 * 60 // 25 min
let workDaySeconds: Double = 400 * 60 // 400 min
let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()

@State var isSetCounterView = false
@State var newCounter: String = ""
@State var isSessionView = false
@State var counter: Double = 0
@State var isTimerRunning = false

Expand Down Expand Up @@ -45,39 +49,73 @@ struct ContentView: View {
UNUserNotificationCenter.current().add(request)
}

func getSessionSeconds() -> Double {
return counter.truncatingRemainder(dividingBy: workSessionSeconds)
}

func updateTrayMins(inProgress: Bool) -> Double {
let remainder = counter.truncatingRemainder(dividingBy: workSessionSeconds)
let remainder = getSessionSeconds()
appDelegate.updateIcon(formatMinutes(remainder), inProgress: inProgress)
return remainder
}

func pause() {
isTimerRunning = false
let _ = updateTrayMins(inProgress: isTimerRunning)
}

var body: some View {
VStack(alignment: .center) {
Text("\(String(formatTime(counter))) / \(String(formatTime(workDaySeconds)))")
.font(.largeTitle)
.onReceive(timer) { time in
if (isTimerRunning) {
counter += 1
let remainder = updateTrayMins(inProgress: true)

if (counter == workDaySeconds) {
isTimerRunning = false
VStack(spacing: 5) {
Text(!isSessionView ? "\(String(formatTime(counter))) / \(String(formatTime(workDaySeconds)))" : String(" \(formatMinutes(getSessionSeconds())) / 25"))
.font(.largeTitle)
.onTapGesture {
isSessionView.toggle()
}
.onReceive(timer) { time in
if (isTimerRunning) {
counter += 1
let remainder = updateTrayMins(inProgress: true)

showNotification(message: "Workday is over!")
appDelegate.activateApplication()
} else if (remainder == 0) {
var message = "Time for a 5 min break"
if (counter.truncatingRemainder(dividingBy: workSessionSeconds * 4) == 0) {
message = "Time for a 15 min break"
if (counter == workDaySeconds) {
isTimerRunning = false

showNotification(message: "Workday is over!")
appDelegate.activateApplication()
} else if (remainder == 0) {
var message = "Time for a 5 min break"
if (counter.truncatingRemainder(dividingBy: workSessionSeconds * 4) == 0) {
message = "Time for a 15 min break"
}

showNotification(message: message)
appDelegate.activateApplication()
}

showNotification(message: message)
appDelegate.activateApplication()
}
}


if (!isSetCounterView) {
Button {
isSetCounterView.toggle()
pause()
} label: {
Text("Set seconds")
}
} else {
HStack {
TextField("Seconds", text: $newCounter).frame(width: 150)
Button {
let new = Double(newCounter) ?? counter
if (new < workDaySeconds && new >= 0) {
counter = new
isSetCounterView.toggle()
}
} label: {
Text("Save")
}
}
}
}

Progress(value: counter, total: workDaySeconds)

HStack {
Expand All @@ -87,30 +125,33 @@ struct ContentView: View {
let _ = updateTrayMins(inProgress: false)
} label: {
Image(systemName: "chevron.left")
}.disabled(counter == 0)
}.disabled(counter == 0 || isSetCounterView)
Button {
isTimerRunning = false
counter = 0
appDelegate.resetIcon()
} label: {
Text("Reset")
}.disabled(counter == 0)
}.disabled(counter == 0 || isSetCounterView)
Button {
isTimerRunning.toggle()
let _ = updateTrayMins(inProgress: isTimerRunning)
} label: {
Text(isTimerRunning ? "Pause" : counter != 0 ? "Continue" : "Start")
}
}.disabled(isSetCounterView)
Button {
let index = floor(counter / workSessionSeconds) + 1
counter = index * workSessionSeconds
let _ = updateTrayMins(inProgress: false)
} label: {
Image(systemName: "chevron.right")
}.disabled(counter == workDaySeconds)
}.disabled(counter == workDaySeconds || isSetCounterView)
}
}
.padding()
.onChange(of: counter, perform: { counter in
newCounter = String(counter)
})
}
}

0 comments on commit 5740cfc

Please sign in to comment.