passepartout-apple/Passepartout/App/Reusable/EditableTextList.swift

207 lines
6.1 KiB
Swift
Raw Normal View History

2022-04-12 13:09:14 +00:00
//
// EditableTextList.swift
// Passepartout
//
// Created by Davide De Rosa on 3/31/22.
2023-03-17 15:56:19 +00:00
// Copyright (c) 2023 Davide De Rosa. All rights reserved.
2022-04-12 13:09:14 +00:00
//
// https://github.com/passepartoutvpn
//
// This file is part of Passepartout.
//
// Passepartout is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Passepartout is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Passepartout. If not, see <http://www.gnu.org/licenses/>.
//
import SwiftUI
struct IdentifiableString: Identifiable, Equatable {
var id = UUID()
var string: String
}
struct EditableTextList<Field: View, ActionLabel: View>: View {
typealias FieldCallback = (
isNewElement: Bool,
text: Binding<String>,
onEditingChanged: (Bool) -> Void,
onCommit: () -> Void
)
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
@Binding var elements: [String]
var allowsDuplicates = true
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
var mapping: ([IdentifiableString]) -> [IdentifiableString] = { $0 }
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
var onAdd: ((Binding<String>) -> Void)?
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
let textField: (FieldCallback) -> Field
let addLabel: () -> ActionLabel
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
var commitLabel: (() -> ActionLabel)?
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
@State private var isLoaded = false
@State private var identifiableElements: [IdentifiableString] = []
@State private var editedTextStrings: [UUID: String] = [:]
private let addedUUID = UUID()
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
private var addedText: Binding<String> {
.init {
editedTextStrings[addedUUID] ?? ""
} set: {
editedTextStrings[addedUUID] = $0
}
}
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
var body: some View {
debugChanges()
return Group {
ForEach(mapping(identifiableElements), content: existingRow)
.onDelete(perform: onDelete)
.onMove(perform: onMove)
newRow
}.onAppear {
guard !isLoaded else {
return
}
isLoaded = true
identifiableElements = elements.map {
IdentifiableString(string: $0)
}
}.onChange(of: elements, perform: remapElements)
}
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
private func existingRow(_ element: IdentifiableString) -> some View {
let editedText = binding(toEditedElement: element)
return textField((false, editedText, {
if $0 {
editedTextStrings.removeValue(forKey: element.id)
// print(">>> editing: '\(text.wrappedValue.string)' (\(text.wrappedValue.id))")
}
}, {
replaceElement(at: element.id, with: editedText)
}))
}
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
private var newRow: some View {
AddingTextField(
onAdd: {
addedText.wrappedValue = ""
onAdd?(addedText)
},
onCommit: addElement,
textField: {
textField((true, addedText, { _ in }, $0))
},
addLabel: addLabel,
commitLabel: commitLabel
)
}
}
// MARK: View model
extension EditableTextList {
private func remapElements(_ newElements: [String]) {
var oldIdentifiableElements = identifiableElements
var newIdentifiableElements: [IdentifiableString] = []
newElements.forEach { newString in
let id: UUID
if let found = oldIdentifiableElements.firstIndex(where: {
$0.string == newString
}) {
id = oldIdentifiableElements[found].id
oldIdentifiableElements.remove(at: found)
} else {
id = UUID()
}
newIdentifiableElements.append(.init(id: id, string: newString))
}
guard newIdentifiableElements != identifiableElements else {
return
}
withAnimation {
identifiableElements = newIdentifiableElements
}
}
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
private func addElement() {
guard allowsDuplicates || !identifiableElements.contains(where: {
$0.string == addedText.wrappedValue
}) else {
return
}
// print(">>> + \(addedElement.wrappedValue)")
identifiableElements.append(.init(string: addedText.wrappedValue))
commit()
}
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
private func binding(toEditedElement element: IdentifiableString) -> Binding<String> {
// print(">>> <-> \(element)")
2022-09-04 18:09:31 +00:00
.init {
2022-04-12 13:09:14 +00:00
editedTextStrings[element.id] ?? element.string
} set: {
editedTextStrings[element.id] = $0
}
}
private func replaceElement(at id: UUID, with editedText: Binding<String>) {
// print(">>> \(identifiableElements[id].string) -> \(editedText.wrappedValue)")
guard let i = identifiableElements.firstIndex(where: {
$0.id == id
}) else {
assertionFailure("Editing removed element?")
return
}
guard allowsDuplicates || !identifiableElements.contains(where: {
$0.string == editedText.wrappedValue
}) else {
editedText.wrappedValue = identifiableElements[i].string
return
}
withAnimation {
identifiableElements[i].string = editedText.wrappedValue
}
commit()
}
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
private func onDelete(offsets: IndexSet) {
var mapped = mapping(identifiableElements)
mapped.remove(atOffsets: offsets)
identifiableElements = mapped
commit()
}
2023-03-17 20:55:47 +00:00
2023-03-19 13:41:53 +00:00
private func onMove(indexSet: IndexSet, to offset: Int) {
2022-04-12 13:09:14 +00:00
var mapped = mapping(identifiableElements)
2023-03-19 13:41:53 +00:00
mapped.move(fromOffsets: indexSet, toOffset: offset)
2022-04-12 13:09:14 +00:00
identifiableElements = mapped
commit()
}
private func commit() {
// print(">>> identifiableElements = \(identifiableElements.map { "\($0.string) (\($0.id))" })")
elements = identifiableElements.map(\.string)
}
}