Browse Source

better threads

Signed-off-by: William Casarin <jb55@jb55.com>
profiles-everywhere
William Casarin 3 years ago
parent
commit
d950ad75b8
  1. 4
      damus/ContentView.swift
  2. 22
      damus/Nostr/NostrEvent.swift
  3. 28
      damus/Views/EventActionBar.swift
  4. 36
      damus/Views/EventDetailView.swift
  5. 30
      damus/Views/EventView.swift
  6. 55
      damus/Views/ProfilePicView.swift

4
damus/ContentView.swift

@ -51,10 +51,10 @@ struct ContentView: View {
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 evdet = EventDetailView(event: ev, pool: pool) let evdet = EventDetailView(event: ev, pool: pool)
.navigationBarTitle("Note") .navigationBarTitle("Thread")
.environmentObject(profiles) .environmentObject(profiles)
NavigationLink(destination: evdet) { NavigationLink(destination: evdet) {
EventView(event: ev, highlighted: false, has_action_bar: true) EventView(event: ev, highlight: .none, has_action_bar: true)
} }
.buttonStyle(PlainButtonStyle()) .buttonStyle(PlainButtonStyle())
} }

22
damus/Nostr/NostrEvent.swift

@ -64,6 +64,28 @@ class NostrEvent: Codable, Identifiable {
} }
} }
public func references(id: String, key: String) -> Bool {
for tag in tags {
if tag.count >= 2 && tag[0] == key {
if tag[1] == id {
return true
}
}
}
return false
}
public var is_reply: Bool {
for tag in tags {
if tag[0] == "e" {
return true
}
}
return false
}
public var referenced_ids: [ReferencedId] { public var referenced_ids: [ReferencedId] {
return get_referenced_ids(key: "e") return get_referenced_ids(key: "e")
} }

28
damus/Views/EventActionBar.swift

@ -7,20 +7,46 @@
import SwiftUI import SwiftUI
extension Notification.Name {
static var thread_focus: Notification.Name {
return Notification.Name("thread focus")
}
}
enum ActionBarSheet: Identifiable {
case reply
var id: String {
switch self {
case .reply: return "reply"
}
}
}
struct EventActionBar: View { struct EventActionBar: View {
let event: NostrEvent let event: NostrEvent
@State var sheet: ActionBarSheet? = nil
@EnvironmentObject var profiles: Profiles
var body: some View { var body: some View {
HStack { HStack {
EventActionButton(img: "bubble.left") { EventActionButton(img: "bubble.left") {
print("reply") self.sheet = .reply
} }
Spacer() Spacer()
EventActionButton(img: "square.and.arrow.up") { EventActionButton(img: "square.and.arrow.up") {
print("share") print("share")
} }
} }
.sheet(item: $sheet) { sheet in
switch sheet {
case .reply:
ReplyView(replying_to: event)
}
}
} }
} }

36
damus/Views/EventDetailView.swift

@ -8,7 +8,7 @@
import SwiftUI import SwiftUI
struct EventDetailView: View { struct EventDetailView: View {
let event: NostrEvent @State var event: NostrEvent
let sub_id = UUID().description let sub_id = UUID().description
@ -21,8 +21,8 @@ struct EventDetailView: View {
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)")
self.pool.send(.unsubscribe(sub_id))
self.pool.remove_handler(sub_id: sub_id) self.pool.remove_handler(sub_id: sub_id)
self.pool.send(.unsubscribe(sub_id))
} }
func subscribe_to_thread() { func subscribe_to_thread() {
@ -53,8 +53,11 @@ struct EventDetailView: View {
} }
self.add_event(ev) self.add_event(ev)
case .notice(_): case .notice(let note):
// TODO: handle notices in threads? if note.contains("Too many subscription filters") {
// TODO: resend filters?
pool.reconnect(to: [relay_id])
}
break break
} }
} }
@ -64,9 +67,10 @@ struct EventDetailView: View {
ScrollViewReader { proxy in ScrollViewReader { proxy in
ScrollView { ScrollView {
ForEach(events, id: \.id) { ev in ForEach(events, id: \.id) { ev in
Group {
let is_active_id = ev.id == event.id let is_active_id = ev.id == event.id
if is_active_id { if is_active_id {
EventView(event: ev, highlighted: is_active_id, has_action_bar: true) EventView(event: ev, highlight: .main, has_action_bar: true)
.onAppear() { .onAppear() {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
withAnimation { withAnimation {
@ -75,14 +79,12 @@ struct EventDetailView: View {
} }
} }
} else { } else {
let evdet = EventDetailView(event: ev, pool: pool) let highlight = determine_highlight(current: ev, active: event)
.navigationBarTitle("Note") EventView(event: ev, highlight: highlight, has_action_bar: true)
.environmentObject(profiles) .onTapGesture {
self.event = ev
NavigationLink(destination: evdet) { }
EventView(event: ev, highlighted: is_active_id, has_action_bar: true)
} }
.buttonStyle(PlainButtonStyle())
} }
} }
} }
@ -115,3 +117,13 @@ struct EventDetailView_Previews: PreviewProvider {
} }
} }
*/ */
func determine_highlight(current: NostrEvent, active: NostrEvent) -> Highlight
{
if active.references(id: current.id, key: "e") {
return .replied_to(active.id)
} else if current.references(id: active.id, key: "e") {
return .replied_to(current.id)
}
return .none
}

30
damus/Views/EventView.swift

@ -9,9 +9,30 @@ import Foundation
import SwiftUI import SwiftUI
import CachedAsyncImage import CachedAsyncImage
enum Highlight {
case none
case main
case referenced(String)
case replied_to(String)
var is_none: Bool {
switch self {
case .none: return true
default: return false
}
}
var is_replied_to: Bool {
switch self {
case .replied_to: return true
default: return false
}
}
}
struct EventView: View { struct EventView: View {
let event: NostrEvent let event: NostrEvent
let highlighted: Bool let highlight: Highlight
let has_action_bar: Bool let has_action_bar: Bool
@EnvironmentObject var profiles: Profiles @EnvironmentObject var profiles: Profiles
@ -20,7 +41,7 @@ struct EventView: View {
let profile = profiles.lookup(id: event.pubkey) 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, highlight: highlight)
Spacer() Spacer()
} }
@ -30,6 +51,11 @@ struct EventView: View {
ProfileName(pubkey: event.pubkey, profile: profile) ProfileName(pubkey: event.pubkey, profile: profile)
Text("\(format_relative_time(event.created_at))") Text("\(format_relative_time(event.created_at))")
.foregroundColor(.gray) .foregroundColor(.gray)
if event.is_reply {
Label("", systemImage: "arrowshape.turn.up.left")
.font(.footnote)
.foregroundColor(.gray)
}
Spacer() Spacer()
if (event.pow ?? 0) >= 10 { if (event.pow ?? 0) >= 10 {
PowView(event.pow) PowView(event.pow)

55
damus/Views/ProfilePicView.swift

@ -11,10 +11,30 @@ import CachedAsyncImage
let PFP_SIZE: CGFloat? = 64 let PFP_SIZE: CGFloat? = 64
let CORNER_RADIUS: CGFloat = 32 let CORNER_RADIUS: CGFloat = 32
func id_to_color(_ id: String) -> Color {
return .init(hex: String(id.reversed().prefix(6)))
}
func highlight_color(_ h: Highlight) -> Color {
switch h {
case .none: return Color.black
case .main: return Color.red
case .referenced(let id): return Color.blue
case .replied_to: return Color.blue
}
}
func pfp_line_width(_ h: Highlight) -> CGFloat {
if h.is_none {
return 0
}
return 4
}
struct ProfilePicView: View { struct ProfilePicView: View {
let picture: String? let picture: String?
let size: CGFloat let size: CGFloat
let highlighted: Bool let highlight: Highlight
var body: some View { var body: some View {
if let pic = picture.flatMap({ URL(string: $0) }) { if let pic = picture.flatMap({ URL(string: $0) }) {
@ -25,13 +45,13 @@ struct ProfilePicView: View {
} }
.frame(width: PFP_SIZE, height: PFP_SIZE) .frame(width: PFP_SIZE, height: PFP_SIZE)
.clipShape(Circle()) .clipShape(Circle())
.overlay(Circle().stroke(highlighted ? Color.red : Color.black, lineWidth: highlighted ? 4 : 0)) .overlay(Circle().stroke(highlight_color(highlight), lineWidth: pfp_line_width(highlight)))
.padding(2) .padding(2)
} else { } else {
Color.purple.opacity(0.1) Color.purple.opacity(0.1)
.frame(width: PFP_SIZE, height: PFP_SIZE) .frame(width: PFP_SIZE, height: PFP_SIZE)
.cornerRadius(CORNER_RADIUS) .cornerRadius(CORNER_RADIUS)
.overlay(Circle().stroke(highlighted ? Color.red : Color.black, lineWidth: highlighted ? 4 : 0)) .overlay(Circle().stroke(highlight_color(highlight), lineWidth: pfp_line_width(highlight)))
.padding(2) .padding(2)
} }
} }
@ -39,6 +59,33 @@ struct ProfilePicView: View {
struct ProfilePicView_Previews: PreviewProvider { struct ProfilePicView_Previews: PreviewProvider {
static var previews: some View { static var previews: some View {
ProfilePicView(picture: "http://cdn.jb55.com/img/red-me.jpg", size: 64, highlighted: false) ProfilePicView(picture: "http://cdn.jb55.com/img/red-me.jpg", size: 64, highlight: .none)
}
}
extension Color {
init(hex: String) {
var int: UInt64 = 0
Scanner(string: hex).scanHexInt64(&int)
let a, r, g, b: UInt64
switch hex.count {
case 3: // RGB (12-bit)
(a, r, g, b) = (255, (int >> 8) * 17, (int >> 4 & 0xF) * 17, (int & 0xF) * 17)
case 6: // RGB (24-bit)
(a, r, g, b) = (255, int >> 16, int >> 8 & 0xFF, int & 0xFF)
case 8: // ARGB (32-bit)
(a, r, g, b) = (int >> 24, int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF)
default:
(a, r, g, b) = (1, 1, 1, 0)
}
self.init(
.sRGB,
red: Double(r) / 255,
green: Double(g) / 255,
blue: Double(b) / 255,
opacity: Double(a) / 255
)
} }
} }

Loading…
Cancel
Save