NCManageAutoUploadFileName.swift 9.7 KB

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