likes, mention parsing, lots of stuff

Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
William Casarin
2022-05-04 16:26:10 -07:00
parent 403fa74f8d
commit f42bc2e91e
25 changed files with 608 additions and 118 deletions

View File

@@ -11,7 +11,10 @@ struct ChatView: View {
let event: NostrEvent
let prev_ev: NostrEvent?
let next_ev: NostrEvent?
let likes: EventCounter
let our_pubkey: String
@EnvironmentObject var profiles: Profiles
@EnvironmentObject var thread: ThreadModel
@@ -130,7 +133,7 @@ struct ChatView: View {
.textSelection(.enabled)
if is_active || next_ev == nil || next_ev!.pubkey != event.pubkey {
EventActionBar(event: event)
EventActionBar(event: event, our_pubkey: our_pubkey, bar: make_actionbar_model(ev: event, counter: likes))
.environmentObject(profiles)
}

View File

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

View File

@@ -19,9 +19,10 @@ enum ActionBarSheet: Identifiable {
struct EventActionBar: View {
let event: NostrEvent
let our_pubkey: String
@State var sheet: ActionBarSheet? = nil
@EnvironmentObject var profiles: Profiles
@StateObject var bar: ActionBarModel = ActionBarModel()
@StateObject var bar: ActionBarModel
var body: some View {
HStack {
@@ -38,25 +39,40 @@ struct EventActionBar: View {
}
.padding([.trailing], 40)
EventActionButton(img: bar.liked ? "heart.fill" : "heart", col: bar.liked ? Color.red : nil) {
if bar.liked {
notify(.delete, bar.our_like_event)
} else {
notify(.like, event)
HStack(alignment: .bottom) {
Text("\(bar.likes > 0 ? "\(bar.likes)" : "")")
.font(.footnote)
.foregroundColor(Color.gray)
EventActionButton(img: bar.liked ? "heart.fill" : "heart", col: bar.liked ? Color.red : nil) {
if bar.liked {
notify(.delete, bar.our_like)
} else {
notify(.like, event)
}
}
}
.padding([.trailing], 40)
EventActionButton(img: "arrow.2.squarepath", col: bar.boosted ? Color.green : nil) {
if bar.boosted {
notify(.delete, bar.our_boost_event)
notify(.delete, bar.our_boost)
} else {
notify(.boost, event)
}
}
}
.contentShape(Rectangle())
.onReceive(handle_notify(.liked)) { n in
let liked = n.object as! Liked
if liked.id != event.id {
return
}
self.bar.likes = liked.total
if liked.like.pubkey == our_pubkey {
self.bar.our_like = liked.like
}
}
}
}
@@ -67,6 +83,5 @@ func EventActionButton(img: String, col: Color?, action: @escaping () -> ()) ->
.font(.footnote)
.foregroundColor(col == nil ? Color.gray : col!)
}
.contentShape(Rectangle())
}

View File

@@ -32,7 +32,7 @@ enum CollapsedEvent: Identifiable {
struct EventDetailView: View {
let sub_id = UUID().description
let pool: RelayPool
let damus: DamusState
@StateObject var thread: ThreadModel
@State var collapsed: Bool = true
@@ -70,7 +70,7 @@ struct EventDetailView: View {
toggle_thread_view()
}
case .event(let ev, let highlight):
EventView(event: ev, highlight: highlight, has_action_bar: true, pool: pool)
EventView(event: ev, highlight: highlight, has_action_bar: true, damus: damus)
.onTapGesture {
if thread.event.id == ev.id {
toggle_thread_view()

View File

@@ -40,7 +40,7 @@ struct EventView: View {
let event: NostrEvent
let highlight: Highlight
let has_action_bar: Bool
let pool: RelayPool
let damus: DamusState
@EnvironmentObject var profiles: Profiles
@EnvironmentObject var action_bar: ActionBarModel
@@ -49,7 +49,7 @@ struct EventView: View {
let profile = profiles.lookup(id: event.pubkey)
HStack {
VStack {
let pv = ProfileView(pool: pool, profile: ProfileModel(pubkey: event.pubkey, pool: pool))
let pv = ProfileView(damus: damus, profile: ProfileModel(pubkey: event.pubkey, damus: damus))
.environmentObject(profiles)
NavigationLink(destination: pv) {
@@ -81,7 +81,7 @@ struct EventView: View {
Spacer()
if has_action_bar {
EventActionBar(event: event)
EventActionBar(event: event, our_pubkey: damus.pubkey, bar: make_actionbar_model(ev: event, counter: damus.likes))
.environmentObject(profiles)
}
@@ -152,3 +152,11 @@ func reply_others_desc(n: Int, n_pubkeys: Int) -> String {
}
func make_actionbar_model(ev: NostrEvent, counter: EventCounter) -> ActionBarModel {
let likes = counter.counts[ev.id]
let our_like = counter.our_events[ev.id]
let our_boost: NostrEvent? = nil
return ActionBarModel(likes: likes ?? 0, our_like: our_like, our_boost: our_boost)
}

View File

@@ -24,14 +24,6 @@ struct NostrPost {
tag.append(relay_id)
}
new_ev.tags.append(tag)
// filter our pubkeys
new_ev.tags = new_ev.tags.filter {
if $0[0] == "p" {
return $0[1] != pubkey
} else {
return true
}
}
}
new_ev.calculate_id()
new_ev.sign(privkey: privkey)

View File

@@ -13,7 +13,8 @@ enum ProfileTab: Hashable {
}
struct ProfileView: View {
let pool: RelayPool
let damus: DamusState
@State private var selected_tab: ProfileTab = .posts
@StateObject var profile: ProfileModel
@@ -54,7 +55,7 @@ struct ProfileView: View {
Group {
switch(selected_tab) {
case .posts:
TimelineView(events: $profile.events, pool: pool)
TimelineView(events: $profile.events, damus: damus)
.environmentObject(profiles)
case .following:
Text("Following")

View File

@@ -16,7 +16,7 @@ func all_referenced_pubkeys(_ ev: NostrEvent) -> [ReferencedId] {
struct ReplyView: View {
let replying_to: NostrEvent
let pool: RelayPool
let damus: DamusState
@EnvironmentObject var profiles: Profiles
@@ -35,8 +35,8 @@ struct ReplyView: View {
.foregroundColor(.gray)
.font(.footnote)
}
EventView(event: replying_to, highlight: .none, has_action_bar: false, pool: pool)
PostView(references: replying_to.reply_ids())
EventView(event: replying_to, highlight: .none, has_action_bar: false, damus: damus)
PostView(references: gather_reply_ids(our_pubkey: damus.pubkey, from: replying_to))
Spacer()
}

View File

@@ -11,7 +11,7 @@ import SwiftUI
struct ThreadView: View {
@State var is_chatroom: Bool = false
@StateObject var thread: ThreadModel
let pool: RelayPool
let damus: DamusState
@EnvironmentObject var profiles: Profiles
@Environment(\.dismiss) var dismiss
@@ -19,12 +19,12 @@ struct ThreadView: View {
var body: some View {
Group {
if is_chatroom {
ChatroomView()
ChatroomView(likes: damus.likes, our_pubkey: damus.pubkey)
.navigationBarTitle("Chat")
.environmentObject(profiles)
.environmentObject(thread)
} else {
EventDetailView(pool: pool, thread: thread)
EventDetailView(damus: damus, thread: thread)
.navigationBarTitle("Thread")
.environmentObject(profiles)
.environmentObject(thread)

View File

@@ -17,7 +17,7 @@ struct TimelineView: View {
@EnvironmentObject var profiles: Profiles
let pool: RelayPool
let damus: DamusState
var body: some View {
MainContent
@@ -37,11 +37,11 @@ struct TimelineView: View {
.environmentObject(profiles)
*/
let tv = ThreadView(thread: ThreadModel(ev: ev, pool: pool), pool: pool)
let tv = ThreadView(thread: ThreadModel(ev: ev, pool: damus.pool), damus: damus)
.environmentObject(profiles)
NavigationLink(destination: tv) {
EventView(event: ev, highlight: .none, has_action_bar: true, pool: pool)
EventView(event: ev, highlight: .none, has_action_bar: true, damus: damus)
}
.isDetailLink(true)
.buttonStyle(PlainButtonStyle())