NCManageAutoUploadFileName.swift 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. let dateExample = Date()
  12. required init(coder aDecoder: NSCoder) {
  13. super.init(coder: aDecoder)
  14. self.initializeForm()
  15. }
  16. override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
  17. super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
  18. self.initializeForm()
  19. }
  20. func initializeForm() {
  21. let form : XLFormDescriptor = XLFormDescriptor(title: NSLocalizedString("_autoupload_filename_title_", comment: "")) as XLFormDescriptor
  22. form.rowNavigationOptions = XLFormRowNavigationOptions.stopDisableRow
  23. var section : XLFormSectionDescriptor
  24. var row : XLFormRowDescriptor
  25. // Section: Add File Name Type
  26. section = XLFormSectionDescriptor.formSection()
  27. form.addFormSection(section)
  28. row = XLFormRowDescriptor(tag: "addFileNameType", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_filenametype_photo_video_", comment: ""))
  29. row.value = CCUtility.getFileNameType(k_keyFileNameAutoUploadType)
  30. section.addFormRow(row)
  31. // Section: Rename File Name
  32. section = XLFormSectionDescriptor.formSection()
  33. form.addFormSection(section)
  34. row = XLFormRowDescriptor(tag: "maskFileName", rowType: XLFormRowDescriptorTypeAccount, title: NSLocalizedString("_filename_", comment: ""))
  35. let fileNameMask : String = CCUtility.getFileNameMask(k_keyFileNameAutoUploadMask)
  36. if fileNameMask.characters.count > 0 {
  37. row.value = fileNameMask
  38. }
  39. section.addFormRow(row)
  40. // Section: Preview File Name
  41. row = XLFormRowDescriptor(tag: "previewFileName", rowType: XLFormRowDescriptorTypeTextView, title: "")
  42. row.height = 180
  43. row.cellConfig.setObject(NCBrandColor.sharedInstance.tableBackground, forKey: "backgroundColor" as NSCopying)
  44. row.cellConfig.setObject(NCBrandColor.sharedInstance.tableBackground, forKey: "textView.backgroundColor" as NSCopying)
  45. row.disabled = true
  46. section.addFormRow(row)
  47. self.form = form
  48. }
  49. override func formRowDescriptorValueHasChanged(_ formRow: XLFormRowDescriptor!, oldValue: Any!, newValue: Any!) {
  50. super.formRowDescriptorValueHasChanged(formRow, oldValue: oldValue, newValue: newValue)
  51. if formRow.tag == "addFileNameType" {
  52. CCUtility.setFileNameType((formRow.value! as AnyObject).boolValue, key: k_keyFileNameAutoUploadType)
  53. self.reloadForm()
  54. }
  55. else if formRow.tag == "maskFileName" {
  56. let fileName = formRow.value as? String
  57. self.form.delegate = nil
  58. if let fileName = fileName {
  59. formRow.value = CCUtility.removeForbiddenCharactersServer(fileName)
  60. }
  61. self.form.delegate = self
  62. let previewFileName : XLFormRowDescriptor = self.form.formRow(withTag: "previewFileName")!
  63. previewFileName.value = self.previewFileName(valueRename: formRow.value as? String)
  64. // reload cell
  65. if fileName != nil {
  66. if newValue as! String != formRow.value as! String {
  67. self.reloadFormRow(formRow)
  68. appDelegate.messageNotification("_info_", description: "_forbidden_characters_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.info, errorCode: 0)
  69. }
  70. }
  71. self.reloadFormRow(previewFileName)
  72. }
  73. }
  74. // MARK: - View Life Cycle
  75. override func viewDidLoad() {
  76. super.viewDidLoad()
  77. self.navigationController?.navigationBar.isTranslucent = false
  78. self.navigationController?.navigationBar.barTintColor = NCBrandColor.sharedInstance.brand
  79. self.navigationController?.navigationBar.tintColor = NCBrandColor.sharedInstance.navigationBarText
  80. self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: NCBrandColor.sharedInstance.navigationBarText]
  81. self.tableView.separatorStyle = UITableViewCellSeparatorStyle.none
  82. self.tableView.backgroundColor = NCBrandColor.sharedInstance.tableBackground
  83. self.reloadForm()
  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_keyFileNameAutoUploadMask)
  126. self.form.delegate = self
  127. returnString = CCUtility.createFileName("IMG_0001.JPG", fileDate: dateExample, fileType: PHAssetMediaType.image, keyFileName: k_keyFileNameAutoUploadMask, keyFileNameType: k_keyFileNameAutoUploadType)
  128. } else {
  129. CCUtility.setFileNameMask("", key: k_keyFileNameAutoUploadMask)
  130. returnString = CCUtility.createFileName("IMG_0001.JPG", fileDate: dateExample, fileType: PHAssetMediaType.image, keyFileName: nil, keyFileNameType: k_keyFileNameAutoUploadType)
  131. }
  132. } else {
  133. CCUtility.setFileNameMask("", key: k_keyFileNameAutoUploadMask)
  134. returnString = CCUtility.createFileName("IMG_0001.JPG", fileDate: dateExample, fileType: PHAssetMediaType.image, keyFileName: nil, keyFileNameType: k_keyFileNameAutoUploadType)
  135. }
  136. return NSLocalizedString("_preview_filename_", comment: "") + ":" + "\n\n" + returnString
  137. }
  138. }