From 385bce2ea75f1679002323cec38937da97835fc3 Mon Sep 17 00:00:00 2001 From: Andrew Cope Date: Sun, 27 Feb 2022 18:15:08 -0500 Subject: [PATCH] Update download attachment, show expiration relative time --- ntfy-ios/Models/NtfyAttachment.swift | 15 ++++++- .../Views/NotificationAttachmentView.swift | 42 ++++++------------- ntfy-ios/Views/NotificationRow.swift | 2 +- 3 files changed, 27 insertions(+), 32 deletions(-) diff --git a/ntfy-ios/Models/NtfyAttachment.swift b/ntfy-ios/Models/NtfyAttachment.swift index 508cab6..8f9dbcf 100644 --- a/ntfy-ios/Models/NtfyAttachment.swift +++ b/ntfy-ios/Models/NtfyAttachment.swift @@ -56,8 +56,21 @@ class NtfyAttachment: Codable { return !contentUrl.isEmpty } + func downloadedString() -> String { + return self.isDownloaded() ? "Downloaded" : "Not downloaded" + } + + func isExpired() -> Bool { + return TimeInterval(self.expires) < NSDate().timeIntervalSince1970 + } + 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() { diff --git a/ntfy-ios/Views/NotificationAttachmentView.swift b/ntfy-ios/Views/NotificationAttachmentView.swift index 334fc7d..086a4f3 100644 --- a/ntfy-ios/Views/NotificationAttachmentView.swift +++ b/ntfy-ios/Views/NotificationAttachmentView.swift @@ -15,39 +15,21 @@ struct NotificationAttachmentView: View { @State private var isAttachmentOpen = false var body: some View { - if attachment.isDownloaded() { - if let imageUrl = UIImage(contentsOfFile: attachment.contentUrl) { - let image = Image(uiImage: imageUrl) - .resizable() - .scaledToFit() - ZStack { - image - } - .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) + HStack { + // TODO: Replace paperclip here with mimetype icon + Image(systemName: "paperclip") + VStack(alignment: .leading) { + Text(attachment.name) + .font(.footnote) + HStack { + Text(attachment.sizeString()) .font(.footnote) - HStack { - Text(attachment.sizeString()) + .foregroundColor(.gray) + if (attachment.isDownloaded()) { + Text("Downloaded") .font(.footnote) .foregroundColor(.gray) + } else { Text("Not downloaded") .font(.footnote) .foregroundColor(.gray) diff --git a/ntfy-ios/Views/NotificationRow.swift b/ntfy-ios/Views/NotificationRow.swift index 81e5e30..36ca02e 100644 --- a/ntfy-ios/Views/NotificationRow.swift +++ b/ntfy-ios/Views/NotificationRow.swift @@ -50,7 +50,7 @@ struct NotificationRow: View { .padding(.all, 4) .onTapGesture { if let attachment = notification.attachment { - if !attachment.isDownloaded() { + if !attachment.isDownloaded() && !attachment.isExpired() { attachment.download() } }