Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 🐛 [jira: 2313] toolbar for keyboard on iOS 15 & save func in DataTable #546

Merged
merged 1 commit into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions Sources/FioriSwiftUICore/DataTable/DataTable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,33 +67,50 @@ public struct DataTable: View {
.foregroundColor(Color.preferredColor(.tertiaryLabel))
.font(.fiori(forTextStyle: .body))
} else if self.model.showListView {
TableListView(layoutManager: layoutManager)
TableListView(layoutManager: self.layoutManager)
} else {
if !layoutManager.isLayoutFinished(rect.size) {
if !self.layoutManager.isLayoutFinished(rect.size) {
if #available(iOS 14.0, *) {
ProgressView().progressViewStyle(CircularProgressViewStyle())
} else {
// Fallback on earlier versions
Text("Loading...", tableName: "FioriSwiftUICore", bundle: Bundle.accessor)
}
} else {
ScrollAndZoomView(layoutManager: layoutManager, size: rect.size)
ScrollAndZoomView(layoutManager: self.layoutManager, size: rect.size)
}
}
}
.frame(width: rect.size.width, height: rect.size.height, alignment: .center)
.toolbar {
ToolbarItemGroup(placement: .keyboard) {
Spacer()

Button {
// save text changes
self.layoutManager.saveEditingTextChange()
} label: {
Text("Done", tableName: "FioriSwiftUICore", bundle: Bundle.accessor)
.font(.fiori(forTextStyle: .body).bold())
.foregroundColor(Color.preferredColor(.tintColor))
}
}
}
.onReceive(Publishers.keyboardHeight) { keyboardHeight in
self.layoutManager.keyboardHeight = keyboardHeight
}
.onReceive(NotificationCenter.default.publisher(for: UIDevice.orientationDidChangeNotification)) { _ in
if layoutManager.currentCell != nil {
layoutManager.currentCell = nil
// save text changes
self.layoutManager.saveEditingTextChange()

if self.layoutManager.currentCell != nil {
self.layoutManager.currentCell = nil
}
}
.onChange(of: self.sizeCategory, perform: { newValue in
layoutManager.sizeCategory = newValue
layoutManager.layoutData = nil
})
.onChange(of: self.sizeCategory) { newValue in
self.layoutManager.sizeCategory = newValue
self.layoutManager.layoutData = nil
}
.clipped()
.environmentObject(self.layoutManager)
.background(self.model.backgroundColor)
Expand Down
19 changes: 6 additions & 13 deletions Sources/FioriSwiftUICore/DataTable/InlineEditingView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,19 +109,6 @@ struct InlineEditingView: View {
.padding(contentInset)
.frame(width: cellWidth, height: cellHeight)
.border(isValid.0 ? Color.preferredColor(.tintColor) : Color.preferredColor(.negativeLabel), width: 2)
.toolbar {
ToolbarItemGroup(placement: .keyboard) {
Spacer()

Button {
self.updateText(self.editingText)
} label: {
Text("Done", tableName: "FioriSwiftUICore", bundle: Bundle.accessor)
.font(.fiori(forTextStyle: .body).bold())
.foregroundColor(Color.preferredColor(.tintColor))
}
}
}
.onAppear {
DispatchQueue.main.async {
self.focusState = true
Expand All @@ -130,6 +117,12 @@ struct InlineEditingView: View {
.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) {
Expand Down
10 changes: 5 additions & 5 deletions Sources/FioriSwiftUICore/DataTable/ItemView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,11 @@ struct ItemView: View {
let tapGesture = TapGesture()
.onEnded { _ in
if self.layoutManager.model.editMode == .inline {
// save text changes if an other cell is tapped
if let currentCell = layoutManager.currentCell, layoutData.allDataItems[currentCell.0][currentCell.1].type == .text {
self.layoutManager.updateText(rowIndex: currentCell.0, columnIndex: currentCell.1)
}

// header is not editable
if isHeader {
if self.layoutManager.currentCell != nil {
Expand All @@ -410,11 +415,6 @@ struct ItemView: View {
return
}

// save text changes if an other cell is tapped
if let currentCell = layoutManager.currentCell, layoutData.allDataItems[currentCell.0][currentCell.1].type == .text {
self.layoutManager.updateText(rowIndex: currentCell.0, columnIndex: currentCell.1)
}

self.layoutManager.currentCell = (self.rowIndex, self.columnIndex)
self.showBanner = !dataItem.isValid
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ struct LeadingAccessoryView: View {
switch items[index] {
case .button(let button):
Button(action: {
// save text changes
self.layoutManager.saveEditingTextChange()

button.action()
}) {
button.image?
Expand Down Expand Up @@ -57,6 +60,10 @@ struct LeadingAccessoryView: View {
height: self.layoutData.rowHeights[self.rowIndex] * self.layoutManager.scaleY)
.background(self.layoutManager.model.editMode == .select && self.isSelected ? Color.preferredColor(.informationBackground) : Color.clear)
.background(self.layoutManager.model.backgroundColor)
.onTapGesture {
// save text changes
self.layoutManager.saveEditingTextChange()
}
}

func makeSectionButton(layoutData: LayoutData) -> some View {
Expand Down
16 changes: 14 additions & 2 deletions Sources/FioriSwiftUICore/DataTable/TableLayoutManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class TableLayoutManager: ObservableObject {

guard let ld = layoutData, let cacheLd = cacheLayoutData else { return ([], [], false) }

var isThereRejectedErrors: Bool = false
var isThereRejectedErrors = false
var changes: [DataTableChange] = []
var newRowData: [TableRowItem] = ld.rowData

Expand Down Expand Up @@ -262,13 +262,14 @@ class TableLayoutManager: ObservableObject {
}

// update model
guard let ld = layoutData, cacheLayoutData != nil else { return [] }
guard let ld = layoutData else { return [] }

// save text changes if an other cell is tapped
if let tmpCell = cacheCurrentCell, ld.allDataItems[tmpCell.0][tmpCell.1].type == .text {
self.updateText(rowIndex: tmpCell.0, columnIndex: tmpCell.1, updateItValidOnly: true)
}

guard self.cacheLayoutData != nil else { return [] }
let changeResult = self.queryInlineEditChanges(applyValidation: true, findFirstChangeThenReturn: false)
let isThereRejectedErrors: Bool = changeResult.isThereRejectedErrors
let changes: [DataTableChange] = changeResult.changes
Expand All @@ -292,6 +293,17 @@ class TableLayoutManager: ObservableObject {
return changes
}

func saveEditingTextChange() {
// save text changes
guard self.model.editMode == .inline else { return }

if let tmpCell = currentCell, let ld = layoutData, ld.allDataItems[tmpCell.0][tmpCell.1].type == .text {
self.updateText(rowIndex: tmpCell.0, columnIndex: tmpCell.1, updateItValidOnly: false)

self.currentCell = nil
}
}

func updateText(rowIndex: Int, columnIndex: Int, updateItValidOnly: Bool = false) {
guard let ld = layoutData else { return }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ extension TableLayoutManager {
tmpLayoutData.allDataItems = tmpLayoutData.updatedItemsPos()

DispatchQueue.main.async {
if self.model.editMode != .inline {
if self.cacheLayoutData == nil || self.model.editMode != .inline {
self.cacheLayoutData = tmpLayoutData.copy()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ struct TrailingAccessoryView: View {
switch item {
case .button(let button):
Button(action: {
self.layoutManager.saveEditingTextChange()

button.action()
}) {
button.image?
Expand All @@ -44,5 +46,9 @@ struct TrailingAccessoryView: View {
EmptyView()
}
}
.onTapGesture {
// save text changes
self.layoutManager.saveEditingTextChange()
}
}
}