Repository navigation
Expand file tree
/
Copy pathAddToNoteListSheet.swift
More file actions
160 lines (147 loc) · 5.79 KB
/
Copy pathAddToNoteListSheet.swift
File metadata and controls
160 lines (147 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import SwiftUI
/// Sheet shown from the post bookmark icon. Lets the user check-toggle which
/// note lists contain the given event, and offers a "Quick create" row to
/// spin up a new list and add the note in one tap.
struct AddToNoteListSheet: View {
let keypair: Keypair
let event: NostrEvent
@Environment(\.dismiss) private var dismiss
@State private var repo = NoteListRepository.shared
@State private var showCreate = false
@State private var newListName = ""
@State private var isPrivate = false
private var noteId: String { event.id.lowercased() }
var body: some View {
ScrollView {
VStack(alignment: .leading, spacing: 16) {
Text("Bookmark to lists")
.font(.headline)
privacyToggle
if repo.lists.isEmpty {
emptyState
} else {
listsSection
}
createButton
Spacer(minLength: 24)
}
.padding(20)
}
.background(Color.wispBackground.ignoresSafeArea())
.toolbar {
ToolbarItem(placement: .topBarTrailing) {
Button("Done") { dismiss() }
}
}
.alert("New note list", isPresented: $showCreate) {
TextField("List name", text: $newListName)
Button("Cancel", role: .cancel) {}
Button("Create") {
let trimmed = newListName.trimmingCharacters(in: .whitespacesAndNewlines)
guard !trimmed.isEmpty else { return }
if let created = repo.createList(name: trimmed, keypair: keypair) {
repo.addNote(noteId, to: created.dTag, isPrivate: isPrivate, keypair: keypair)
}
}
} message: {
Text("Give your new list a name. The current note will be added automatically.")
}
}
private var privacyToggle: some View {
HStack(spacing: 8) {
Image(systemName: isPrivate ? "lock.fill" : "lock.open")
.foregroundStyle(isPrivate ? Color.wispPrimary : .secondary)
Text(isPrivate ? "Private (encrypted)" : "Public")
.font(.subheadline)
.foregroundStyle(isPrivate ? .primary : .secondary)
Spacer()
Toggle("", isOn: $isPrivate)
.labelsHidden()
}
.padding(.horizontal, 12)
.padding(.vertical, 8)
.background(Color.wispSurfaceVariant.opacity(0.25),
in: RoundedRectangle(cornerRadius: 10))
}
private var emptyState: some View {
VStack(alignment: .leading, spacing: 8) {
Text("You don't have any note lists yet.")
.font(.subheadline)
.foregroundStyle(.secondary)
Text("Create one to bookmark this post.")
.font(.caption)
.foregroundStyle(.tertiary)
}
.frame(maxWidth: .infinity, alignment: .leading)
.padding(12)
.background(Color.wispSurfaceVariant.opacity(0.4),
in: RoundedRectangle(cornerRadius: 12))
}
private var listsSection: some View {
VStack(spacing: 0) {
ForEach(repo.lists) { list in
listRow(list)
if list.id != repo.lists.last?.id {
Divider().overlay(Color.wispSurfaceVariant.opacity(0.4))
}
}
}
.background(Color.wispSurfaceVariant.opacity(0.25),
in: RoundedRectangle(cornerRadius: 12))
}
private func listRow(_ list: NoteList) -> some View {
let alreadyIn = list.publicNotes.contains(noteId) || list.privateNotes.contains(noteId)
return Button {
if alreadyIn {
repo.removeNote(noteId, from: list.dTag, keypair: keypair)
} else {
repo.addNote(noteId, to: list.dTag, isPrivate: isPrivate, keypair: keypair)
}
} label: {
HStack(spacing: 12) {
Image(systemName: alreadyIn ? "checkmark.square.fill" : "square")
.font(.system(size: 18))
.foregroundStyle(alreadyIn ? Color.wispPrimary : .secondary)
VStack(alignment: .leading, spacing: 2) {
HStack(spacing: 4) {
Text(list.name)
.font(.subheadline.weight(.medium))
if !list.privateNotes.isEmpty {
Image(systemName: "lock.fill")
.font(.system(size: 10))
.foregroundStyle(.secondary)
}
}
Text("\(list.allNotes.count) note\(list.allNotes.count == 1 ? "" : "s")")
.font(.caption2)
.foregroundStyle(.secondary)
}
Spacer()
}
.padding(.horizontal, 12)
.padding(.vertical, 10)
.contentShape(Rectangle())
}
.buttonStyle(.plain)
}
private var createButton: some View {
Button {
newListName = ""
showCreate = true
} label: {
HStack(spacing: 12) {
Image(systemName: "plus.circle.fill")
.font(.system(size: 18))
.foregroundStyle(Color.wispPrimary)
Text("Create new list with this note")
.font(.subheadline.weight(.semibold))
.foregroundStyle(.primary)
Spacer()
}
.padding(12)
.background(Color.wispSurfaceVariant.opacity(0.4),
in: RoundedRectangle(cornerRadius: 12))
}
.buttonStyle(.plain)
}
}