diff --git a/Mail/Components/AttachmentView.swift b/Mail/Components/AttachmentView.swift new file mode 100644 index 0000000000..88bfde8a91 --- /dev/null +++ b/Mail/Components/AttachmentView.swift @@ -0,0 +1,71 @@ +/* + Infomaniak Mail - iOS App + Copyright (C) 2022 Infomaniak Network SA + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + */ + +import MailCore +import MailResources +import SwiftUI + +struct AttachmentView: View { + let attachment: Attachment + let subtitle: String + @ViewBuilder let accessory: () -> Content? + + init(attachment: Attachment, subtitle: String, accessory: @escaping () -> Content? = { EmptyView() }) { + self.attachment = attachment + self.subtitle = subtitle + self.accessory = accessory + } + + var body: some View { + VStack(spacing: 0) { + HStack { + attachment.icon.swiftUIImage + .resizable() + .frame(width: 24, height: 24) + + HStack(spacing: 8) { + VStack(alignment: .leading, spacing: 0) { + Text(attachment.name) + .textStyle(.bodySmall) + .lineLimit(1) + .truncationMode(.middle) + + Text(subtitle) + .textStyle(.labelSecondary) + } + + accessory() + } + } + .padding(.horizontal, 8) + .padding(.vertical, 4) + } + .background( + RoundedRectangle(cornerRadius: 6) + .stroke(MailResourcesAsset.elementsColor.swiftUIColor, lineWidth: 1) + ) + .cornerRadius(6) + .frame(maxWidth: 200) + } +} + +struct AttachmentView_Previews: PreviewProvider { + static var previews: some View { + AttachmentView(attachment: PreviewHelper.sampleAttachment, subtitle: "24ko") + } +} diff --git a/Mail/Views/New Message/Attachments/AttachmentUploadCell.swift b/Mail/Views/New Message/Attachments/AttachmentUploadCell.swift index c4e378cff0..8efcf8ec18 100644 --- a/Mail/Views/New Message/Attachments/AttachmentUploadCell.swift +++ b/Mail/Views/New Message/Attachments/AttachmentUploadCell.swift @@ -21,60 +21,39 @@ import MailResources import SwiftUI struct AttachmentUploadCell: View { - let attachment: Attachment @ObservedObject var uploadTask: AttachmentUploadTask + + let attachment: Attachment let attachmentRemoved: ((Attachment) -> Void)? var body: some View { - VStack(spacing: 0) { - HStack { - attachment.icon.swiftUIImage - - VStack(alignment: .leading, spacing: 0) { - Text(attachment.name) - .textStyle(.bodySmall) - .lineLimit(1) - .truncationMode(.middle) - if let error = uploadTask.error { - Text(error.localizedDescription) - .textStyle(.labelSecondary) - } else { - Text(attachment.size, format: .defaultByteCount) - .textStyle(.labelSecondary) - .opacity(attachment.size == 0 ? 0 : 1) - } + AttachmentView( + attachment: attachment, + subtitle: uploadTask.error != nil ? uploadTask.error!.localizedDescription : attachment.size + .formatted(.defaultByteCount) + ) { + Button { + if let attachmentRemoved { + attachmentRemoved(attachment) } - - Button { - if let attachmentRemoved { - attachmentRemoved(attachment) - } - } label: { - MailResourcesAsset.closeSmall.swiftUIImage - .resizable() - .foregroundColor(MailResourcesAsset.textSecondaryColor) - .frame(width: 16, height: 16) - } - .buttonStyle(.borderless) - .padding(.leading, 8) + } label: { + MailResourcesAsset.closeSmall.swiftUIImage + .resizable() + .foregroundColor(MailResourcesAsset.textSecondaryColor) + .frame(width: 12, height: 12) } - .padding(6) + .buttonStyle(.borderless) + } + .overlay(alignment: .bottom) { IndeterminateProgressView(indeterminate: uploadTask.progress == 0, progress: uploadTask.progress) .opacity(uploadTask.progress == 1 ? 0 : 1) } - .background( - RoundedRectangle(cornerRadius: 6) - .stroke(MailResourcesAsset.elementsColor.swiftUIColor, lineWidth: 1) - ) - .cornerRadius(6) - .frame(maxWidth: 200) - .padding(.top, 16) } } struct AttachmentUploadCell_Previews: PreviewProvider { static var previews: some View { - AttachmentUploadCell(attachment: PreviewHelper.sampleAttachment, uploadTask: AttachmentUploadTask()) { _ in + AttachmentUploadCell(uploadTask: AttachmentUploadTask(), attachment: PreviewHelper.sampleAttachment) { _ in /* Preview */ } } diff --git a/Mail/Views/New Message/Attachments/AttachmentsHeaderView.swift b/Mail/Views/New Message/Attachments/AttachmentsHeaderView.swift index f0e99cecc9..a062997152 100644 --- a/Mail/Views/New Message/Attachments/AttachmentsHeaderView.swift +++ b/Mail/Views/New Message/Attachments/AttachmentsHeaderView.swift @@ -30,10 +30,10 @@ struct AttachmentsHeaderView: View { ScrollView(.horizontal, showsIndicators: false) { HStack(spacing: 8) { ForEach(attachmentsManager.attachments) { attachment in - AttachmentUploadCell(attachment: attachment, - uploadTask: attachmentsManager - .attachmentUploadTaskOrFinishedTask(for: attachment - .uuid)) { attachmentRemoved in + AttachmentUploadCell( + uploadTask: attachmentsManager.attachmentUploadTaskOrFinishedTask(for: attachment.uuid), + attachment: attachment + ) { attachmentRemoved in attachmentsManager.removeAttachment(attachmentRemoved) } } @@ -41,6 +41,7 @@ struct AttachmentsHeaderView: View { .padding(.vertical, 1) .padding(.horizontal, 16) } + .padding(.top, 16) } } .customAlert(item: $attachmentsManager.globalError) { error in diff --git a/Mail/Views/Thread/AttachmentCell.swift b/Mail/Views/Thread/AttachmentCell.swift index d707030fe1..cdd83703e0 100644 --- a/Mail/Views/Thread/AttachmentCell.swift +++ b/Mail/Views/Thread/AttachmentCell.swift @@ -24,28 +24,7 @@ struct AttachmentCell: View { let attachment: Attachment var body: some View { - VStack(spacing: 0) { - HStack { - attachment.icon.swiftUIImage - - VStack(alignment: .leading, spacing: 0) { - Text(attachment.name) - .textStyle(.bodySmall) - .lineLimit(1) - .truncationMode(.middle) - Text(attachment.size, format: .defaultByteCount) - .textStyle(.labelSecondary) - .opacity(attachment.size == 0 ? 0 : 1) - } - } - .padding(4) - } - .background( - RoundedRectangle(cornerRadius: 6) - .stroke(MailResourcesAsset.elementsColor.swiftUIColor, lineWidth: 1) - ) - .cornerRadius(6) - .frame(maxWidth: 200) + AttachmentView(attachment: attachment, subtitle: attachment.size.formatted(.defaultByteCount)) } }