NCText.swift 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. //
  2. // NCText.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 24/07/17.
  6. // Copyright (c) 2017 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import Foundation
  24. @objc protocol NCTextDelegate {
  25. func dismissTextView()
  26. }
  27. class NCText: UIViewController, UITextViewDelegate {
  28. @IBOutlet weak var cancelButton: UIBarButtonItem!
  29. @IBOutlet weak var nextButton: UIBarButtonItem!
  30. @IBOutlet weak var textView: UITextView!
  31. @IBOutlet weak var bottomConstraint: NSLayoutConstraint!
  32. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  33. @objc var metadata: tableMetadata?
  34. @objc var delegate: NCTextDelegate?
  35. var loadText: String?
  36. override func viewDidLoad() {
  37. super.viewDidLoad()
  38. NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShowHandle(info:)), name: UIResponder.keyboardWillShowNotification, object: nil)
  39. NotificationCenter.default.addObserver(self, selector:#selector(self.keyboardWillHideHandle), name: UIResponder.keyboardWillHideNotification, object: nil)
  40. self.navigationController?.navigationBar.topItem?.title = NSLocalizedString("_untitled_txt_", comment: "")
  41. cancelButton.title = NSLocalizedString("_cancel_", comment: "")
  42. nextButton.title = NSLocalizedString("_next_", comment: "")
  43. // Modify
  44. if let metadata = metadata {
  45. loadText = ""
  46. let path = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  47. let data = NSData(contentsOfFile: path)
  48. if let data = data {
  49. let encodingCFName = NCUchardet.sharedNUCharDet().encodingCFStringDetect(with: data as Data)
  50. let se = CFStringConvertEncodingToNSStringEncoding(encodingCFName)
  51. let encoding = String.Encoding(rawValue: se)
  52. loadText = try? String(contentsOfFile: path, encoding: encoding)
  53. textView.text = loadText
  54. nextButton.title = NSLocalizedString("_save_", comment: "")
  55. self.navigationController?.navigationBar.topItem?.title = NSLocalizedString(metadata.fileNameView, comment: "")
  56. }
  57. } else {
  58. loadText = ""
  59. }
  60. textView.isUserInteractionEnabled = true
  61. textView.becomeFirstResponder()
  62. textView.delegate = self
  63. textView.selectedTextRange = textView.textRange(from: textView.beginningOfDocument, to: textView.beginningOfDocument)
  64. textView.font = UIFont(name: "NameOfTheFont", size: 20)
  65. // Theming view
  66. NotificationCenter.default.addObserver(self, selector: #selector(self.changeTheming), name: NSNotification.Name(rawValue: "changeTheming"), object: nil)
  67. changeTheming()
  68. textViewDidChange(textView)
  69. }
  70. @objc func keyboardWillShowHandle(info:NSNotification) {
  71. let frameView = self.view.convert(self.view.bounds, to: self.view.window)
  72. let endView = frameView.origin.y + frameView.size.height
  73. if let keyboardSize = (info.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue, let _ = self.view.window?.frame {
  74. if endView - keyboardSize.origin.y > 0 {
  75. bottomConstraint.constant = endView - keyboardSize.origin.y
  76. } else {
  77. bottomConstraint.constant = 0
  78. }
  79. }
  80. }
  81. override func viewDidDisappear(_ animated: Bool) {
  82. appDelegate.activeDetail?.viewFile()
  83. }
  84. @objc func keyboardWillHideHandle() {
  85. bottomConstraint.constant = 0
  86. }
  87. func textViewDidChange(_ textView: UITextView) {
  88. if textView.text.count == 0 {
  89. nextButton.isEnabled = false
  90. } else {
  91. nextButton.isEnabled = true
  92. }
  93. }
  94. @objc func changeTheming() {
  95. appDelegate.changeTheming(self, tableView: nil, collectionView: nil, form: false)
  96. textView.backgroundColor = NCBrandColor.sharedInstance.backgroundForm
  97. textView.textColor = NCBrandColor.sharedInstance.textView
  98. }
  99. @IBAction func cancelButtonTapped(_ sender: AnyObject) {
  100. if textView.text != loadText {
  101. let alertController = UIAlertController(title: NSLocalizedString("_info_", comment: ""), message: NSLocalizedString("_save_exit_", comment: ""), preferredStyle: .alert)
  102. let actionYes = UIAlertAction(title: NSLocalizedString("_yes_", comment: ""), style: .default) { (action:UIAlertAction) in
  103. self.dismiss(animated: true, completion: {
  104. self.delegate?.dismissTextView()
  105. })
  106. }
  107. let actionNo = UIAlertAction(title: NSLocalizedString("_no_", comment: ""), style: .cancel) { (action:UIAlertAction) in
  108. print("You've pressed No button")
  109. }
  110. alertController.addAction(actionYes)
  111. alertController.addAction(actionNo)
  112. self.present(alertController, animated: true, completion:nil)
  113. } else {
  114. self.dismiss(animated: true, completion: {
  115. self.delegate?.dismissTextView()
  116. })
  117. }
  118. }
  119. @IBAction func nextButtonTapped(_ sender: AnyObject) {
  120. let serverUrl = self.appDelegate.getTabBarControllerActiveServerUrl()
  121. if let metadata = metadata {
  122. if textView.text != loadText {
  123. let data = textView.text.data(using: .utf8)
  124. let success = FileManager.default.createFile(atPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView), contents: data, attributes: nil)
  125. if success {
  126. appDelegate.activeMain.clearDateReadDataSource(nil)
  127. self.dismiss(animated: true, completion: {
  128. metadata.session = k_upload_session
  129. metadata.sessionSelector = selectorUploadFile
  130. metadata.status = Int(k_metadataStatusWaitUpload)
  131. _ = NCManageDatabase.sharedInstance.addMetadata(metadata)
  132. NCMainCommon.sharedInstance.reloadDatasource(ServerUrl: serverUrl, ocId: metadata.ocId, action: Int32(k_action_MOD))
  133. self.appDelegate.startLoadAutoDownloadUpload()
  134. self.delegate?.dismissTextView()
  135. })
  136. } else {
  137. self.appDelegate.messageNotification("_error_", description: "_error_creation_file_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: Int(k_CCErrorInternalError))
  138. }
  139. } else {
  140. self.dismiss(animated: true, completion: {
  141. self.delegate?.dismissTextView()
  142. })
  143. }
  144. } else {
  145. let formViewController = NCCreateFormUploadFileText.init(serverUrl: serverUrl!, text: self.textView.text, fileName: NSLocalizedString("_untitled_txt_", comment: ""))
  146. self.navigationController?.pushViewController(formViewController, animated: true)
  147. }
  148. }
  149. }