Files
damus/damus/Views/PostView.swift

95 lines
2.5 KiB
Swift

//
// Post.swift
// damus
//
// Created by William Casarin on 2022-04-03.
//
import SwiftUI
enum NostrPostResult {
case post(NostrPost)
case cancel
}
let POST_PLACEHOLDER = NSLocalizedString("Type your post here...", comment: "Text box prompt to ask user to type their post.")
struct PostView: View {
@State var post: String = ""
let replying_to: NostrEvent?
@FocusState var focus: Bool
let references: [ReferencedId]
@Environment(\.presentationMode) var presentationMode
enum FocusField: Hashable {
case post
}
func cancel() {
NotificationCenter.default.post(name: .post, object: NostrPostResult.cancel)
dismiss()
}
func dismiss() {
self.presentationMode.wrappedValue.dismiss()
}
func send_post() {
var kind: NostrKind = .text
if replying_to?.known_kind == .chat {
kind = .chat
}
let content = self.post.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
let new_post = NostrPost(content: content, references: references, kind: kind)
NotificationCenter.default.post(name: .post, object: NostrPostResult.post(new_post))
dismiss()
}
var is_post_empty: Bool {
return post.allSatisfy { $0.isWhitespace }
}
var body: some View {
VStack {
HStack {
Button(NSLocalizedString("Cancel", comment: "Button to cancel out of posting a note.")) {
self.cancel()
}
.foregroundColor(.primary)
Spacer()
if !is_post_empty {
Button(NSLocalizedString("Post", comment: "Button to post a note.")) {
self.send_post()
}
}
}
.padding([.top, .bottom], 4)
ZStack(alignment: .topLeading) {
TextEditor(text: $post)
.focused($focus)
.textInputAutocapitalization(.sentences)
if post.isEmpty {
Text(POST_PLACEHOLDER)
.padding(.top, 8)
.padding(.leading, 4)
.foregroundColor(Color(uiColor: .placeholderText))
.allowsHitTesting(false)
}
}
}
.onAppear() {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
self.focus = true
}
}
.padding()
}
}