NCTrash.swift 5.9 KB

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