Skip to content

Commit 44e7210

Browse files
author
ash
authored
Merge pull request #1 from asboy2035/continue-timers
Add ability to continue timers
2 parents e33cf9a + 2d3598e commit 44e7210

4 files changed

Lines changed: 155 additions & 62 deletions

File tree

BigTime/HistoryView.swift

Lines changed: 132 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ struct HistoryView: View {
1111
@EnvironmentObject var timerViewModel: TimerViewModel
1212
@State private var editingSession: TimerSession?
1313
@State private var editedLabel: String = ""
14+
@State private var editedDuration: Int = 0
1415

1516
var filteredSessions: [TimerSession] {
1617
let calendar = Calendar.current
@@ -22,75 +23,149 @@ struct HistoryView: View {
2223
}
2324

2425
var body: some View {
25-
List {
26-
ForEach(filteredSessions) { session in
27-
VStack(alignment: .leading) {
28-
Text(session.label)
29-
.font(.headline)
30-
31-
HStack {
32-
Text(session.startDate, style: .date)
33-
.font(.subheadline)
34-
.foregroundColor(.secondary)
35-
36-
Spacer()
26+
VStack(alignment: .leading) {
27+
Text("History")
28+
.font(.system(.title, design: .monospaced))
29+
.padding()
30+
List {
31+
ForEach(filteredSessions) { session in
32+
VStack(alignment: .leading) {
33+
Text(session.label)
34+
.font(.headline)
3735

38-
Text(session.formattedDuration)
39-
.font(.system(.subheadline, design: .monospaced))
40-
.foregroundColor(.secondary)
36+
HStack {
37+
Text(session.startDate, style: .date)
38+
.font(.subheadline)
39+
.foregroundColor(.secondary)
40+
41+
Spacer()
42+
43+
Text(session.formattedDuration)
44+
.font(.system(.subheadline, design: .monospaced))
45+
.foregroundColor(.secondary)
46+
}
47+
}
48+
.contentShape(Rectangle())
49+
.onTapGesture {
50+
editingSession = session
51+
editedLabel = session.label
4152
}
42-
}
43-
.contentShape(Rectangle())
44-
.onTapGesture {
45-
editingSession = session
46-
editedLabel = session.label
4753
}
4854
}
49-
}
50-
.navigationTitle("BigTime")
51-
.sheet(item: $editingSession) { session in
52-
NavigationView {
53-
VStack(spacing: 20) {
54-
Text("Edit Session")
55-
.font(.system(.title2, design: .monospaced))
56-
57-
Form {
58-
Section {
59-
TextField("Session Label", text: $editedLabel)
60-
.textFieldStyle(RoundedBorderTextFieldStyle())
61-
}
62-
}
63-
64-
HStack {
65-
Button("Cancel") {
66-
editingSession = nil
67-
}
68-
Button("Save") {
69-
if let session = editingSession {
70-
timerViewModel.updateSessionLabel(session, newLabel: editedLabel)
55+
.scrollContentBackground(.hidden) // Hides the default background
56+
.background(Color.clear) // Sets the background to transparent
57+
.navigationTitle("BigTime")
58+
.sheet(item: $editingSession) { session in
59+
NavigationView {
60+
VStack(spacing: 20) {
61+
Text("Edit Session")
62+
.font(.system(.title2, design: .monospaced))
63+
64+
Form {
65+
Section {
66+
TextField("Session Label", text: $editedLabel)
67+
.textFieldStyle(RoundedBorderTextFieldStyle())
68+
69+
// Timer preview/editor
70+
HStack {
71+
Text("Duration:")
72+
Spacer()
73+
HStack(spacing: 2) {
74+
// Hours
75+
Stepper(
76+
value: Binding(
77+
get: { editedDuration / 3600 },
78+
set: { editedDuration = ($0 * 3600) + (editedDuration % 3600) }
79+
),
80+
in: 0...23
81+
) {
82+
Text("\(editedDuration / 3600)h")
83+
.font(.system(.body, design: .monospaced))
84+
.frame(width: 40)
85+
}
86+
87+
// Minutes
88+
Stepper(
89+
value: Binding(
90+
get: { (editedDuration % 3600) / 60 },
91+
set: { editedDuration = (editedDuration / 3600 * 3600) + ($0 * 60) + (editedDuration % 60) }
92+
),
93+
in: 0...59
94+
) {
95+
Text("\((editedDuration % 3600) / 60)m")
96+
.font(.system(.body, design: .monospaced))
97+
.frame(width: 40)
98+
}
99+
100+
// Seconds
101+
Stepper(
102+
value: Binding(
103+
get: { editedDuration % 60 },
104+
set: { editedDuration = (editedDuration / 60 * 60) + $0 }
105+
),
106+
in: 0...59
107+
) {
108+
Text("\(editedDuration % 60)s")
109+
.font(.system(.body, design: .monospaced))
110+
.frame(width: 40)
111+
}
112+
}
113+
}
71114
}
72-
editingSession = nil
73115
}
74-
.font(.system(size: 12, weight: .medium))
75-
.background(Color.accentColor) // Use the accent color as the background
76-
.cornerRadius(5)
77116

78-
Button("Delete") {
79-
if let session = editingSession {
80-
timerViewModel.deleteSession(session)
117+
HStack(spacing: 12) {
118+
Button("Cancel") {
119+
editingSession = nil
120+
}
121+
.buttonStyle(.bordered)
122+
123+
Button("Continue") {
124+
if let session = editingSession {
125+
let updatedSession = TimerSession(
126+
id: session.id,
127+
startDate: session.startDate,
128+
duration: editedDuration,
129+
label: editedLabel
130+
)
131+
timerViewModel.updateSessionLabel(session, newLabel: editedLabel)
132+
timerViewModel.continueFromSession(updatedSession)
133+
}
134+
editingSession = nil
135+
}
136+
.buttonStyle(.borderedProminent)
137+
138+
Button("Save") {
139+
if let session = editingSession {
140+
timerViewModel.updateSessionLabel(session, newLabel: editedLabel)
141+
}
142+
editingSession = nil
143+
}
144+
.buttonStyle(.bordered)
145+
.background(Color.accentColor) // Use the accent color as the background
146+
.cornerRadius(5)
147+
148+
Button("Delete") {
149+
if let session = editingSession {
150+
timerViewModel.deleteSession(session)
151+
}
152+
editingSession = nil
81153
}
82-
editingSession = nil
154+
.background(Color.red.opacity(0.5)) // Light red background
155+
.cornerRadius(5)
156+
}
157+
}
158+
.onAppear {
159+
if let session = editingSession {
160+
editedLabel = session.label
161+
editedDuration = session.duration
83162
}
84-
.font(.system(size: 12, weight: .medium))
85-
.foregroundColor(.red) // Red color for the delete button
86-
.background(Color.red.opacity(0.5)) // Light red background
87-
.cornerRadius(5)
88163
}
164+
.padding()
165+
.frame(width: 300, height: 200, alignment: .center)
89166
}
90-
.padding()
91-
.padding(.vertical, 50)
167+
.frame(width: 300, height: 200, alignment: .center)
92168
}
93-
.frame(width: 300, height: 200, alignment: .center)
94169
}
95170
}
96171
}

BigTime/TimerView.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ struct TimerView: View {
2626
HStack {
2727
Button(action: timerViewModel.toggleTimer) {
2828
HStack(spacing: 4) {
29-
Image(systemName: timerViewModel.isRunning ? "pause" : "play.fill")
30-
.padding(.vertical, 5)
31-
Text(timerViewModel.isRunning ? "Pause" : "Start")
29+
Image(systemName: timerViewModel.isRunning ? "clock.badge.checkmark" : "play.fill")
30+
.frame(width: 15, height: 25)
31+
Text(timerViewModel.isRunning ? "Done" : "Start")
3232
.font(.body)
3333
}
3434
.frame(maxWidth: .infinity, alignment: .leading)
@@ -37,7 +37,7 @@ struct TimerView: View {
3737
Button(action: timerViewModel.resetTimer) {
3838
HStack(spacing: 4) {
3939
Image(systemName: "xmark")
40-
.padding(.vertical, 5)
40+
.frame(width: 15, height: 25)
4141
Text("Reset")
4242
.font(.body)
4343
}

BigTime/TimerViewModel.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,17 @@ class TimerViewModel: ObservableObject {
3737
} else {
3838
stopTimer()
3939
saveSession()
40+
// Reset after saving
41+
resetTimer()
42+
currentTask = ""
4043
}
4144
}
4245

4346
func resetTimer() {
4447
stopTimer()
4548
elapsedSeconds = 0
4649
startDate = nil
50+
isRunning = false
4751
}
4852

4953
private func startTimer() {
@@ -102,4 +106,18 @@ class TimerViewModel: ObservableObject {
102106
saveSessions()
103107
}
104108
}
109+
110+
func continueFromSession(_ session: TimerSession) {
111+
resetTimer()
112+
elapsedSeconds = session.duration
113+
currentTask = session.label
114+
deleteSession(session) // delete old timer instance
115+
startTimer()
116+
startDate = Date()
117+
isRunning = true
118+
}
119+
120+
func updateCurrentTime(_ newSeconds: Int) {
121+
elapsedSeconds = max(0, newSeconds)
122+
}
105123
}

BigTime/VisualEffectBlur.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ struct VisualEffectBlur: NSViewRepresentable {
1212
func makeNSView(context: Context) -> NSVisualEffectView {
1313
let view = NSVisualEffectView()
1414
view.blendingMode = .behindWindow
15-
view.material = .hudWindow
15+
view.material = .headerView
1616
view.state = .active
1717
return view
1818
}

0 commit comments

Comments
 (0)