NCText.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  14. var metadata: tableMetadata?
  15. var loadText: String?
  16. override func viewDidLoad() {
  17. super.viewDidLoad()
  18. self.navigationController?.navigationBar.topItem?.title = NSLocalizedString("_title_new_text_file_", comment: "")
  19. self.navigationController?.navigationBar.barTintColor = NCBrandColor.sharedInstance.brand
  20. self.navigationController?.navigationBar.tintColor = NCBrandColor.sharedInstance.navigationBarText
  21. self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: NCBrandColor.sharedInstance.navigationBarText]
  22. self.navigationController?.toolbar.barTintColor = NCBrandColor.sharedInstance.navigationBarText
  23. self.navigationController?.toolbar.tintColor = NCBrandColor.sharedInstance.brand
  24. cancelButton.title = NSLocalizedString("_cancel_", comment: "")
  25. nextButton.title = NSLocalizedString("_next_", comment: "")
  26. // Modify
  27. if let metadata = metadata {
  28. let path = "\(appDelegate.directoryUser!)/\(metadata.fileID)"
  29. loadText = try? String(contentsOfFile: path, encoding: String.Encoding.utf8)
  30. textView.text = loadText
  31. nextButton.title = NSLocalizedString("_save_", comment: "")
  32. self.navigationController?.navigationBar.topItem?.title = NSLocalizedString(metadata.fileNamePrint, comment: "")
  33. }
  34. textView.isUserInteractionEnabled = true
  35. textView.becomeFirstResponder()
  36. textView.delegate = self
  37. textViewDidChange(textView)
  38. }
  39. override func viewWillAppear(_ animated: Bool) {
  40. super.viewWillAppear(animated)
  41. }
  42. func textViewDidChange(_ textView: UITextView) {
  43. if textView.text.characters.count == 0 {
  44. nextButton.isEnabled = false
  45. } else {
  46. nextButton.isEnabled = true
  47. }
  48. }
  49. @IBAction func cancelButtonTapped(_ sender: AnyObject) {
  50. if textView.text != loadText {
  51. let alertController = UIAlertController(title: NSLocalizedString("_info_", comment: ""), message: NSLocalizedString("_save_exit_", comment: ""), preferredStyle: .alert)
  52. let actionYes = UIAlertAction(title: NSLocalizedString("_yes_", comment: ""), style: .default) { (action:UIAlertAction) in
  53. self.dismiss(animated: true, completion: nil)
  54. }
  55. let actionNo = UIAlertAction(title: NSLocalizedString("_no_", comment: ""), style: .cancel) { (action:UIAlertAction) in
  56. print("You've pressed No button")
  57. }
  58. alertController.addAction(actionYes)
  59. alertController.addAction(actionNo)
  60. self.present(alertController, animated: true, completion:nil)
  61. } else {
  62. self.dismiss(animated: true, completion: nil)
  63. }
  64. }
  65. @IBAction func nextButtonTapped(_ sender: AnyObject) {
  66. if let metadata = metadata {
  67. let uploadID = k_uploadSessionID + CCUtility.createRandomString(16)
  68. let data = textView.text.data(using: .utf8)
  69. let success = FileManager.default.createFile(atPath: "\(self.appDelegate.directoryUser!)/\(uploadID)", contents: data, attributes: nil)
  70. if success {
  71. // Prepare for send Metadata
  72. metadata.fileID = uploadID
  73. metadata.sessionID = uploadID
  74. metadata.session = k_upload_session
  75. metadata.sessionTaskIdentifier = Int(k_taskIdentifierWaitStart)
  76. _ = NCManageDatabase.sharedInstance.updateMetadata(metadata)
  77. self.dismiss(animated: true, completion: {
  78. CCNetworking.shared().uploadFileMetadata(metadata, taskStatus: Int(k_taskStatusResume))
  79. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "detailBack"), object: nil)
  80. })
  81. } else {
  82. self.appDelegate.messageNotification("_error_", description: "_error_creation_file_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.info, errorCode: 0)
  83. }
  84. } else {
  85. let formViewController = CreateFormUploadFile.init(NSLocalizedString("_untitled_txt_", comment: ""), serverUrl: appDelegate.activeMain.serverUrl, text: self.textView.text, fileName: NSLocalizedString("_untitled_txt_", comment: ""))
  86. self.navigationController?.pushViewController(formViewController, animated: true)
  87. }
  88. }
  89. }