NCTrash.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 {
  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. let tableTrash = datasource[indexPath.item]
  43. cell.configure(with: nil, title: tableTrash.trashbinFileName, info: "")
  44. return cell
  45. }
  46. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  47. super.viewWillTransition(to: size, with: coordinator)
  48. collectionView.collectionViewLayout.invalidateLayout()
  49. }
  50. }
  51. class ListLayout: UICollectionViewFlowLayout {
  52. var itemHeight: CGFloat = 60
  53. init(itemHeight: CGFloat) {
  54. super.init()
  55. minimumLineSpacing = 1
  56. minimumInteritemSpacing = 1
  57. self.itemHeight = itemHeight
  58. }
  59. required init?(coder aDecoder: NSCoder) {
  60. fatalError("init(coder:) has not been implemented")
  61. }
  62. override var itemSize: CGSize {
  63. get {
  64. if let collectionView = collectionView {
  65. let itemWidth: CGFloat = collectionView.frame.width
  66. return CGSize(width: itemWidth, height: self.itemHeight)
  67. }
  68. // Default fallback
  69. return CGSize(width: 100, height: 100)
  70. }
  71. set {
  72. super.itemSize = newValue
  73. }
  74. }
  75. override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
  76. return proposedContentOffset
  77. }
  78. }