insert sort, profile updates revamp

Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
William Casarin
2022-05-09 10:33:03 -07:00
parent 0ab1494b1e
commit 6ac4214be7
26 changed files with 250 additions and 95 deletions

View File

@@ -14,7 +14,6 @@ struct ChatView: View {
let damus: DamusState
@EnvironmentObject var profiles: Profiles
@EnvironmentObject var thread: ThreadModel
var just_started: Bool {
@@ -72,7 +71,7 @@ struct ChatView: View {
}
var ReplyDescription: some View {
Text("\(reply_desc(profiles: profiles, event: event))")
Text("\(reply_desc(profiles: damus.profiles, event: event))")
.font(.footnote)
.foregroundColor(.gray)
.frame(alignment: .leading)
@@ -84,8 +83,7 @@ struct ChatView: View {
HStack {
VStack {
if is_active || just_started {
ProfilePicView(pubkey: event.pubkey, size: 32, highlight: is_active ? .main : .none, image_cache: damus.image_cache)
.environmentObject(profiles)
ProfilePicView(pubkey: event.pubkey, size: 32, highlight: is_active ? .main : .none, image_cache: damus.image_cache, profiles: damus.profiles)
}
Spacer()
@@ -96,7 +94,7 @@ struct ChatView: View {
VStack(alignment: .leading) {
if just_started {
HStack {
ProfileName(pubkey: event.pubkey, profile: profiles.lookup(id: event.pubkey))
ProfileName(pubkey: event.pubkey, profile: damus.profiles.lookup(id: event.pubkey))
.foregroundColor(colorScheme == .dark ? id_to_color(event.pubkey) : Color.black)
//.shadow(color: Color.black, radius: 2)
Text("\(format_relative_time(event.created_at))")
@@ -106,21 +104,20 @@ struct ChatView: View {
if let ref_id = thread.replies.lookup(event.id) {
if !is_reply_to_prev() {
ReplyQuoteView(quoter: event, event_id: ref_id, image_cache: damus.image_cache)
ReplyQuoteView(quoter: event, event_id: ref_id, image_cache: damus.image_cache, profiles: damus.profiles)
.environmentObject(thread)
.environmentObject(profiles)
ReplyDescription
}
}
NoteContentView(event: event, profiles: profiles, content: event.content)
NoteContentView(event: event, profiles: damus.profiles, content: event.content)
if is_active || next_ev == nil || next_ev!.pubkey != event.pubkey {
EventActionBar(event: event,
our_pubkey: damus.pubkey,
profiles: damus.profiles,
bar: make_actionbar_model(ev: event, counter: damus.likes)
)
.environmentObject(profiles)
}
//Spacer()

View File

@@ -21,7 +21,7 @@ struct EventActionBar: View {
let event: NostrEvent
let our_pubkey: String
@State var sheet: ActionBarSheet? = nil
@EnvironmentObject var profiles: Profiles
let profiles: Profiles
@StateObject var bar: ActionBarModel
var body: some View {

View File

@@ -36,8 +36,6 @@ struct EventDetailView: View {
@StateObject var thread: ThreadModel
@State var collapsed: Bool = true
@EnvironmentObject var profiles: Profiles
func toggle_collapse_thread(scroller: ScrollViewProxy, id mid: String?, animate: Bool = true, anchor: UnitPoint = .center) {
self.collapsed = !self.collapsed

View File

@@ -42,19 +42,16 @@ struct EventView: View {
let has_action_bar: Bool
let damus: DamusState
@EnvironmentObject var profiles: Profiles
@EnvironmentObject var action_bar: ActionBarModel
var body: some View {
let profile = profiles.lookup(id: event.pubkey)
let profile = damus.profiles.lookup(id: event.pubkey)
HStack {
VStack {
let pv = ProfileView(damus: damus, profile: ProfileModel(pubkey: event.pubkey, damus: damus))
.environmentObject(profiles)
NavigationLink(destination: pv) {
ProfilePicView(pubkey: event.pubkey, size: PFP_SIZE!, highlight: highlight, image_cache: damus.image_cache)
.environmentObject(profiles)
ProfilePicView(pubkey: event.pubkey, size: PFP_SIZE!, highlight: highlight, image_cache: damus.image_cache, profiles: damus.profiles)
}
Spacer()
@@ -69,21 +66,20 @@ struct EventView: View {
}
if event.is_reply {
Text("\(reply_desc(profiles: profiles, event: event))")
Text("\(reply_desc(profiles: damus.profiles, event: event))")
.font(.footnote)
.foregroundColor(.gray)
.frame(maxWidth: .infinity, alignment: .leading)
}
NoteContentView(event: event, profiles: profiles, content: event.content)
NoteContentView(event: event, profiles: damus.profiles, content: event.content)
.frame(maxWidth: .infinity, alignment: .leading)
.textSelection(.enabled)
Spacer()
if has_action_bar {
EventActionBar(event: event, our_pubkey: damus.pubkey, bar: make_actionbar_model(ev: event, counter: damus.likes))
.environmentObject(profiles)
EventActionBar(event: event, our_pubkey: damus.pubkey, profiles: damus.profiles, bar: make_actionbar_model(ev: event, counter: damus.likes))
}
Divider()

View File

@@ -9,14 +9,12 @@ import SwiftUI
struct MentionView: View {
let mention: Mention
@EnvironmentObject var profiles: Profiles
let profiles: Profiles
var body: some View {
switch mention.type {
case .pubkey:
PubkeyView(pubkey: mention.ref.ref_id, relay: mention.ref.relay_id)
.environmentObject(profiles)
case .event:
Text("< e >")
//EventBlockView(pubkey: mention.ref.ref_id, relay: mention.ref.relay_id)

View File

@@ -43,7 +43,7 @@ struct NoteContentView: View {
.onAppear() {
self.content = render_note_content(ev: event, profiles: profiles)
}
.onReceive(handle_notify(.profile_update)) { notif in
.onReceive(handle_notify(.profile_updated)) { notif in
let profile = notif.object as! ProfileUpdate
for block in event.blocks {
switch block {

View File

@@ -7,9 +7,24 @@
import SwiftUI
func ProfileName(pubkey: String, profile: Profile?) -> some View {
Text(String(Profile.displayName(profile: profile, pubkey: pubkey)))
//.foregroundColor(hex_to_rgb(pubkey))
.bold()
struct ProfileName: View {
let pubkey: String
let profile: Profile?
@State var display_name: String?
var body: some View {
Text(String(display_name ?? Profile.displayName(profile: profile, pubkey: pubkey)))
//.foregroundColor(hex_to_rgb(pubkey))
.bold()
.onReceive(handle_notify(.profile_updated)) { notif in
let update = notif.object as! ProfileUpdate
if update.pubkey != pubkey {
return
}
display_name = Profile.displayName(profile: update.profile, pubkey: pubkey)
}
}
}

View File

@@ -37,11 +37,11 @@ struct ProfilePicView: View {
let size: CGFloat
let highlight: Highlight
let image_cache: ImageCache
let profiles: Profiles
@State var picture: String? = nil
@State var img: Image? = nil
@EnvironmentObject var profiles: Profiles
var PlaceholderColor: Color {
return id_to_color(pubkey)
}
@@ -76,7 +76,7 @@ struct ProfilePicView: View {
var MainContent: some View {
Group {
let picture = profiles.lookup(id: pubkey)?.picture
let picture = picture ?? profiles.lookup(id: pubkey)?.picture
if let pic_url = picture.flatMap { URL(string: $0) } {
ProfilePic(pic_url)
} else {
@@ -87,6 +87,16 @@ struct ProfilePicView: View {
var body: some View {
MainContent
.onReceive(handle_notify(.profile_updated)) { notif in
let updated = notif.object as! ProfileUpdate
if updated.pubkey != pubkey {
return
}
if updated.profile.picture != picture {
picture = updated.profile.picture
}
}
}
}

View File

@@ -19,14 +19,12 @@ struct ProfileView: View {
@StateObject var profile: ProfileModel
//@EnvironmentObject var profile: ProfileModel
@EnvironmentObject var profiles: Profiles
var TopSection: some View {
VStack(alignment: .leading) {
let data = profiles.lookup(id: profile.pubkey)
let data = damus.profiles.lookup(id: profile.pubkey)
HStack(alignment: .top) {
ProfilePicView(pubkey: profile.pubkey, size: PFP_SIZE!, highlight: .custom(Color.black, 2), image_cache: damus.image_cache)
.environmentObject(profiles)
ProfilePicView(pubkey: profile.pubkey, size: PFP_SIZE!, highlight: .custom(Color.black, 2), image_cache: damus.image_cache, profiles: damus.profiles)
Spacer()
@@ -56,7 +54,6 @@ struct ProfileView: View {
Divider()
InnerTimelineView(events: $profile.events, damus: damus)
.environmentObject(profiles)
}
.frame(maxHeight: .infinity, alignment: .topLeading)
}

View File

@@ -11,8 +11,8 @@ struct ReplyQuoteView: View {
let quoter: NostrEvent
let event_id: String
let image_cache: ImageCache
let profiles: Profiles
@EnvironmentObject var profiles: Profiles
@EnvironmentObject var thread: ThreadModel
func MainContent(event: NostrEvent) -> some View {
@@ -23,8 +23,7 @@ struct ReplyQuoteView: View {
VStack(alignment: .leading) {
HStack(alignment: .top) {
ProfilePicView(pubkey: event.pubkey, size: 16, highlight: .reply, image_cache: image_cache)
.environmentObject(profiles)
ProfilePicView(pubkey: event.pubkey, size: 16, highlight: .reply, image_cache: image_cache, profiles: profiles)
Text(Profile.displayName(profile: profiles.lookup(id: event.pubkey), pubkey: event.pubkey))
.foregroundColor(.accentColor)
Text("\(format_relative_time(event.created_at))")

View File

@@ -18,8 +18,6 @@ struct ReplyView: View {
let replying_to: NostrEvent
let damus: DamusState
@EnvironmentObject var profiles: Profiles
var body: some View {
VStack {
Text("Replying to:")
@@ -27,7 +25,7 @@ struct ReplyView: View {
let names = all_referenced_pubkeys(replying_to)
.map { pubkey in
let pk = pubkey.ref_id
let prof = profiles.lookup(id: pk)
let prof = damus.profiles.lookup(id: pk)
return Profile.displayName(profile: prof, pubkey: pk)
}
.joined(separator: ", ")

View File

@@ -0,0 +1,32 @@
//
// SearchView.swift
// damus
//
// Created by William Casarin on 2022-05-09.
//
import SwiftUI
struct SearchView: View {
let appstate: DamusState
@StateObject var search: SearchModel
var body: some View {
TimelineView(events: $search.events, damus: appstate)
.padding([.leading, .trailing], 6)
.onAppear() {
search.subscribe()
}
.onDisappear() {
search.unsubscribe()
}
}
}
/*
struct SearchView_Previews: PreviewProvider {
static var previews: some View {
SearchView()
}
}
*/

View File

@@ -13,7 +13,6 @@ struct ThreadView: View {
@StateObject var thread: ThreadModel
let damus: DamusState
@EnvironmentObject var profiles: Profiles
@Environment(\.dismiss) var dismiss
var body: some View {
@@ -21,12 +20,10 @@ struct ThreadView: View {
if is_chatroom {
ChatroomView(damus: damus)
.navigationBarTitle("Chat")
.environmentObject(profiles)
.environmentObject(thread)
} else {
EventDetailView(damus: damus, thread: thread)
.navigationBarTitle("Thread")
.environmentObject(profiles)
.environmentObject(thread)
}

View File

@@ -14,14 +14,12 @@ enum TimelineAction {
struct InnerTimelineView: View {
@Binding var events: [NostrEvent]
@EnvironmentObject var profiles: Profiles
let damus: DamusState
var body: some View {
LazyVStack {
ForEach(events, id: \.id) { (ev: NostrEvent) in
let tv = ThreadView(thread: ThreadModel(event: ev, pool: damus.pool), damus: damus)
.environmentObject(profiles)
NavigationLink(destination: tv) {
EventView(event: ev, highlight: .none, has_action_bar: true, damus: damus)
@@ -36,21 +34,17 @@ struct InnerTimelineView: View {
struct TimelineView: View {
@Binding var events: [NostrEvent]
@EnvironmentObject var profiles: Profiles
let damus: DamusState
var body: some View {
MainContent
.padding([.leading, .trailing], 6)
.environmentObject(profiles)
}
var MainContent: some View {
ScrollViewReader { scroller in
ScrollView {
InnerTimelineView(events: $events, damus: damus)
.environmentObject(profiles)
}
.onReceive(NotificationCenter.default.publisher(for: .scroll_to_top)) { _ in
guard let event = events.first else {