-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimerViewViewModel.swift
91 lines (79 loc) · 3.12 KB
/
TimerViewViewModel.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//
// TimerViewViewModel.swift
// group_app
//
// Created by Emily Markova on 8/2/23.
//
import SwiftUI
import Foundation
class TimerViewViewModel: NSObject, ObservableObject, UNUserNotificationCenterDelegate {
//timer properties
@Published var progress: CGFloat = 0.0
@Published var timerStringValue: String = "00:00"
@Published var isStarted: Bool = false
@Published var addNewTimer: Bool = false
@Published var hour: Int = 0
@Published var minutes: Int = 0
@Published var seconds: Int = 0
//total seconds
@Published var totalSeconds: Int = 0
@Published var staticTotalSeconds: Int = 0
//post timer properties
@Published var isFinished: Bool = false
//since its NSobject
override init(){
super.init()
self.authorizationNotification()
}
//request notification access
func authorizationNotification(){
UNUserNotificationCenter.current().requestAuthorization(options: [.sound,.alert,.badge]){
_, _ in
}
//show in app notification
UNUserNotificationCenter.current().delegate = self
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.sound,.banner])
}
func startTimer(){
withAnimation(.easeInOut(duration: 0.25)){isStarted = true}
timerStringValue = "\(hour == 0 ? "" : "\(hour):")\(minutes >= 10 ? "\(minutes)":"0\(minutes)"):\(seconds >= 10 ? "\(seconds)":"0\(seconds)")"
//calculating total seconds for timer animation
totalSeconds = (hour*3600) + (minutes * 60) + seconds
staticTotalSeconds = totalSeconds
}
func stopTimer(){
withAnimation{
isStarted = false
hour = 0
minutes = 0
seconds = 0
progress = 1
}
totalSeconds = 0
staticTotalSeconds = 0
timerStringValue = "00:00"
}
func updateTimer(){
totalSeconds -= 1
progress = CGFloat(totalSeconds) / CGFloat(staticTotalSeconds)
progress = (progress < 0 ? 0 : progress)
hour = totalSeconds / 3600
minutes = (totalSeconds / 60) % 60
seconds = (totalSeconds % 60)
timerStringValue = "\(hour == 0 ? "" : "\(hour):")\(minutes >= 10 ? "\(minutes)" : "0\(minutes)"):\(seconds >= 10 ? "\(seconds)" : "0\(seconds)")"
if hour == 0 && seconds == 0 && minutes == 0 {
isStarted = false
isFinished = true
}
}
func addNotification(){
let content = UNMutableNotificationContent()
content.title = "Pomodoro Timer"
content.subtitle = "Woohoo! You finished it!!"
content.sound = UNNotificationSound.default
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: UNTimeIntervalNotificationTrigger(timeInterval: TimeInterval(staticTotalSeconds), repeats: false))
UNUserNotificationCenter.current().add(request)
}
}