/
Habit.swift
165 lines (125 loc) · 4.74 KB
/
Habit.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//
// Habit.swift
// HabitTracker
//
// Created by Deepesh Deshmukh on 10/12/19.
// Copyright © 2019 Deepesh Deshmukh. All rights reserved.
//
import Foundation
import UserNotifications
struct Habit: Identifiable, Codable{
let id : UUID = UUID()
let type: String
let name: String
let note: String
let startDate: Date
var bestStreak: Int = 0
var currentStreak: Int = 0
var lastCompletionDate: Date?
var numberOfCompletion: Int = 0
var reminder: Bool?
var reminderTitle: String?
var reminderTime: Date?
var calculatedCurrentStreak: Int{
if let lastDate = lastCompletionDate{
if lastDate.isYesterday || lastDate.isToday{
return currentStreak
}else{
return 0
}
}else{
return 0
}
}
var totalDays: Int{
return countDays(startDate, Date()) + 1
}
var success: Double{
return Double(numberOfCompletion) / Double(totalDays)
}
var hasCompletedForToday: Bool{
return lastCompletionDate?.isToday ?? false
}
}
class HabitItems: ObservableObject{
@Published var habits = [Habit](){
didSet{
let enc = JSONEncoder()
if let encoded = try? enc.encode(habits){
UserDefaults.standard.set(encoded, forKey: "habits")
}
else{
print("ERROR in encoding")
}
}
}
init(){
if let habits = UserDefaults.standard.data(forKey: "habits"){
let decode = JSONDecoder()
if let decoded = try? decode.decode([Habit].self, from: habits){
self.habits = decoded
}else{
print("Eror decoding")
}
}else{
print("Error finding key")
}
}
func complete(withHabitId: UUID){
for index in 0..<self.habits.count{
if self.habits[index].id == withHabitId{
self.habits[index].currentStreak = self.habits[index].calculatedCurrentStreak + 1
self.habits[index].lastCompletionDate = Date()
if self.habits[index].currentStreak >= self.habits[index].bestStreak{
self.habits[index].bestStreak = self.habits[index].currentStreak
}
self.habits[index].numberOfCompletion += 1
break
}
}
}
func setReminder(withHabitId: UUID, setValue: Bool, title: String, date: Date){
for index in 0..<self.habits.count{
if self.habits[index].id == withHabitId{
self.habits[index].reminder = setValue
self.habits[index].reminderTitle = title
self.habits[index].reminderTime = date
if setValue == true{
let content = UNMutableNotificationContent()
content.title = title
content.sound = UNNotificationSound.default
var dateComponents = DateComponents()
dateComponents.hour = Calendar.current.component(.hour, from: date)
dateComponents.minute = Calendar.current.component(.minute, from: date)
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
// choose a random identifier
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
// add our notification request
UNUserNotificationCenter.current().add(request)
print("Notification Added")
}else{
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
print("Notification Removed")
}
break
}
}
}
func remove(withHabitId: UUID){
var habitTobeRemoved = 0
for index in 0..<self.habits.count{
if self.habits[index].id == withHabitId{
habitTobeRemoved = index
break
}
}
self.habits.remove(at: habitTobeRemoved)
}
}
func countDays(_ from: Date, _ to: Date) -> Int{
let formatter = DateFormatter()
formatter.dateFormat = "MMMM-dd-yyyy"
let formattedFrom = formatter.date(from: formatter.string(from: from))!
let formattedTo = formatter.date(from: formatter.string(from: to))!
return Calendar.current.dateComponents([.day], from: formattedFrom, to: formattedTo).day!
}