NCManageAutoUploadFileName.swift 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. //
  2. // NCManageAutoUploadFileName.swift
  3. // Nextcloud iOS
  4. //
  5. // Created by Marino Faggiana on 19/07/17.
  6. // Copyright (c) 2017 TWS. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import Foundation
  24. class NCManageAutoUploadFileName: XLFormViewController {
  25. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  26. let dateExample = Date()
  27. required init(coder aDecoder: NSCoder) {
  28. super.init(coder: aDecoder)
  29. self.initializeForm()
  30. }
  31. override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
  32. super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
  33. self.initializeForm()
  34. }
  35. func initializeForm() {
  36. let form : XLFormDescriptor = XLFormDescriptor(title: NSLocalizedString("_autoupload_filename_title_", comment: "")) as XLFormDescriptor
  37. form.rowNavigationOptions = XLFormRowNavigationOptions.stopDisableRow
  38. var section : XLFormSectionDescriptor
  39. var row : XLFormRowDescriptor
  40. section = XLFormSectionDescriptor.formSection()
  41. form.addFormSection(section)
  42. // Maintain the original fileName
  43. row = XLFormRowDescriptor(tag: "maintainOriginalFileName", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_maintain_original_filename_", comment: ""))
  44. row.value = CCUtility.getOriginalFileName(k_keyFileNameOriginalAutoUpload)
  45. section.addFormRow(row)
  46. // Add File Name Type
  47. row = XLFormRowDescriptor(tag: "addFileNameType", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_add_filenametype_", comment: ""))
  48. row.hidden = "$\("maintainOriginalFileName") == 1"
  49. row.value = CCUtility.getFileNameType(k_keyFileNameAutoUploadType)
  50. section.addFormRow(row)
  51. // Section: Rename File Name
  52. section = XLFormSectionDescriptor.formSection()
  53. form.addFormSection(section)
  54. row = XLFormRowDescriptor(tag: "maskFileName", rowType: XLFormRowDescriptorTypeAccount, title: NSLocalizedString("_filename_", comment: ""))
  55. row.hidden = "$\("maintainOriginalFileName") == 1"
  56. let fileNameMask : String = CCUtility.getFileNameMask(k_keyFileNameAutoUploadMask)
  57. if fileNameMask.count > 0 {
  58. row.value = fileNameMask
  59. }
  60. section.addFormRow(row)
  61. // Section: Preview File Name
  62. row = XLFormRowDescriptor(tag: "previewFileName", rowType: XLFormRowDescriptorTypeTextView, title: "")
  63. row.height = 180
  64. row.cellConfig.setObject(NCBrandColor.sharedInstance.backgroundView, forKey: "backgroundColor" as NSCopying)
  65. row.cellConfig.setObject(NCBrandColor.sharedInstance.backgroundView, forKey: "textView.backgroundColor" as NSCopying)
  66. row.disabled = true
  67. section.addFormRow(row)
  68. self.form = form
  69. }
  70. override func formRowDescriptorValueHasChanged(_ formRow: XLFormRowDescriptor!, oldValue: Any!, newValue: Any!) {
  71. super.formRowDescriptorValueHasChanged(formRow, oldValue: oldValue, newValue: newValue)
  72. if formRow.tag == "addFileNameType" {
  73. CCUtility.setFileNameType((formRow.value! as AnyObject).boolValue, key: k_keyFileNameAutoUploadType)
  74. self.reloadForm()
  75. }
  76. else if formRow.tag == "maintainOriginalFileName" {
  77. CCUtility.setOriginalFileName((formRow.value! as AnyObject).boolValue, key:k_keyFileNameOriginalAutoUpload)
  78. self.reloadForm()
  79. }
  80. else if formRow.tag == "maskFileName" {
  81. let fileName = formRow.value as? String
  82. self.form.delegate = nil
  83. if let fileName = fileName {
  84. formRow.value = CCUtility.removeForbiddenCharactersServer(fileName)
  85. }
  86. self.form.delegate = self
  87. let previewFileName : XLFormRowDescriptor = self.form.formRow(withTag: "previewFileName")!
  88. previewFileName.value = self.previewFileName(valueRename: formRow.value as? String)
  89. // reload cell
  90. if fileName != nil {
  91. if newValue as! String != formRow.value as! String {
  92. self.reloadFormRow(formRow)
  93. appDelegate.messageNotification("_info_", description: "_forbidden_characters_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.info, errorCode: 0)
  94. }
  95. }
  96. self.reloadFormRow(previewFileName)
  97. }
  98. }
  99. // MARK: - View Life Cycle
  100. override func viewDidLoad() {
  101. super.viewDidLoad()
  102. self.navigationController?.navigationBar.isTranslucent = false
  103. self.navigationController?.navigationBar.barTintColor = NCBrandColor.sharedInstance.brand
  104. self.navigationController?.navigationBar.tintColor = NCBrandColor.sharedInstance.brandText
  105. self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: NCBrandColor.sharedInstance.brandText]
  106. self.tableView.separatorStyle = UITableViewCellSeparatorStyle.none
  107. self.tableView.backgroundColor = NCBrandColor.sharedInstance.backgroundView
  108. self.reloadForm()
  109. }
  110. func reloadForm() {
  111. self.form.delegate = nil
  112. let maskFileName : XLFormRowDescriptor = self.form.formRow(withTag: "maskFileName")!
  113. let previewFileName : XLFormRowDescriptor = self.form.formRow(withTag: "previewFileName")!
  114. previewFileName.value = self.previewFileName(valueRename: maskFileName.value as? String)
  115. self.tableView.reloadData()
  116. self.form.delegate = self
  117. }
  118. //MARK: TableView
  119. /*
  120. override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  121. switch section {
  122. case 0:
  123. return " " + NSLocalizedString("_add_filenametype_", comment: "")
  124. case 1:
  125. return " " + NSLocalizedString("_rename_filename_", comment: "")
  126. case 2:
  127. return String(format: NSLocalizedString("_preview_filename_", comment: ""), "MM,MMM,DD,YY,YYYY and HH,hh,mm,ss,ampm")
  128. default:
  129. return ""
  130. }
  131. }
  132. override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
  133. switch section {
  134. /*
  135. case 2:
  136. let buttonDestinationFolder : XLFormRowDescriptor = self.form.formRow(withTag: "maskFileName")!
  137. let text = self.writePreviewFileName(buttonDestinationFolder)
  138. return text
  139. */
  140. default:
  141. return ""
  142. }
  143. }
  144. */
  145. // MARK: - Utility
  146. func previewFileName(valueRename : String?) -> String {
  147. var returnString : String = ""
  148. if let valueRename = valueRename {
  149. let valueRenameTrimming = valueRename.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
  150. if valueRenameTrimming.count > 0 {
  151. self.form.delegate = nil
  152. CCUtility.setFileNameMask(valueRename, key: k_keyFileNameAutoUploadMask)
  153. self.form.delegate = self
  154. returnString = CCUtility.createFileName("IMG_0001.JPG", fileDate: dateExample, fileType: PHAssetMediaType.image, keyFileName: k_keyFileNameAutoUploadMask, keyFileNameType: k_keyFileNameAutoUploadType, keyFileNameOriginal: k_keyFileNameOriginalAutoUpload)
  155. } else {
  156. CCUtility.setFileNameMask("", key: k_keyFileNameAutoUploadMask)
  157. returnString = CCUtility.createFileName("IMG_0001.JPG", fileDate: dateExample, fileType: PHAssetMediaType.image, keyFileName: nil, keyFileNameType: k_keyFileNameAutoUploadType, keyFileNameOriginal: k_keyFileNameOriginalAutoUpload)
  158. }
  159. } else {
  160. CCUtility.setFileNameMask("", key: k_keyFileNameAutoUploadMask)
  161. returnString = CCUtility.createFileName("IMG_0001.JPG", fileDate: dateExample, fileType: PHAssetMediaType.image, keyFileName: nil, keyFileNameType: k_keyFileNameAutoUploadType, keyFileNameOriginal: k_keyFileNameOriginalAutoUpload)
  162. }
  163. return String(format: NSLocalizedString("_preview_filename_", comment: ""), "MM,MMM,DD,YY,YYYY and HH,hh,mm,ss,ampm") + ":" + "\n\n" + returnString
  164. }
  165. }