1
0
mirror of git://jb55.com/damus synced 2024-09-30 00:40:45 +00:00

show relative created time on events

Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
William Casarin 2022-04-16 09:53:34 -07:00
parent 8a3bf8b44f
commit e48af81b75
5 changed files with 69 additions and 5 deletions

View File

@ -32,6 +32,7 @@
4CEE2AEF2805BE2500AB5EEF /* NostrTimeline.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CEE2AEE2805BE2500AB5EEF /* NostrTimeline.swift */; }; 4CEE2AEF2805BE2500AB5EEF /* NostrTimeline.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CEE2AEE2805BE2500AB5EEF /* NostrTimeline.swift */; };
4CEE2AF1280B216B00AB5EEF /* EventDetailView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CEE2AF0280B216B00AB5EEF /* EventDetailView.swift */; }; 4CEE2AF1280B216B00AB5EEF /* EventDetailView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CEE2AF0280B216B00AB5EEF /* EventDetailView.swift */; };
4CEE2AF3280B25C500AB5EEF /* ProfilePicView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CEE2AF2280B25C500AB5EEF /* ProfilePicView.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 */ /* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */ /* Begin PBXContainerItemProxy section */
@ -79,6 +80,7 @@
4CEE2AEE2805BE2500AB5EEF /* NostrTimeline.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NostrTimeline.swift; sourceTree = "<group>"; }; 4CEE2AEE2805BE2500AB5EEF /* NostrTimeline.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NostrTimeline.swift; sourceTree = "<group>"; };
4CEE2AF0280B216B00AB5EEF /* EventDetailView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EventDetailView.swift; sourceTree = "<group>"; }; 4CEE2AF0280B216B00AB5EEF /* EventDetailView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EventDetailView.swift; sourceTree = "<group>"; };
4CEE2AF2280B25C500AB5EEF /* ProfilePicView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProfilePicView.swift; sourceTree = "<group>"; }; 4CEE2AF2280B25C500AB5EEF /* ProfilePicView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProfilePicView.swift; sourceTree = "<group>"; };
4CEE2AF4280B29E600AB5EEF /* TimeAgo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TimeAgo.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */ /* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */ /* Begin PBXFrameworksBuildPhase section */
@ -169,6 +171,7 @@
4CE6DEE827F7A08100C66700 /* ContentView.swift */, 4CE6DEE827F7A08100C66700 /* ContentView.swift */,
4CE6DEEA27F7A08200C66700 /* Assets.xcassets */, 4CE6DEEA27F7A08200C66700 /* Assets.xcassets */,
4CE6DEEC27F7A08200C66700 /* Preview Content */, 4CE6DEEC27F7A08200C66700 /* Preview Content */,
4CEE2AF4280B29E600AB5EEF /* TimeAgo.swift */,
); );
path = damus; path = damus;
sourceTree = "<group>"; sourceTree = "<group>";
@ -348,6 +351,7 @@
files = ( files = (
4C75EFB728049D990006080F /* RelayPool.swift in Sources */, 4C75EFB728049D990006080F /* RelayPool.swift in Sources */,
4CE6DEE927F7A08100C66700 /* ContentView.swift in Sources */, 4CE6DEE927F7A08100C66700 /* ContentView.swift in Sources */,
4CEE2AF5280B29E600AB5EEF /* TimeAgo.swift in Sources */,
4C75EFAD28049CFB0006080F /* PostButton.swift in Sources */, 4C75EFAD28049CFB0006080F /* PostButton.swift in Sources */,
4C75EFB92804A2740006080F /* EventView.swift in Sources */, 4C75EFB92804A2740006080F /* EventView.swift in Sources */,
4C75EFA627FF87A20006080F /* Nostr.swift in Sources */, 4C75EFA627FF87A20006080F /* Nostr.swift in Sources */,

View File

@ -49,8 +49,9 @@ struct ContentView: View {
ScrollView { ScrollView {
ForEach(events, id: \.id) { ev in ForEach(events, id: \.id) { ev 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)) {
NavigationLink(destination: EventDetailView(event: ev)) { let profile: Profile? = profiles[ev.pubkey]?.profile
EventView(event: ev, profile: profiles[ev.pubkey]?.profile) NavigationLink(destination: EventDetailView(event: ev, profile: profile)) {
EventView(event: ev, profile: profile)
} }
.buttonStyle(PlainButtonStyle()) .buttonStyle(PlainButtonStyle())
} }

46
damus/TimeAgo.swift Normal file
View File

@ -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"
}

View File

@ -9,14 +9,21 @@ import SwiftUI
struct EventDetailView: View { struct EventDetailView: View {
let event: NostrEvent let event: NostrEvent
let profile: Profile?
var body: some View { var body: some View {
Text("EventDetailView") HStack {
VStack {
ProfilePicView(picture: profile?.picture, size: 64)
Spacer()
}
}
} }
} }
struct EventDetailView_Previews: PreviewProvider { struct EventDetailView_Previews: PreviewProvider {
static var previews: some View { static var previews: some View {
EventDetailView(event: NostrEvent(content: "Hello", pubkey: "Guy")) EventDetailView(event: NostrEvent(content: "Hello", pubkey: "Guy"), profile: nil)
} }
} }

View File

@ -28,7 +28,8 @@ struct EventView: View {
.onTapGesture { .onTapGesture {
UIPasteboard.general.string = event.pubkey UIPasteboard.general.string = event.pubkey
} }
.frame(maxWidth: .infinity, alignment: .leading) Text("\(format_relative_time(event.created_at))")
.foregroundColor(.gray)
Spacer() Spacer()
if (event.pow ?? 0) >= 10 { if (event.pow ?? 0) >= 10 {
Text("\(event.pow ?? 0)") Text("\(event.pow ?? 0)")
@ -56,3 +57,8 @@ func calculate_pow_color(_ pow: Int) -> Color
let x = Double(pow) / 30.0; let x = Double(pow) / 30.0;
return Color(.sRGB, red: 2.0 * (1.0 - x), green: 2.0 * x, blue: 0, opacity: 0.5) 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)))
}