From a32243ab15810b715d215f6e4609487aaf3c9e69 Mon Sep 17 00:00:00 2001 From: William Casarin Date: Mon, 4 Apr 2022 10:20:39 -0700 Subject: [PATCH] post view Signed-off-by: William Casarin --- .gitignore | 2 + damus/ContentView.swift | 17 +++++++-- damus/Views/PostView.swift | 78 +++++++++++++++++++++++++++++++++++--- 3 files changed, 88 insertions(+), 9 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5f129a6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +xcuserdata +Preview\ Content diff --git a/damus/ContentView.swift b/damus/ContentView.swift index 983c306..e5586ad 100644 --- a/damus/ContentView.swift +++ b/damus/ContentView.swift @@ -43,6 +43,8 @@ struct ContentView: View { @State var sub_id: String? = nil @State var active_sheet: Sheets? = nil @State var events: [NostrEvent] = [] + @State var has_events: [String: Bool] = [:] + @State var loading: Bool = true @State var connection: NostrConnection? = nil var MainContent: some View { @@ -56,6 +58,7 @@ struct ContentView: View { var body: some View { ZStack { MainContent + .padding() VStack { Spacer() @@ -76,6 +79,10 @@ struct ContentView: View { PostView() } } + .onReceive(NotificationCenter.default.publisher(for: .post)) { obj in + let post = obj.object as! NostrPost + print("post \(post.content)") + } } func connect() { @@ -97,6 +104,7 @@ struct ContentView: View { if self.sub_id != sub_id { self.sub_id = sub_id } + print("subscribing to \(sub_id)") self.connection?.send(filter, sub_id: sub_id) case .cancelled: self.connection?.connect() @@ -108,11 +116,14 @@ struct ContentView: View { case .nostr_event(let ev): switch ev { case .event(_, let ev): + if self.loading { + self.loading = false + } self.sub_id = sub_id - if ev.kind == 1 { + if ev.kind == 1 && !(has_events[ev.id] ?? false) { + has_events[ev.id] = true self.events.append(ev) } - print(ev) case .notice(let msg): print(msg) } @@ -130,7 +141,7 @@ func PostButton(action: @escaping () -> ()) -> some View { return Button(action: action, label: { Text("+") .font(.system(.largeTitle)) - .frame(width: 67, height: 60) + .frame(width: 57, height: 50) .foregroundColor(Color.white) .padding(.bottom, 7) }) diff --git a/damus/Views/PostView.swift b/damus/Views/PostView.swift index fbc961a..3538f1e 100644 --- a/damus/Views/PostView.swift +++ b/damus/Views/PostView.swift @@ -7,14 +7,80 @@ import SwiftUI -struct PostView: View { - var body: some View { - Text("New post") +extension Notification.Name { + static var post: Notification.Name { + return Notification.Name("send post") } } -struct Post_Previews: PreviewProvider { - static var previews: some View { - PostView() +struct NostrPost { + let content: String +} + + +struct PostView: View { + @State var post: String = "" + @FocusState var focus: Bool + + @Environment(\.presentationMode) var presmode + + enum FocusField: Hashable { + case post + } + + func dismiss() { + presmode.wrappedValue.dismiss() + } + + func send_post() { + let new_post = NostrPost(content: self.post) + NotificationCenter.default.post(name: .post, object: new_post) + dismiss() + } + + var body: some View { + VStack { + HStack { + Button("Cancel") { + self.dismiss() + } + .foregroundColor(.primary) + + Spacer() + + Button("Post") { + self.send_post() + } + } + .padding([.top, .bottom], 4) + + HStack(alignment: .top) { + ZStack(alignment: .leading) { + TextEditor(text: $post) + .focused($focus) + + if self.post == "" { + VStack { + Text("What's happening?") + .foregroundColor(.gray) + .padding(6) + Spacer() + } + } + } + + + Spacer() + } + + Spacer() + } + .onAppear() { + DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { + self.focus = true + } + } + .padding() } } +