NCManageAutoUploadFileName.swift 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. view.backgroundColor = NCBrandColor.shared.backgroundForm
  45. tableView.backgroundColor = NCBrandColor.shared.backgroundForm
  46. tableView.reloadData()
  47. initializeForm()
  48. self.reloadForm()
  49. }
  50. //MARK: XLForm
  51. func initializeForm() {
  52. let form : XLFormDescriptor = XLFormDescriptor() as XLFormDescriptor
  53. form.rowNavigationOptions = XLFormRowNavigationOptions.stopDisableRow
  54. var section : XLFormSectionDescriptor
  55. var row : XLFormRowDescriptor
  56. // Section Mode filename
  57. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_mode_filename_", comment: ""))
  58. form.addFormSection(section)
  59. // Maintain the original fileName
  60. row = XLFormRowDescriptor(tag: "maintainOriginalFileName", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_maintain_original_filename_", comment: ""))
  61. row.value = CCUtility.getOriginalFileName(k_keyFileNameOriginalAutoUpload)
  62. row.cellConfig["backgroundColor"] = NCBrandColor.shared.backgroundView
  63. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  64. row.cellConfig["textLabel.textColor"] = NCBrandColor.shared.textView
  65. section.addFormRow(row)
  66. // Add File Name Type
  67. row = XLFormRowDescriptor(tag: "addFileNameType", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_add_filenametype_", comment: ""))
  68. row.value = CCUtility.getFileNameType(k_keyFileNameAutoUploadType)
  69. row.hidden = "$\("maintainOriginalFileName") == 1"
  70. row.cellConfig["backgroundColor"] = NCBrandColor.shared.backgroundView
  71. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  72. row.cellConfig["textLabel.textColor"] = NCBrandColor.shared.textView
  73. section.addFormRow(row)
  74. // Section: Rename File Name
  75. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_filename_", comment: ""))
  76. form.addFormSection(section)
  77. row = XLFormRowDescriptor(tag: "maskFileName", rowType: XLFormRowDescriptorTypeAccount, title: (NSLocalizedString("_filename_", comment: "")))
  78. let fileNameMask : String = CCUtility.getFileNameMask(k_keyFileNameAutoUploadMask)
  79. if fileNameMask.count > 0 {
  80. row.value = fileNameMask
  81. }
  82. row.hidden = "$\("maintainOriginalFileName") == 1"
  83. row.cellConfig["backgroundColor"] = NCBrandColor.shared.backgroundView
  84. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  85. row.cellConfig["textLabel.textColor"] = NCBrandColor.shared.textView
  86. row.cellConfig["textField.textAlignment"] = NSTextAlignment.right.rawValue
  87. row.cellConfig["textField.font"] = UIFont.systemFont(ofSize: 15.0)
  88. row.cellConfig["textField.textColor"] = NCBrandColor.shared.textView
  89. section.addFormRow(row)
  90. // Section: Preview File Name
  91. row = XLFormRowDescriptor(tag: "previewFileName", rowType: XLFormRowDescriptorTypeTextView, title: "")
  92. row.height = 180
  93. row.disabled = true
  94. row.cellConfig["backgroundColor"] = NCBrandColor.shared.backgroundView
  95. row.cellConfig["textView.backgroundColor"] = NCBrandColor.shared.backgroundView
  96. row.cellConfig["textView.font"] = UIFont.systemFont(ofSize: 14.0)
  97. row.cellConfig["textView.textColor"] = NCBrandColor.shared.textView
  98. section.addFormRow(row)
  99. self.tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
  100. self.form = form
  101. }
  102. override func formRowDescriptorValueHasChanged(_ formRow: XLFormRowDescriptor!, oldValue: Any!, newValue: Any!) {
  103. super.formRowDescriptorValueHasChanged(formRow, oldValue: oldValue, newValue: newValue)
  104. if formRow.tag == "addFileNameType" {
  105. CCUtility.setFileNameType((formRow.value! as AnyObject).boolValue, key: k_keyFileNameAutoUploadType)
  106. self.reloadForm()
  107. }
  108. else if formRow.tag == "maintainOriginalFileName" {
  109. CCUtility.setOriginalFileName((formRow.value! as AnyObject).boolValue, key:k_keyFileNameOriginalAutoUpload)
  110. self.reloadForm()
  111. }
  112. else if formRow.tag == "maskFileName" {
  113. let fileName = formRow.value as? String
  114. self.form.delegate = nil
  115. if let fileName = fileName {
  116. formRow.value = CCUtility.removeForbiddenCharactersServer(fileName)
  117. }
  118. self.form.delegate = self
  119. let previewFileName : XLFormRowDescriptor = self.form.formRow(withTag: "previewFileName")!
  120. previewFileName.value = self.previewFileName(valueRename: formRow.value as? String)
  121. // reload cell
  122. if fileName != nil {
  123. if newValue as! String != formRow.value as! String {
  124. self.reloadFormRow(formRow)
  125. NCContentPresenter.shared.messageNotification("_info_", description: "_forbidden_characters_", delay: NCBrandGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.info, errorCode: Int(k_CCErrorCharactersForbidden), forced: true)
  126. }
  127. }
  128. self.reloadFormRow(previewFileName)
  129. }
  130. }
  131. // MARK: - Utility
  132. func previewFileName(valueRename : String?) -> String {
  133. var returnString : String = ""
  134. if CCUtility.getOriginalFileName(k_keyFileNameOriginalAutoUpload) {
  135. return (NSLocalizedString("_filename_", comment: "") + ": IMG_0001.JPG")
  136. } else if let valueRename = valueRename {
  137. let valueRenameTrimming = valueRename.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
  138. if valueRenameTrimming.count > 0 {
  139. self.form.delegate = nil
  140. CCUtility.setFileNameMask(valueRename, key: k_keyFileNameAutoUploadMask)
  141. self.form.delegate = self
  142. returnString = CCUtility.createFileName("IMG_0001.JPG", fileDate: dateExample, fileType: PHAssetMediaType.image, keyFileName: k_keyFileNameAutoUploadMask, keyFileNameType: k_keyFileNameAutoUploadType, keyFileNameOriginal: k_keyFileNameOriginalAutoUpload)
  143. } else {
  144. CCUtility.setFileNameMask("", key: k_keyFileNameAutoUploadMask)
  145. returnString = CCUtility.createFileName("IMG_0001.JPG", fileDate: dateExample, fileType: PHAssetMediaType.image, keyFileName: nil, keyFileNameType: k_keyFileNameAutoUploadType, keyFileNameOriginal: k_keyFileNameOriginalAutoUpload)
  146. }
  147. } else {
  148. CCUtility.setFileNameMask("", key: k_keyFileNameAutoUploadMask)
  149. returnString = CCUtility.createFileName("IMG_0001.JPG", fileDate: dateExample, fileType: PHAssetMediaType.image, keyFileName: nil, keyFileNameType: k_keyFileNameAutoUploadType, keyFileNameOriginal: k_keyFileNameOriginalAutoUpload)
  150. }
  151. return String(format: NSLocalizedString("_preview_filename_", comment: ""), "MM,MMM,DD,YY,YYYY and HH,hh,mm,ss,ampm") + ":" + "\n\n" + returnString
  152. }
  153. }