Update download attachment, show expiration relative time

This commit is contained in:
Andrew Cope 2022-02-27 18:15:08 -05:00
parent 9f53188f71
commit 385bce2ea7
3 changed files with 27 additions and 32 deletions

View file

@ -56,8 +56,21 @@ class NtfyAttachment: Codable {
return !contentUrl.isEmpty return !contentUrl.isEmpty
} }
func downloadedString() -> String {
return self.isDownloaded() ? "Downloaded" : "Not downloaded"
}
func isExpired() -> Bool {
return TimeInterval(self.expires) < NSDate().timeIntervalSince1970
}
func expiresString() -> String { func expiresString() -> String {
return "Expires \(self.expires)" if (self.isExpired()) {
return "Expired"
}
let date = NSDate(timeIntervalSince1970: TimeInterval(self.expires))
let formatter = RelativeDateTimeFormatter()
return "Expires \(formatter.localizedString(for: date as Date, relativeTo: Date()))"
} }
func download() { func download() {

View file

@ -15,39 +15,21 @@ struct NotificationAttachmentView: View {
@State private var isAttachmentOpen = false @State private var isAttachmentOpen = false
var body: some View { var body: some View {
if attachment.isDownloaded() { HStack {
if let imageUrl = UIImage(contentsOfFile: attachment.contentUrl) { // TODO: Replace paperclip here with mimetype icon
let image = Image(uiImage: imageUrl) Image(systemName: "paperclip")
.resizable() VStack(alignment: .leading) {
.scaledToFit() Text(attachment.name)
ZStack { .font(.footnote)
image HStack {
} Text(attachment.sizeString())
.onTapGesture {
isAttachmentOpen.toggle()
}
.fullScreenCover(isPresented: $isAttachmentOpen, onDismiss: {
// Dismiss logic here
}, content: {
VStack {
image
}
.onTapGesture {
isAttachmentOpen.toggle()
}
})
}
} else {
HStack {
// TODO: Replace paperclip here with mimetype icon
Image(systemName: "paperclip")
VStack(alignment: .leading) {
Text(attachment.name)
.font(.footnote) .font(.footnote)
HStack { .foregroundColor(.gray)
Text(attachment.sizeString()) if (attachment.isDownloaded()) {
Text("Downloaded")
.font(.footnote) .font(.footnote)
.foregroundColor(.gray) .foregroundColor(.gray)
} else {
Text("Not downloaded") Text("Not downloaded")
.font(.footnote) .font(.footnote)
.foregroundColor(.gray) .foregroundColor(.gray)

View file

@ -50,7 +50,7 @@ struct NotificationRow: View {
.padding(.all, 4) .padding(.all, 4)
.onTapGesture { .onTapGesture {
if let attachment = notification.attachment { if let attachment = notification.attachment {
if !attachment.isDownloaded() { if !attachment.isDownloaded() && !attachment.isExpired() {
attachment.download() attachment.download()
} }
} }