From 5020ae7e3c0b154f0a68ac2bb386786a15acd3d5 Mon Sep 17 00:00:00 2001 From: James Carucci Date: Mon, 8 Aug 2022 08:53:23 -0400 Subject: [PATCH] Add default placeholder for post UI :) Closes: #18 Changlog-Added: Added post placeholder text (thanks jcarucci27) Signed-off-by: William Casarin --- damus/Views/PostView.swift | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/damus/Views/PostView.swift b/damus/Views/PostView.swift index a52be397..9e9c909d 100644 --- a/damus/Views/PostView.swift +++ b/damus/Views/PostView.swift @@ -12,11 +12,15 @@ enum NostrPostResult { case cancel } +let POST_PLACEHOLDER = "Type your post here..." + struct PostView: View { - @State var post: String = "" + @State var post: String = POST_PLACEHOLDER + @State var new: Bool = true + @FocusState var focus: Bool let references: [ReferencedId] - + @Environment(\.presentationMode) var presentationMode enum FocusField: Hashable { @@ -27,7 +31,7 @@ struct PostView: View { NotificationCenter.default.post(name: .post, object: NostrPostResult.cancel) dismiss() } - + func dismiss() { self.presentationMode.wrappedValue.dismiss() } @@ -37,7 +41,7 @@ struct PostView: View { NotificationCenter.default.post(name: .post, object: NostrPostResult.post(new_post)) dismiss() } - + var is_post_empty: Bool { return post.allSatisfy { $0.isWhitespace } } @@ -60,8 +64,17 @@ struct PostView: View { } .padding([.top, .bottom], 4) + TextEditor(text: $post) + .foregroundColor(self.post == POST_PLACEHOLDER ? .gray : .primary) .focused($focus) + .onTapGesture { + handle_post_placeholder() + } + .onChange(of: post) { value in + handle_post_placeholder() + } + } .onAppear() { DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { @@ -70,5 +83,14 @@ struct PostView: View { } .padding() } + + func handle_post_placeholder() { + guard new else { + return + } + + new = false + post = post.replacingOccurrences(of: POST_PLACEHOLDER, with: "") + } }