1
0
mirror of git://jb55.com/damus synced 2024-09-18 19:23:49 +00:00

post view

Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
William Casarin 2022-04-04 10:20:39 -07:00
parent dc376a4a72
commit a32243ab15
3 changed files with 88 additions and 9 deletions

2
.gitignore vendored Normal file
View File

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

View File

@ -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)
})

View File

@ -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()
}
}