-
Notifications
You must be signed in to change notification settings - Fork 54
/
InlineEditingView.swift
169 lines (155 loc) · 8.28 KB
/
InlineEditingView.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
167
168
169
import SwiftUI
class InlineEditingModel: ObservableObject {
@Published var keyboardHeight: CGFloat = 0
@Published var size = CGSize.zero
@Published var currentCell: (Int, Int)? = nil
}
struct InlineEditingView: View {
let layoutManager: TableLayoutManager
let rowIndex: Int
let columnIndex: Int
let layoutData: LayoutData
@ObservedObject var inlineEditingModel: InlineEditingModel
@Binding var showBanner: Bool
@State var editingText: String = ""
@State var isValid: (Bool, String?) = (true, nil)
@FocusState var focusState: Bool
init(layoutManager: TableLayoutManager, layoutData: LayoutData, showBanner: Binding<Bool>) {
self.layoutManager = layoutManager
self.inlineEditingModel = layoutManager.inlineEditingModel
self.layoutData = layoutData
self._showBanner = showBanner
self.rowIndex = (layoutManager.currentCell ?? (0, 0)).0
self.columnIndex = (layoutManager.currentCell ?? (0, 0)).1
let dataItem = layoutManager.layoutData?.allDataItems[self.rowIndex][self.columnIndex]
self._editingText = State(initialValue: dataItem?.text ?? "")
self._isValid = State(initialValue: (dataItem?.isValid ?? true, ""))
}
var toolbarItemPlacement: ToolbarItemPlacement {
#if os(visionOS)
return .automatic
#else
return .keyboard
#endif
}
var body: some View {
let dataItem = self.layoutData.allDataItems[self.rowIndex][self.columnIndex]
let foregroundColor: Color? = dataItem.foregroundColor
let isHeader: Bool = self.rowIndex == 0 && self.layoutManager.model.hasHeader
let cellWidth = self.layoutData.columnWidths[self.columnIndex] * self.layoutManager.scaleX
let cellHeight = self.layoutData.rowHeights[self.rowIndex] * self.layoutManager.scaleY
let contentInset = self.layoutData.cellContentInsets(for: self.rowIndex, columnIndex: self.columnIndex)
let contentWidth = max(0, cellWidth - contentInset.horizontal * self.layoutManager.scaleX)
let contentHeight = max(0, cellHeight - contentInset.vertical * self.layoutManager.scaleY)
let isValid = self.layoutManager.checkIsValid(for: self.layoutData.allDataItems[self.rowIndex][self.columnIndex])
let uifont = dataItem.uifont ?? TableViewLayout.defaultUIFont(isHeader)
let baselineHeightOffset = (layoutData.firstBaselineHeights[self.rowIndex] - dataItem.firstBaselineHeight) * self.layoutManager.scaleY
let fontSize: CGFloat = uifont.pointSize * self.layoutManager.scaleX
let finalFont = Font(uifont.withSize(fontSize))
return VStack(alignment: .leading, spacing: 0) {
if self.layoutManager.model.rowAlignment == .baseline {
Spacer().frame(height: baselineHeightOffset)
}
HStack(alignment: .top) {
if dataItem.textAlignment == .center || dataItem.textAlignment == .trailing {
Spacer(minLength: 0)
}
#if swift(>=5.7)
if #available(iOS 16, *) {
TextField("", text: $editingText, axis: Axis.vertical)
.font(finalFont)
.foregroundColor(isValid.0 ? foregroundColor : Color.preferredColor(.negativeLabel))
.accentColor(isValid.0 ? Color.preferredColor(.tintColor) : Color.preferredColor(.negativeLabel))
.lineLimit(dataItem.lineLimit)
.multilineTextAlignment(dataItem.textAlignment)
.focused($focusState)
.frame(width: contentWidth, height: contentHeight, alignment: dataItem.textAlignment.toTextFrameAlignment())
.onSubmit {
updateText(editingText)
}
} else {
TextField("", text: self.$editingText)
.font(finalFont)
.foregroundColor(isValid.0 ? foregroundColor : Color.preferredColor(.negativeLabel))
.accentColor(isValid.0 ? Color.preferredColor(.tintColor) : Color.preferredColor(.negativeLabel))
.lineLimit(dataItem.lineLimit)
.multilineTextAlignment(dataItem.textAlignment)
.textInputAutocapitalization(.never)
.disableAutocorrection(true)
.focused(self.$focusState)
.frame(width: contentWidth, height: contentHeight, alignment: dataItem.textAlignment.toTextFrameAlignment())
.onSubmit {
self.updateText(self.editingText)
}
}
#else
TextField("", text: self.$editingText)
.focused(self.$focusState)
.onSubmit {
self.updateText(self.editingText)
}
.lineLimit(dataItem.lineLimit)
.multilineTextAlignment(dataItem.textAlignment)
.textInputAutocapitalization(.never)
.disableAutocorrection(true)
.frame(width: contentWidth, height: contentHeight, alignment: dataItem.textAlignment.toTextFrameAlignment())
.font(finalFont)
.foregroundColor(isValid.0 ? foregroundColor : Color.preferredColor(.negativeLabel))
.accentColor(isValid.0 ? Color.preferredColor(.tintColor) : Color.preferredColor(.negativeLabel))
#endif
if dataItem.textAlignment == .center || dataItem.textAlignment == .leading {
Spacer(minLength: 0)
}
}
Spacer(minLength: 0)
}
.padding(contentInset)
.frame(width: cellWidth, height: cellHeight)
.border(isValid.0 ? Color.preferredColor(.tintColor) : Color.preferredColor(.negativeLabel), width: 2)
.toolbar {
ToolbarItemGroup(placement: toolbarItemPlacement) {
Spacer()
Button {
self.updateText(self.editingText)
} label: {
Text("Done", tableName: "FioriSwiftUICore", bundle: Bundle.accessor)
.font(Font.fiori(forTextStyle: .body).bold())
.foregroundColor(Color.preferredColor(.tintColor))
}
}
}
.onAppear {
DispatchQueue.main.async {
self.focusState = true
}
}
.onChange(of: self.editingText, perform: { _ in
self.layoutManager.cacheEditingText = self.editingText
})
.onChange(of: self.focusState) { newValue in
// lost focus
if !newValue {
self.updateText(self.editingText)
}
}
}
func updateText(_ newValue: String) {
guard let layoutData = layoutManager.layoutData else { return }
var dataItem = layoutData.allDataItems[self.rowIndex][self.columnIndex]
if dataItem.text != newValue {
dataItem.text = newValue
self.isValid = self.layoutManager.checkIsValid(for: dataItem)
let errorChange: Int = dataItem.isValid != self.isValid.0 ? (self.isValid.0 ? -1 : 1) : 0
layoutData.numOfErrors += errorChange
dataItem.isValid = self.isValid.0
dataItem.size = layoutData.calcDataItemSize(dataItem)
layoutData.allDataItems[self.rowIndex][self.columnIndex] = dataItem
layoutData.updateCellLayout(for: self.rowIndex, columnIndex: self.columnIndex)
self.layoutManager.needRefresh.toggle()
self.layoutManager.isValid = self.isValid
self.showBanner = !self.isValid.0
self.layoutManager.model.valueDidChange?(DataTableChange(rowIndex: self.rowIndex, columnIndex: self.columnIndex, value: .text(self.editingText), text: self.editingText))
}
self.layoutManager.currentCell = nil
}
}