NCText.swift 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. //
  2. // NCText.swift
  3. // Nextcloud iOS
  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. self.navigationController?.navigationBar.barTintColor = NCBrandColor.sharedInstance.brand
  42. self.navigationController?.navigationBar.tintColor = NCBrandColor.sharedInstance.brandText
  43. self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: NCBrandColor.sharedInstance.brandText]
  44. self.navigationController?.navigationBar.isTranslucent = false
  45. self.navigationController?.toolbar.barTintColor = NCBrandColor.sharedInstance.brandText
  46. self.navigationController?.toolbar.tintColor = NCBrandColor.sharedInstance.brandElement
  47. cancelButton.title = NSLocalizedString("_cancel_", comment: "")
  48. nextButton.title = NSLocalizedString("_next_", comment: "")
  49. // Modify
  50. if let metadata = metadata {
  51. loadText = ""
  52. let path = CCUtility.getDirectoryProviderStorageFileID(metadata.fileID, fileNameView: metadata.fileNameView)!
  53. let data = NSData(contentsOfFile: path)
  54. if let data = data {
  55. let encodingCFName = NCUchardet.sharedNUCharDet().encodingCFStringDetect(with: data as Data)
  56. let se = CFStringConvertEncodingToNSStringEncoding(encodingCFName)
  57. let encoding = String.Encoding(rawValue: se)
  58. loadText = try? String(contentsOfFile: path, encoding: encoding)
  59. textView.text = loadText
  60. nextButton.title = NSLocalizedString("_save_", comment: "")
  61. self.navigationController?.navigationBar.topItem?.title = NSLocalizedString(metadata.fileNameView, comment: "")
  62. }
  63. } else {
  64. loadText = ""
  65. }
  66. textView.isUserInteractionEnabled = true
  67. textView.becomeFirstResponder()
  68. textView.delegate = self
  69. textView.selectedTextRange = textView.textRange(from: textView.beginningOfDocument, to: textView.beginningOfDocument)
  70. //textView.font = UIFont(name: "NameOfTheFont", size: 20)
  71. textViewDidChange(textView)
  72. }
  73. @objc func keyboardWillShowHandle(info:NSNotification) {
  74. let frameView = self.view.convert(self.view.bounds, to: self.view.window)
  75. let endView = frameView.origin.y + frameView.size.height
  76. if let keyboardSize = (info.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue, let _ = self.view.window?.frame {
  77. if endView - keyboardSize.origin.y > 0 {
  78. bottomConstraint.constant = endView - keyboardSize.origin.y
  79. } else {
  80. bottomConstraint.constant = 0
  81. }
  82. }
  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. @IBAction func cancelButtonTapped(_ sender: AnyObject) {
  95. if textView.text != loadText {
  96. let alertController = UIAlertController(title: NSLocalizedString("_info_", comment: ""), message: NSLocalizedString("_save_exit_", comment: ""), preferredStyle: .alert)
  97. let actionYes = UIAlertAction(title: NSLocalizedString("_yes_", comment: ""), style: .default) { (action:UIAlertAction) in
  98. self.dismiss(animated: true, completion: {
  99. self.delegate?.dismissTextView()
  100. })
  101. }
  102. let actionNo = UIAlertAction(title: NSLocalizedString("_no_", comment: ""), style: .cancel) { (action:UIAlertAction) in
  103. print("You've pressed No button")
  104. }
  105. alertController.addAction(actionYes)
  106. alertController.addAction(actionNo)
  107. self.present(alertController, animated: true, completion:nil)
  108. } else {
  109. self.dismiss(animated: true, completion: {
  110. self.delegate?.dismissTextView()
  111. })
  112. }
  113. }
  114. @IBAction func nextButtonTapped(_ sender: AnyObject) {
  115. let serverUrl = self.appDelegate.getTabBarControllerActiveServerUrl()
  116. if let metadata = metadata {
  117. if textView.text != loadText {
  118. let data = textView.text.data(using: .utf8)
  119. let success = FileManager.default.createFile(atPath: CCUtility.getDirectoryProviderStorageFileID(metadata.fileID, fileNameView: metadata.fileNameView), contents: data, attributes: nil)
  120. if success {
  121. appDelegate.activeMain.clearDateReadDataSource(nil)
  122. self.dismiss(animated: true, completion: {
  123. metadata.session = k_upload_session
  124. metadata.sessionSelector = selectorUploadFile
  125. metadata.status = Int(k_metadataStatusWaitUpload)
  126. _ = NCManageDatabase.sharedInstance.addMetadata(metadata)
  127. NCMainCommon.sharedInstance.reloadDatasource(ServerUrl: serverUrl, fileID: metadata.fileID, action: Int32(k_action_MOD))
  128. self.appDelegate.startLoadAutoDownloadUpload()
  129. self.delegate?.dismissTextView()
  130. })
  131. } else {
  132. self.appDelegate.messageNotification("_error_", description: "_error_creation_file_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: Int(k_CCErrorInternalError))
  133. }
  134. } else {
  135. self.dismiss(animated: true, completion: {
  136. self.delegate?.dismissTextView()
  137. })
  138. }
  139. } else {
  140. let formViewController = NCCreateFormUploadFileText.init(serverUrl: serverUrl!, text: self.textView.text, fileName: NSLocalizedString("_untitled_txt_", comment: ""))
  141. self.navigationController?.pushViewController(formViewController, animated: true)
  142. }
  143. }
  144. }