Skip to content

Commit 84b58a9

Browse files
committed
Enhance habit entries
1 parent 5a7cf4c commit 84b58a9

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

Habits.xcodeproj/project.pbxproj

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
/* Begin PBXBuildFile section */
1010
BC025C5728DE3CDC00E55CF0 /* HistoryChartView.swift in Sources */ = {isa = PBXBuildFile; fileRef = BC025C5628DE3CDC00E55CF0 /* HistoryChartView.swift */; };
1111
BC50DD2128D3AC33000A632F /* HabitStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = BC50DD2028D3AC33000A632F /* HabitStore.swift */; };
12+
BC50DD2528D5C83A000A632F /* Entry.swift in Sources */ = {isa = PBXBuildFile; fileRef = BC50DD2428D5C83A000A632F /* Entry.swift */; };
1213
BC5A3BCF28CDEEA100CAF319 /* Habit.swift in Sources */ = {isa = PBXBuildFile; fileRef = BC5A3BCE28CDEEA100CAF319 /* Habit.swift */; };
1314
BC5A3BD628CDF44700CAF319 /* HabitsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = BC5A3BD528CDF44700CAF319 /* HabitsView.swift */; };
1415
BC5A3BD828CDF4B800CAF319 /* HabitSummaryView.swift in Sources */ = {isa = PBXBuildFile; fileRef = BC5A3BD728CDF4B800CAF319 /* HabitSummaryView.swift */; };
@@ -45,6 +46,7 @@
4546
/* Begin PBXFileReference section */
4647
BC025C5628DE3CDC00E55CF0 /* HistoryChartView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HistoryChartView.swift; sourceTree = "<group>"; };
4748
BC50DD2028D3AC33000A632F /* HabitStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HabitStore.swift; sourceTree = "<group>"; };
49+
BC50DD2428D5C83A000A632F /* Entry.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Entry.swift; sourceTree = "<group>"; };
4850
BC5A3BCE28CDEEA100CAF319 /* Habit.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Habit.swift; sourceTree = "<group>"; };
4951
BC5A3BD428CDF32500CAF319 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = SOURCE_ROOT; };
5052
BC5A3BD528CDF44700CAF319 /* HabitsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HabitsView.swift; sourceTree = "<group>"; };
@@ -95,6 +97,7 @@
9597
children = (
9698
BC5A3BCE28CDEEA100CAF319 /* Habit.swift */,
9799
BC50DD2028D3AC33000A632F /* HabitStore.swift */,
100+
BC50DD2428D5C83A000A632F /* Entry.swift */,
98101
BCC3857928DF1A9D00DB8E49 /* ChartEntry.swift */,
99102
BCC3857B28DF215C00DB8E49 /* Utils.swift */,
100103
);
@@ -302,6 +305,7 @@
302305
BCC3857E28DF255E00DB8E49 /* ScoreChartView.swift in Sources */,
303306
BC5A3BD828CDF4B800CAF319 /* HabitSummaryView.swift in Sources */,
304307
BC025C5728DE3CDC00E55CF0 /* HistoryChartView.swift in Sources */,
308+
BC50DD2528D5C83A000A632F /* Entry.swift in Sources */,
305309
BC50DD2128D3AC33000A632F /* HabitStore.swift in Sources */,
306310
BCD3692F28CCADD300B48135 /* HabitsApp.swift in Sources */,
307311
BCC3857C28DF215C00DB8E49 /* Utils.swift in Sources */,

Habits/Models/Entry.swift

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import Foundation
2+
3+
struct Entry: Identifiable, Codable {
4+
let id: UUID
5+
var date: Date
6+
var value: Float
7+
8+
init(id: UUID = UUID(), date: Date, value: Float) {
9+
self.id = id
10+
self.date = date
11+
self.value = value
12+
}
13+
14+
}

Habits/Models/Habit.swift

+28-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ struct Habit: Identifiable, Codable {
55
let id: UUID
66
var name: String = ""
77
var color: CodableColor = CodableColor(color: .black)
8-
var days: [Date : Bool] = [:]
8+
var entries: [Date : Entry] = [:]
99

1010
init(id: UUID = UUID()) {
1111
self.id = id
@@ -21,6 +21,33 @@ struct Habit: Identifiable, Codable {
2121
name = habit.name
2222
color = habit.color
2323
}
24+
25+
mutating func addEntry(date: Date) {
26+
entries.updateValue(Entry(date: date.withoutTime, value: 1), forKey: date.withoutTime)
27+
}
28+
29+
mutating func deleteEntry(date: Date) {
30+
entries.removeValue(forKey: date.withoutTime)
31+
}
32+
33+
func checkEntry(date: Date) -> Bool {
34+
return entries.keys.contains(date.withoutTime)
35+
}
36+
37+
func entryCount(_ component: Calendar.Component?) -> Int {
38+
if component == nil {
39+
return entries.count
40+
}
41+
42+
var count = 0
43+
for date in entries.keys {
44+
if date.get(component!) == Date.now.get(component!) {
45+
count += 1
46+
}
47+
}
48+
49+
return count
50+
}
2451
}
2552

2653
extension Habit {

Habits/Models/Utils.swift

+4
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,8 @@ extension Date {
2828
return entries
2929
}
3030

31+
var withoutTime: Date {
32+
return Calendar.current.startOfDay(for: self)
33+
}
34+
3135
}

0 commit comments

Comments
 (0)