NCText.swift 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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("_title_new_text_file_", 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. textViewDidChange(textView)
  43. }
  44. func keyboardWillShowHandle(info:NSNotification) {
  45. let frameView = self.view.convert(self.view.bounds, to: self.view.window)
  46. let endView = frameView.origin.y + frameView.size.height
  47. if let keyboardSize = (info.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue, let _ = self.view.window?.frame {
  48. if endView - keyboardSize.origin.y > 0 {
  49. bottomConstraint.constant = endView - keyboardSize.origin.y
  50. } else {
  51. bottomConstraint.constant = 0
  52. }
  53. }
  54. }
  55. func keyboardWillHideHandle() {
  56. bottomConstraint.constant = 0
  57. }
  58. func textViewDidChange(_ textView: UITextView) {
  59. if textView.text.characters.count == 0 {
  60. nextButton.isEnabled = false
  61. } else {
  62. nextButton.isEnabled = true
  63. }
  64. }
  65. @IBAction func cancelButtonTapped(_ sender: AnyObject) {
  66. if textView.text != loadText {
  67. let alertController = UIAlertController(title: NSLocalizedString("_info_", comment: ""), message: NSLocalizedString("_save_exit_", comment: ""), preferredStyle: .alert)
  68. let actionYes = UIAlertAction(title: NSLocalizedString("_yes_", comment: ""), style: .default) { (action:UIAlertAction) in
  69. self.dismiss(animated: true, completion: nil)
  70. }
  71. let actionNo = UIAlertAction(title: NSLocalizedString("_no_", comment: ""), style: .cancel) { (action:UIAlertAction) in
  72. print("You've pressed No button")
  73. }
  74. alertController.addAction(actionYes)
  75. alertController.addAction(actionNo)
  76. self.present(alertController, animated: true, completion:nil)
  77. } else {
  78. self.dismiss(animated: true, completion: nil)
  79. }
  80. }
  81. @IBAction func nextButtonTapped(_ sender: AnyObject) {
  82. if let metadata = metadata {
  83. let uploadID = k_uploadSessionID + CCUtility.createRandomString(16)
  84. let data = textView.text.data(using: .utf8)
  85. let success = FileManager.default.createFile(atPath: "\(self.appDelegate.directoryUser!)/\(uploadID)", contents: data, attributes: nil)
  86. if success {
  87. // Prepare for send Metadata
  88. metadata.fileID = uploadID
  89. metadata.sessionID = uploadID
  90. metadata.session = k_upload_session
  91. metadata.sessionTaskIdentifier = Int(k_taskIdentifierWaitStart)
  92. _ = NCManageDatabase.sharedInstance.updateMetadata(metadata)
  93. self.dismiss(animated: true, completion: {
  94. CCNetworking.shared().uploadFileMetadata(metadata, taskStatus: Int(k_taskStatusResume))
  95. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "detailBack"), object: nil)
  96. })
  97. } else {
  98. self.appDelegate.messageNotification("_error_", description: "_error_creation_file_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.info, errorCode: 0)
  99. }
  100. } else {
  101. let formViewController = CreateFormUploadFile.init(serverUrl: appDelegate.activeMain.serverUrl, text: self.textView.text, fileName: NSLocalizedString("_untitled_txt_", comment: ""))
  102. self.navigationController?.pushViewController(formViewController, animated: true)
  103. }
  104. }
  105. }