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