NCText.swift 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. import WebKit
  25. @objc protocol NCTextDelegate {
  26. func dismissTextView()
  27. }
  28. class NCText: UIViewController, UITextViewDelegate {
  29. @IBOutlet weak var cancelButton: UIBarButtonItem!
  30. @IBOutlet weak var nextButton: UIBarButtonItem!
  31. @IBOutlet weak var textView: UITextView!
  32. @IBOutlet weak var bottomConstraint: NSLayoutConstraint!
  33. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  34. @objc var metadata: tableMetadata?
  35. @objc var delegate: NCTextDelegate?
  36. var loadText: String?
  37. override func viewDidLoad() {
  38. super.viewDidLoad()
  39. NotificationCenter.default.addObserver(self, selector:#selector(keyboardWillShowHandle(info:)), name: UIResponder.keyboardWillShowNotification, object: nil)
  40. NotificationCenter.default.addObserver(self, selector:#selector(keyboardWillHideHandle), name: UIResponder.keyboardWillHideNotification, object: nil)
  41. self.navigationController?.navigationBar.topItem?.title = NSLocalizedString("_untitled_txt_", comment: "")
  42. cancelButton.title = NSLocalizedString("_cancel_", comment: "")
  43. nextButton.title = NSLocalizedString("_next_", comment: "")
  44. // Modify
  45. if let metadata = metadata {
  46. loadText = ""
  47. let path = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  48. let data = NSData(contentsOfFile: path)
  49. if let data = data {
  50. let encodingCFName = NCUchardet.sharedNUCharDet().encodingCFStringDetect(with: data as Data)
  51. let se = CFStringConvertEncodingToNSStringEncoding(encodingCFName)
  52. let encoding = String.Encoding(rawValue: se)
  53. loadText = try? String(contentsOfFile: path, encoding: encoding)
  54. textView.text = loadText
  55. nextButton.title = NSLocalizedString("_save_", comment: "")
  56. self.navigationController?.navigationBar.topItem?.title = NSLocalizedString(metadata.fileNameView, comment: "")
  57. }
  58. } else {
  59. loadText = ""
  60. }
  61. textView.isUserInteractionEnabled = true
  62. textView.becomeFirstResponder()
  63. textView.delegate = self
  64. textView.selectedTextRange = textView.textRange(from: textView.beginningOfDocument, to: textView.beginningOfDocument)
  65. textView.font = UIFont(name: "NameOfTheFont", size: 20)
  66. // Theming view
  67. NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: k_notificationCenter_changeTheming), object: nil)
  68. changeTheming()
  69. textViewDidChange(textView)
  70. }
  71. @objc func keyboardWillShowHandle(info:NSNotification) {
  72. let frameView = self.view.convert(self.view.bounds, to: self.view.window)
  73. let endView = frameView.origin.y + frameView.size.height
  74. if let keyboardSize = (info.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue, let _ = self.view.window?.frame {
  75. if endView - keyboardSize.origin.y > 0 {
  76. bottomConstraint.constant = endView - keyboardSize.origin.y
  77. } else {
  78. bottomConstraint.constant = 0
  79. }
  80. }
  81. }
  82. override func viewDidDisappear(_ animated: Bool) {
  83. super.viewDidDisappear(animated)
  84. if appDelegate.activeDetail != nil {
  85. if let view = appDelegate.activeDetail.subViewActive() {
  86. if view is WKWebView {
  87. appDelegate.activeDetail.viewFile(metadata: metadata!, selector: nil)
  88. }
  89. }
  90. }
  91. }
  92. @objc func keyboardWillHideHandle() {
  93. bottomConstraint.constant = 0
  94. }
  95. func textViewDidChange(_ textView: UITextView) {
  96. if textView.text.count == 0 {
  97. nextButton.isEnabled = false
  98. } else {
  99. nextButton.isEnabled = true
  100. }
  101. }
  102. @objc func changeTheming() {
  103. appDelegate.changeTheming(self, tableView: nil, collectionView: nil, form: false)
  104. textView.backgroundColor = NCBrandColor.sharedInstance.backgroundForm
  105. textView.textColor = NCBrandColor.sharedInstance.textView
  106. }
  107. @IBAction func cancelButtonTapped(_ sender: AnyObject) {
  108. if textView.text != loadText {
  109. let alertController = UIAlertController(title: NSLocalizedString("_info_", comment: ""), message: NSLocalizedString("_save_exit_", comment: ""), preferredStyle: .alert)
  110. let actionYes = UIAlertAction(title: NSLocalizedString("_yes_", comment: ""), style: .default) { (action:UIAlertAction) in
  111. self.dismiss(animated: true, completion: {
  112. self.delegate?.dismissTextView()
  113. })
  114. }
  115. let actionNo = UIAlertAction(title: NSLocalizedString("_no_", comment: ""), style: .cancel) { (action:UIAlertAction) in
  116. print("You've pressed No button")
  117. }
  118. alertController.addAction(actionYes)
  119. alertController.addAction(actionNo)
  120. self.present(alertController, animated: true, completion:nil)
  121. } else {
  122. self.dismiss(animated: true, completion: {
  123. self.delegate?.dismissTextView()
  124. })
  125. }
  126. }
  127. @IBAction func nextButtonTapped(_ sender: AnyObject) {
  128. let serverUrl = self.appDelegate.getTabBarControllerActiveServerUrl()
  129. if let metadata = metadata {
  130. if textView.text != loadText {
  131. let data = textView.text.data(using: .utf8)
  132. let success = FileManager.default.createFile(atPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView), contents: data, attributes: nil)
  133. if success {
  134. appDelegate.activeMain.clearDateReadDataSource(nil)
  135. self.dismiss(animated: true, completion: {
  136. metadata.session = k_upload_session
  137. metadata.sessionSelector = selectorUploadFile
  138. metadata.status = Int(k_metadataStatusWaitUpload)
  139. _ = NCManageDatabase.sharedInstance.addMetadata(metadata)
  140. NCMainCommon.sharedInstance.reloadDatasource(ServerUrl: serverUrl, ocId: metadata.ocId, action: Int32(k_action_MOD))
  141. self.appDelegate.startLoadAutoDownloadUpload()
  142. self.delegate?.dismissTextView()
  143. })
  144. } else {
  145. NCContentPresenter.shared.messageNotification("_error_", description: "_error_creation_file_", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: Int(k_CCErrorInternalError))
  146. }
  147. } else {
  148. self.dismiss(animated: true, completion: {
  149. self.delegate?.dismissTextView()
  150. })
  151. }
  152. } else {
  153. let formViewController = NCCreateFormUploadFileText.init(serverUrl: serverUrl!, text: self.textView.text, fileName: NSLocalizedString("_untitled_txt_", comment: ""))
  154. self.navigationController?.pushViewController(formViewController, animated: true)
  155. }
  156. }
  157. }