NCManageAutoUploadFileName.swift 9.3 KB

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