NCCreateFormUploadConflict.swift 8.3 KB

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