make profiles and environment object

Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
William Casarin
2022-04-17 05:31:38 -07:00
parent f314a4163f
commit f2631bd086
6 changed files with 34 additions and 45 deletions

View File

@@ -34,7 +34,7 @@ struct ContentView: View {
@State var status: String = "Not connected"
@State var active_sheet: Sheets? = nil
@State var events: [NostrEvent] = []
@State var profiles: [String: TimestampedProfile] = [:]
@State var profiles: Profiles = Profiles()
@State var friends: [String: ()] = [:]
@State var has_events: [String: ()] = [:]
@State var profile_count: Int = 0
@@ -50,16 +50,17 @@ struct ContentView: View {
ScrollView {
ForEach(self.events, id: \.id) { (ev: NostrEvent) in
if ev.is_local && timeline == .debug || (timeline == .global && !ev.is_local) || (timeline == .friends && is_friend(ev.pubkey)) {
let profile: Profile? = profiles[ev.pubkey]?.profile
let evdet = EventDetailView(event: ev, pool: pool, profiles: profiles)
let evdet = EventDetailView(event: ev, pool: pool)
.navigationBarTitle("Note")
.environmentObject(profiles)
NavigationLink(destination: evdet) {
EventView(event: ev, profile: profile, highlighted: false)
EventView(event: ev, highlighted: false)
}
.buttonStyle(PlainButtonStyle())
}
}
}
.environmentObject(profiles)
}
func TimelineButton(timeline: Timeline, img: String) -> some View {
@@ -134,11 +135,12 @@ struct ContentView: View {
func add_relay(_ pool: RelayPool, _ relay: String) {
//add_rw_relay(pool, "wss://nostr-pub.wellorder.net")
add_rw_relay(pool, "wss://\(relay)")
let wssrelay = "wss://\(relay)"
add_rw_relay(pool, wssrelay)
let profile = Profile(name: relay, about: nil, picture: nil)
let ts = Int64(Date().timeIntervalSince1970)
let tsprofile = TimestampedProfile(profile: profile, timestamp: ts)
self.profiles["wss://\(relay)"] = tsprofile
self.profiles.add(id: wssrelay, profile: tsprofile)
}
func connect() {
@@ -172,14 +174,15 @@ struct ContentView: View {
return
}
if let mprof = self.profiles[ev.pubkey] {
if let mprof = self.profiles.lookup_with_timestamp(id: ev.pubkey) {
if mprof.timestamp > ev.created_at {
// skip if we already have an newer profile
return
}
}
self.profiles[ev.pubkey] = TimestampedProfile(profile: profile, timestamp: ev.created_at)
let tprof = TimestampedProfile(profile: profile, timestamp: ev.created_at)
self.profiles.add(id: ev.pubkey, profile: tprof)
}
func send_filters(relay_id: String) {

View File

@@ -12,6 +12,10 @@ struct Profile: Decodable {
let name: String?
let about: String?
let picture: String?
static func displayName(profile: Profile?, pubkey: String) -> String {
return profile?.name ?? String(pubkey.prefix(16))
}
}
enum NostrKind: Int {

View File

@@ -14,9 +14,10 @@ struct EventDetailView: View {
@State var events: [NostrEvent] = []
@State var has_event: [String: ()] = [:]
@EnvironmentObject var profiles: Profiles
let pool: RelayPool
let profiles: [String: TimestampedProfile]
func unsubscribe_to_thread() {
print("unsubscribing from thread \(event.id) with sub_id \(sub_id)")
@@ -59,45 +60,18 @@ struct EventDetailView: View {
}
}
var NoteBody: some View {
HStack {
let profile = profiles[event.pubkey]?.profile
VStack {
ProfilePicView(picture: profile?.picture, size: 64, highlighted: false)
Spacer()
}
VStack {
HStack {
ProfileName(pubkey: event.pubkey, profile: profile)
Text("\(format_relative_time(event.created_at))")
.foregroundColor(.gray)
Spacer()
PowView(event.pow)
}
Text(event.content)
.frame(maxWidth: .infinity, alignment: .leading)
EventActionBar(event: event)
Divider()
.padding([.bottom], 10)
}
}
}
var body: some View {
ScrollView {
ForEach(events, id: \.id) { ev in
if ev.id == event.id {
EventView(event: ev, profile: self.profiles[ev.pubkey]?.profile, highlighted: ev.id == event.id)
EventView(event: ev, highlighted: ev.id == event.id)
} else {
let evdet = EventDetailView(event: ev, pool: pool, profiles: profiles)
let evdet = EventDetailView(event: ev, pool: pool)
.navigationBarTitle("Note")
.environmentObject(profiles)
NavigationLink(destination: evdet) {
EventView(event: ev, profile: self.profiles[ev.pubkey]?.profile, highlighted: ev.id == event.id)
EventView(event: ev, highlighted: ev.id == event.id)
}
.buttonStyle(PlainButtonStyle())
}

View File

@@ -11,10 +11,12 @@ import CachedAsyncImage
struct EventView: View {
let event: NostrEvent
let profile: Profile?
let highlighted: Bool
@EnvironmentObject var profiles: Profiles
var body: some View {
let profile = profiles.lookup(id: event.pubkey)
HStack {
VStack {
ProfilePicView(picture: profile?.picture, size: 64, highlighted: highlighted)
@@ -32,12 +34,14 @@ struct EventView: View {
PowView(event.pow)
}
}
Text(event.content)
.frame(maxWidth: .infinity, alignment: .leading)
.textSelection(.enabled)
Spacer()
EventActionBar(event: event)
EventActionBar(event: event, profiles: profiles)
Divider()
.padding([.top], 4)

View File

@@ -8,7 +8,7 @@
import SwiftUI
func ProfileName(pubkey: String, profile: Profile?) -> some View {
Text(String(profile?.name ?? String(pubkey.prefix(16))))
Text(String(Profile.displayName(profile: profile, pubkey: pubkey)))
.bold()
}