Add labels to posts
This commit is contained in:
19
CHDataManagement/Views/Posts/PageIconView.swift
Normal file
19
CHDataManagement/Views/Posts/PageIconView.swift
Normal file
@ -0,0 +1,19 @@
|
||||
import SwiftUI
|
||||
import SVGView
|
||||
|
||||
struct PageIconView: View {
|
||||
|
||||
@Environment(\.colorScheme)
|
||||
private var colorScheme
|
||||
|
||||
let icon: PageIcon
|
||||
|
||||
var body: some View {
|
||||
if colorScheme == .light {
|
||||
SVGView(string: icon.svgString)
|
||||
} else {
|
||||
SVGView(string: icon.svgString)
|
||||
.colorInvert()
|
||||
}
|
||||
}
|
||||
}
|
@ -177,6 +177,7 @@ struct LocalizedPostContentView: View {
|
||||
} else {
|
||||
TagDisplayView(tags: $tags)
|
||||
}
|
||||
PostLabelsView(post: post, other: other)
|
||||
TextEditor(text: $post.text)
|
||||
.font(.body)
|
||||
.frame(minHeight: 150)
|
||||
|
113
CHDataManagement/Views/Posts/PostLabelsView.swift
Normal file
113
CHDataManagement/Views/Posts/PostLabelsView.swift
Normal file
@ -0,0 +1,113 @@
|
||||
import SwiftUI
|
||||
|
||||
struct LabelEditingView: View {
|
||||
|
||||
@ObservedObject
|
||||
var label: ContentLabel
|
||||
|
||||
@State
|
||||
private var showIconPicker: Bool = false
|
||||
|
||||
var body: some View {
|
||||
HStack {
|
||||
Button(action: { showIconPicker = true }) {
|
||||
PageIconView(icon: label.icon)
|
||||
.frame(maxWidth: 20, maxHeight: 20)
|
||||
.contentShape(Rectangle())
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
TextField("", text: $label.value)
|
||||
.textFieldStyle(.plain)
|
||||
}
|
||||
.sheet(isPresented: $showIconPicker) {
|
||||
LabelIconSelectionView(selected: $label.icon)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private struct LabelIconSelectionView: View {
|
||||
|
||||
@Environment(\.dismiss)
|
||||
var dismiss
|
||||
|
||||
@Binding
|
||||
var selected: PageIcon
|
||||
|
||||
var body: some View {
|
||||
VStack {
|
||||
List(PageIcon.allCases, id: \.rawValue) { icon in
|
||||
HStack {
|
||||
Image(systemSymbol: selected == icon ? .checkmarkCircleFill : .circle)
|
||||
PageIconView(icon: icon)
|
||||
.frame(maxWidth: 20, maxHeight: 20)
|
||||
Text(icon.name)
|
||||
Spacer()
|
||||
}
|
||||
.contentShape(Rectangle())
|
||||
.onTapGesture {
|
||||
selected = icon
|
||||
dismiss()
|
||||
}
|
||||
}.frame(minHeight: 300)
|
||||
Button("Done") {
|
||||
dismiss()
|
||||
}
|
||||
}.padding()
|
||||
}
|
||||
}
|
||||
|
||||
struct PostLabelsView: View {
|
||||
|
||||
@ObservedObject
|
||||
var post: LocalizedPost
|
||||
|
||||
@ObservedObject
|
||||
var other: LocalizedPost
|
||||
|
||||
@Environment(\.colorScheme)
|
||||
var colorScheme
|
||||
|
||||
var body: some View {
|
||||
ScrollView(.horizontal) {
|
||||
HStack(spacing: 5) {
|
||||
Text("Labels")
|
||||
.font(.headline)
|
||||
ForEach(post.labels, id: \.icon) { label in
|
||||
HStack {
|
||||
Button(action: { remove(label) }) {
|
||||
Image(systemSymbol: .minusCircleFill)
|
||||
.foregroundStyle(.red)
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
LabelEditingView(label: label)
|
||||
}
|
||||
.padding(.vertical, 2)
|
||||
.padding(.horizontal, 8)
|
||||
.background(colorScheme == .light ? Color.white : Color.black)
|
||||
.cornerRadius(8)
|
||||
}
|
||||
Button("Add", action: addLabel)
|
||||
if !other.labels.isEmpty {
|
||||
Button("Transfer") {
|
||||
post.labels = other.labels.map {
|
||||
// Copy instead of reference
|
||||
ContentLabel(icon: $0.icon, value: $0.value)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.padding(.vertical, 2)
|
||||
}
|
||||
}
|
||||
|
||||
func addLabel() {
|
||||
post.labels.append(.init(icon: .clockFill, value: "Value"))
|
||||
}
|
||||
|
||||
func remove(_ label: ContentLabel) {
|
||||
guard let index = post.labels.firstIndex(of: label) else {
|
||||
return
|
||||
}
|
||||
post.labels.remove(at: index)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user