damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | LICENSE

commit f2631bd0868ee3ccea2e6cbb651e0d36b5691667
parent f314a4163fb6189a24dbae6a379c2f303159bff6
Author: William Casarin <jb55@jb55.com>
Date:   Sun, 17 Apr 2022 05:31:38 -0700

make profiles and environment object

Signed-off-by: William Casarin <jb55@jb55.com>

Diffstat:
Mdamus.xcodeproj/project.pbxproj | 4++++
Mdamus/ContentView.swift | 19+++++++++++--------
Mdamus/Nostr/Nostr.swift | 4++++
Mdamus/Views/EventDetailView.swift | 42++++++++----------------------------------
Mdamus/Views/EventView.swift | 8++++++--
Mdamus/Views/ProfileName.swift | 2+-
6 files changed, 34 insertions(+), 45 deletions(-)

diff --git a/damus.xcodeproj/project.pbxproj b/damus.xcodeproj/project.pbxproj @@ -17,6 +17,7 @@ 4C75EFB728049D990006080F /* RelayPool.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C75EFB628049D990006080F /* RelayPool.swift */; }; 4C75EFB92804A2740006080F /* EventView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C75EFB82804A2740006080F /* EventView.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 */; }; 4CE6DEE927F7A08100C66700 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CE6DEE827F7A08100C66700 /* ContentView.swift */; }; 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>"; }; 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>"; }; + 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; }; 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>"; }; @@ -143,6 +145,7 @@ 4C75EFBA2804A34C0006080F /* ProofOfWork.swift */, 4CEE2AEC2805B22500AB5EEF /* NostrRequest.swift */, 4CEE2AEE2805BE2500AB5EEF /* NostrTimeline.swift */, + 4CACA9DB280C38C000D9BBE8 /* Profiles.swift */, ); path = Nostr; sourceTree = "<group>"; @@ -365,6 +368,7 @@ 4CEE2AF7280B2DEA00AB5EEF /* ProfileName.swift in Sources */, 4CEE2B02280B39E800AB5EEF /* EventActionBar.swift in Sources */, 4CEE2AEF2805BE2500AB5EEF /* NostrTimeline.swift in Sources */, + 4CACA9DC280C38C000D9BBE8 /* Profiles.swift in Sources */, 4C75EFAF28049D350006080F /* NostrFilter.swift in Sources */, 4CE6DF1627F8DEBF00C66700 /* RelayConnection.swift in Sources */, 4CEE2AF3280B25C500AB5EEF /* ProfilePicView.swift in Sources */, diff --git a/damus/ContentView.swift b/damus/ContentView.swift @@ -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) { diff --git a/damus/Nostr/Nostr.swift b/damus/Nostr/Nostr.swift @@ -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 { diff --git a/damus/Views/EventDetailView.swift b/damus/Views/EventDetailView.swift @@ -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()) } diff --git a/damus/Views/EventView.swift b/damus/Views/EventView.swift @@ -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) diff --git a/damus/Views/ProfileName.swift b/damus/Views/ProfileName.swift @@ -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() }