Browse Source

post view

Signed-off-by: William Casarin <jb55@jb55.com>
profiles-everywhere
William Casarin 3 years ago
parent
commit
a32243ab15
  1. 2
      .gitignore
  2. 17
      damus/ContentView.swift
  3. 78
      damus/Views/PostView.swift

2
.gitignore

@ -0,0 +1,2 @@
xcuserdata
Preview\ Content

17
damus/ContentView.swift

@ -43,6 +43,8 @@ struct ContentView: View {
@State var sub_id: String? = nil @State var sub_id: String? = nil
@State var active_sheet: Sheets? = nil @State var active_sheet: Sheets? = nil
@State var events: [NostrEvent] = [] @State var events: [NostrEvent] = []
@State var has_events: [String: Bool] = [:]
@State var loading: Bool = true
@State var connection: NostrConnection? = nil @State var connection: NostrConnection? = nil
var MainContent: some View { var MainContent: some View {
@ -56,6 +58,7 @@ struct ContentView: View {
var body: some View { var body: some View {
ZStack { ZStack {
MainContent MainContent
.padding()
VStack { VStack {
Spacer() Spacer()
@ -76,6 +79,10 @@ struct ContentView: View {
PostView() PostView()
} }
} }
.onReceive(NotificationCenter.default.publisher(for: .post)) { obj in
let post = obj.object as! NostrPost
print("post \(post.content)")
}
} }
func connect() { func connect() {
@ -97,6 +104,7 @@ struct ContentView: View {
if self.sub_id != sub_id { if self.sub_id != sub_id {
self.sub_id = sub_id self.sub_id = sub_id
} }
print("subscribing to \(sub_id)")
self.connection?.send(filter, sub_id: sub_id) self.connection?.send(filter, sub_id: sub_id)
case .cancelled: case .cancelled:
self.connection?.connect() self.connection?.connect()
@ -108,11 +116,14 @@ struct ContentView: View {
case .nostr_event(let ev): case .nostr_event(let ev):
switch ev { switch ev {
case .event(_, let ev): case .event(_, let ev):
if self.loading {
self.loading = false
}
self.sub_id = sub_id 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) self.events.append(ev)
} }
print(ev)
case .notice(let msg): case .notice(let msg):
print(msg) print(msg)
} }
@ -130,7 +141,7 @@ func PostButton(action: @escaping () -> ()) -> some View {
return Button(action: action, label: { return Button(action: action, label: {
Text("+") Text("+")
.font(.system(.largeTitle)) .font(.system(.largeTitle))
.frame(width: 67, height: 60) .frame(width: 57, height: 50)
.foregroundColor(Color.white) .foregroundColor(Color.white)
.padding(.bottom, 7) .padding(.bottom, 7)
}) })

78
damus/Views/PostView.swift

@ -7,14 +7,80 @@
import SwiftUI import SwiftUI
struct PostView: View { extension Notification.Name {
var body: some View { static var post: Notification.Name {
Text("New post") return Notification.Name("send post")
} }
} }
struct Post_Previews: PreviewProvider { struct NostrPost {
static var previews: some View { let content: String
PostView() }
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()
} }
} }

Loading…
Cancel
Save