NCCreateFormUploadConflict.swift 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. //
  2. // NCCreateFormUploadConflict.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 29/03/2020.
  6. // Copyright © 2020 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 Foundation
  24. @objc class NCCreateFormUploadConflict: UIViewController {
  25. @IBOutlet weak var labelTitle: UILabel!
  26. @IBOutlet weak var labelSubTitle: UILabel!
  27. @IBOutlet weak var switchNewFiles: UISwitch!
  28. @IBOutlet weak var switchAlreadyExistingFiles: UISwitch!
  29. @IBOutlet weak var labelNewFiles: UILabel!
  30. @IBOutlet weak var labelAlreadyExistingFiles: UILabel!
  31. @IBOutlet weak var tableView: UITableView!
  32. @IBOutlet weak var buttonCancel: UIButton!
  33. @IBOutlet weak var buttonContinue: UIButton!
  34. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  35. @objc var metadatas: [tableMetadata]
  36. @objc var metadatasConflict: [tableMetadata]
  37. var metadatasConflictNewFiles = [String]()
  38. var metadatasConflictAlreadyExistingFiles = [String]()
  39. // MARK: - Cicle
  40. @objc required init?(coder aDecoder: NSCoder) {
  41. self.metadatas = [tableMetadata]()
  42. self.metadatasConflict = [tableMetadata]()
  43. super.init(coder: aDecoder)
  44. }
  45. override func viewDidLoad() {
  46. super.viewDidLoad()
  47. tableView.dataSource = self
  48. tableView.delegate = self
  49. tableView.tableFooterView = UIView()
  50. tableView.register(UINib.init(nibName: "NCCreateFormUploadConflictCell", bundle: nil), forCellReuseIdentifier: "Cell")
  51. labelTitle.text = String(metadatasConflict.count) + " " + NSLocalizedString("_file_conflict_num_", comment: "")
  52. labelSubTitle.text = NSLocalizedString("_file_conflict_desc_", comment: "")
  53. labelNewFiles.text = NSLocalizedString("_file_conflict_new_", comment: "")
  54. labelAlreadyExistingFiles.text = NSLocalizedString("_file_conflict_exists_", comment: "")
  55. switchNewFiles.isOn = false
  56. switchAlreadyExistingFiles.isOn = false
  57. buttonCancel.layer.cornerRadius = 20
  58. buttonCancel.layer.masksToBounds = true
  59. buttonCancel.setTitle(NSLocalizedString("_cancel_", comment: ""), for: .normal)
  60. buttonCancel.layer.backgroundColor = NCBrandColor.sharedInstance.graySoft.withAlphaComponent(0.5).cgColor
  61. buttonContinue.layer.cornerRadius = 20
  62. buttonContinue.layer.masksToBounds = true
  63. buttonContinue.setTitle(NSLocalizedString("_continue_", comment: ""), for: .normal)
  64. buttonContinue.isEnabled = false
  65. buttonContinue.setTitleColor(.lightGray, for: .normal)
  66. buttonContinue.layer.backgroundColor = NCBrandColor.sharedInstance.graySoft.withAlphaComponent(0.5).cgColor
  67. }
  68. @IBAction func valueChangedSwitchNewFiles(_ sender: Any) {
  69. metadatasConflictNewFiles.removeAll()
  70. if switchNewFiles.isOn {
  71. for metadata in metadatasConflict {
  72. metadatasConflictNewFiles.append(metadata.ocId)
  73. }
  74. }
  75. tableView.reloadData()
  76. canContinue()
  77. }
  78. @IBAction func valueChangedSwitchAlreadyExistingFiles(_ sender: Any) {
  79. metadatasConflictAlreadyExistingFiles.removeAll()
  80. if switchAlreadyExistingFiles.isOn {
  81. for metadata in metadatasConflict {
  82. metadatasConflictAlreadyExistingFiles.append(metadata.ocId)
  83. }
  84. }
  85. tableView.reloadData()
  86. canContinue()
  87. }
  88. @IBAction func buttonCancelTouch(_ sender: Any) {
  89. dismiss(animated: true)
  90. }
  91. @IBAction func buttonContinueTouch(_ sender: Any) {
  92. for metadata in metadatasConflict {
  93. // new filename + num
  94. if metadatasConflictNewFiles.contains(metadata.ocId) && metadatasConflictAlreadyExistingFiles.contains(metadata.ocId) {
  95. // overwrite
  96. } else if metadatasConflictNewFiles.contains(metadata.ocId) {
  97. // remove
  98. } else if metadatasConflictAlreadyExistingFiles.contains(metadata.ocId) {
  99. } else {
  100. return
  101. }
  102. }
  103. }
  104. }
  105. // MARK: - UITableViewDelegate
  106. extension NCCreateFormUploadConflict: UITableViewDelegate {
  107. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  108. return 135
  109. }
  110. }
  111. // MARK: - UITableViewDataSource
  112. extension NCCreateFormUploadConflict: UITableViewDataSource {
  113. func numberOfSections(in tableView: UITableView) -> Int {
  114. return 1
  115. }
  116. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  117. return metadatasConflict.count
  118. }
  119. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  120. if let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as? NCCreateFormUploadConflictCell {
  121. let metadata = metadatasConflict[indexPath.row]
  122. cell.ocId = metadata.ocId
  123. cell.delegate = self
  124. if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, fileNameView: metadata.fileName)) {
  125. NCUtility.sharedInstance.loadImage(ocId: metadata.ocId, fileNameView: metadata.fileNameView) { (image) in
  126. cell.imageFile.image = image
  127. }
  128. } else {
  129. if metadata.iconName.count > 0 {
  130. cell.imageFile.image = UIImage.init(named: metadata.iconName)
  131. } else {
  132. cell.imageFile.image = UIImage.init(named: "file")
  133. }
  134. }
  135. cell.labelFileName.text = metadata.fileNameView
  136. cell.labelDetail.text = CCUtility.dateDiff(metadata.date as Date) + ", " + CCUtility.transformedSize(metadata.size)
  137. if metadatasConflictNewFiles.contains(metadata.ocId) {
  138. cell.switchNewFile.isOn = true
  139. } else {
  140. cell.switchNewFile.isOn = false
  141. }
  142. if metadatasConflictAlreadyExistingFiles.contains(metadata.ocId) {
  143. cell.switchAlreadyExistingFile.isOn = true
  144. } else {
  145. cell.switchAlreadyExistingFile.isOn = false
  146. }
  147. return cell
  148. }
  149. return UITableViewCell()
  150. }
  151. }
  152. // MARK: - NCCreateFormUploadConflictCellDelegate
  153. extension NCCreateFormUploadConflict: NCCreateFormUploadConflictCellDelegate {
  154. func valueChangedSwitchNewFile(with ocId: String, isOn: Bool) {
  155. if let index = metadatasConflictNewFiles.firstIndex(of: ocId) {
  156. metadatasConflictNewFiles.remove(at: index)
  157. }
  158. if isOn {
  159. metadatasConflictNewFiles.append(ocId)
  160. }
  161. if metadatasConflictNewFiles.count == metadatasConflict.count {
  162. switchNewFiles.isOn = true
  163. } else {
  164. switchNewFiles.isOn = false
  165. }
  166. canContinue()
  167. }
  168. func valueChangedSwitchAlreadyExistingFile(with ocId: String, isOn: Bool) {
  169. if let index = metadatasConflictAlreadyExistingFiles.firstIndex(of: ocId) {
  170. metadatasConflictAlreadyExistingFiles.remove(at: index)
  171. }
  172. if isOn {
  173. metadatasConflictAlreadyExistingFiles.append(ocId)
  174. }
  175. if metadatasConflictAlreadyExistingFiles.count == metadatasConflict.count {
  176. switchAlreadyExistingFiles.isOn = true
  177. } else {
  178. switchAlreadyExistingFiles.isOn = false
  179. }
  180. canContinue()
  181. }
  182. func canContinue() {
  183. var result = true
  184. for metadata in metadatasConflict {
  185. if !metadatasConflictNewFiles.contains(metadata.ocId) && !metadatasConflictAlreadyExistingFiles.contains(metadata.ocId) {
  186. result = false
  187. }
  188. }
  189. if result {
  190. buttonContinue.isEnabled = true
  191. buttonContinue.setTitleColor(.black, for: .normal)
  192. } else {
  193. buttonContinue.isEnabled = false
  194. buttonContinue.setTitleColor(.lightGray, for: .normal)
  195. }
  196. }
  197. }