diff --git a/damus.xcodeproj/project.pbxproj b/damus.xcodeproj/project.pbxproj index c60f1cf..b37c3b3 100644 --- a/damus.xcodeproj/project.pbxproj +++ b/damus.xcodeproj/project.pbxproj @@ -32,6 +32,7 @@ 4CEE2AEF2805BE2500AB5EEF /* NostrTimeline.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CEE2AEE2805BE2500AB5EEF /* NostrTimeline.swift */; }; 4CEE2AF1280B216B00AB5EEF /* EventDetailView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CEE2AF0280B216B00AB5EEF /* EventDetailView.swift */; }; 4CEE2AF3280B25C500AB5EEF /* ProfilePicView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CEE2AF2280B25C500AB5EEF /* ProfilePicView.swift */; }; + 4CEE2AF5280B29E600AB5EEF /* TimeAgo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CEE2AF4280B29E600AB5EEF /* TimeAgo.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -79,6 +80,7 @@ 4CEE2AEE2805BE2500AB5EEF /* NostrTimeline.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NostrTimeline.swift; sourceTree = ""; }; 4CEE2AF0280B216B00AB5EEF /* EventDetailView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EventDetailView.swift; sourceTree = ""; }; 4CEE2AF2280B25C500AB5EEF /* ProfilePicView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProfilePicView.swift; sourceTree = ""; }; + 4CEE2AF4280B29E600AB5EEF /* TimeAgo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TimeAgo.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -169,6 +171,7 @@ 4CE6DEE827F7A08100C66700 /* ContentView.swift */, 4CE6DEEA27F7A08200C66700 /* Assets.xcassets */, 4CE6DEEC27F7A08200C66700 /* Preview Content */, + 4CEE2AF4280B29E600AB5EEF /* TimeAgo.swift */, ); path = damus; sourceTree = ""; @@ -348,6 +351,7 @@ files = ( 4C75EFB728049D990006080F /* RelayPool.swift in Sources */, 4CE6DEE927F7A08100C66700 /* ContentView.swift in Sources */, + 4CEE2AF5280B29E600AB5EEF /* TimeAgo.swift in Sources */, 4C75EFAD28049CFB0006080F /* PostButton.swift in Sources */, 4C75EFB92804A2740006080F /* EventView.swift in Sources */, 4C75EFA627FF87A20006080F /* Nostr.swift in Sources */, diff --git a/damus/ContentView.swift b/damus/ContentView.swift index 342b152..3340e65 100644 --- a/damus/ContentView.swift +++ b/damus/ContentView.swift @@ -49,8 +49,9 @@ struct ContentView: View { ScrollView { ForEach(events, id: \.id) { ev in if ev.is_local && timeline == .debug || (timeline == .global && !ev.is_local) || (timeline == .friends && is_friend(ev.pubkey)) { - NavigationLink(destination: EventDetailView(event: ev)) { - EventView(event: ev, profile: profiles[ev.pubkey]?.profile) + let profile: Profile? = profiles[ev.pubkey]?.profile + NavigationLink(destination: EventDetailView(event: ev, profile: profile)) { + EventView(event: ev, profile: profile) } .buttonStyle(PlainButtonStyle()) } diff --git a/damus/TimeAgo.swift b/damus/TimeAgo.swift new file mode 100644 index 0000000..23a4b47 --- /dev/null +++ b/damus/TimeAgo.swift @@ -0,0 +1,46 @@ +// +// TimeAgo.swift +// damus +// +// Created by William Casarin on 2022-04-16. +// + +import Foundation + +public func time_ago_since(_ date: Date) -> String { + + let calendar = Calendar.current + let now = Date() + let unitFlags: NSCalendar.Unit = [.second, .minute, .hour, .day, .weekOfYear, .month, .year] + let components = (calendar as NSCalendar).components(unitFlags, from: date, to: now, options: []) + + if let year = components.year, year >= 1 { + return "\(year)yr" + } + + if let month = components.month, month >= 1 { + return "\(month)mth" + } + + if let week = components.weekOfYear, week >= 1 { + return "\(week)wk" + } + + if let day = components.day, day >= 1 { + return "\(day)d" + } + + if let hour = components.hour, hour >= 1 { + return "\(hour)h" + } + + if let minute = components.minute, minute >= 1 { + return "\(minute)m" + } + + if let second = components.second, second >= 3 { + return "\(second)s" + } + + return "now" +} diff --git a/damus/Views/EventDetailView.swift b/damus/Views/EventDetailView.swift index ff17bcd..dad7b93 100644 --- a/damus/Views/EventDetailView.swift +++ b/damus/Views/EventDetailView.swift @@ -9,14 +9,21 @@ import SwiftUI struct EventDetailView: View { let event: NostrEvent + let profile: Profile? var body: some View { - Text("EventDetailView") + HStack { + VStack { + ProfilePicView(picture: profile?.picture, size: 64) + + Spacer() + } + } } } struct EventDetailView_Previews: PreviewProvider { static var previews: some View { - EventDetailView(event: NostrEvent(content: "Hello", pubkey: "Guy")) + EventDetailView(event: NostrEvent(content: "Hello", pubkey: "Guy"), profile: nil) } } diff --git a/damus/Views/EventView.swift b/damus/Views/EventView.swift index 1a7f0b7..6dd6107 100644 --- a/damus/Views/EventView.swift +++ b/damus/Views/EventView.swift @@ -28,7 +28,8 @@ struct EventView: View { .onTapGesture { UIPasteboard.general.string = event.pubkey } - .frame(maxWidth: .infinity, alignment: .leading) + Text("\(format_relative_time(event.created_at))") + .foregroundColor(.gray) Spacer() if (event.pow ?? 0) >= 10 { Text("\(event.pow ?? 0)") @@ -56,3 +57,8 @@ func calculate_pow_color(_ pow: Int) -> Color let x = Double(pow) / 30.0; return Color(.sRGB, red: 2.0 * (1.0 - x), green: 2.0 * x, blue: 0, opacity: 0.5) } + +func format_relative_time(_ created_at: Int64) -> String +{ + return time_ago_since(Date(timeIntervalSince1970: Double(created_at))) +}