1
0
mirror of git://jb55.com/damus synced 2024-09-19 19:46:51 +00:00

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

@ -17,6 +17,7 @@
4C75EFB728049D990006080F /* RelayPool.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C75EFB628049D990006080F /* RelayPool.swift */; }; 4C75EFB728049D990006080F /* RelayPool.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C75EFB628049D990006080F /* RelayPool.swift */; };
4C75EFB92804A2740006080F /* EventView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C75EFB82804A2740006080F /* EventView.swift */; }; 4C75EFB92804A2740006080F /* EventView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C75EFB82804A2740006080F /* EventView.swift */; };
4C75EFBB2804A34C0006080F /* ProofOfWork.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C75EFBA2804A34C0006080F /* ProofOfWork.swift */; }; 4C75EFBB2804A34C0006080F /* ProofOfWork.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C75EFBA2804A34C0006080F /* ProofOfWork.swift */; };
4CACA9DC280C38C000D9BBE8 /* Profiles.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CACA9DB280C38C000D9BBE8 /* Profiles.swift */; };
4CE6DEE727F7A08100C66700 /* damusApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CE6DEE627F7A08100C66700 /* damusApp.swift */; }; 4CE6DEE727F7A08100C66700 /* damusApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CE6DEE627F7A08100C66700 /* damusApp.swift */; };
4CE6DEE927F7A08100C66700 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CE6DEE827F7A08100C66700 /* ContentView.swift */; }; 4CE6DEE927F7A08100C66700 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CE6DEE827F7A08100C66700 /* ContentView.swift */; };
4CE6DEEB27F7A08200C66700 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 4CE6DEEA27F7A08200C66700 /* Assets.xcassets */; }; 4CE6DEEB27F7A08200C66700 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 4CE6DEEA27F7A08200C66700 /* Assets.xcassets */; };
@ -66,6 +67,7 @@
4C75EFB628049D990006080F /* RelayPool.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RelayPool.swift; sourceTree = "<group>"; }; 4C75EFB628049D990006080F /* RelayPool.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RelayPool.swift; sourceTree = "<group>"; };
4C75EFB82804A2740006080F /* EventView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EventView.swift; sourceTree = "<group>"; }; 4C75EFB82804A2740006080F /* EventView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EventView.swift; sourceTree = "<group>"; };
4C75EFBA2804A34C0006080F /* ProofOfWork.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProofOfWork.swift; sourceTree = "<group>"; }; 4C75EFBA2804A34C0006080F /* ProofOfWork.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProofOfWork.swift; sourceTree = "<group>"; };
4CACA9DB280C38C000D9BBE8 /* Profiles.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Profiles.swift; sourceTree = "<group>"; };
4CE6DEE327F7A08100C66700 /* damus.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = damus.app; sourceTree = BUILT_PRODUCTS_DIR; }; 4CE6DEE327F7A08100C66700 /* damus.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = damus.app; sourceTree = BUILT_PRODUCTS_DIR; };
4CE6DEE627F7A08100C66700 /* damusApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = damusApp.swift; sourceTree = "<group>"; }; 4CE6DEE627F7A08100C66700 /* damusApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = damusApp.swift; sourceTree = "<group>"; };
4CE6DEE827F7A08100C66700 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; }; 4CE6DEE827F7A08100C66700 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
@ -143,6 +145,7 @@
4C75EFBA2804A34C0006080F /* ProofOfWork.swift */, 4C75EFBA2804A34C0006080F /* ProofOfWork.swift */,
4CEE2AEC2805B22500AB5EEF /* NostrRequest.swift */, 4CEE2AEC2805B22500AB5EEF /* NostrRequest.swift */,
4CEE2AEE2805BE2500AB5EEF /* NostrTimeline.swift */, 4CEE2AEE2805BE2500AB5EEF /* NostrTimeline.swift */,
4CACA9DB280C38C000D9BBE8 /* Profiles.swift */,
); );
path = Nostr; path = Nostr;
sourceTree = "<group>"; sourceTree = "<group>";
@ -365,6 +368,7 @@
4CEE2AF7280B2DEA00AB5EEF /* ProfileName.swift in Sources */, 4CEE2AF7280B2DEA00AB5EEF /* ProfileName.swift in Sources */,
4CEE2B02280B39E800AB5EEF /* EventActionBar.swift in Sources */, 4CEE2B02280B39E800AB5EEF /* EventActionBar.swift in Sources */,
4CEE2AEF2805BE2500AB5EEF /* NostrTimeline.swift in Sources */, 4CEE2AEF2805BE2500AB5EEF /* NostrTimeline.swift in Sources */,
4CACA9DC280C38C000D9BBE8 /* Profiles.swift in Sources */,
4C75EFAF28049D350006080F /* NostrFilter.swift in Sources */, 4C75EFAF28049D350006080F /* NostrFilter.swift in Sources */,
4CE6DF1627F8DEBF00C66700 /* RelayConnection.swift in Sources */, 4CE6DF1627F8DEBF00C66700 /* RelayConnection.swift in Sources */,
4CEE2AF3280B25C500AB5EEF /* ProfilePicView.swift in Sources */, 4CEE2AF3280B25C500AB5EEF /* ProfilePicView.swift in Sources */,

View File

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

View File

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

View File

@ -15,8 +15,9 @@ struct EventDetailView: View {
@State var events: [NostrEvent] = [] @State var events: [NostrEvent] = []
@State var has_event: [String: ()] = [:] @State var has_event: [String: ()] = [:]
@EnvironmentObject var profiles: Profiles
let pool: RelayPool let pool: RelayPool
let profiles: [String: TimestampedProfile]
func unsubscribe_to_thread() { func unsubscribe_to_thread() {
print("unsubscribing from thread \(event.id) with sub_id \(sub_id)") 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 { var body: some View {
ScrollView { ScrollView {
ForEach(events, id: \.id) { ev in ForEach(events, id: \.id) { ev in
if ev.id == event.id { 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 { } else {
let evdet = EventDetailView(event: ev, pool: pool, profiles: profiles) let evdet = EventDetailView(event: ev, pool: pool)
.navigationBarTitle("Note") .navigationBarTitle("Note")
.environmentObject(profiles)
NavigationLink(destination: evdet) { 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()) .buttonStyle(PlainButtonStyle())
} }

View File

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

View File

@ -8,7 +8,7 @@
import SwiftUI import SwiftUI
func ProfileName(pubkey: String, profile: Profile?) -> some View { 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() .bold()
} }