-
Notifications
You must be signed in to change notification settings - Fork 0
/
MedsView.swift
executable file
·166 lines (151 loc) · 6.11 KB
/
MedsView.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
166
//
// MedicationsView.swift
// PainMedsBuddy
//
// Created by Jules Moorhouse.
//
// INFO: This view shows all the available medication
import CoreData
import SwiftUI
struct MedsView: View {
static let medsTag: String? = "Medications"
static let medsIcon: String = SFSymbol.pillsFill.systemName
@StateObject private var viewModel: ViewModel
@EnvironmentObject private var dataController: DataController
@EnvironmentObject private var presentableToast: PresentableToastModel
@State private var showSheetAdd = false
@State private var showSheetEdit = false
@State private var navigationButtonId = UUID()
var items: [Med] {
viewModel.meds.allMeds.filter { !$0.hidden }.sortedItems(using: viewModel.sortOrder)
}
var medsList: some View {
List {
ForEach(items, id: \.self) { med in
Button(action: {
showSheetEdit.toggle()
}, label: {
MedRowView(med: med)
})
.sheet(isPresented: $showSheetEdit) {
MedEditView(
dataController: dataController,
med: med,
add: false,
hasRelationship: viewModel.hasRelationship(med: med)
)
.allowAutoDismiss(false)
}
}
.onDelete { offsets in
viewModel.deleteMed(offsets, items: items)
}
}
.listStyle(InsetGroupedListStyle())
.disabled(viewModel.showingSortOrder == true)
}
var addMedToolbarItem: some ToolbarContent {
ToolbarItem(placement: .navigationBarTrailing) {
HStack {
Text("")
.accessibilityHidden(true)
Button(action: {
showSheetAdd.toggle()
}, label: {
// INFO: In iOS 14.3 VoiceOver has a glitch that reads the label
// "Add Med" as "Add" no matter what accessibility label
// we give this toolbar button when using a label.
// As a result, when VoiceOver is running, we use a text
// view for the button instead, forcing a correct reading
// without losing the original layout.
if UIAccessibility.isVoiceOverRunning {
Text(.medEditAddMed)
.accessibilityIdentifier(.medEditAddMed)
} else {
Label(.medEditAddMed, systemImage: SFSymbol.plus.systemName)
.accessibilityElement()
.accessibility(addTraits: .isButton)
.accessibilityLabel(.medEditAddMed)
.accessibilityIdentifier(.medEditAddMed)
}
})
}
.id(self.navigationButtonId) // NOTE: Force new instance creation
}
}
var sortToolbarItem: some ToolbarContent {
ToolbarItem(placement: .navigationBarLeading) {
if !viewModel.meds.isEmpty {
HStack {
Text("")
.accessibilityHidden(true)
Button(action: {
viewModel.showingSortOrder = true
}, label: {
Label(.commonSort, systemImage: SFSymbol.arrowUpArrowDown.systemName)
.accessibilityElement()
.accessibility(addTraits: .isButton)
.accessibilityLabel(.commonSort)
.accessibilityIdentifier(.commonSort)
})
}
.id(self.navigationButtonId) // NOTE: Force new instance creation
}
}
}
var body: some View {
NavigationViewChild {
Group {
Group {
if items.isEmpty {
PlaceholderView(
string: .commonEmptyView,
imageString: MedsView.medsIcon
)
} else {
ZStack {
medsList
}
}
}
.toolbar {
addMedToolbarItem
sortToolbarItem
}
.actionSheet(isPresented: $viewModel.showingSortOrder) {
ActionSheet(title: Text(Strings.sortSortOrder.rawValue), buttons: [
.default(Text(Strings.sortOptimised.rawValue)) { viewModel.sortOrder = .optimised },
.default(Text(Strings.sortCreatedDate.rawValue)) { viewModel.sortOrder = .creationDate },
.default(Text(Strings.sortTitle.rawValue)) { viewModel.sortOrder = .title },
.cancel(),
])
}
.sheet(isPresented: $showSheetAdd) {
MedAddView()
.allowAutoDismiss(false)
.environmentObject(dataController)
.onDisappear {
// NOTE: Update button id after sheet got closed
self.navigationButtonId = UUID()
}
}
.navigationTitle(Strings.tabTitleMedications.rawValue)
.navigationBarAccessibilityIdentifier(.tabTitleMedications)
PlaceholderView(
string: .medsPleaseSelect,
imageString: MedsView.medsIcon
)
}
.toasted(show: $presentableToast.show, data: $presentableToast.data)
}
}
init(dataController: DataController) {
let viewModel = ViewModel(dataController: dataController)
_viewModel = StateObject(wrappedValue: viewModel)
}
}
struct MedicationsView_Previews: PreviewProvider {
static var previews: some View {
MedsView(dataController: DataController.preview)
}
}