image cache

Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
William Casarin
2022-05-04 18:49:40 -07:00
parent 96d8d854e2
commit 4704431c74
12 changed files with 141 additions and 26 deletions

View File

@@ -12,8 +12,7 @@ struct ChatView: View {
let prev_ev: NostrEvent?
let next_ev: NostrEvent?
let likes: EventCounter
let our_pubkey: String
let damus: DamusState
@EnvironmentObject var profiles: Profiles
@EnvironmentObject var thread: ThreadModel
@@ -91,7 +90,7 @@ struct ChatView: View {
VStack {
if is_active || just_started {
ProfilePicView(picture: profile?.picture, size: 32, highlight: is_active ? .main : .none)
ProfilePicView(picture: profile?.picture, size: 32, highlight: is_active ? .main : .none, image_cache: damus.image_cache)
}
/*
if just_started {
@@ -122,7 +121,7 @@ struct ChatView: View {
if let ref_id = thread.replies.lookup(event.id) {
if !is_reply_to_prev() {
ReplyQuoteView(quoter: event, event_id: ref_id)
ReplyQuoteView(quoter: event, event_id: ref_id, image_cache: damus.image_cache)
.environmentObject(thread)
.environmentObject(profiles)
ReplyDescription
@@ -133,7 +132,10 @@ struct ChatView: View {
.textSelection(.enabled)
if is_active || next_ev == nil || next_ev!.pubkey != event.pubkey {
EventActionBar(event: event, our_pubkey: our_pubkey, bar: make_actionbar_model(ev: event, counter: likes))
EventActionBar(event: event,
our_pubkey: damus.pubkey,
bar: make_actionbar_model(ev: event, counter: damus.likes)
)
.environmentObject(profiles)
}

View File

@@ -10,8 +10,7 @@ import SwiftUI
struct ChatroomView: View {
@EnvironmentObject var thread: ThreadModel
@Environment(\.dismiss) var dismiss
let likes: EventCounter
let our_pubkey: String
let damus: DamusState
var body: some View {
ScrollViewReader { scroller in
@@ -22,8 +21,7 @@ struct ChatroomView: View {
ChatView(event: thread.events[ind],
prev_ev: ind > 0 ? thread.events[ind-1] : nil,
next_ev: ind == count-1 ? nil : thread.events[ind+1],
likes: likes,
our_pubkey: our_pubkey
damus: damus
)
.onTapGesture {
if thread.event.id == ev.id {

View File

@@ -53,7 +53,7 @@ struct EventView: View {
.environmentObject(profiles)
NavigationLink(destination: pv) {
ProfilePicView(picture: profile?.picture, size: PFP_SIZE!, highlight: highlight)
ProfilePicView(picture: profile?.picture, size: PFP_SIZE!, highlight: highlight, image_cache: damus.image_cache)
}
Spacer()

View File

@@ -36,27 +36,46 @@ struct ProfilePicView: View {
let picture: String?
let size: CGFloat
let highlight: Highlight
let image_cache: ImageCache
@State var img: Image? = nil
@EnvironmentObject var profiles: Profiles
var Placeholder: some View {
Color.purple.opacity(0.2)
.frame(width: size, height: size)
.cornerRadius(CORNER_RADIUS)
.overlay(Circle().stroke(highlight_color(highlight), lineWidth: pfp_line_width(highlight)))
.padding(2)
}
func ProfilePic(_ url: URL) -> some View {
let pub = load_image(cache: image_cache, from: url)
return Group {
if let img = self.img {
img
.frame(width: size, height: size)
.clipShape(Circle())
.overlay(Circle().stroke(highlight_color(highlight), lineWidth: pfp_line_width(highlight)))
.padding(2)
} else {
Placeholder
}
}
.onReceive(pub) { mimg in
if let img = mimg {
self.img = Image(uiImage: img)
}
}
}
var MainContent: some View {
Group {
if let pic = picture.flatMap({ URL(string: $0) }) {
AsyncImage(url: pic) { img in
img.resizable()
} placeholder: { Placeholder }
.frame(width: size, height: size)
.clipShape(Circle())
.overlay(Circle().stroke(highlight_color(highlight), lineWidth: pfp_line_width(highlight)))
.padding(2)
if let pic_url = picture.flatMap { URL(string: $0) } {
ProfilePic(pic_url)
} else {
Placeholder
.frame(width: size, height: size)
.cornerRadius(CORNER_RADIUS)
.overlay(Circle().stroke(highlight_color(highlight), lineWidth: pfp_line_width(highlight)))
.padding(2)
}
}
}
@@ -66,11 +85,13 @@ struct ProfilePicView: View {
}
}
/*
struct ProfilePicView_Previews: PreviewProvider {
static var previews: some View {
ProfilePicView(picture: "http://cdn.jb55.com/img/red-me.jpg", size: 64, highlight: .none)
}
}
*/
func hex_to_rgb(_ hex: String) -> Color {

View File

@@ -24,7 +24,7 @@ struct ProfileView: View {
var TopSection: some View {
HStack(alignment: .top) {
let data = profiles.lookup(id: profile.pubkey)
ProfilePicView(picture: data?.picture, size: 64, highlight: .custom(Color.black, 4))
ProfilePicView(picture: data?.picture, size: 64, highlight: .custom(Color.black, 4), image_cache: damus.image_cache)
//.border(Color.blue)
VStack(alignment: .leading) {
if let pubkey = profile.pubkey {

View File

@@ -10,6 +10,7 @@ import SwiftUI
struct ReplyQuoteView: View {
let quoter: NostrEvent
let event_id: String
let image_cache: ImageCache
@EnvironmentObject var profiles: Profiles
@EnvironmentObject var thread: ThreadModel
@@ -22,7 +23,7 @@ struct ReplyQuoteView: View {
VStack(alignment: .leading) {
HStack(alignment: .top) {
ProfilePicView(picture: profiles.lookup(id: event.pubkey)?.picture, size: 16, highlight: .reply)
ProfilePicView(picture: profiles.lookup(id: event.pubkey)?.picture, size: 16, highlight: .reply, image_cache: image_cache)
Text(Profile.displayName(profile: profiles.lookup(id: event.pubkey), pubkey: event.pubkey))
.foregroundColor(.accentColor)
Text("\(format_relative_time(event.created_at))")

View File

@@ -19,7 +19,7 @@ struct ThreadView: View {
var body: some View {
Group {
if is_chatroom {
ChatroomView(likes: damus.likes, our_pubkey: damus.pubkey)
ChatroomView(damus: damus)
.navigationBarTitle("Chat")
.environmentObject(profiles)
.environmentObject(thread)