Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttachmentsFeatureElementView - Block empty file upload/download/preview #791

Merged
merged 4 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions Sources/ArcGISToolkit/Common/AttachmentList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct AttachmentRow: View {
@ObservedObject var attachmentModel: AttachmentModel

/// The url of the the attachment, used to display the attachment via `QuickLook`.
@State var url: URL?
@State private var url: URL?

var body: some View {
HStack {
Expand Down Expand Up @@ -67,9 +67,14 @@ struct AttachmentRow: View {
struct AttachmentLoadButton: View {
@ObservedObject var attachmentModel: AttachmentModel

/// A Boolean value indicating whether the download alert is presented.
@State private var downloadAlertIsPresented = false

var body: some View {
Button {
if attachmentModel.loadStatus == .notLoaded {
if attachmentModel.attachment.measuredSize.value.isZero {
downloadAlertIsPresented = true
} else if attachmentModel.loadStatus == .notLoaded {
attachmentModel.load()
}
} label: {
Expand All @@ -95,5 +100,8 @@ struct AttachmentLoadButton: View {
.frame(width: 24, height: 24)
.padding(.leading)
}
.alert(isPresented: $downloadAlertIsPresented) {
Alert(title: Text.emptyAttachmentDownloadErrorMessage)
}
Comment on lines +103 to +105
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: This API is deprecated.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, thank you. Not sure how I missed that. #794

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're welcome.

}
}
8 changes: 6 additions & 2 deletions Sources/ArcGISToolkit/Common/AttachmentModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

import ArcGIS
import Combine
import OSLog
import QuickLook
import SwiftUI

Expand Down Expand Up @@ -86,7 +86,11 @@ import SwiftUI
func load() {
Task {
loadStatus = .loading
try await attachment.load()
do {
try await attachment.load()
} catch {
Logger.attachmentsFeatureElementView.error("Attachment loading failed \(error.localizedDescription)")
}
sync()
if loadStatus == .failed || attachment.fileURL == nil {
systemImageName = "exclamationmark.circle.fill"
Expand Down
8 changes: 8 additions & 0 deletions Sources/ArcGISToolkit/Common/AttachmentPreview.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ struct AttachmentPreview: View {
/// The model representing the attachment to display.
@ObservedObject var attachmentModel: AttachmentModel

/// A Boolean value indicating whether the download alert is presented.
@State private var downloadAlertIsPresented = false

/// The url of the the attachment, used to display the attachment via `QuickLook`.
@State private var url: URL?

Expand Down Expand Up @@ -205,12 +208,17 @@ struct AttachmentPreview: View {
if attachmentModel.attachment.loadStatus == .loaded {
// Set the url to trigger `.quickLookPreview`.
url = attachmentModel.attachment.fileURL
} else if attachmentModel.attachment.measuredSize.value.isZero {
downloadAlertIsPresented = true
} else if attachmentModel.attachment.loadStatus == .notLoaded {
// Load the attachment model with the given size.
attachmentModel.load()
}
}
.quickLookPreview($url)
.alert(isPresented: $downloadAlertIsPresented) {
Alert(title: Text.emptyAttachmentDownloadErrorMessage)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
/// The possible errors when importing an attachment.
enum AttachmentImportError: Error, Equatable {
case dataInaccessible
case emptyFilesNotSupported
case sizeLimitExceeded
case system(String)
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ struct AttachmentImportMenu: View {
importState = .errored(.sizeLimitExceeded)
return
}
guard attachmentSize.value > .zero else {
importState = .errored(.emptyFilesNotSupported)
return
}

defer { importState = .none }
let fileName: String
Expand Down Expand Up @@ -260,6 +264,15 @@ private extension AttachmentImportMenu {
)
}

/// An error message indicating the selected attachment is an empty file and not supported.
var emptyFilesNotSupportedAlertMessage: String {
.init(
localized: "Empty files are not supported.",
bundle: .toolkitModule,
comment: "An error message indicating the selected attachment is an empty file and not supported."
)
}

/// A label for a button to choose an file from the user's files.
var filesButtonLabel: String {
.init(
Expand All @@ -285,6 +298,8 @@ private extension AttachmentImportMenu {
var importFailureAlertMessage: String {
guard case .errored(let attachmentImportError) = importState else { return "" }
return switch attachmentImportError {
case .emptyFilesNotSupported:
emptyFilesNotSupportedAlertMessage
case .sizeLimitExceeded:
sizeLimitExceededImportFailureAlertMessage
default:
Expand Down
22 changes: 22 additions & 0 deletions Sources/ArcGISToolkit/Extensions/OS/Logger.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2024 Esri
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import OSLog

extension Logger {
/// A logger for the common `AttachmentsFeatureElementView` view.
static let attachmentsFeatureElementView: Logger = {
Logger(subsystem: Bundle.toolkitIdentifier, category: "AttachmentsFeatureElementView")
}()
}
9 changes: 9 additions & 0 deletions Sources/ArcGISToolkit/Extensions/SwiftUI/Text.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ extension Text {
)
}

/// An error message explaining attachments with empty files (0 bytes) cannot be downloaded.
static var emptyAttachmentDownloadErrorMessage: Self {
.init(
"Empty attachments cannot be downloaded.",
bundle: .toolkitModule,
comment: "An error message explaining attachments with empty files (0 bytes) cannot be downloaded."
)
}

/// Localized text for the word "Required".
static var required: Self {
Text(
Expand Down
6 changes: 6 additions & 0 deletions Sources/ArcGISToolkit/Resources/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ for a utility network trace. */
AR experience. */
"elevation" = "elevation";

/* An error message explaining attachments with empty files (0 bytes) cannot be downloaded. */
"Empty attachments cannot be downloaded." = "Empty attachments cannot be downloaded.";

/* An error message indicating the selected attachment is an empty file and not supported. */
"Empty files are not supported." = "Empty files are not supported.";

/* Text indicating a field's value must be within the
allowed length range. The first and second parameter
hold the minimum and maximum length respectively. */
Expand Down