NCManageAutoUploadFileName.swift 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //
  2. // NCManageAutoUploadFileName.swift
  3. // Nextcloud iOS
  4. //
  5. // Created by Marino Faggiana on 19/07/17.
  6. // Copyright (c) 2017 TWS. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  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. required init(coder aDecoder: NSCoder) {
  28. super.init(coder: aDecoder)
  29. self.initializeForm()
  30. }
  31. override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
  32. super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
  33. self.initializeForm()
  34. }
  35. func initializeForm() {
  36. let form : XLFormDescriptor = XLFormDescriptor(title: NSLocalizedString("_autoupload_filename_title_", comment: "")) as XLFormDescriptor
  37. form.rowNavigationOptions = XLFormRowNavigationOptions.stopDisableRow
  38. var section : XLFormSectionDescriptor
  39. var row : XLFormRowDescriptor
  40. // Section Mode filename
  41. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_mode_filename_", comment: ""))
  42. form.addFormSection(section)
  43. // Maintain the original fileName
  44. row = XLFormRowDescriptor(tag: "maintainOriginalFileName", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_maintain_original_filename_", comment: ""))
  45. row.value = CCUtility.getOriginalFileName(k_keyFileNameOriginalAutoUpload)
  46. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  47. row.cellConfig["textLabel.textColor"] = UIColor.black
  48. section.addFormRow(row)
  49. // Add File Name Type
  50. row = XLFormRowDescriptor(tag: "addFileNameType", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_add_filenametype_", comment: ""))
  51. row.value = CCUtility.getFileNameType(k_keyFileNameAutoUploadType)
  52. row.hidden = "$\("maintainOriginalFileName") == 1"
  53. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  54. row.cellConfig["textLabel.textColor"] = UIColor.black
  55. section.addFormRow(row)
  56. // Section: Rename File Name
  57. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_filename_", comment: ""))
  58. form.addFormSection(section)
  59. row = XLFormRowDescriptor(tag: "maskFileName", rowType: XLFormRowDescriptorTypeAccount, title: (NSLocalizedString("_filename_", comment: ""))+":")
  60. let fileNameMask : String = CCUtility.getFileNameMask(k_keyFileNameAutoUploadMask)
  61. if fileNameMask.count > 0 {
  62. row.value = fileNameMask
  63. }
  64. row.hidden = "$\("maintainOriginalFileName") == 1"
  65. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  66. row.cellConfig["textLabel.textColor"] = UIColor.black
  67. section.addFormRow(row)
  68. // Section: Preview File Name
  69. row = XLFormRowDescriptor(tag: "previewFileName", rowType: XLFormRowDescriptorTypeTextView, title: "")
  70. row.cellConfig.setObject(UIFont.systemFont(ofSize: 15.0), forKey: "textLabel.font" as NSCopying)
  71. row.height = 180
  72. row.cellConfig.setObject(NCBrandColor.sharedInstance.backgroundView, forKey: "backgroundColor" as NSCopying)
  73. row.cellConfig.setObject(NCBrandColor.sharedInstance.backgroundView, forKey: "textView.backgroundColor" as NSCopying)
  74. row.disabled = true
  75. section.addFormRow(row)
  76. self.form = form
  77. }
  78. override func formRowDescriptorValueHasChanged(_ formRow: XLFormRowDescriptor!, oldValue: Any!, newValue: Any!) {
  79. super.formRowDescriptorValueHasChanged(formRow, oldValue: oldValue, newValue: newValue)
  80. if formRow.tag == "addFileNameType" {
  81. CCUtility.setFileNameType((formRow.value! as AnyObject).boolValue, key: k_keyFileNameAutoUploadType)
  82. self.reloadForm()
  83. }
  84. else if formRow.tag == "maintainOriginalFileName" {
  85. CCUtility.setOriginalFileName((formRow.value! as AnyObject).boolValue, key:k_keyFileNameOriginalAutoUpload)
  86. self.reloadForm()
  87. }
  88. else if formRow.tag == "maskFileName" {
  89. let fileName = formRow.value as? String
  90. self.form.delegate = nil
  91. if let fileName = fileName {
  92. formRow.value = CCUtility.removeForbiddenCharactersServer(fileName)
  93. }
  94. self.form.delegate = self
  95. let previewFileName : XLFormRowDescriptor = self.form.formRow(withTag: "previewFileName")!
  96. previewFileName.value = self.previewFileName(valueRename: formRow.value as? String)
  97. // reload cell
  98. if fileName != nil {
  99. if newValue as! String != formRow.value as! String {
  100. self.reloadFormRow(formRow)
  101. appDelegate.messageNotification("_info_", description: "_forbidden_characters_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.info, errorCode: 0)
  102. }
  103. }
  104. self.reloadFormRow(previewFileName)
  105. }
  106. }
  107. // MARK: - View Life Cycle
  108. override func viewDidLoad() {
  109. super.viewDidLoad()
  110. self.navigationController?.navigationBar.isTranslucent = false
  111. self.navigationController?.navigationBar.barTintColor = NCBrandColor.sharedInstance.brand
  112. self.navigationController?.navigationBar.tintColor = NCBrandColor.sharedInstance.brandText
  113. self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: NCBrandColor.sharedInstance.brandText]
  114. self.tableView.separatorStyle = UITableViewCellSeparatorStyle.none
  115. self.reloadForm()
  116. }
  117. func reloadForm() {
  118. self.form.delegate = nil
  119. let maskFileName : XLFormRowDescriptor = self.form.formRow(withTag: "maskFileName")!
  120. let previewFileName : XLFormRowDescriptor = self.form.formRow(withTag: "previewFileName")!
  121. previewFileName.value = self.previewFileName(valueRename: maskFileName.value as? String)
  122. self.tableView.reloadData()
  123. self.form.delegate = self
  124. }
  125. // MARK: - Utility
  126. func previewFileName(valueRename : String?) -> String {
  127. var returnString : String = ""
  128. if CCUtility.getOriginalFileName(k_keyFileNameOriginalAutoUpload) {
  129. return (NSLocalizedString("_filename_", comment: "") + ": IMG_0001.JPG")
  130. } else if let valueRename = valueRename {
  131. let valueRenameTrimming = valueRename.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
  132. if valueRenameTrimming.count > 0 {
  133. self.form.delegate = nil
  134. CCUtility.setFileNameMask(valueRename, key: k_keyFileNameAutoUploadMask)
  135. self.form.delegate = self
  136. returnString = CCUtility.createFileName("IMG_0001.JPG", fileDate: dateExample, fileType: PHAssetMediaType.image, keyFileName: k_keyFileNameAutoUploadMask, keyFileNameType: k_keyFileNameAutoUploadType, keyFileNameOriginal: k_keyFileNameOriginalAutoUpload)
  137. } else {
  138. CCUtility.setFileNameMask("", key: k_keyFileNameAutoUploadMask)
  139. returnString = CCUtility.createFileName("IMG_0001.JPG", fileDate: dateExample, fileType: PHAssetMediaType.image, keyFileName: nil, keyFileNameType: k_keyFileNameAutoUploadType, keyFileNameOriginal: k_keyFileNameOriginalAutoUpload)
  140. }
  141. } else {
  142. CCUtility.setFileNameMask("", key: k_keyFileNameAutoUploadMask)
  143. returnString = CCUtility.createFileName("IMG_0001.JPG", fileDate: dateExample, fileType: PHAssetMediaType.image, keyFileName: nil, keyFileNameType: k_keyFileNameAutoUploadType, keyFileNameOriginal: k_keyFileNameOriginalAutoUpload)
  144. }
  145. return String(format: NSLocalizedString("_preview_filename_", comment: ""), "MM,MMM,DD,YY,YYYY and HH,hh,mm,ss,ampm") + ":" + "\n\n" + returnString
  146. }
  147. }