NCManageAutoUploadFileName.swift 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. //
  2. // NCManageAutoUploadFileName.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 19/07/17.
  6. // Copyright (c) 2017 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  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. // MARK: - View Life Cycle
  28. override func viewDidLoad() {
  29. super.viewDidLoad()
  30. self.title = NSLocalizedString("_mode_filename_", comment: "")
  31. // Theming view
  32. NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: k_notificationCenter_changeTheming), object: nil)
  33. changeTheming()
  34. }
  35. func reloadForm() {
  36. self.form.delegate = nil
  37. let maskFileName : XLFormRowDescriptor = self.form.formRow(withTag: "maskFileName")!
  38. let previewFileName : XLFormRowDescriptor = self.form.formRow(withTag: "previewFileName")!
  39. previewFileName.value = self.previewFileName(valueRename: maskFileName.value as? String)
  40. self.tableView.reloadData()
  41. self.form.delegate = self
  42. }
  43. @objc func changeTheming() {
  44. appDelegate.changeTheming(self, tableView: tableView, collectionView: nil, form: true)
  45. initializeForm()
  46. self.reloadForm()
  47. }
  48. //MARK: XLForm
  49. func initializeForm() {
  50. let form : XLFormDescriptor = XLFormDescriptor() as XLFormDescriptor
  51. form.rowNavigationOptions = XLFormRowNavigationOptions.stopDisableRow
  52. var section : XLFormSectionDescriptor
  53. var row : XLFormRowDescriptor
  54. // Section Mode filename
  55. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_mode_filename_", comment: ""))
  56. form.addFormSection(section)
  57. // Maintain the original fileName
  58. row = XLFormRowDescriptor(tag: "maintainOriginalFileName", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_maintain_original_filename_", comment: ""))
  59. row.value = CCUtility.getOriginalFileName(k_keyFileNameOriginalAutoUpload)
  60. row.cellConfig["backgroundColor"] = NCBrandColor.sharedInstance.backgroundView
  61. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  62. row.cellConfig["textLabel.textColor"] = NCBrandColor.sharedInstance.textView
  63. section.addFormRow(row)
  64. // Add File Name Type
  65. row = XLFormRowDescriptor(tag: "addFileNameType", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_add_filenametype_", comment: ""))
  66. row.value = CCUtility.getFileNameType(k_keyFileNameAutoUploadType)
  67. row.hidden = "$\("maintainOriginalFileName") == 1"
  68. row.cellConfig["backgroundColor"] = NCBrandColor.sharedInstance.backgroundView
  69. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  70. row.cellConfig["textLabel.textColor"] = NCBrandColor.sharedInstance.textView
  71. section.addFormRow(row)
  72. // Section: Rename File Name
  73. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_filename_", comment: ""))
  74. form.addFormSection(section)
  75. row = XLFormRowDescriptor(tag: "maskFileName", rowType: XLFormRowDescriptorTypeAccount, title: (NSLocalizedString("_filename_", comment: "")))
  76. let fileNameMask : String = CCUtility.getFileNameMask(k_keyFileNameAutoUploadMask)
  77. if fileNameMask.count > 0 {
  78. row.value = fileNameMask
  79. }
  80. row.hidden = "$\("maintainOriginalFileName") == 1"
  81. row.cellConfig["backgroundColor"] = NCBrandColor.sharedInstance.backgroundView
  82. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  83. row.cellConfig["textLabel.textColor"] = NCBrandColor.sharedInstance.textView
  84. row.cellConfig["textField.textAlignment"] = NSTextAlignment.right.rawValue
  85. row.cellConfig["textField.font"] = UIFont.systemFont(ofSize: 15.0)
  86. row.cellConfig["textField.textColor"] = NCBrandColor.sharedInstance.textView
  87. section.addFormRow(row)
  88. // Section: Preview File Name
  89. row = XLFormRowDescriptor(tag: "previewFileName", rowType: XLFormRowDescriptorTypeTextView, title: "")
  90. row.height = 180
  91. row.disabled = true
  92. row.cellConfig["backgroundColor"] = NCBrandColor.sharedInstance.backgroundView
  93. row.cellConfig["textView.backgroundColor"] = NCBrandColor.sharedInstance.backgroundView
  94. row.cellConfig["textView.font"] = UIFont.systemFont(ofSize: 14.0)
  95. row.cellConfig["textView.textColor"] = NCBrandColor.sharedInstance.textView
  96. section.addFormRow(row)
  97. self.tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
  98. self.form = form
  99. }
  100. override func formRowDescriptorValueHasChanged(_ formRow: XLFormRowDescriptor!, oldValue: Any!, newValue: Any!) {
  101. super.formRowDescriptorValueHasChanged(formRow, oldValue: oldValue, newValue: newValue)
  102. if formRow.tag == "addFileNameType" {
  103. CCUtility.setFileNameType((formRow.value! as AnyObject).boolValue, key: k_keyFileNameAutoUploadType)
  104. self.reloadForm()
  105. }
  106. else if formRow.tag == "maintainOriginalFileName" {
  107. CCUtility.setOriginalFileName((formRow.value! as AnyObject).boolValue, key:k_keyFileNameOriginalAutoUpload)
  108. self.reloadForm()
  109. }
  110. else if formRow.tag == "maskFileName" {
  111. let fileName = formRow.value as? String
  112. self.form.delegate = nil
  113. if let fileName = fileName {
  114. formRow.value = CCUtility.removeForbiddenCharactersServer(fileName)
  115. }
  116. self.form.delegate = self
  117. let previewFileName : XLFormRowDescriptor = self.form.formRow(withTag: "previewFileName")!
  118. previewFileName.value = self.previewFileName(valueRename: formRow.value as? String)
  119. // reload cell
  120. if fileName != nil {
  121. if newValue as! String != formRow.value as! String {
  122. self.reloadFormRow(formRow)
  123. NCContentPresenter.shared.messageNotification("_info_", description: "_forbidden_characters_", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.info, errorCode: 0)
  124. }
  125. }
  126. self.reloadFormRow(previewFileName)
  127. }
  128. }
  129. // MARK: - Utility
  130. func previewFileName(valueRename : String?) -> String {
  131. var returnString : String = ""
  132. if CCUtility.getOriginalFileName(k_keyFileNameOriginalAutoUpload) {
  133. return (NSLocalizedString("_filename_", comment: "") + ": IMG_0001.JPG")
  134. } else if let valueRename = valueRename {
  135. let valueRenameTrimming = valueRename.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
  136. if valueRenameTrimming.count > 0 {
  137. self.form.delegate = nil
  138. CCUtility.setFileNameMask(valueRename, key: k_keyFileNameAutoUploadMask)
  139. self.form.delegate = self
  140. returnString = CCUtility.createFileName("IMG_0001.JPG", fileDate: dateExample, fileType: PHAssetMediaType.image, keyFileName: k_keyFileNameAutoUploadMask, keyFileNameType: k_keyFileNameAutoUploadType, keyFileNameOriginal: k_keyFileNameOriginalAutoUpload)
  141. } else {
  142. CCUtility.setFileNameMask("", key: k_keyFileNameAutoUploadMask)
  143. returnString = CCUtility.createFileName("IMG_0001.JPG", fileDate: dateExample, fileType: PHAssetMediaType.image, keyFileName: nil, keyFileNameType: k_keyFileNameAutoUploadType, keyFileNameOriginal: k_keyFileNameOriginalAutoUpload)
  144. }
  145. } else {
  146. CCUtility.setFileNameMask("", key: k_keyFileNameAutoUploadMask)
  147. returnString = CCUtility.createFileName("IMG_0001.JPG", fileDate: dateExample, fileType: PHAssetMediaType.image, keyFileName: nil, keyFileNameType: k_keyFileNameAutoUploadType, keyFileNameOriginal: k_keyFileNameOriginalAutoUpload)
  148. }
  149. return String(format: NSLocalizedString("_preview_filename_", comment: ""), "MM,MMM,DD,YY,YYYY and HH,hh,mm,ss,ampm") + ":" + "\n\n" + returnString
  150. }
  151. }