NCTrash.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 {
  10. @IBOutlet fileprivate weak var collectionView: UICollectionView!
  11. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  12. var path = ""
  13. var itemHeight: CGFloat = 60
  14. var datasource = [tableTrash]()
  15. var gridLayout: ListLayout!
  16. override func viewDidLoad() {
  17. super.viewDidLoad()
  18. collectionView.register(UINib.init(nibName: "NCTrashListCell", bundle: nil), forCellWithReuseIdentifier: "cell")
  19. collectionView.collectionViewLayout = ListLayout(itemHeight: 60)
  20. }
  21. override func viewWillAppear(_ animated: Bool) {
  22. super.viewWillAppear(animated)
  23. if path == "" {
  24. let userID = (appDelegate.activeUserID as NSString).addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlFragmentAllowed)
  25. path = k_dav + "/trashbin/" + userID! + "/trash/"
  26. }
  27. let ocNetworking = OCnetworking.init(delegate: self, metadataNet: nil, withUser: appDelegate.activeUser, withUserID: appDelegate.activeUserID, withPassword: appDelegate.activePassword, withUrl: appDelegate.activeUrl)
  28. ocNetworking?.listingTrash(appDelegate.activeUrl, path:path, account: appDelegate.activeAccount, success: { (item) in
  29. NCManageDatabase.sharedInstance.deleteTrash(filePath: self.path)
  30. self.datasource = NCManageDatabase.sharedInstance.addTrashs(item as! [tableTrash])!
  31. self.collectionView.reloadData()
  32. }, failure: { (message, errorCode) in
  33. print("error " + message!)
  34. })
  35. }
  36. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  37. super.viewWillTransition(to: size, with: coordinator)
  38. coordinator.animate(alongsideTransition: nil) { _ in
  39. self.collectionView.collectionViewLayout.invalidateLayout()
  40. }
  41. }
  42. // MARK: tap cell
  43. func tapRestoreDelegate(with fileID: String) {
  44. }
  45. func tapMoreDelegate(with fileID: String) {
  46. }
  47. // MARK: collectionView methods
  48. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  49. return datasource.count
  50. }
  51. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  52. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! NCTrashListCell
  53. cell.delegate = self
  54. let tableTrash = datasource[indexPath.item]
  55. if tableTrash.directory {
  56. 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))
  57. } else {
  58. var image: UIImage?
  59. if tableTrash.iconName.count > 0 {
  60. image = UIImage.init(named: tableTrash.iconName)
  61. } else {
  62. image = UIImage.init(named: "file")
  63. }
  64. cell.configure(with: tableTrash.fileID, image: image, title: tableTrash.trashbinFileName, info: CCUtility.dateDiff(tableTrash.date as Date) + " " + CCUtility.transformedSize(tableTrash.size))
  65. }
  66. return cell
  67. }
  68. }
  69. class ListLayout: UICollectionViewFlowLayout {
  70. var itemHeight: CGFloat = 60
  71. init(itemHeight: CGFloat) {
  72. super.init()
  73. minimumLineSpacing = 1
  74. minimumInteritemSpacing = 1
  75. self.itemHeight = itemHeight
  76. }
  77. required init?(coder aDecoder: NSCoder) {
  78. fatalError("init(coder:) has not been implemented")
  79. }
  80. override var itemSize: CGSize {
  81. get {
  82. if let collectionView = collectionView {
  83. let itemWidth: CGFloat = collectionView.frame.width
  84. return CGSize(width: itemWidth, height: self.itemHeight)
  85. }
  86. // Default fallback
  87. return CGSize(width: 100, height: 100)
  88. }
  89. set {
  90. super.itemSize = newValue
  91. }
  92. }
  93. override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
  94. return proposedContentOffset
  95. }
  96. }