status: support clickable status urls

Changelog-Added: Add support for status URLs
This commit is contained in:
William Casarin
2023-08-23 17:46:15 -07:00
parent 981d500c25
commit 2c6999e15c
3 changed files with 54 additions and 24 deletions

View File

@@ -11,16 +11,14 @@ import MediaPlayer
struct Song {
let started_playing: Date
let content: String
}
struct UserStatus {
let type: UserStatusType
let expires_at: Date?
let content: String
var content: String
let created_at: UInt32
let url: URL?
var url: URL?
func to_note(keypair: FullKeypair) -> NostrEvent? {
return make_user_status_note(status: self, keypair: keypair)

View File

@@ -38,6 +38,7 @@ struct UserStatusSheet: View {
let keypair: Keypair
@State var duration: StatusDuration = .never
@ObservedObject var status: UserStatusModel
@Environment(\.dismiss) var dismiss
@@ -45,7 +46,23 @@ struct UserStatusSheet: View {
Binding(get: {
status.general?.content ?? ""
}, set: { v in
status.general = UserStatus(type: .general, expires_at: duration.expiration, content: v, created_at: UInt32(Date.now.timeIntervalSince1970))
if let general = status.general {
status.general = UserStatus(type: .general, expires_at: duration.expiration, content: v, created_at: UInt32(Date.now.timeIntervalSince1970), url: general.url)
} else {
status.general = UserStatus(type: .general, expires_at: duration.expiration, content: v, created_at: UInt32(Date.now.timeIntervalSince1970), url: nil)
}
})
}
var url_binding: Binding<String> {
Binding(get: {
status.general?.url?.absoluteString ?? ""
}, set: { v in
if let general = status.general {
status.general = UserStatus(type: .general, expires_at: duration.expiration, content: general.content, created_at: UInt32(Date.now.timeIntervalSince1970), url: URL(string: v))
} else {
status.general = UserStatus(type: .general, expires_at: duration.expiration, content: "", created_at: UInt32(Date.now.timeIntervalSince1970), url: URL(string: v))
}
})
}
@@ -58,6 +75,14 @@ struct UserStatusSheet: View {
Text("📋 Working")
})
HStack {
Image("link")
TextField(text: url_binding, label: {
Text("https://example.com")
})
}
HStack {
Text("Clear status")

View File

@@ -17,38 +17,45 @@ struct UserStatusView: View {
@Environment(\.openURL) var openURL
func Status(st: UserStatus, prefix: String = "") -> some View {
HStack {
Text(verbatim: "\(prefix)\(st.content)")
.lineLimit(1)
.foregroundColor(.gray)
.font(.callout.italic())
if st.url != nil {
Image("link")
.resizable()
.frame(width: 16, height: 16)
.foregroundColor(.gray)
}
}
.onTapGesture {
if let url = st.url {
openURL(url)
}
}
}
var body: some View {
VStack(alignment: .leading, spacing: 2) {
if show_general, let general = status.general {
Text(verbatim: "\(general.content)")
.lineLimit(1)
.foregroundColor(.gray)
.font(.callout.italic())
.onTapGesture {
if let url = general.url {
openURL(url)
}
}
Status(st: general)
}
if show_music, let playing = status.music {
Text(verbatim: "🎵\(playing.content)")
.lineLimit(1)
.foregroundColor(.gray)
.font(.callout.italic())
.onTapGesture {
if let url = playing.url {
openURL(url)
}
}
Status(st: playing, prefix: "🎵")
}
}
}
}
/*
struct UserStatusView_Previews: PreviewProvider {
static var previews: some View {
UserStatusView(status: .init(), show_general: true, show_music: true)
UserStatusView(status: UserStatus(type: .music, expires_at: nil, content: "Track - Artist", created_at: 0, url: URL(string: "spotify:search:abc")), show_general: true, show_music: true)
}
}
*/