NCTrash.swift 5.9 KB

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