NCTrash.swift 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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, 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")
  21. listLayout = ListLayout(itemHeight: 60)
  22. collectionView.collectionViewLayout = listLayout
  23. }
  24. override func viewWillAppear(_ animated: Bool) {
  25. super.viewWillAppear(animated)
  26. self.navigationItem.title = titleCurrentFolder
  27. if path == "" {
  28. let userID = (appDelegate.activeUserID as NSString).addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlFragmentAllowed)
  29. path = k_dav + "/trashbin/" + userID! + "/trash/"
  30. }
  31. let ocNetworking = OCnetworking.init(delegate: self, metadataNet: nil, withUser: appDelegate.activeUser, withUserID: appDelegate.activeUserID, withPassword: appDelegate.activePassword, withUrl: appDelegate.activeUrl)
  32. ocNetworking?.listingTrash(appDelegate.activeUrl, path:path, account: appDelegate.activeAccount, success: { (item) in
  33. NCManageDatabase.sharedInstance.deleteTrash(filePath: self.path)
  34. self.datasource = NCManageDatabase.sharedInstance.addTrashs(item as! [tableTrash])!
  35. self.collectionView.reloadData()
  36. }, failure: { (message, errorCode) in
  37. print("error " + message!)
  38. })
  39. }
  40. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  41. super.viewWillTransition(to: size, with: coordinator)
  42. coordinator.animate(alongsideTransition: nil) { _ in
  43. self.collectionView.collectionViewLayout.invalidateLayout()
  44. }
  45. }
  46. // MARK: tap
  47. func tapRestoreItem(with fileID: String) {
  48. print("tap item restore")
  49. }
  50. func tapMoreItem(with fileID: String) {
  51. print("tap item more")
  52. }
  53. func tapSwitchHeader() {
  54. print("tap header switch")
  55. }
  56. func tapMoreHeader() {
  57. print("tap header more")
  58. }
  59. // MARK: collectionView methods
  60. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  61. let trashHeader = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "header", for: indexPath) as! NCTrashHeader
  62. trashHeader.delegate = self
  63. return trashHeader
  64. }
  65. func numberOfSections(in collectionView: UICollectionView) -> Int {
  66. return 1
  67. }
  68. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  69. return datasource.count
  70. }
  71. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  72. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! NCTrashListCell
  73. cell.delegate = self
  74. let tableTrash = datasource[indexPath.item]
  75. if tableTrash.directory {
  76. cell.configure(with: tableTrash.fileID ,image: CCGraphics.changeThemingColorImage(UIImage.init(named: "folder"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement), title: tableTrash.trashbinFileName, info: CCUtility.dateDiff(tableTrash.date as Date))
  77. } else {
  78. var image: UIImage?
  79. if tableTrash.iconName.count > 0 {
  80. image = UIImage.init(named: tableTrash.iconName)
  81. } else {
  82. image = UIImage.init(named: "file")
  83. }
  84. cell.configure(with: tableTrash.fileID, image: image, title: tableTrash.trashbinFileName, info: CCUtility.dateDiff(tableTrash.date as Date) + " " + CCUtility.transformedSize(tableTrash.size))
  85. }
  86. return cell
  87. }
  88. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  89. let tableTrash = datasource[indexPath.item]
  90. if tableTrash.directory {
  91. let ncTrash:NCTrash = UIStoryboard(name: "NCTrash", bundle: nil).instantiateInitialViewController() as! NCTrash
  92. ncTrash.path = tableTrash.filePath + tableTrash.fileName
  93. ncTrash.titleCurrentFolder = tableTrash.trashbinFileName
  94. self.navigationController?.pushViewController(ncTrash, animated: true)
  95. }
  96. }
  97. }
  98. class ListLayout: UICollectionViewFlowLayout {
  99. var itemHeight: CGFloat = 60
  100. var headerHeight: CGFloat = 30
  101. init(itemHeight: CGFloat) {
  102. super.init()
  103. minimumLineSpacing = 1
  104. minimumInteritemSpacing = 1
  105. self.itemHeight = itemHeight
  106. self.scrollDirection = .vertical
  107. self.headerReferenceSize = CGSize(width: 0, height: headerHeight)
  108. }
  109. required init?(coder aDecoder: NSCoder) {
  110. fatalError("init(coder:) has not been implemented")
  111. }
  112. override var itemSize: CGSize {
  113. get {
  114. if let collectionView = collectionView {
  115. let itemWidth: CGFloat = collectionView.frame.width
  116. return CGSize(width: itemWidth, height: self.itemHeight)
  117. }
  118. // Default fallback
  119. return CGSize(width: 100, height: 100)
  120. }
  121. set {
  122. super.itemSize = newValue
  123. }
  124. }
  125. }
  126. class GridLayout: UICollectionViewFlowLayout {
  127. var numberOfColumns: Int = 3
  128. var headerHeight: CGFloat = 30
  129. init(numberOfColumns: Int) {
  130. super.init()
  131. minimumLineSpacing = 1
  132. minimumInteritemSpacing = 1
  133. self.numberOfColumns = numberOfColumns
  134. self.scrollDirection = .vertical
  135. self.headerReferenceSize = CGSize(width: 0, height: headerHeight)
  136. }
  137. required init?(coder aDecoder: NSCoder) {
  138. fatalError("init(coder:) has not been implemented")
  139. }
  140. override var itemSize: CGSize {
  141. get {
  142. if let collectionView = collectionView {
  143. let itemWidth: CGFloat = (collectionView.frame.width/CGFloat(self.numberOfColumns)) - self.minimumInteritemSpacing
  144. let itemHeight: CGFloat = 100.0
  145. return CGSize(width: itemWidth, height: itemHeight)
  146. }
  147. // Default fallback
  148. return CGSize(width: 100, height: 100)
  149. }
  150. set {
  151. super.itemSize = newValue
  152. }
  153. }
  154. }