NCManageAutoUploadFileName.swift 8.5 KB

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