Skip to content

Commit

Permalink
feat: Add the progress bar
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthieu-dgl committed Sep 19, 2024
1 parent 407aaa7 commit 62c9d20
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 30 deletions.
50 changes: 29 additions & 21 deletions Mail/Views/Thread/Message/Attachment/AttachmentsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ struct AttachmentsView: View {
@ModalState private var attachmentsURL: AttachmentsURL?
@State private var downloadInProgress = false
@State private var downloadProgressState: [String: Double] = [:]
@State private var isDownloadDisabled = false
@State private var showProgressCircle = false

private var attachments: [Attachment] {
return message.attachments.filter { $0.disposition == .attachment || $0.contentId == nil }
Expand Down Expand Up @@ -65,34 +67,32 @@ struct AttachmentsView: View {
HStack(spacing: IKPadding.small) {
ForEach(attachments) { attachment in
let progress = downloadProgressState[attachment.uuid] ?? 0

Button {
openAttachment(attachment)
} label: {
AttachmentView(
attachment: attachment
) {
if progress > 0 && progress < 1 {
CircleIndeterminateProgressView(progress: progress)
} else {
nil
}
}
attachment: attachment,
isDownloading: progress > 0 && progress < 1,
downloadProgress: progress
)
}
.disabled(isDownloadDisabled)
}
if let swissTransferAttachment = message.swissTransferAttachment {
ForEach(swissTransferAttachment.files) { file in
let progress = downloadProgressState[file.uuid] ?? 0

Button {
downloadSwissTransferAttachment(stUuid: swissTransferAttachment.uuid, fileUuid: file.uuid)
} label: {
AttachmentView(swissTransferFile: file) {
if progress > 0 && progress < 1 {
CircleIndeterminateProgressView(progress: progress)
} else {
nil
}
}
AttachmentView(
swissTransferFile: file,
isDownloading: progress > 0 && progress < 1,
downloadProgress: progress
)
}
.disabled(isDownloadDisabled)
}
}
}
Expand All @@ -110,21 +110,24 @@ struct AttachmentsView: View {
.textStyle(.bodySmallSecondary)
.frame(maxWidth: .infinity, alignment: .leading)
.multilineTextAlignment(.leading)
HStack{

HStack {
let progress = downloadProgressState[message.swissTransferAttachment?.uuid ?? ""] ?? 0
CircleIndeterminateProgressView(progress: progress)
if showProgressCircle {
CircleIndeterminateProgressView(progress: progress)
.opacity(progress == 1 ? 0 : 1)
}
Button {
showProgressCircle = true
isDownloadDisabled = true
downloadAllAttachments()
} label: {
Text(MailResourcesStrings.Localizable.buttonDownloadAll)

}
.buttonStyle(.ikBorderless(isInlined: true))
.controlSize(.small)
.disabled(isDownloadDisabled)
}

}
}
.padding(.horizontal, value: .medium)
Expand All @@ -146,8 +149,10 @@ struct AttachmentsView: View {
matomo.track(eventWithCategory: .attachmentActions, name: "open")
previewedAttachment = attachment
if !FileManager.default.fileExists(atPath: attachment.getLocalURL(mailboxManager: mailboxManager).path) {
Task {
downloadProgressState[attachment.uuid] = 0.0
Task { @MainActor in
await mailboxManager.saveAttachmentLocally(attachment: attachment)
downloadProgressState[attachment.uuid] = 1.0
}
}
}
Expand All @@ -171,6 +176,7 @@ struct AttachmentsView: View {

private func downloadAllAttachments() {
downloadInProgress = true
isDownloadDisabled = true
Task {
await tryOrDisplayError {
matomo.track(eventWithCategory: .message, name: "downloadAll")
Expand All @@ -197,6 +203,8 @@ struct AttachmentsView: View {
}
}
downloadInProgress = false
isDownloadDisabled = false
showProgressCircle = false
}
}
}
Expand Down
25 changes: 20 additions & 5 deletions MailCoreUI/Components/AttachmentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,38 +27,53 @@ public struct AttachmentView<Content: View>: View {
private let icon: MailResourcesImages

@ViewBuilder let accessory: () -> Content?
let isDownloading: Bool
let downloadProgress: Double

public init(
title: String,
subtitle: String,
icon: MailResourcesImages,
isDownloading: Bool = false,
downloadProgress: Double = 0,
accessory: @escaping () -> Content? = { EmptyView() }
) {
self.title = title
self.subtitle = subtitle
self.icon = icon
self.isDownloading = isDownloading
self.downloadProgress = downloadProgress
self.accessory = accessory
}

public init(attachment: Attachment, accessory: @escaping () -> Content? = { EmptyView() }) {
public init(attachment: Attachment, isDownloading: Bool = false, downloadProgress: Double = 0, accessory: @escaping () -> Content? = { EmptyView() }) {

Check warning on line 49 in MailCoreUI/Components/AttachmentView.swift

View workflow job for this annotation

GitHub Actions / SwiftFormat

Wrap lines that exceed the specified maximum width. (wrap)
title = attachment.name
subtitle = attachment.size.formatted(.defaultByteCount)
icon = attachment.icon
self.isDownloading = isDownloading
self.downloadProgress = downloadProgress
self.accessory = accessory
}

public init(swissTransferFile: File, accessory: @escaping () -> Content? = { EmptyView() }) {
public init(swissTransferFile: File, isDownloading: Bool = false, downloadProgress: Double = 0, accessory: @escaping () -> Content? = { EmptyView() }) {

Check warning on line 58 in MailCoreUI/Components/AttachmentView.swift

View workflow job for this annotation

GitHub Actions / SwiftFormat

Wrap lines that exceed the specified maximum width. (wrap)
title = swissTransferFile.name
subtitle = swissTransferFile.size.formatted(.defaultByteCount)
icon = swissTransferFile.icon
self.isDownloading = isDownloading
self.downloadProgress = downloadProgress
self.accessory = accessory
}

public var body: some View {
HStack(spacing: IKPadding.small) {
icon
.iconSize(.large)
.foregroundStyle(MailResourcesAsset.textSecondaryColor)
if isDownloading {
CircleIndeterminateProgressView(progress: 0.5)
.frame(height: 40)
} else {
icon
.iconSize(.large)
.foregroundStyle(MailResourcesAsset.textSecondaryColor)
}

VStack(alignment: .leading, spacing: 0) {
Text(title)
Expand Down
8 changes: 4 additions & 4 deletions MailCoreUI/Components/CircleIndeterminateProgressView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@ public struct CircleIndeterminateProgressView: View {
public var body: some View {
ZStack {

Check warning on line 29 in MailCoreUI/Components/CircleIndeterminateProgressView.swift

View workflow job for this annotation

GitHub Actions / SwiftFormat

Indent code in accordance with the scope level. (indent)
Circle()

Check warning on line 30 in MailCoreUI/Components/CircleIndeterminateProgressView.swift

View workflow job for this annotation

GitHub Actions / SwiftFormat

Indent code in accordance with the scope level. (indent)
.stroke(lineWidth: 5)
.stroke(lineWidth: 2)

Check warning on line 31 in MailCoreUI/Components/CircleIndeterminateProgressView.swift

View workflow job for this annotation

GitHub Actions / SwiftFormat

Indent code in accordance with the scope level. (indent)
.opacity(0.1)

Check warning on line 32 in MailCoreUI/Components/CircleIndeterminateProgressView.swift

View workflow job for this annotation

GitHub Actions / SwiftFormat

Indent code in accordance with the scope level. (indent)
.foregroundColor(Color.gray)

Check warning on line 33 in MailCoreUI/Components/CircleIndeterminateProgressView.swift

View workflow job for this annotation

GitHub Actions / SwiftFormat

Indent code in accordance with the scope level. (indent)
.frame(height: 20)
.frame(height: 15)

Check warning on line 34 in MailCoreUI/Components/CircleIndeterminateProgressView.swift

View workflow job for this annotation

GitHub Actions / SwiftFormat

Indent code in accordance with the scope level. (indent)

Circle()

Check warning on line 36 in MailCoreUI/Components/CircleIndeterminateProgressView.swift

View workflow job for this annotation

GitHub Actions / SwiftFormat

Indent code in accordance with the scope level. (indent)
.trim(from: 0.0, to: min(progress, 1.0))

Check warning on line 37 in MailCoreUI/Components/CircleIndeterminateProgressView.swift

View workflow job for this annotation

GitHub Actions / SwiftFormat

Indent code in accordance with the scope level. (indent)
.stroke(style: StrokeStyle(lineWidth: 5, lineCap: .round, lineJoin: .round))
.stroke(style: StrokeStyle(lineWidth: 2, lineCap: .round, lineJoin: .round))
.foregroundColor(Color.accentColor)
.rotationEffect(Angle(degrees: 270.0))
.animation(.linear, value: progress)
.frame(height: 20)
.frame(height: 15)
}
}
}
Expand Down

0 comments on commit 62c9d20

Please sign in to comment.