-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrid.swift
144 lines (117 loc) · 4.03 KB
/
Grid.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
import WidgetKit
import SwiftUI
import SwiftData
import AppIntents
struct Provider: AppIntentTimelineProvider {
func placeholder(in context: Context) -> SimpleEntry {
SimpleEntry(date: Date())
}
func snapshot(for configuration: ConfigurationAppIntent, in context: Context) async -> SimpleEntry {
SimpleEntry(date: Date())
}
func timeline(for configuration: ConfigurationAppIntent, in context: Context) async -> Timeline<SimpleEntry> {
var entries: [SimpleEntry] = []
var date = Date.now
for i in 0 ..< 5 {
date = date.byAdding(.hour, value: i)
let entry = SimpleEntry(date: date)
entries.append(entry)
}
let timeline = Timeline(entries: entries, policy: .atEnd)
return timeline
}
}
struct SimpleEntry: TimelineEntry {
let date: Date
}
struct GridEntryView : View {
@Environment(\.widgetFamily) private var widgetFamily
@Query(sort: \Habit.order) private var habits: [Habit]
var entry: Provider.Entry
var body: some View {
if habits.isEmpty {
emptyView
} else {
listView
}
}
var emptyView: some View {
VStack(spacing: 8) {
Text("No habits")
.font(.callout)
.fontWeight(.medium)
Text("Widgets display the first items of the list.")
.font(.subheadline)
.opacity(0.7)
}
.fontDesign(.rounded)
.opacity(0.6)
.multilineTextAlignment(.center)
.padding(.horizontal, 12)
}
var listView: some View {
GeometryReader { proxy in
HabitsView(habitsNumber: habitsNumber(of: proxy.size.width), width: proxy.size.width)
}
}
func habitsNumber(of width: CGFloat) -> Int {
switch widgetFamily {
case .systemLarge: return width < 335 ? 11 : 12
case .systemMedium: return 5
default: return 5
}
}
}
struct LogIntent: AppIntent {
// Empty to conform to AppIntent protocol
init(){}
static var title: LocalizedStringResource = "Log a day"
@Parameter(title: "habit id")
var habitID: Int
@Parameter(title: "log id")
var logID: Int
init(habitID: Int, logID: Int) {
self.habitID = habitID
self.logID = logID
}
@MainActor
func perform() async throws -> some IntentResult {
do {
let descriptor = FetchDescriptor<Log>()
let logs = try modelContainer.mainContext.fetch(descriptor)
let date = Date().reset(after: .day).byAdding(.day, value: logID)
if let log = logs.first(where: { $0.habit?.order == habitID && $0.date.isDate(date) }) {
modelContainer.mainContext.delete(log)
} else {
let log = Log(date: date)
let habits = try modelContainer.mainContext.fetch(FetchDescriptor<Habit>())
if let habit = habits.first(where: { $0.order == habitID }) {
log.habit = habit
}
modelContainer.mainContext.insert(log)
}
} catch {
print("Error updating logs from widget. \(error.localizedDescription)")
}
return .result()
}
}
struct Grid: Widget {
let kind: String = "MyWidget"
var body: some WidgetConfiguration {
AppIntentConfiguration(kind: kind, intent: ConfigurationAppIntent.self, provider: Provider()) { entry in
GridEntryView(entry: entry)
.containerBackground(.backgroundWidget, for: .widget)
.modelContainer(modelContainer)
}
.contentMarginsDisabled()
.configurationDisplayName("Habits Grid")
.description("Log your habits directly from widgets.")
.supportedFamilies([.systemSmall, .systemMedium, .systemLarge])
}
}
#Preview(as: .systemSmall) {
Grid()
} timeline: {
SimpleEntry(date: .now)
}