Skip to content

Commit

Permalink
rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
suhailsaqan committed Aug 8, 2023
1 parent 4c8b58c commit 3180829
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 44 deletions.
8 changes: 4 additions & 4 deletions damus/Nostr/NostrEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -955,8 +955,8 @@ func first_eref_mention(ev: NostrEvent, privkey: Privkey?) -> Mention<NoteId>? {
return nil
}

func separate_images(ev: NostrEvent, privkey: String?) -> [MediaUrl]? {
let urlBlocks: [URL] = ev.blocks(privkey).reduce(into: []) { urls, block in
func separate_images(ev: NostrEvent, privkey: Privkey?) -> [MediaUrl]? {
let urlBlocks: [URL] = ev.blocks(privkey).blocks.reduce(into: []) { urls, block in
guard case .url(let url) = block else {
return
}
Expand All @@ -968,8 +968,8 @@ func separate_images(ev: NostrEvent, privkey: String?) -> [MediaUrl]? {
return mediaUrls.isEmpty ? nil : mediaUrls
}

func separate_invoices(ev: NostrEvent, privkey: String?) -> [Invoice]? {
let invoiceBlocks: [Invoice] = ev.blocks(privkey).reduce(into: []) { invoices, block in
func separate_invoices(ev: NostrEvent, privkey: Privkey?) -> [Invoice]? {
let invoiceBlocks: [Invoice] = ev.blocks(privkey).blocks.reduce(into: []) { invoices, block in
guard case .invoice(let invoice) = block else {
return
}
Expand Down
91 changes: 61 additions & 30 deletions damus/Views/DMChatView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,36 +60,10 @@ struct DMChatView: View, KeyboardReadable {
var Messages: some View {
ScrollViewReader { scroller in
ScrollView {
if (dms.events.isEmpty) {
Text("Send a message to start the conversation...", comment: "Text prompt for user to send a message to the other user.")
.lineLimit(nil)
.multilineTextAlignment(.center)
.padding(.horizontal, 40)
.padding(.vertical)
.foregroundColor(.gray)
if dms.events.isEmpty {
EmptyMessageView()
} else {
VStack(alignment: .leading) {
let groups = groupEventsByDateAndPubkey(events: dms.events)

ForEach(Array(groups.keys).sorted(), id: \.self) { date in
VStack(alignment: .leading) {
Text(date, style: .date)
.font(.footnote)
.fontWeight(.bold)
.foregroundColor(.gray)
.padding(.horizontal)
.frame(maxWidth: .infinity)

ForEach(Array(groups[date]!.keys).sorted(), id: \.self) { key in
let events = groups[date]![key]!
ForEach(events) { event in
DMView(event: event, damus_state: damus_state, isLastInGroup: event == events.last)
}
}
}
}
EndBlock(height: 5)
}.padding(.horizontal)
GroupedEventsView(groups: groupEventsByDateAndPubkey(events: dms.events), damus_state: damus_state)
}
}
.onAppear {
Expand All @@ -102,6 +76,63 @@ struct DMChatView: View, KeyboardReadable {
}
}

struct EmptyMessageView: View {
var body: some View {
Text("Send a message to start the conversation...", comment: "Text prompt for user to send a message to the other user.")
.lineLimit(nil)
.multilineTextAlignment(.center)
.padding(.horizontal, 40)
.padding(.vertical)
.foregroundColor(.gray)
}
}

struct GroupedEventsView: View {
var groups: [Date: [String: [NostrEvent]]]
var damus_state: DamusState

var body: some View {
VStack(alignment: .leading) {
ForEach(Array(groups.keys).sorted(), id: \.self) { date in
GroupedDateView(date: date, events: groups[date]!, damus_state: damus_state)
}
EndBlock(height: 5)
}.padding(.horizontal)
}
}

struct GroupedDateView: View {
var date: Date
var events: [String: [NostrEvent]]
var damus_state: DamusState

var body: some View {
VStack(alignment: .leading) {
Text(date, style: .date)
.font(.footnote)
.fontWeight(.bold)
.foregroundColor(.gray)
.padding(.horizontal)
.frame(maxWidth: .infinity)

ForEach(Array(events.keys).sorted(), id: \.self) { key in
GroupedEventView(events: events[key]!, damus_state: damus_state)
}
}
}
}

struct GroupedEventView: View {
var events: [NostrEvent]
var damus_state: DamusState

var body: some View {
ForEach(events, id: \.self) { event in
DMView(event: event, damus_state: damus_state, isLastInGroup: event == events.last)
}
}
}

func scroll_to_end(_ scroller: ScrollViewProxy, animated: Bool = false) {
if animated {
withAnimation {
Expand Down Expand Up @@ -311,7 +342,7 @@ extension String {
}
}

func create_dm(_ message: String, to_pk: String, tags: [[String]], keypair: Keypair, created_at: UInt32? = nil) -> NostrEvent?
func create_dm(_ message: String, to_pk: Pubkey, tags: [[String]], keypair: Keypair, created_at: UInt32? = nil) -> NostrEvent?
{
let created = created_at ?? UInt32(Date().timeIntervalSince1970)

Expand Down
21 changes: 12 additions & 9 deletions damus/Views/DMView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ struct DMView: View {
return options
}

func format_timestamp(timestamp: Int64) -> String {
func format_timestamp(timestamp: UInt32) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "h:mm a"
let date = Date(timeIntervalSince1970: TimeInterval(timestamp))
Expand Down Expand Up @@ -83,7 +83,7 @@ struct DMView: View {
}
}

func Mention(mention: Mention) -> some View {
func Mention(mention: Mention<NoteId>) -> some View {
Group {
HStack {
if is_ours {
Expand All @@ -100,6 +100,7 @@ struct DMView: View {
}
}

@MainActor
func Image(urls: [MediaUrl], isLastInDM: Bool) -> some View {
return Group {
HStack {
Expand Down Expand Up @@ -166,7 +167,9 @@ struct DMView: View {
}
}

func filter_content(blocks: [Block], profiles: Profiles, privkey: String?) -> (Bool, CompatibleText?) {
func filter_content(blocks bs: Blocks, profiles: Profiles, privkey: Privkey?) -> (Bool, CompatibleText?) {
let blocks = bs.blocks

let one_note_ref = blocks
.filter({ $0.is_note_mention })
.count == 1
Expand All @@ -178,10 +181,10 @@ struct DMView: View {

switch block {
case .mention(let m):
if m.type == .event && one_note_ref {
if case .note = m.ref, one_note_ref {
return str
}
if m.type == .pubkey {
if case .pubkey(_) = m.ref {
show_text = true
}
return str + mention_str(m, profiles: profiles)
Expand All @@ -192,9 +195,9 @@ struct DMView: View {
}

if let next = blocks[safe: ind+1] {
if case .url(let u) = next, classify_url(u).is_media != nil {
if case .url(let u) = next, classify_url(u).is_media != nil {
trimmed = trim_suffix(trimmed)
} else if case .mention(let m) = next, m.type == .event, one_note_ref {
} else if case .mention(let m) = next, case .note = m.ref, one_note_ref {
trimmed = trim_suffix(trimmed)
}
}
Expand All @@ -219,11 +222,11 @@ struct DMView: View {
}
}
}

return (show_text, txt)
}

func getLastInDM(text: CompatibleText?, mention: Mention?, url: [MediaUrl]?, invoices: [Invoice]?) -> DMContentType? {
func getLastInDM(text: CompatibleText?, mention: Mention<NoteId>?, url: [MediaUrl]?, invoices: [Invoice]?) -> DMContentType? {
var last: DMContentType?
if let text {
last = .text
Expand Down
2 changes: 1 addition & 1 deletion damus/Views/NoteContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ func render_note_content(ev: NostrEvent, profiles: Profiles, privkey: Privkey?,
return .longform(LongformContent(ev.content))
}

return .separated(render_blocks(blocks: blocks, profiles: profiles))
return .separated(render_blocks(blocks: blocks, profiles: profiles, only_text: only_text))
}

fileprivate func artifact_part_last_text_ind(parts: [ArtifactPart]) -> (Int, Text)? {
Expand Down

0 comments on commit 3180829

Please sign in to comment.