NCTrash.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. // MARK: collectionView methods
  37. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  38. return datasource.count
  39. }
  40. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  41. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! NCTrashListCell
  42. cell.delegate = self
  43. let tableTrash = datasource[indexPath.item]
  44. if tableTrash.directory {
  45. 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))
  46. } else {
  47. var image: UIImage?
  48. if tableTrash.iconName.count > 0 {
  49. image = UIImage.init(named: tableTrash.iconName)
  50. } else {
  51. image = UIImage.init(named: "file")
  52. }
  53. cell.configure(with: tableTrash.fileID, image: image, title: tableTrash.trashbinFileName, info: CCUtility.dateDiff(tableTrash.date as Date) + " " + CCUtility.transformedSize(tableTrash.size))
  54. }
  55. return cell
  56. }
  57. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  58. super.viewWillTransition(to: size, with: coordinator)
  59. collectionView.collectionViewLayout.invalidateLayout()
  60. }
  61. // MARK: tap cell
  62. func tapRestoreDelegate(with fileID: String) {
  63. }
  64. func tapMoreDelegate(with fileID: String) {
  65. }
  66. }
  67. class ListLayout: UICollectionViewFlowLayout {
  68. var itemHeight: CGFloat = 60
  69. init(itemHeight: CGFloat) {
  70. super.init()
  71. minimumLineSpacing = 1
  72. minimumInteritemSpacing = 1
  73. self.itemHeight = itemHeight
  74. }
  75. required init?(coder aDecoder: NSCoder) {
  76. fatalError("init(coder:) has not been implemented")
  77. }
  78. override var itemSize: CGSize {
  79. get {
  80. if let collectionView = collectionView {
  81. let itemWidth: CGFloat = collectionView.frame.width
  82. return CGSize(width: itemWidth, height: self.itemHeight)
  83. }
  84. // Default fallback
  85. return CGSize(width: 100, height: 100)
  86. }
  87. set {
  88. super.itemSize = newValue
  89. }
  90. }
  91. override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
  92. return proposedContentOffset
  93. }
  94. }