forked from signalapp/Signal-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSharingThreadPickerProgressSheet.swift
More file actions
140 lines (112 loc) · 4.87 KB
/
Copy pathSharingThreadPickerProgressSheet.swift
File metadata and controls
140 lines (112 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//
// Copyright 2024 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
//
public import SignalServiceKit
public import SignalUI
public class SharingThreadPickerProgressSheet: ActionSheetController {
private var attachmentIds: [Attachment.IDType]
/// Note: progress for _all_ attachments, not just those in attachmentIds.
/// Filter down to just attachmentIds for display purposes.
private var progressPerAttachment: [Attachment.IDType: Float] = [:]
public init(
attachmentIds: [Attachment.IDType],
delegate: ShareViewDelegate?,
) {
self.attachmentIds = attachmentIds
super.init()
setupSubviews()
let cancelAction = ActionSheetAction(
title: CommonStrings.dismissButton,
style: .cancel,
) { [weak delegate] _ in
delegate?.shareViewWasCancelled()
}
super.addAction(cancelAction)
NotificationCenter.default.addObserver(
self,
selector: #selector(handleAttachmentProgressNotification(_:)),
name: Upload.Constants.attachmentUploadProgressNotification,
object: nil,
)
}
deinit {
NotificationCenter.default.removeObserver(self)
}
// MARK: - API
public func updateSendingAttachmentIds(_ ids: [Attachment.IDType]) {
// the next upload progress update
self.attachmentIds = ids
renderProgress()
}
// MARK: - UI Elements
private lazy var headerWithProgress: UIView = {
let headerWithProgress = UIView()
headerWithProgress.layoutMargins = UIEdgeInsets(top: 16, left: 16, bottom: 16, right: 16)
return headerWithProgress
}()
private lazy var progressLabel: UILabel = {
let progressLabel = UILabel()
progressLabel.textAlignment = .center
progressLabel.numberOfLines = 0
progressLabel.lineBreakMode = .byWordWrapping
progressLabel.font = UIFont.dynamicTypeSubheadlineClamped.semibold()
progressLabel.textColor = Theme.primaryTextColor
progressLabel.text = OWSLocalizedString("SHARE_EXTENSION_SENDING_IN_PROGRESS_TITLE", comment: "Alert title")
return progressLabel
}()
private lazy var progressView = UIProgressView(progressViewStyle: .default)
private func setupSubviews() {
headerWithProgress.addSubview(progressLabel)
progressLabel.autoPinWidthToSuperviewMargins()
progressLabel.autoPinTopToSuperviewMargin()
headerWithProgress.addSubview(progressView)
progressView.autoPinWidthToSuperviewMargins()
progressView.autoPinEdge(.top, to: .bottom, of: progressLabel, withOffset: 8)
progressView.autoPinBottomToSuperviewMargin()
super.customHeader = headerWithProgress
}
// MARK: - Updating UI
private func renderProgress() {
guard attachmentIds.isEmpty.negated else {
progressLabel.text = OWSLocalizedString(
"MESSAGE_STATUS_SENDING",
comment: "message status while message is sending.",
)
return
}
let progressValues = attachmentIds.map { progressPerAttachment[$0] ?? 0 }
// Attachments can upload in parallel, so we show the progress
// of the average of all the individual attachment's progress.
progressView.progress = progressValues.reduce(0, +) / Float(attachmentIds.count)
// In order to indicate approximately how many attachments remain
// to upload, we look at the number that have had their progress
// reach 100%.
let totalCompleted = progressValues.filter { $0 == 1 }.count
progressLabel.text = String.nonPluralLocalizedStringWithFormat(
Self.progressFormat,
OWSFormat.formatInt(min(totalCompleted + 1, attachmentIds.count)),
OWSFormat.formatInt(attachmentIds.count),
)
}
private static let progressFormat = OWSLocalizedString(
"SHARE_EXTENSION_SENDING_IN_PROGRESS_FORMAT",
comment: "Send progress for share extension. Embeds {{ %1$@ number of attachments uploaded, %2$@ total number of attachments}}",
)
// MARK: Notifications
@objc
private func handleAttachmentProgressNotification(_ notification: NSNotification) {
// We can safely show the progress for just the first message,
// all the messages share the same attachment upload progress.
guard let notificationAttachmentId = notification.userInfo?[Upload.Constants.uploadAttachmentIDKey] as? Attachment.IDType else {
owsFailDebug("Missing notificationAttachmentId.")
return
}
guard let progress = notification.userInfo?[Upload.Constants.uploadProgressKey] as? Float else {
owsFailDebug("Missing progress.")
return
}
progressPerAttachment[notificationAttachmentId] = progress
renderProgress()
}
}