Skip to content

Commit

Permalink
Add indication of publishing progress to the publish button.
Browse files Browse the repository at this point in the history
Contributes to  #846 Toot lost in poor network conditions
  • Loading branch information
whattherestimefor committed Dec 13, 2024
1 parent a992c3a commit 54ee560
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion Mastodon/Scene/Compose/ComposeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,51 @@ import MastodonSDK

final class ComposeViewController: UIViewController {
static let minAutoCompleteVisibleHeight: CGFloat = 100
lazy var publishProgressView: UIProgressView = {
let progressView = UIProgressView(progressViewStyle: .bar)
progressView.alpha = 0
return progressView
}()

var disposeBag = Set<AnyCancellable>()
var viewModel: ComposeViewModel

init(viewModel: ComposeViewModel) {
self.viewModel = viewModel
super.init(nibName: nil, bundle: nil)
self.setUpPublishingIndicator()
}

required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }

func setUpPublishingIndicator() {
publishProgressView.translatesAutoresizingMaskIntoConstraints = false
publishProgressView.tintColor = .systemIndigo
publishProgressView.trackTintColor = .systemGray
publishButton.addSubview(publishProgressView)
let constraints = [
publishProgressView.leadingAnchor.constraint(equalTo: publishButton.leadingAnchor),
publishProgressView.trailingAnchor.constraint(equalTo: publishButton.trailingAnchor),
publishProgressView.topAnchor.constraint(equalTo: publishButton.topAnchor),
publishProgressView.bottomAnchor.constraint(equalTo: publishButton.bottomAnchor),
publishProgressView.heightAnchor.constraint(greaterThanOrEqualToConstant: 35)
]
NSLayoutConstraint.activate(constraints)

PublisherService.shared.$currentPublishProgress
.receive(on: DispatchQueue.main)
.sink { [weak self] progress in
guard let self = self else { return }
let progress = Float(progress)
if progress > 0 {
UIView.animate(withDuration: 0.25) {
self.publishProgressView.alpha = 1
}
self.publishProgressView.setProgress(progress, animated: true)
}
}
.store(in: &disposeBag)
}

lazy var composeContentViewModel: ComposeContentViewModel = {

Expand Down Expand Up @@ -241,13 +276,24 @@ extension ComposeViewController {
private func enqueuePublishStatus() {
do {
let statusPublisher = try composeContentViewModel.statusPublisher()
cancelBarButtonItem.isEnabled = false
publishButton.isEnabled = false
statusPublisher.state
.receive(on: DispatchQueue.main)
.sink { [weak self] result in
self?.cancelBarButtonItem.isEnabled = true

switch result {
case .success:
self?.dismiss(animated: true, completion: nil)
self?.publishProgressView.progress = 100
DispatchQueue.main.asyncAfter(deadline: .now() + 0.25) {
self?.dismiss(animated: true, completion: nil)
}
case .failure(let error):
UIView.animate(withDuration: 0.25) {
self?.publishProgressView.alpha = 0
}
self?.publishButton.isEnabled = true
let alertController = UIAlertController.standardAlert(of: error)
self?.present(alertController, animated: true)
// HomeTimelineViewController is also listening and will post the alert if this view has been dismissed
Expand Down

0 comments on commit 54ee560

Please sign in to comment.