NCTrash.swift 5.8 KB

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