-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHistoryView.swift
More file actions
143 lines (122 loc) · 4.82 KB
/
HistoryView.swift
File metadata and controls
143 lines (122 loc) · 4.82 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
//
// HistoryView.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 TrackerUI
import TroutLib
import TroutUI
struct HistoryView: View {
@Environment(\.managedObjectContext) private var viewContext
@EnvironmentObject private var manager: CoreDataStack
@EnvironmentObject private var router: TroutRouter
// MARK: - Parameters
let withSettings = false
// MARK: - Locals
@State private var showClearDialog = false
private let logger = Logger(subsystem: Bundle.main.bundleIdentifier!,
category: String(describing: HistoryView.self))
// MARK: - Views
var body: some View {
if let archiveStore = manager.getArchiveStore(viewContext) {
MRoutineRunList(archiveStore: archiveStore)
.toolbar {
ToolbarItem(placement: .destructiveAction) {
Button(action: {
Haptics.play(.warning)
showClearDialog = true
}) {
Text("Clear")
}
}
if withSettings {
ToolbarItem {
Button(action: {
router.path.append(TroutRoute.settings)
}) {
Text("Settings")
}
}
}
}
// NOTE: using an alert, as confirmationDialog may be clipped at top of view on iPad
// .confirmationDialog(
.alert("",
isPresented: $showClearDialog,
actions: {
Button("Clear", role: .destructive, action: clearHistoryAction)
},
message: {
Text("This will remove all historical data.")
})
.navigationTitle(navigationTitle)
.task(priority: .userInitiated, taskAction)
} else {
Text("History not available.")
}
}
// MARK: - Properties
private var navigationTitle: String {
"History"
}
// MARK: - Actions
private func clearHistoryAction() {
do {
// clear all 'z' records from both mainStore and archiveStore
try manager.clearZEntities(viewContext)
try viewContext.save()
} catch {
logger.error("\(#function): \(error.localizedDescription)")
}
}
@Sendable
private func taskAction() async {
logger.notice("\(#function) START")
// transfer any 'Z' records from the 'Main' store to the 'Archive' store.
await manager.container.performBackgroundTask { backgroundContext in
guard let mainStore = manager.getMainStore(backgroundContext),
let archiveStore = manager.getArchiveStore(backgroundContext)
else {
logger.error("\(#function): unable to acquire configuration to transfer log records.")
return
}
do {
try transferToArchive(backgroundContext,
mainStore: mainStore,
archiveStore: archiveStore,
thresholdSecs: freshThresholdSecs)
try backgroundContext.save()
} catch {
logger.error("\(#function): TRANSFER \(error.localizedDescription)")
}
}
logger.notice("\(#function) END")
}
}
struct HistoryView_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 startedAt2 = Date.now.addingTimeInterval(-10000)
let duration2 = 400.0
let zR = ZRoutine.create(ctx, routineArchiveID: routineArchiveID, routineName: "blah", toStore: archiveStore)
_ = ZRoutineRun.create(ctx, zRoutine: zR, startedAt: startedAt1, elapsedSecs: duration1, toStore: archiveStore)
_ = ZRoutineRun.create(ctx, zRoutine: zR, startedAt: startedAt2, elapsedSecs: duration2, toStore: archiveStore)
try! ctx.save()
return NavigationStack {
HistoryView()
.environment(\.managedObjectContext, ctx)
}
}
}