NCManageAutoUploadFileName.swift 8.9 KB

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