NCShareNewUserAddComment.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. class NCShareNewUserAddComment: UIViewController, NCShareDetail {
  24. @IBOutlet weak var headerContainerView: UIView!
  25. @IBOutlet weak var sharingLabel: UILabel!
  26. @IBOutlet weak var noteTextField: UITextView!
  27. let contentInsets: CGFloat = 16
  28. var onDismiss: (() -> Void)?
  29. public var share: NCTableShareable!
  30. public var metadata: tableMetadata!
  31. override func viewDidLoad() {
  32. super.viewDidLoad()
  33. self.setNavigationTitle()
  34. NotificationCenter.default.addObserver(self, selector: #selector(adjustForKeyboard), name: UIResponder.keyboardWillHideNotification, object: nil)
  35. NotificationCenter.default.addObserver(self, selector: #selector(adjustForKeyboard), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
  36. sharingLabel.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. } onDone: {
  44. self.noteTextField.resignFirstResponder()
  45. self.share.note = self.noteTextField.text
  46. self.navigationController?.popViewController(animated: true)
  47. }
  48. noteTextField.inputAccessoryView = toolbar.wrappedSafeAreaContainer
  49. guard let headerView = (Bundle.main.loadNibNamed("NCShareHeader", owner: self, options: nil)?.first as? NCShareHeader) else { return }
  50. headerContainerView.addSubview(headerView)
  51. headerView.frame = headerContainerView.frame
  52. headerView.translatesAutoresizingMaskIntoConstraints = false
  53. headerView.topAnchor.constraint(equalTo: headerContainerView.topAnchor).isActive = true
  54. headerView.bottomAnchor.constraint(equalTo: headerContainerView.bottomAnchor).isActive = true
  55. headerView.leftAnchor.constraint(equalTo: headerContainerView.leftAnchor).isActive = true
  56. headerView.rightAnchor.constraint(equalTo: headerContainerView.rightAnchor).isActive = true
  57. headerView.setupUI(with: metadata)
  58. }
  59. override func viewWillDisappear(_ animated: Bool) {
  60. super.viewWillDisappear(animated)
  61. share.note = noteTextField.text
  62. onDismiss?()
  63. }
  64. @objc func adjustForKeyboard(notification: Notification) {
  65. guard let keyboardValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue,
  66. let globalTextViewFrame = noteTextField.superview?.convert(noteTextField.frame, to: nil) else { return }
  67. let keyboardScreenEndFrame = keyboardValue.cgRectValue
  68. let portionCovoredByLeyboard = globalTextViewFrame.maxY - keyboardScreenEndFrame.minY
  69. if notification.name == UIResponder.keyboardWillHideNotification || portionCovoredByLeyboard < 0 {
  70. noteTextField.contentInset = .zero
  71. } else {
  72. noteTextField.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: portionCovoredByLeyboard, right: 0)
  73. }
  74. noteTextField.scrollIndicatorInsets = noteTextField.contentInset
  75. }
  76. }