-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskRunList.swift
More file actions
239 lines (199 loc) · 7.99 KB
/
TaskRunList.swift
File metadata and controls
239 lines (199 loc) · 7.99 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
//
// TaskRunList.swift
//
// Copyright 2023 OpenAlloc LLC
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//
import CoreData
import os
import SwiftUI
import Compactor
import Tabler
import TrackerLib
import TrackerUI
import TroutLib
import TroutUI
struct TaskRunList<Header: View>: View {
@Environment(\.verticalSizeClass) private var verticalSizeClass
@Environment(\.managedObjectContext) private var viewContext
typealias Sort = TablerSort<ZTaskRun>
typealias Context = TablerContext<ZTaskRun>
typealias ProjectedValue = ObservedObject<ZTaskRun>.Wrapper
// MARK: - Parameters
private var zRoutineRun: ZRoutineRun
private var inStore: NSPersistentStore
private var tableHeader: () -> Header
init(zRoutineRun: ZRoutineRun,
inStore: NSPersistentStore,
tableHeader: @escaping () -> Header = { EmptyView() })
{
self.zRoutineRun = zRoutineRun
self.inStore = inStore
self.tableHeader = tableHeader
let predicate = ZTaskRun.getPredicate(zRoutineRun: zRoutineRun, userRemoved: false)
let sortDescriptors = ZTaskRun.byCompletedAt(ascending: true)
let request = makeRequest(ZTaskRun.self,
predicate: predicate,
sortDescriptors: sortDescriptors,
inStore: inStore)
_taskRuns = FetchRequest<ZTaskRun>(fetchRequest: request)
}
// MARK: - Locals
private let logger = Logger(subsystem: Bundle.main.bundleIdentifier!,
category: String(describing: TaskRunList.self))
private let columnSpacing: CGFloat = 10
private var columnPadding: EdgeInsets {
EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0)
// EdgeInsets(top: 0, leading: 5, bottom: 0, trailing: 5)
}
@FetchRequest private var taskRuns: FetchedResults<ZTaskRun>
private var listConfig: TablerListConfig<ZTaskRun> {
TablerListConfig<ZTaskRun>(
onDelete: userRemoveAction
)
}
private var gridItems: [GridItem] { [
GridItem(.flexible(minimum: 70), spacing: columnSpacing, alignment: .leading),
GridItem(.flexible(minimum: 120), spacing: columnSpacing, alignment: .leading),
// GridItem(.flexible(minimum: 80), spacing: columnSpacing, alignment: .trailing),
] }
private let df: DateFormatter = {
let df = DateFormatter()
df.dateStyle = .short
df.timeStyle = .short
return df
}()
private let tc = TimeCompactor(ifZero: "", style: .full, roundSmallToWhole: false)
// MARK: - Views
var body: some View {
TablerList(listConfig,
header: header,
footer: footer,
row: listRow,
rowBackground: rowBackground,
results: taskRuns)
.listStyle(.plain)
// .navigationTitle(navigationTitle)
}
@ViewBuilder
private func header(ctx _: Binding<Context>) -> some View {
tableHeader()
LazyVGrid(columns: gridItems, alignment: .leading) {
Text("Elapsed")
.padding(columnPadding)
Text("Task")
.padding(columnPadding)
// Text("Intensity")
// .padding(columnPadding)
}
}
@ViewBuilder
private func listRow(element: ZTaskRun) -> some View {
LazyVGrid(columns: gridItems, alignment: .leading) {
elapsedText(element.completedAt)
.padding(columnPadding)
Text(element.zTask?.name ?? "")
.padding(columnPadding)
// intensityText(element.intensity, element.zTask?.units)
// .padding(columnPadding)
}
}
@ViewBuilder
private func footer(ctx _: Binding<Context>) -> some View {
HStack {
GroupBox {
startedAtText
.lineLimit(1)
} label: {
Text("Started")
.foregroundStyle(.tint)
.padding(.bottom, 3)
}
GroupBox {
durationText(zRoutineRun.elapsedSecs)
.lineLimit(1)
} label: {
Text("Duration")
.foregroundStyle(.tint)
.padding(.bottom, 3)
}
}
}
private func rowBackground(_: ZTaskRun) -> some View {
EntityBackground(taskColorDarkBg)
}
private var startedAtText: some View {
VStack {
if let startedAt = zRoutineRun.startedAt,
case let dateStr = df.string(from: startedAt)
{
Text(dateStr)
} else {
EmptyView()
}
}
}
private func elapsedText(_ completedAt: Date?) -> some View {
ElapsedTimeText(elapsedSecs: getDuration(completedAt) ?? 0, timeElapsedFormat: .hh_mm_ss)
}
private func durationText(_ duration: TimeInterval) -> some View {
Text(tc.string(from: duration as NSNumber) ?? "")
}
// MARK: - Properties
// MARK: - Actions
// NOTE: 'removes' matching records, where present, from both mainStore and archiveStore.
private func userRemoveAction(at offsets: IndexSet) {
do {
for index in offsets {
let zTaskRun = taskRuns[index]
guard let taskArchiveID = zTaskRun.zTask?.taskArchiveID,
let completedAt = zTaskRun.completedAt
else { continue }
try ZTaskRun.userRemove(viewContext, taskArchiveID: taskArchiveID, completedAt: completedAt)
}
try viewContext.save()
} catch {
logger.error("\(#function): \(error.localizedDescription)")
}
}
// MARK: - Helpers
private func getDuration(_ completedAt: Date?) -> TimeInterval? {
guard let startedAt = zRoutineRun.startedAt,
let completedAt
else { return nil }
return completedAt.timeIntervalSince(startedAt)
}
}
struct TaskRunList_Previews: PreviewProvider {
static var previews: some View {
let manager = CoreDataStack.getPreviewStack()
let ctx = manager.container.viewContext
let archiveStore = manager.getArchiveStore(ctx)!
let routineArchiveID = UUID()
let startedAt1 = Date.now.addingTimeInterval(-20000)
let duration1 = 500.0
let zR = ZRoutine.create(ctx, routineArchiveID: routineArchiveID, routineName: "blah", toStore: archiveStore)
let zRR = ZRoutineRun.create(ctx, zRoutine: zR, startedAt: startedAt1, elapsedSecs: duration1, toStore: archiveStore)
let taskArchiveID1 = UUID()
let taskArchiveID2 = UUID()
let taskArchiveID3 = UUID()
let completedAt1 = startedAt1.addingTimeInterval(116)
let completedAt2 = completedAt1.addingTimeInterval(173)
let completedAt3 = completedAt1.addingTimeInterval(210)
let zE1 = ZTask.create(ctx, zRoutine: zR, taskArchiveID: taskArchiveID1, taskName: "Lat Pulldown", toStore: archiveStore)
let zE2 = ZTask.create(ctx, zRoutine: zR, taskArchiveID: taskArchiveID2, taskName: "Rear Delt", toStore: archiveStore)
let zE3 = ZTask.create(ctx, zRoutine: zR, taskArchiveID: taskArchiveID3, taskName: "Arm Curl", toStore: archiveStore)
_ = ZTaskRun.create(ctx, zRoutineRun: zRR, zTask: zE1, completedAt: completedAt1, toStore: archiveStore)
let er2 = ZTaskRun.create(ctx, zRoutineRun: zRR, zTask: zE2, completedAt: completedAt2, toStore: archiveStore)
_ = ZTaskRun.create(ctx, zRoutineRun: zRR, zTask: zE3, completedAt: completedAt3, toStore: archiveStore)
er2.userRemoved = true
try! ctx.save()
return NavigationStack {
TaskRunList(zRoutineRun: zRR, inStore: archiveStore)
.environment(\.managedObjectContext, ctx)
}
}
}