NCText.swift 6.3 KB

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