NCText.swift 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. //
  2. // NCText.swift
  3. // Nextcloud iOS
  4. //
  5. // Created by Marino Faggiana on 24/07/17.
  6. // Copyright (c) 2017 TWS. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  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. class NCText: UIViewController, UITextViewDelegate {
  25. @IBOutlet weak var cancelButton: UIBarButtonItem!
  26. @IBOutlet weak var nextButton: UIBarButtonItem!
  27. @IBOutlet weak var textView: UITextView!
  28. @IBOutlet weak var bottomConstraint: NSLayoutConstraint!
  29. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  30. @objc var metadata: tableMetadata?
  31. var loadText: String?
  32. override func viewDidLoad() {
  33. super.viewDidLoad()
  34. NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShowHandle(info:)), name: .UIKeyboardWillShow, object: nil)
  35. NotificationCenter.default.addObserver(self, selector:#selector(self.keyboardWillHideHandle), name: .UIKeyboardWillHide, object: nil)
  36. self.navigationController?.navigationBar.topItem?.title = NSLocalizedString("_untitled_txt_", comment: "")
  37. self.navigationController?.navigationBar.barTintColor = NCBrandColor.sharedInstance.brand
  38. self.navigationController?.navigationBar.tintColor = NCBrandColor.sharedInstance.navigationBarText
  39. self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: NCBrandColor.sharedInstance.navigationBarText]
  40. self.navigationController?.navigationBar.isTranslucent = false
  41. self.navigationController?.toolbar.barTintColor = NCBrandColor.sharedInstance.navigationBarText
  42. self.navigationController?.toolbar.tintColor = NCBrandColor.sharedInstance.brand
  43. cancelButton.title = NSLocalizedString("_cancel_", comment: "")
  44. nextButton.title = NSLocalizedString("_next_", comment: "")
  45. // Modify
  46. if let metadata = metadata {
  47. loadText = ""
  48. let path = "\(appDelegate.directoryUser!)/\(metadata.fileID)"
  49. let data = NSData(contentsOfFile: path)
  50. if let data = data {
  51. let encodingCFName = NCUchardet.sharedNUCharDet().encodingCFStringDetect(with: data as Data)
  52. let se = CFStringConvertEncodingToNSStringEncoding(encodingCFName)
  53. let encoding = String.Encoding(rawValue: se)
  54. loadText = try? String(contentsOfFile: path, encoding: encoding)
  55. textView.text = loadText
  56. nextButton.title = NSLocalizedString("_save_", comment: "")
  57. self.navigationController?.navigationBar.topItem?.title = NSLocalizedString(metadata.fileName, comment: "")
  58. }
  59. } else {
  60. loadText = ""
  61. }
  62. textView.isUserInteractionEnabled = true
  63. textView.becomeFirstResponder()
  64. textView.delegate = self
  65. textView.selectedTextRange = textView.textRange(from: textView.beginningOfDocument, to: textView.beginningOfDocument)
  66. textViewDidChange(textView)
  67. }
  68. @objc func keyboardWillShowHandle(info:NSNotification) {
  69. let frameView = self.view.convert(self.view.bounds, to: self.view.window)
  70. let endView = frameView.origin.y + frameView.size.height
  71. if let keyboardSize = (info.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue, let _ = self.view.window?.frame {
  72. if endView - keyboardSize.origin.y > 0 {
  73. bottomConstraint.constant = endView - keyboardSize.origin.y
  74. } else {
  75. bottomConstraint.constant = 0
  76. }
  77. }
  78. }
  79. @objc func keyboardWillHideHandle() {
  80. bottomConstraint.constant = 0
  81. }
  82. func textViewDidChange(_ textView: UITextView) {
  83. if textView.text.count == 0 {
  84. nextButton.isEnabled = false
  85. } else {
  86. nextButton.isEnabled = true
  87. }
  88. }
  89. @IBAction func cancelButtonTapped(_ sender: AnyObject) {
  90. if textView.text != loadText {
  91. let alertController = UIAlertController(title: NSLocalizedString("_info_", comment: ""), message: NSLocalizedString("_save_exit_", comment: ""), preferredStyle: .alert)
  92. let actionYes = UIAlertAction(title: NSLocalizedString("_yes_", comment: ""), style: .default) { (action:UIAlertAction) in
  93. self.dismiss(animated: true, completion: nil)
  94. }
  95. let actionNo = UIAlertAction(title: NSLocalizedString("_no_", comment: ""), style: .cancel) { (action:UIAlertAction) in
  96. print("You've pressed No button")
  97. }
  98. alertController.addAction(actionYes)
  99. alertController.addAction(actionNo)
  100. self.present(alertController, animated: true, completion:nil)
  101. } else {
  102. self.dismiss(animated: true, completion: nil)
  103. }
  104. }
  105. @IBAction func nextButtonTapped(_ sender: AnyObject) {
  106. if let metadata = metadata {
  107. if textView.text != loadText {
  108. let uploadID = k_uploadSessionID + CCUtility.createRandomString(16)
  109. let data = textView.text.data(using: .utf8)
  110. let success = FileManager.default.createFile(atPath: "\(self.appDelegate.directoryUser!)/\(uploadID)", contents: data, attributes: nil)
  111. if success {
  112. // Prepare for send Metadata
  113. metadata.sessionID = uploadID
  114. metadata.session = k_upload_session
  115. metadata.sessionTaskIdentifier = Int(k_taskIdentifierWaitStart)
  116. _ = NCManageDatabase.sharedInstance.updateMetadata(metadata)
  117. appDelegate.activeMain.clearDateReadDataSource(nil)
  118. self.dismiss(animated: true, completion: {
  119. // Send file
  120. CCNetworking.shared().verifyUploadInErrorOrWait()
  121. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "detailBack"), object: nil)
  122. })
  123. } else {
  124. self.appDelegate.messageNotification("_error_", description: "_error_creation_file_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.info, errorCode: 0)
  125. }
  126. } else {
  127. self.dismiss(animated: true, completion: nil)
  128. }
  129. } else {
  130. let serverUrl = self.appDelegate.getTabBarControllerActiveServerUrl()
  131. let formViewController = CreateFormUploadFile.init(serverUrl: serverUrl!, text: self.textView.text, fileName: NSLocalizedString("_untitled_txt_", comment: ""))
  132. self.navigationController?.pushViewController(formViewController, animated: true)
  133. }
  134. }
  135. }