NCShareNewUserAddComment.swift 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // NCShareNewUserAddComment.swift
  3. // Nextcloud
  4. //
  5. // Created by TSI-mc on 21/06/21.
  6. // Copyright © 2022 All rights reserved.
  7. //
  8. // This program is free software: you can redistribute it and/or modify
  9. // it under the terms of the GNU General Public License as published by
  10. // the Free Software Foundation, either version 3 of the License, or
  11. // (at your option) any later version.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU General Public License
  19. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. //
  21. import UIKit
  22. import NextcloudKit
  23. import SVGKit
  24. class NCShareNewUserAddComment: UIViewController, NCShareDetail {
  25. @IBOutlet weak var headerContainerView: UIView!
  26. @IBOutlet weak var sharingLabel: UILabel!
  27. @IBOutlet weak var noteTextField: UITextView!
  28. let contentInsets: CGFloat = 16
  29. var onDismiss: (() -> Void)?
  30. public var share: NCTableShareable!
  31. public var metadata: tableMetadata!
  32. override func viewDidLoad() {
  33. super.viewDidLoad()
  34. self.setNavigationTitle()
  35. NotificationCenter.default.addObserver(self, selector: #selector(adjustForKeyboard), name: UIResponder.keyboardWillHideNotification, object: nil)
  36. NotificationCenter.default.addObserver(self, selector: #selector(adjustForKeyboard), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
  37. sharingLabel.text = NSLocalizedString("_share_note_recipient_", comment: "")
  38. noteTextField.textContainerInset = UIEdgeInsets(top: contentInsets, left: contentInsets, bottom: contentInsets, right: contentInsets)
  39. noteTextField.text = share.note
  40. let toolbar = UIToolbar.toolbar {
  41. self.noteTextField.resignFirstResponder()
  42. self.noteTextField.text = ""
  43. self.share.note = ""
  44. } onDone: {
  45. self.noteTextField.resignFirstResponder()
  46. self.share.note = self.noteTextField.text
  47. self.navigationController?.popViewController(animated: true)
  48. }
  49. noteTextField.inputAccessoryView = toolbar.wrappedSafeAreaContainer
  50. guard let headerView = (Bundle.main.loadNibNamed("NCShareHeader", owner: self, options: nil)?.first as? NCShareHeader) else { return }
  51. headerContainerView.addSubview(headerView)
  52. headerView.frame = headerContainerView.frame
  53. headerView.translatesAutoresizingMaskIntoConstraints = false
  54. headerView.topAnchor.constraint(equalTo: headerContainerView.topAnchor).isActive = true
  55. headerView.bottomAnchor.constraint(equalTo: headerContainerView.bottomAnchor).isActive = true
  56. headerView.leftAnchor.constraint(equalTo: headerContainerView.leftAnchor).isActive = true
  57. headerView.rightAnchor.constraint(equalTo: headerContainerView.rightAnchor).isActive = true
  58. headerView.setupUI(with: metadata)
  59. }
  60. override func viewWillDisappear(_ animated: Bool) {
  61. super.viewWillDisappear(animated)
  62. share.note = noteTextField.text
  63. onDismiss?()
  64. }
  65. @objc func adjustForKeyboard(notification: Notification) {
  66. guard let keyboardValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue,
  67. let globalTextViewFrame = noteTextField.superview?.convert(noteTextField.frame, to: nil) else { return }
  68. let keyboardScreenEndFrame = keyboardValue.cgRectValue
  69. let portionCovoredByLeyboard = globalTextViewFrame.maxY - keyboardScreenEndFrame.minY
  70. if notification.name == UIResponder.keyboardWillHideNotification || portionCovoredByLeyboard < 0 {
  71. noteTextField.contentInset = .zero
  72. } else {
  73. noteTextField.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: portionCovoredByLeyboard, right: 0)
  74. }
  75. noteTextField.scrollIndicatorInsets = noteTextField.contentInset
  76. }
  77. }