NCShareNewUserAddComment.swift 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //
  2. // NCShareNewUserAddComment.swift
  3. // Nextcloud
  4. //
  5. // Created by TSI-mc on 21/06/21.
  6. // Copyright © 2021 Marino Faggiana. All rights reserved.
  7. // Copyright © 2021 TSI-mc. All rights reserved.
  8. //
  9. import UIKit
  10. import NCCommunication
  11. import SVGKit
  12. class NCShareNewUserAddComment: UIViewController, NCShareDetail {
  13. @IBOutlet weak var headerContainerView: UIView!
  14. @IBOutlet weak var sharingLabel: UILabel!
  15. @IBOutlet weak var sharingNote: UILabel!
  16. @IBOutlet weak var noteTextField: UITextView!
  17. let contentInsets: CGFloat = 16
  18. var onDismiss: (() -> Void)?
  19. public var share: TableShareable!
  20. public var metadata: tableMetadata!
  21. override func viewDidLoad() {
  22. super.viewDidLoad()
  23. self.setNavigationTitle()
  24. NotificationCenter.default.addObserver(self, selector: #selector(adjustForKeyboard), name: UIResponder.keyboardWillHideNotification, object: nil)
  25. NotificationCenter.default.addObserver(self, selector: #selector(adjustForKeyboard), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
  26. guard let headerView = (Bundle.main.loadNibNamed("NCShareAdvancePermissionHeader", owner: self, options: nil)?.first as? NCShareAdvancePermissionHeader) else { return }
  27. headerContainerView.addSubview(headerView)
  28. headerView.frame = headerContainerView.frame
  29. headerView.translatesAutoresizingMaskIntoConstraints = false
  30. headerView.topAnchor.constraint(equalTo: headerContainerView.topAnchor).isActive = true
  31. headerView.bottomAnchor.constraint(equalTo: headerContainerView.bottomAnchor).isActive = true
  32. headerView.leftAnchor.constraint(equalTo: headerContainerView.leftAnchor).isActive = true
  33. headerView.rightAnchor.constraint(equalTo: headerContainerView.rightAnchor).isActive = true
  34. headerView.setupUI(with: metadata)
  35. sharingLabel.text = NSLocalizedString("_sharing_", comment: "")
  36. sharingNote.text = NSLocalizedString("_share_note_recipient_", comment: "")
  37. noteTextField.textContainerInset = UIEdgeInsets(top: contentInsets, left: contentInsets, bottom: contentInsets, right: contentInsets)
  38. noteTextField.text = share.note
  39. let toolbar = UIToolbar.toolbar {
  40. self.noteTextField.resignFirstResponder()
  41. self.noteTextField.text = ""
  42. self.share.note = ""
  43. } completion: {
  44. self.noteTextField.resignFirstResponder()
  45. self.share.note = self.noteTextField.text
  46. }
  47. noteTextField.inputAccessoryView = toolbar
  48. }
  49. override func viewWillDisappear(_ animated: Bool) {
  50. super.viewWillDisappear(animated)
  51. share.note = noteTextField.text
  52. onDismiss?()
  53. }
  54. @objc func adjustForKeyboard(notification: Notification) {
  55. guard let keyboardValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue,
  56. let globalTextViewFrame = noteTextField.superview?.convert(noteTextField.frame, to: nil) else { return }
  57. let keyboardScreenEndFrame = keyboardValue.cgRectValue
  58. let portionCovoredByLeyboard = globalTextViewFrame.maxY - keyboardScreenEndFrame.minY
  59. if notification.name == UIResponder.keyboardWillHideNotification || portionCovoredByLeyboard < 0 {
  60. noteTextField.contentInset = .zero
  61. } else {
  62. noteTextField.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: portionCovoredByLeyboard, right: 0)
  63. }
  64. noteTextField.scrollIndicatorInsets = noteTextField.contentInset
  65. }
  66. }