| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- //
- // TagBarView.swift
- // Todos
- //
- // Created by Sam Jaffe on 2/28/26.
- //
- import SwiftUI
- import SwiftData
- struct TagBarView: View {
- @Environment(\.modelContext) private var modelContext
- @Binding var task: Task
- @AppStorage(UserDefaultsKeys.UrlHints) var allHints = CodableArray<URLHint>()
- @State private var active: String = ""
- @FocusState private var isFocused: Bool
- var body: some View {
- HStack {
- ForEach($task.tags) { tag in
- let url = allHints.filter({ $0.matches(tag.wrappedValue) })
- .first?.url(tag.wrappedValue)
- if url != nil {
- Link(destination: url!) {
- Label("", systemImage: "link")
- }
- .padding(.leading, -8)
- .padding(.trailing, -10)
- }
- TextField("", text: tag.id)
- .focused($isFocused)
- .onChange(of: isFocused) {
- task.tags.removeAll(where: { $0.id.isEmpty })
- }
- }
- .scaledToFit()
- TextField("Tag", text: $active)
- .onSubmit(addItem)
- }
- }
- private func addItem() {
- if !active.isEmpty && !task.tags.contains(where: { $0.like(active) }) {
- let newTag = Tag(id: active, parent: task)
- modelContext.insert(newTag)
- task.tags.append(newTag)
- }
- active = ""
- }
- }
- #Preview {
- @Previewable @State var task = Task()
- TagBarView(task: $task)
- }
|