metatext/Views/AttachmentUploadView.swift

73 lines
3.1 KiB
Swift
Raw Normal View History

2020-12-17 06:48:06 +00:00
// Copyright © 2020 Metabolist. All rights reserved.
import Combine
import UIKit
import ViewModels
final class AttachmentUploadView: UIView {
2021-01-03 01:22:17 +00:00
let label = UILabel()
2021-01-03 23:57:40 +00:00
let cancelButton = UIButton(type: .system)
2020-12-17 06:48:06 +00:00
let progressView = UIProgressView(progressViewStyle: .default)
2021-01-03 23:57:40 +00:00
private let viewModel: CompositionViewModel
2020-12-17 06:48:06 +00:00
private var progressCancellable: AnyCancellable?
2021-01-03 23:57:40 +00:00
private var cancellables = Set<AnyCancellable>()
2020-12-17 06:48:06 +00:00
2021-01-03 23:57:40 +00:00
init(viewModel: CompositionViewModel) {
self.viewModel = viewModel
2020-12-17 06:48:06 +00:00
super.init(frame: .zero)
2021-01-03 01:22:17 +00: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-03 23:57:40 +00: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)
2020-12-17 06:48:06 +00:00
addSubview(progressView)
progressView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
2021-01-03 01:22:17 +00:00
label.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor),
label.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor),
2021-01-03 23:57:40 +00: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 01:22:17 +00:00
progressView.topAnchor.constraint(equalTo: label.bottomAnchor, constant: .defaultSpacing),
2020-12-17 06:48:06 +00:00
progressView.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor),
progressView.trailingAnchor.constraint(equalTo: layoutMarginsGuide.trailingAnchor),
2021-01-03 01:22:17 +00:00
progressView.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor)
2020-12-17 06:48:06 +00:00
])
2021-01-03 23:57:40 +00:00
viewModel.$attachmentUpload.sink { [weak self] in
guard let self = self else { return }
if let attachmentUpload = $0 {
self.progressCancellable = attachmentUpload.progress.publisher(for: \.fractionCompleted)
.receive(on: DispatchQueue.main)
.sink { self.progressView.progress = Float($0) }
self.isHidden = false
} else {
self.isHidden = true
}
}
.store(in: &cancellables)
2020-12-17 06:48:06 +00:00
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}