2020-12-17 07:48:06 +01:00
|
|
|
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
|
|
|
|
import Combine
|
|
|
|
import UIKit
|
|
|
|
import ViewModels
|
|
|
|
|
|
|
|
final class AttachmentUploadView: UIView {
|
2021-01-03 02:22:17 +01:00
|
|
|
let label = UILabel()
|
2021-01-04 00:57:40 +01:00
|
|
|
let cancelButton = UIButton(type: .system)
|
2020-12-17 07:48:06 +01:00
|
|
|
let progressView = UIProgressView(progressViewStyle: .default)
|
2021-01-04 00:57:40 +01:00
|
|
|
|
|
|
|
private let viewModel: CompositionViewModel
|
2020-12-17 07:48:06 +01:00
|
|
|
private var progressCancellable: AnyCancellable?
|
2021-01-04 00:57:40 +01:00
|
|
|
private var cancellables = Set<AnyCancellable>()
|
2020-12-17 07:48:06 +01:00
|
|
|
|
2021-02-02 19:02:30 +01:00
|
|
|
// swiftlint:disable:next function_body_length
|
2021-01-04 00:57:40 +01:00
|
|
|
init(viewModel: CompositionViewModel) {
|
|
|
|
self.viewModel = viewModel
|
2020-12-17 07:48:06 +01:00
|
|
|
|
|
|
|
super.init(frame: .zero)
|
|
|
|
|
2021-01-03 02:22:17 +01:00
|
|
|
addSubview(label)
|
|
|
|
label.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
label.adjustsFontForContentSizeCategory = true
|
|
|
|
label.font = .preferredFont(forTextStyle: .callout)
|
|
|
|
label.textAlignment = .center
|
|
|
|
label.text = NSLocalizedString("compose.attachment.uploading", comment: "")
|
|
|
|
label.textColor = .secondaryLabel
|
|
|
|
label.numberOfLines = 0
|
|
|
|
|
2021-01-04 00:57:40 +01:00
|
|
|
addSubview(cancelButton)
|
|
|
|
cancelButton.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
cancelButton.titleLabel?.adjustsFontForContentSizeCategory = true
|
|
|
|
cancelButton.titleLabel?.font = .preferredFont(forTextStyle: .callout)
|
|
|
|
cancelButton.setTitle(NSLocalizedString("cancel", comment: ""), for: .normal)
|
|
|
|
cancelButton.addAction(UIAction { _ in viewModel.cancelUpload() }, for: .touchUpInside)
|
2021-02-02 19:02:30 +01:00
|
|
|
cancelButton.accessibilityLabel =
|
|
|
|
NSLocalizedString("compose.attachment.cancel-upload.accessibility-label", comment: "")
|
2021-01-04 00:57:40 +01:00
|
|
|
|
2020-12-17 07:48:06 +01:00
|
|
|
addSubview(progressView)
|
|
|
|
progressView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
|
|
|
|
NSLayoutConstraint.activate([
|
2021-01-03 02:22:17 +01:00
|
|
|
label.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor),
|
|
|
|
label.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor),
|
2021-01-04 00:57:40 +01:00
|
|
|
label.trailingAnchor.constraint(equalTo: cancelButton.leadingAnchor, constant: .defaultSpacing),
|
|
|
|
cancelButton.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor),
|
|
|
|
cancelButton.trailingAnchor.constraint(equalTo: layoutMarginsGuide.trailingAnchor),
|
|
|
|
cancelButton.bottomAnchor.constraint(equalTo: label.bottomAnchor),
|
2021-01-03 02:22:17 +01:00
|
|
|
progressView.topAnchor.constraint(equalTo: label.bottomAnchor, constant: .defaultSpacing),
|
2020-12-17 07:48:06 +01:00
|
|
|
progressView.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor),
|
|
|
|
progressView.trailingAnchor.constraint(equalTo: layoutMarginsGuide.trailingAnchor),
|
2021-01-03 02:22:17 +01:00
|
|
|
progressView.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor)
|
2020-12-17 07:48:06 +01:00
|
|
|
])
|
2021-01-04 00:57:40 +01:00
|
|
|
|
2021-01-19 20:59:20 +01:00
|
|
|
viewModel.$attachmentUpload.sink { [weak self] attachmentUpload in
|
2021-01-04 00:57:40 +01:00
|
|
|
guard let self = self else { return }
|
|
|
|
|
2021-01-19 20:59:20 +01:00
|
|
|
UIView.animate(withDuration: .zeroIfReduceMotion(.shortAnimationDuration)) {
|
|
|
|
if let attachmentUpload = attachmentUpload {
|
|
|
|
self.progressCancellable = attachmentUpload.progress.publisher(for: \.fractionCompleted)
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { self.progressView.progress = Float($0) }
|
|
|
|
self.isHidden = false
|
|
|
|
} else {
|
|
|
|
self.isHidden = true
|
|
|
|
}
|
2021-01-04 00:57:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.store(in: &cancellables)
|
2020-12-17 07:48:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@available(*, unavailable)
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
}
|