NCTrash.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. if tableTrash.directory {
  44. cell.configure(with: CCGraphics.changeThemingColorImage(UIImage.init(named: "folder"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement), title: tableTrash.trashbinFileName, info: CCUtility.dateDiff(tableTrash.date as Date))
  45. } else {
  46. var image: UIImage?
  47. if tableTrash.iconName.count > 0 {
  48. image = UIImage.init(named: tableTrash.iconName)
  49. } else {
  50. image = UIImage.init(named: "file")
  51. }
  52. cell.configure(with: image, title: tableTrash.trashbinFileName, info: CCUtility.dateDiff(tableTrash.date as Date) + " " + CCUtility.transformedSize(tableTrash.size))
  53. }
  54. return cell
  55. }
  56. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  57. super.viewWillTransition(to: size, with: coordinator)
  58. collectionView.collectionViewLayout.invalidateLayout()
  59. }
  60. }
  61. class ListLayout: UICollectionViewFlowLayout {
  62. var itemHeight: CGFloat = 60
  63. init(itemHeight: CGFloat) {
  64. super.init()
  65. minimumLineSpacing = 1
  66. minimumInteritemSpacing = 1
  67. self.itemHeight = itemHeight
  68. }
  69. required init?(coder aDecoder: NSCoder) {
  70. fatalError("init(coder:) has not been implemented")
  71. }
  72. override var itemSize: CGSize {
  73. get {
  74. if let collectionView = collectionView {
  75. let itemWidth: CGFloat = collectionView.frame.width
  76. return CGSize(width: itemWidth, height: self.itemHeight)
  77. }
  78. // Default fallback
  79. return CGSize(width: 100, height: 100)
  80. }
  81. set {
  82. super.itemSize = newValue
  83. }
  84. }
  85. override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
  86. return proposedContentOffset
  87. }
  88. }