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

@@ -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())
}

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 {
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)
}
}

View File

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