NCText.swift 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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.brandText
  39. self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: NCBrandColor.sharedInstance.brandText]
  40. self.navigationController?.navigationBar.isTranslucent = false
  41. self.navigationController?.toolbar.barTintColor = NCBrandColor.sharedInstance.brandText
  42. self.navigationController?.toolbar.tintColor = NCBrandColor.sharedInstance.brandElement
  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.fileNameView, 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. //textView.font = UIFont(name: "NameOfTheFont", size: 20)
  67. textViewDidChange(textView)
  68. }
  69. @objc func keyboardWillShowHandle(info:NSNotification) {
  70. let frameView = self.view.convert(self.view.bounds, to: self.view.window)
  71. let endView = frameView.origin.y + frameView.size.height
  72. if let keyboardSize = (info.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue, let _ = self.view.window?.frame {
  73. if endView - keyboardSize.origin.y > 0 {
  74. bottomConstraint.constant = endView - keyboardSize.origin.y
  75. } else {
  76. bottomConstraint.constant = 0
  77. }
  78. }
  79. }
  80. @objc func keyboardWillHideHandle() {
  81. bottomConstraint.constant = 0
  82. }
  83. func textViewDidChange(_ textView: UITextView) {
  84. if textView.text.count == 0 {
  85. nextButton.isEnabled = false
  86. } else {
  87. nextButton.isEnabled = true
  88. }
  89. }
  90. @IBAction func cancelButtonTapped(_ sender: AnyObject) {
  91. if textView.text != loadText {
  92. let alertController = UIAlertController(title: NSLocalizedString("_info_", comment: ""), message: NSLocalizedString("_save_exit_", comment: ""), preferredStyle: .alert)
  93. let actionYes = UIAlertAction(title: NSLocalizedString("_yes_", comment: ""), style: .default) { (action:UIAlertAction) in
  94. self.dismiss(animated: true, completion: nil)
  95. }
  96. let actionNo = UIAlertAction(title: NSLocalizedString("_no_", comment: ""), style: .cancel) { (action:UIAlertAction) in
  97. print("You've pressed No button")
  98. }
  99. alertController.addAction(actionYes)
  100. alertController.addAction(actionNo)
  101. self.present(alertController, animated: true, completion:nil)
  102. } else {
  103. self.dismiss(animated: true, completion: nil)
  104. }
  105. }
  106. @IBAction func nextButtonTapped(_ sender: AnyObject) {
  107. let serverUrl = self.appDelegate.getTabBarControllerActiveServerUrl()
  108. if let metadata = metadata {
  109. if textView.text != loadText {
  110. let data = textView.text.data(using: .utf8)
  111. let success = FileManager.default.createFile(atPath: "\(self.appDelegate.directoryUser!)/\(metadata.fileNameView)", contents: data, attributes: nil)
  112. if success {
  113. appDelegate.activeMain.clearDateReadDataSource(nil)
  114. self.dismiss(animated: true, completion: {
  115. // Send file
  116. CCNetworking.shared().uploadFile(metadata.fileNameView, serverUrl: serverUrl, fileID: nil, assetLocalIdentifier: nil, session: k_upload_session, taskStatus: Int(k_taskStatusResume), selector: nil, selectorPost: nil, errorCode: 0, delegate: self.appDelegate.activeMain)
  117. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "detailBack"), object: nil)
  118. })
  119. } else {
  120. self.appDelegate.messageNotification("_error_", description: "_error_creation_file_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: Int(k_CCErrorInternalError))
  121. }
  122. } else {
  123. self.dismiss(animated: true, completion: nil)
  124. }
  125. } else {
  126. let formViewController = CreateFormUploadFile.init(serverUrl: serverUrl!, text: self.textView.text, fileName: NSLocalizedString("_untitled_txt_", comment: ""))
  127. self.navigationController?.pushViewController(formViewController, animated: true)
  128. }
  129. }
  130. }