NCManageAutoUploadFileName.swift 8.9 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. section.addFormRow(row)
  48. // Add File Name Type
  49. row = XLFormRowDescriptor(tag: "addFileNameType", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_add_filenametype_", comment: ""))
  50. row.value = CCUtility.getFileNameType(k_keyFileNameAutoUploadType)
  51. row.hidden = "$\("maintainOriginalFileName") == 1"
  52. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  53. section.addFormRow(row)
  54. // Section: Rename File Name
  55. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_filename_", comment: ""))
  56. form.addFormSection(section)
  57. row = XLFormRowDescriptor(tag: "maskFileName", rowType: XLFormRowDescriptorTypeAccount, title: (NSLocalizedString("_filename_", comment: "")))
  58. let fileNameMask : String = CCUtility.getFileNameMask(k_keyFileNameAutoUploadMask)
  59. if fileNameMask.count > 0 {
  60. row.value = fileNameMask
  61. }
  62. row.hidden = "$\("maintainOriginalFileName") == 1"
  63. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  64. row.cellConfig["textField.textAlignment"] = NSTextAlignment.right.rawValue
  65. row.cellConfig["textField.font"] = UIFont.systemFont(ofSize: 15.0)
  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["textView.backgroundColor"] = NCBrandColor.sharedInstance.backgroundView
  72. row.cellConfig["textView.font"] = UIFont.systemFont(ofSize: 14.0)
  73. section.addFormRow(row)
  74. self.form = form
  75. }
  76. override func formRowDescriptorValueHasChanged(_ formRow: XLFormRowDescriptor!, oldValue: Any!, newValue: Any!) {
  77. super.formRowDescriptorValueHasChanged(formRow, oldValue: oldValue, newValue: newValue)
  78. if formRow.tag == "addFileNameType" {
  79. CCUtility.setFileNameType((formRow.value! as AnyObject).boolValue, key: k_keyFileNameAutoUploadType)
  80. self.reloadForm()
  81. }
  82. else if formRow.tag == "maintainOriginalFileName" {
  83. CCUtility.setOriginalFileName((formRow.value! as AnyObject).boolValue, key:k_keyFileNameOriginalAutoUpload)
  84. self.reloadForm()
  85. }
  86. else if formRow.tag == "maskFileName" {
  87. let fileName = formRow.value as? String
  88. self.form.delegate = nil
  89. if let fileName = fileName {
  90. formRow.value = CCUtility.removeForbiddenCharactersServer(fileName)
  91. }
  92. self.form.delegate = self
  93. let previewFileName : XLFormRowDescriptor = self.form.formRow(withTag: "previewFileName")!
  94. previewFileName.value = self.previewFileName(valueRename: formRow.value as? String)
  95. // reload cell
  96. if fileName != nil {
  97. if newValue as! String != formRow.value as! String {
  98. self.reloadFormRow(formRow)
  99. appDelegate.messageNotification("_info_", description: "_forbidden_characters_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.info, errorCode: 0)
  100. }
  101. }
  102. self.reloadFormRow(previewFileName)
  103. }
  104. }
  105. // MARK: - View Life Cycle
  106. override func viewDidLoad() {
  107. super.viewDidLoad()
  108. self.navigationController?.navigationBar.isTranslucent = false
  109. self.navigationController?.navigationBar.barTintColor = NCBrandColor.sharedInstance.brand
  110. self.navigationController?.navigationBar.tintColor = NCBrandColor.sharedInstance.brandText
  111. self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: NCBrandColor.sharedInstance.brandText]
  112. self.tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
  113. self.reloadForm()
  114. }
  115. func reloadForm() {
  116. self.form.delegate = nil
  117. let maskFileName : XLFormRowDescriptor = self.form.formRow(withTag: "maskFileName")!
  118. let previewFileName : XLFormRowDescriptor = self.form.formRow(withTag: "previewFileName")!
  119. previewFileName.value = self.previewFileName(valueRename: maskFileName.value as? String)
  120. self.tableView.reloadData()
  121. self.form.delegate = self
  122. }
  123. // MARK: - Utility
  124. func previewFileName(valueRename : String?) -> String {
  125. var returnString : String = ""
  126. if CCUtility.getOriginalFileName(k_keyFileNameOriginalAutoUpload) {
  127. return (NSLocalizedString("_filename_", comment: "") + ": IMG_0001.JPG")
  128. } else if let valueRename = valueRename {
  129. let valueRenameTrimming = valueRename.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
  130. if valueRenameTrimming.count > 0 {
  131. self.form.delegate = nil
  132. CCUtility.setFileNameMask(valueRename, key: k_keyFileNameAutoUploadMask)
  133. self.form.delegate = self
  134. returnString = CCUtility.createFileName("IMG_0001.JPG", fileDate: dateExample, fileType: PHAssetMediaType.image, keyFileName: k_keyFileNameAutoUploadMask, keyFileNameType: k_keyFileNameAutoUploadType, keyFileNameOriginal: k_keyFileNameOriginalAutoUpload)
  135. } else {
  136. CCUtility.setFileNameMask("", key: k_keyFileNameAutoUploadMask)
  137. returnString = CCUtility.createFileName("IMG_0001.JPG", fileDate: dateExample, fileType: PHAssetMediaType.image, keyFileName: nil, keyFileNameType: k_keyFileNameAutoUploadType, keyFileNameOriginal: k_keyFileNameOriginalAutoUpload)
  138. }
  139. } else {
  140. CCUtility.setFileNameMask("", key: k_keyFileNameAutoUploadMask)
  141. returnString = CCUtility.createFileName("IMG_0001.JPG", fileDate: dateExample, fileType: PHAssetMediaType.image, keyFileName: nil, keyFileNameType: k_keyFileNameAutoUploadType, keyFileNameOriginal: k_keyFileNameOriginalAutoUpload)
  142. }
  143. return String(format: NSLocalizedString("_preview_filename_", comment: ""), "MM,MMM,DD,YY,YYYY and HH,hh,mm,ss,ampm") + ":" + "\n\n" + returnString
  144. }
  145. }