Skip to content

Commit

Permalink
refactor: Create one AttachmentView
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinperignon committed Jul 26, 2023
1 parent 082fbae commit d795c54
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 66 deletions.
71 changes: 71 additions & 0 deletions Mail/Components/AttachmentView.swift
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

import MailCore
import MailResources
import SwiftUI

struct AttachmentView<Content: View>: 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")
}
}
59 changes: 19 additions & 40 deletions Mail/Views/New Message/Attachments/AttachmentUploadCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,18 @@ 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)
}
}
}
.padding(.vertical, 1)
.padding(.horizontal, 16)
}
.padding(.top, 16)
}
}
.customAlert(item: $attachmentsManager.globalError) { error in
Expand Down
23 changes: 1 addition & 22 deletions Mail/Views/Thread/AttachmentCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand Down

0 comments on commit d795c54

Please sign in to comment.