NCTrash.swift 7.3 KB

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