NCManageAutoUploadFileName.swift 7.6 KB

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