metatext/Views/UIKit/AttachmentUploadView.swift

69 lines
3 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: AttachmentUploadViewModel
2021-01-03 23:57:40 +00:00
private var cancellables = Set<AnyCancellable>()
2020-12-17 06:48:06 +00:00
init(viewModel: AttachmentUploadViewModel) {
2021-01-03 23:57:40 +00:00
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.cancel() }, for: .touchUpInside)
2021-02-02 18:02:30 +00:00
cancelButton.accessibilityLabel =
NSLocalizedString("compose.attachment.cancel-upload.accessibility-label", comment: "")
2021-01-03 23:57:40 +00:00
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.progress.publisher(for: \.fractionCompleted)
.receive(on: DispatchQueue.main)
.sink { [weak self] in self?.progressView.progress = Float($0) }
.store(in: &cancellables)
2020-12-17 06:48:06 +00:00
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
extension AttachmentUploadView {
var id: AttachmentUploadViewModel.Id { viewModel.id }
}