NCTrash.swift 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. //
  2. // NCTrash.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 02/10/2018.
  6. // Copyright © 2018 Marino Faggiana. All rights reserved.
  7. //
  8. import Foundation
  9. class NCTrash: UIViewController , UICollectionViewDataSource, UICollectionViewDelegate, UIGestureRecognizerDelegate, NCTrashListDelegate, NCTrashGridDelegate, NCTrashHeaderDelegate {
  10. @IBOutlet fileprivate weak var collectionView: UICollectionView!
  11. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  12. var path = ""
  13. var titleCurrentFolder = NSLocalizedString("_trash_view_", comment: "")
  14. var itemHeight: CGFloat = 60
  15. var datasource = [tableTrash]()
  16. var listLayout: ListLayout!
  17. var gridLayout: GridLayout!
  18. override func viewDidLoad() {
  19. super.viewDidLoad()
  20. collectionView.register(UINib.init(nibName: "NCTrashListCell", bundle: nil), forCellWithReuseIdentifier: "cell-list")
  21. collectionView.register(UINib.init(nibName: "NCTrashGridCell", bundle: nil), forCellWithReuseIdentifier: "cell-grid")
  22. listLayout = ListLayout(itemHeight: 60)
  23. collectionView.collectionViewLayout = listLayout
  24. }
  25. override func viewWillAppear(_ animated: Bool) {
  26. super.viewWillAppear(animated)
  27. self.navigationItem.title = titleCurrentFolder
  28. if path == "" {
  29. let userID = (appDelegate.activeUserID as NSString).addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlFragmentAllowed)
  30. path = k_dav + "/trashbin/" + userID! + "/trash/"
  31. }
  32. let ocNetworking = OCnetworking.init(delegate: self, metadataNet: nil, withUser: appDelegate.activeUser, withUserID: appDelegate.activeUserID, withPassword: appDelegate.activePassword, withUrl: appDelegate.activeUrl)
  33. ocNetworking?.listingTrash(appDelegate.activeUrl, path:path, account: appDelegate.activeAccount, success: { (item) in
  34. NCManageDatabase.sharedInstance.deleteTrash(filePath: self.path)
  35. self.datasource = NCManageDatabase.sharedInstance.addTrashs(item as! [tableTrash])!
  36. self.collectionView.reloadData()
  37. }, failure: { (message, errorCode) in
  38. print("error " + message!)
  39. })
  40. }
  41. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  42. super.viewWillTransition(to: size, with: coordinator)
  43. coordinator.animate(alongsideTransition: nil) { _ in
  44. self.collectionView.collectionViewLayout.invalidateLayout()
  45. }
  46. }
  47. // MARK: tap
  48. func tapRestoreItem(with fileID: String) {
  49. print("tap item restore")
  50. }
  51. func tapMoreItem(with fileID: String) {
  52. print("tap item more")
  53. }
  54. func tapSwitchHeader() {
  55. print("tap header switch")
  56. }
  57. func tapMoreHeader() {
  58. print("tap header more")
  59. }
  60. // MARK: collectionView methods
  61. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  62. let trashHeader = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "header", for: indexPath) as! NCTrashHeader
  63. trashHeader.delegate = self
  64. return trashHeader
  65. }
  66. func numberOfSections(in collectionView: UICollectionView) -> Int {
  67. return 1
  68. }
  69. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  70. return datasource.count
  71. }
  72. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  73. var identifier = ""
  74. if collectionView.collectionViewLayout == listLayout {
  75. identifier = "cell-list"
  76. } else {
  77. identifier = "cell-grid"
  78. }
  79. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath) as! NCTrashListCell
  80. cell.delegate = self
  81. let tableTrash = datasource[indexPath.item]
  82. cell.fileID = tableTrash.fileID
  83. cell.labelTitle.text = tableTrash.trashbinFileName
  84. if tableTrash.directory {
  85. cell.imageItem.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
  86. cell.labelInfo.text = CCUtility.dateDiff(tableTrash.date as Date)
  87. } else {
  88. var image: UIImage?
  89. if tableTrash.iconName.count > 0 {
  90. image = UIImage.init(named: tableTrash.iconName)
  91. } else {
  92. image = UIImage.init(named: "file")
  93. }
  94. cell.imageItem.image = image
  95. cell.labelInfo.text = CCUtility.dateDiff(tableTrash.date as Date) + " " + CCUtility.transformedSize(tableTrash.size)
  96. }
  97. return cell
  98. }
  99. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  100. let tableTrash = datasource[indexPath.item]
  101. if tableTrash.directory {
  102. let ncTrash:NCTrash = UIStoryboard(name: "NCTrash", bundle: nil).instantiateInitialViewController() as! NCTrash
  103. ncTrash.path = tableTrash.filePath + tableTrash.fileName
  104. ncTrash.titleCurrentFolder = tableTrash.trashbinFileName
  105. self.navigationController?.pushViewController(ncTrash, animated: true)
  106. }
  107. }
  108. }
  109. class ListLayout: UICollectionViewFlowLayout {
  110. var itemHeight: CGFloat = 60
  111. var headerHeight: CGFloat = 30
  112. init(itemHeight: CGFloat) {
  113. super.init()
  114. minimumLineSpacing = 1
  115. minimumInteritemSpacing = 1
  116. self.itemHeight = itemHeight
  117. self.scrollDirection = .vertical
  118. self.headerReferenceSize = CGSize(width: 0, height: headerHeight)
  119. }
  120. required init?(coder aDecoder: NSCoder) {
  121. fatalError("init(coder:) has not been implemented")
  122. }
  123. override var itemSize: CGSize {
  124. get {
  125. if let collectionView = collectionView {
  126. let itemWidth: CGFloat = collectionView.frame.width
  127. return CGSize(width: itemWidth, height: self.itemHeight)
  128. }
  129. // Default fallback
  130. return CGSize(width: 100, height: 100)
  131. }
  132. set {
  133. super.itemSize = newValue
  134. }
  135. }
  136. }
  137. class GridLayout: UICollectionViewFlowLayout {
  138. var numberOfColumns: Int = 3
  139. var headerHeight: CGFloat = 30
  140. init(numberOfColumns: Int) {
  141. super.init()
  142. minimumLineSpacing = 1
  143. minimumInteritemSpacing = 1
  144. self.numberOfColumns = numberOfColumns
  145. self.scrollDirection = .vertical
  146. self.headerReferenceSize = CGSize(width: 0, height: headerHeight)
  147. }
  148. required init?(coder aDecoder: NSCoder) {
  149. fatalError("init(coder:) has not been implemented")
  150. }
  151. override var itemSize: CGSize {
  152. get {
  153. if let collectionView = collectionView {
  154. let itemWidth: CGFloat = (collectionView.frame.width/CGFloat(self.numberOfColumns)) - self.minimumInteritemSpacing
  155. let itemHeight: CGFloat = 100.0
  156. return CGSize(width: itemWidth, height: itemHeight)
  157. }
  158. // Default fallback
  159. return CGSize(width: 100, height: 100)
  160. }
  161. set {
  162. super.itemSize = newValue
  163. }
  164. }
  165. }