NCTrash.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  49. let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "header", for: indexPath)
  50. return headerView
  51. }
  52. func numberOfSections(in collectionView: UICollectionView) -> Int {
  53. return 1
  54. }
  55. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  56. return datasource.count
  57. }
  58. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  59. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! NCTrashListCell
  60. cell.delegate = self
  61. let tableTrash = datasource[indexPath.item]
  62. if tableTrash.directory {
  63. 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))
  64. } else {
  65. var image: UIImage?
  66. if tableTrash.iconName.count > 0 {
  67. image = UIImage.init(named: tableTrash.iconName)
  68. } else {
  69. image = UIImage.init(named: "file")
  70. }
  71. cell.configure(with: tableTrash.fileID, image: image, title: tableTrash.trashbinFileName, info: CCUtility.dateDiff(tableTrash.date as Date) + " " + CCUtility.transformedSize(tableTrash.size))
  72. }
  73. return cell
  74. }
  75. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  76. let tableTrash = datasource[indexPath.item]
  77. if tableTrash.directory {
  78. let ncTrash:NCTrash = UIStoryboard(name: "NCTrash", bundle: nil).instantiateInitialViewController() as! NCTrash
  79. ncTrash.path = tableTrash.filePath + tableTrash.fileName
  80. self.navigationController?.pushViewController(ncTrash, animated: true)
  81. }
  82. }
  83. }
  84. class ListLayout: UICollectionViewFlowLayout {
  85. var itemHeight: CGFloat = 60
  86. init(itemHeight: CGFloat) {
  87. super.init()
  88. minimumLineSpacing = 1
  89. minimumInteritemSpacing = 1
  90. self.itemHeight = itemHeight
  91. self.scrollDirection = .vertical
  92. self.headerReferenceSize = CGSize(width: 0, height: 30)
  93. }
  94. required init?(coder aDecoder: NSCoder) {
  95. fatalError("init(coder:) has not been implemented")
  96. }
  97. override var itemSize: CGSize {
  98. get {
  99. if let collectionView = collectionView {
  100. let itemWidth: CGFloat = collectionView.frame.width
  101. return CGSize(width: itemWidth, height: self.itemHeight)
  102. }
  103. // Default fallback
  104. return CGSize(width: 100, height: 100)
  105. }
  106. set {
  107. super.itemSize = newValue
  108. }
  109. }
  110. override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
  111. return proposedContentOffset
  112. }
  113. }