NCManageAutoUploadFileName.swift 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //
  2. // NCManageAutoUploadFileName.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 19/07/17.
  6. // Copyright © 2017 TWS. All rights reserved.
  7. //
  8. import Foundation
  9. class NCManageAutoUploadFileName: XLFormViewController {
  10. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  11. convenience init() {
  12. self.init()
  13. self.initializeForm()
  14. }
  15. //MARK: XLFormDescriptorDelegate
  16. func initializeForm() {
  17. let form : XLFormDescriptor = XLFormDescriptor() as XLFormDescriptor
  18. form.rowNavigationOptions = XLFormRowNavigationOptions.stopDisableRow
  19. var section : XLFormSectionDescriptor
  20. var row : XLFormRowDescriptor
  21. // Section: Add File Name Type
  22. section = XLFormSectionDescriptor.formSection()
  23. form.addFormSection(section)
  24. row = XLFormRowDescriptor(tag: "addFileNameType", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_filenametype_photo_video_", comment: ""))
  25. row.value = CCUtility.getFileNameType(k_keyFileNameType)
  26. section.addFormRow(row)
  27. // Section: Rename File Name
  28. section = XLFormSectionDescriptor.formSection()
  29. form.addFormSection(section)
  30. row = XLFormRowDescriptor(tag: "maskFileName", rowType: XLFormRowDescriptorTypeAccount, title: NSLocalizedString("_filename_", comment: ""))
  31. let fileNameMask : String = CCUtility.getFileNameMask(k_keyFileNameMask)
  32. if fileNameMask.characters.count > 0 {
  33. row.value = fileNameMask
  34. }
  35. section.addFormRow(row)
  36. // Section: Preview File Name
  37. row = XLFormRowDescriptor(tag: "previewFileName", rowType: XLFormRowDescriptorTypeTextView, title: "")
  38. row.height = 180
  39. row.cellConfig.setObject(NCBrandColor.sharedInstance.tableBackground, forKey: "backgroundColor" as NSCopying)
  40. row.cellConfig.setObject(NCBrandColor.sharedInstance.tableBackground, forKey: "textView.backgroundColor" as NSCopying)
  41. row.disabled = true
  42. section.addFormRow(row)
  43. self.form = form
  44. }
  45. override func formRowDescriptorValueHasChanged(_ formRow: XLFormRowDescriptor!, oldValue: Any!, newValue: Any!) {
  46. super.formRowDescriptorValueHasChanged(formRow, oldValue: oldValue, newValue: newValue)
  47. if formRow.tag == "addFileNameType" {
  48. CCUtility.setFileNameType((formRow.value! as AnyObject).boolValue, key: k_keyFileNameType)
  49. self.reloadForm()
  50. }
  51. else if formRow.tag == "maskFileName" {
  52. let fileName = formRow.value as? String
  53. self.form.delegate = nil
  54. if let fileName = fileName {
  55. formRow.value = CCUtility.removeForbiddenCharactersServer(fileName)
  56. }
  57. self.form.delegate = self
  58. let previewFileName : XLFormRowDescriptor = self.form.formRow(withTag: "previewFileName")!
  59. previewFileName.value = self.previewFileName(valueRename: formRow.value as? String)
  60. // reload cell
  61. if fileName != nil {
  62. if newValue as! String != formRow.value as! String {
  63. self.reloadFormRow(formRow)
  64. appDelegate.messageNotification("_info_", description: "_forbidden_characters_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.info, errorCode: 0)
  65. }
  66. }
  67. self.reloadFormRow(previewFileName)
  68. }
  69. }
  70. // MARK: - View Life Cycle
  71. override func viewDidLoad() {
  72. super.viewDidLoad()
  73. self.navigationController?.navigationBar.isTranslucent = false
  74. self.navigationController?.navigationBar.barTintColor = NCBrandColor.sharedInstance.brand
  75. self.navigationController?.navigationBar.tintColor = NCBrandColor.sharedInstance.navigationBarText
  76. self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: NCBrandColor.sharedInstance.navigationBarText]
  77. self.tableView.separatorStyle = UITableViewCellSeparatorStyle.none
  78. self.tableView.backgroundColor = NCBrandColor.sharedInstance.tableBackground
  79. self.reloadForm()
  80. }
  81. override func viewWillDisappear(_ animated: Bool)
  82. {
  83. super.viewWillDisappear(animated)
  84. }
  85. func reloadForm() {
  86. self.form.delegate = nil
  87. let maskFileName : XLFormRowDescriptor = self.form.formRow(withTag: "maskFileName")!
  88. let previewFileName : XLFormRowDescriptor = self.form.formRow(withTag: "previewFileName")!
  89. previewFileName.value = self.previewFileName(valueRename: maskFileName.value as? String)
  90. self.tableView.reloadData()
  91. self.form.delegate = self
  92. }
  93. //MARK: TableView
  94. override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  95. switch section {
  96. case 0:
  97. return " " + NSLocalizedString("_add_filenametype_", comment: "")
  98. case 1:
  99. return " " + NSLocalizedString("_rename_filename_", comment: "")
  100. case 2:
  101. return NSLocalizedString("_preview_filename_", comment: "")
  102. default:
  103. return ""
  104. }
  105. }
  106. override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
  107. switch section {
  108. /*
  109. case 2:
  110. let buttonDestinationFolder : XLFormRowDescriptor = self.form.formRow(withTag: "maskFileName")!
  111. let text = self.writePreviewFileName(buttonDestinationFolder)
  112. return text
  113. */
  114. default:
  115. return ""
  116. }
  117. }
  118. // MARK: - Utility
  119. func previewFileName(valueRename : String?) -> String {
  120. var returnString : String = ""
  121. if let valueRename = valueRename {
  122. let valueRenameTrimming = valueRename.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
  123. if valueRenameTrimming.characters.count > 0 {
  124. self.form.delegate = nil
  125. CCUtility.setFileNameMask(valueRenameTrimming, key: k_keyFileNameMask)
  126. self.form.delegate = self
  127. //returnString = CCUtility.createFileName(from: assets[0] as! PHAsset, keyFileName: k_keyFileNameMask, keyFileNameType: k_keyFileNameType)
  128. } else {
  129. CCUtility.setFileNameMask("", key: k_keyFileNameMask)
  130. //returnString = CCUtility.createFileName(from: assets[0] as! PHAsset, keyFileName: nil, keyFileNameType: k_keyFileNameType)
  131. }
  132. } else {
  133. CCUtility.setFileNameMask("", key: k_keyFileNameMask)
  134. //returnString = CCUtility.createFileName(from: assets[0] as! PHAsset, keyFileName: nil, keyFileNameType: k_keyFileNameType)
  135. }
  136. return NSLocalizedString("_preview_filename_", comment: "") + ":" + "\n\n" + returnString
  137. }
  138. }