NCManageAutoUploadFileName.swift 9.0 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 UIKit
  24. import Photos
  25. class NCManageAutoUploadFileName: XLFormViewController {
  26. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  27. let dateExample = Date()
  28. func initializeForm() {
  29. let form: XLFormDescriptor = XLFormDescriptor() as XLFormDescriptor
  30. form.rowNavigationOptions = XLFormRowNavigationOptions.stopDisableRow
  31. var section: XLFormSectionDescriptor
  32. var row: XLFormRowDescriptor
  33. // Section Mode filename
  34. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_mode_filename_", comment: ""))
  35. form.addFormSection(section)
  36. // Maintain the original fileName
  37. row = XLFormRowDescriptor(tag: "maintainOriginalFileName", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_maintain_original_filename_", comment: ""))
  38. row.value = CCUtility.getOriginalFileName(NCGlobal.shared.keyFileNameOriginalAutoUpload)
  39. row.cellConfig["backgroundColor"] = NCBrandColor.shared.secondarySystemGroupedBackground
  40. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  41. row.cellConfig["textLabel.textColor"] = NCBrandColor.shared.label
  42. section.addFormRow(row)
  43. // Add File Name Type
  44. row = XLFormRowDescriptor(tag: "addFileNameType", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_add_filenametype_", comment: ""))
  45. row.value = CCUtility.getFileNameType(NCGlobal.shared.keyFileNameAutoUploadType)
  46. row.hidden = "$\("maintainOriginalFileName") == 1"
  47. row.cellConfig["backgroundColor"] = NCBrandColor.shared.secondarySystemGroupedBackground
  48. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  49. row.cellConfig["textLabel.textColor"] = NCBrandColor.shared.label
  50. section.addFormRow(row)
  51. // Section: Rename File Name
  52. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_filename_", comment: ""))
  53. form.addFormSection(section)
  54. row = XLFormRowDescriptor(tag: "maskFileName", rowType: XLFormRowDescriptorTypeText, title: (NSLocalizedString("_filename_", comment: "")))
  55. let fileNameMask: String = CCUtility.getFileNameMask(NCGlobal.shared.keyFileNameAutoUploadMask)
  56. if fileNameMask.count > 0 {
  57. row.value = fileNameMask
  58. }
  59. row.hidden = "$\("maintainOriginalFileName") == 1"
  60. row.cellConfig["backgroundColor"] = NCBrandColor.shared.secondarySystemGroupedBackground
  61. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  62. row.cellConfig["textLabel.textColor"] = NCBrandColor.shared.label
  63. row.cellConfig["textField.textAlignment"] = NSTextAlignment.right.rawValue
  64. row.cellConfig["textField.font"] = UIFont.systemFont(ofSize: 15.0)
  65. row.cellConfig["textField.textColor"] = NCBrandColor.shared.label
  66. section.addFormRow(row)
  67. // Section: Preview File Name
  68. row = XLFormRowDescriptor(tag: "previewFileName", rowType: XLFormRowDescriptorTypeTextView, title: "")
  69. row.height = 180
  70. row.disabled = true
  71. row.cellConfig["backgroundColor"] = NCBrandColor.shared.secondarySystemGroupedBackground
  72. row.cellConfig["textView.backgroundColor"] = NCBrandColor.shared.secondarySystemGroupedBackground
  73. row.cellConfig["textView.font"] = UIFont.systemFont(ofSize: 14.0)
  74. row.cellConfig["textView.textColor"] = NCBrandColor.shared.label
  75. section.addFormRow(row)
  76. self.tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
  77. self.form = form
  78. }
  79. // MARK: - View Life Cycle
  80. override func viewDidLoad() {
  81. super.viewDidLoad()
  82. self.title = NSLocalizedString("_mode_filename_", comment: "")
  83. view.backgroundColor = NCBrandColor.shared.systemGroupedBackground
  84. tableView.backgroundColor = NCBrandColor.shared.systemGroupedBackground
  85. initializeForm()
  86. reloadForm()
  87. }
  88. override func viewWillAppear(_ animated: Bool) {
  89. super.viewWillAppear(animated)
  90. appDelegate.activeViewController = self
  91. }
  92. // MARK: XLForm
  93. func reloadForm() {
  94. self.form.delegate = nil
  95. let maskFileName: XLFormRowDescriptor = self.form.formRow(withTag: "maskFileName")!
  96. let previewFileName: XLFormRowDescriptor = self.form.formRow(withTag: "previewFileName")!
  97. previewFileName.value = self.previewFileName(valueRename: maskFileName.value as? String)
  98. self.tableView.reloadData()
  99. self.form.delegate = self
  100. }
  101. override func formRowDescriptorValueHasChanged(_ formRow: XLFormRowDescriptor!, oldValue: Any!, newValue: Any!) {
  102. super.formRowDescriptorValueHasChanged(formRow, oldValue: oldValue, newValue: newValue)
  103. if formRow.tag == "addFileNameType" {
  104. CCUtility.setFileNameType((formRow.value! as AnyObject).boolValue, key: NCGlobal.shared.keyFileNameAutoUploadType)
  105. self.reloadForm()
  106. } else if formRow.tag == "maintainOriginalFileName" {
  107. CCUtility.setOriginalFileName((formRow.value! as AnyObject).boolValue, key: NCGlobal.shared.keyFileNameOriginalAutoUpload)
  108. self.reloadForm()
  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: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.info, errorCode: NCGlobal.shared.errorCharactersForbidden)
  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(NCGlobal.shared.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: NCGlobal.shared.keyFileNameAutoUploadMask)
  138. self.form.delegate = self
  139. returnString = CCUtility.createFileName("IMG_0001.JPG", fileDate: dateExample, fileType: PHAssetMediaType.image, keyFileName: NCGlobal.shared.keyFileNameAutoUploadMask, keyFileNameType: NCGlobal.shared.keyFileNameAutoUploadType, keyFileNameOriginal: NCGlobal.shared.keyFileNameOriginalAutoUpload, forcedNewFileName: false)
  140. } else {
  141. CCUtility.setFileNameMask("", key: NCGlobal.shared.keyFileNameAutoUploadMask)
  142. returnString = CCUtility.createFileName("IMG_0001.JPG", fileDate: dateExample, fileType: PHAssetMediaType.image, keyFileName: nil, keyFileNameType: NCGlobal.shared.keyFileNameAutoUploadType, keyFileNameOriginal: NCGlobal.shared.keyFileNameOriginalAutoUpload, forcedNewFileName: false)
  143. }
  144. } else {
  145. CCUtility.setFileNameMask("", key: NCGlobal.shared.keyFileNameAutoUploadMask)
  146. returnString = CCUtility.createFileName("IMG_0001.JPG", fileDate: dateExample, fileType: PHAssetMediaType.image, keyFileName: nil, keyFileNameType: NCGlobal.shared.keyFileNameAutoUploadType, keyFileNameOriginal: NCGlobal.shared.keyFileNameOriginalAutoUpload, forcedNewFileName: false)
  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. }