SendingController.swift 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import UIKit
  2. import DcCore
  3. protocol SendingControllerDelegate: class {
  4. func onSendingAttemptFinished()
  5. }
  6. class SendingController: UIViewController {
  7. private let dcMsgs: [DcMsg]
  8. private let chatId: Int
  9. private let dcContext: DcContext
  10. weak var delegate: SendingControllerDelegate?
  11. private var progressLabel: UILabel = {
  12. let view = UILabel()
  13. view.translatesAutoresizingMaskIntoConstraints = false
  14. /// TODO: translation!
  15. view.text = "Sending..."
  16. return view
  17. }()
  18. private var activityIndicator: UIActivityIndicatorView = {
  19. let view: UIActivityIndicatorView
  20. if #available(iOS 13, *) {
  21. view = UIActivityIndicatorView(style: .large)
  22. } else {
  23. view = UIActivityIndicatorView(style: .whiteLarge)
  24. view.color = UIColor.gray
  25. }
  26. view.startAnimating()
  27. view.translatesAutoresizingMaskIntoConstraints = false
  28. return view
  29. }()
  30. init(chatId: Int, dcMsgs: [DcMsg], dcContext: DcContext) {
  31. self.chatId = chatId
  32. self.dcMsgs = dcMsgs
  33. self.dcContext = dcContext
  34. super.init(nibName: nil, bundle: nil)
  35. }
  36. required init?(coder: NSCoder) {
  37. fatalError("init(coder:) has not been implemented")
  38. }
  39. override func viewDidLoad() {
  40. view.backgroundColor = DcColors.defaultBackgroundColor
  41. setupViews()
  42. sendMessage()
  43. }
  44. private func setupViews() {
  45. view.addSubview(progressLabel)
  46. view.addSubview(activityIndicator)
  47. view.addConstraints([
  48. progressLabel.constraintCenterXTo(view),
  49. progressLabel.constraintAlignTopTo(view, paddingTop: 25),
  50. activityIndicator.constraintCenterXTo(view),
  51. activityIndicator.constraintCenterYTo(view)
  52. ])
  53. setupNavigationBar()
  54. }
  55. private func setupNavigationBar() {
  56. self.navigationItem.leftBarButtonItem = UIBarButtonItem()
  57. self.navigationItem.titleView = UIImageView(image: UIImage(named: "ic_chat")?.scaleDownImage(toMax: 26))
  58. }
  59. private func sendMessage() {
  60. DispatchQueue.global(qos: .utility).async {
  61. for message in self.dcMsgs {
  62. message.sendInChat(id: self.chatId)
  63. }
  64. self.dcContext.performSmtpJobs()
  65. self.delegate?.onSendingAttemptFinished()
  66. }
  67. }
  68. }