Improve content saving, label editing

This commit is contained in:
Christoph Hagen
2025-05-02 22:11:43 +02:00
parent fea06a93b7
commit 1f4f32c9af
15 changed files with 274 additions and 150 deletions

View File

@ -0,0 +1,65 @@
import SwiftUI
struct LabelEditingView: View {
@Binding
var label: ContentLabel
@State
private var showIconPicker: Bool = false
let scale: CGFloat
init(label: Binding<ContentLabel>, scale: CGFloat = 1.0) {
self._label = label
self.scale = scale
}
var body: some View {
HStack {
Button(action: { showIconPicker = true }) {
PageIconView(icon: label.icon)
.frame(maxWidth: 16, maxHeight: 16)
.scaleEffect(scale)
.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()
}
}

View File

@ -1,61 +1,5 @@
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
@ -67,26 +11,38 @@ struct PostLabelsView: View {
@Environment(\.colorScheme)
var colorScheme
@State
private var showLabelEditor: Bool = false
var body: some View {
ScrollView(.horizontal) {
HStack(spacing: 5) {
Text("Labels")
.font(.headline)
ForEach(post.labels, id: \.icon) { label in
if post.labels.isEmpty {
Text("Labels")
.font(.headline)
}
ForEach(post.labels) { label in
HStack {
Button(action: { remove(label) }) {
Image(systemSymbol: .minusCircleFill)
.foregroundStyle(.red)
}
.buttonStyle(.plain)
LabelEditingView(label: label)
PageIconView(icon: label.icon)
.frame(maxWidth: 16, maxHeight: 16)
.scaleEffect(25/16)
Text(label.value)
}
.padding(.vertical, 2)
.padding(.horizontal, 8)
.background(colorScheme == .light ? Color.white : Color.black)
.cornerRadius(8)
}
Button("Add", action: addLabel)
Button(action: { showLabelEditor = true }) {
Image(systemSymbol: .squareAndPencilCircleFill)
.resizable()
.aspectRatio(1, contentMode: .fit)
.frame(height: 22)
.foregroundColor(Color.gray)
.background(Circle()
.fill(Color.white)
.padding(1))
}.buttonStyle(.plain)
if !other.labels.isEmpty {
Button("Transfer") {
post.labels = other.labels.map {
@ -95,9 +51,24 @@ struct PostLabelsView: View {
}
}
}
if !post.labels.isEmpty {
Button("Copy") {
var command = "```labels"
for label in post.labels {
command += "\n\(label.icon.rawValue): \(label.value)"
}
command += "\n```"
let pasteboard = NSPasteboard.general
pasteboard.clearContents()
pasteboard.setString(command, forType: .string)
}
}
}
.padding(.vertical, 2)
}
.sheet(isPresented: $showLabelEditor) {
LabelModificationView(labels: $post.labels)
}
}
func addLabel() {
@ -111,3 +82,23 @@ struct PostLabelsView: View {
post.labels.remove(at: index)
}
}
private struct LabelModificationView: View {
@Environment(\.dismiss)
private var dismiss
@Binding
var labels: [ContentLabel]
var body: some View {
VStack {
Text("Labels")
.font(.title)
LabelCreationView(labels: $labels)
Button("Save") {
dismiss()
}
}.padding()
}
}