NCTrash.swift 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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, UICollectionViewDelegateFlowLayout, 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 datasource = [tableTrash]()
  15. var listLayout: ListLayout!
  16. var gridLayout: GridLayout!
  17. override func viewDidLoad() {
  18. super.viewDidLoad()
  19. collectionView.register(UINib.init(nibName: "NCTrashListCell", bundle: nil), forCellWithReuseIdentifier: "cell-list")
  20. collectionView.register(UINib.init(nibName: "NCTrashGridCell", bundle: nil), forCellWithReuseIdentifier: "cell-grid")
  21. listLayout = ListLayout(itemHeight: 60)
  22. gridLayout = GridLayout(numberOfColumns: 5)
  23. collectionView.collectionViewLayout = gridLayout
  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. if collectionView.collectionViewLayout == gridLayout {
  56. // list layout
  57. UIView.animate(withDuration: 0.0, animations: {
  58. self.collectionView.collectionViewLayout.invalidateLayout()
  59. self.collectionView.setCollectionViewLayout(self.listLayout, animated: false, completion: { (_) in
  60. self.collectionView.reloadData()
  61. })
  62. })
  63. } else {
  64. // grid layout
  65. UIView.animate(withDuration: 0.0, animations: {
  66. self.collectionView.collectionViewLayout.invalidateLayout()
  67. self.collectionView.setCollectionViewLayout(self.gridLayout, animated: false, completion: { (_) in
  68. self.collectionView.reloadData()
  69. })
  70. })
  71. }
  72. }
  73. func tapMoreHeader() {
  74. print("tap header more")
  75. }
  76. // MARK: collectionView methods
  77. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  78. let trashHeader = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "header", for: indexPath) as! NCTrashHeader
  79. trashHeader.delegate = self
  80. return trashHeader
  81. }
  82. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  83. return CGSize(width: collectionView.frame.width, height: 30)
  84. }
  85. func numberOfSections(in collectionView: UICollectionView) -> Int {
  86. return 1
  87. }
  88. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  89. return datasource.count
  90. }
  91. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  92. let tableTrash = datasource[indexPath.item]
  93. var image: UIImage?
  94. if tableTrash.iconName.count > 0 {
  95. image = UIImage.init(named: tableTrash.iconName)
  96. } else {
  97. image = UIImage.init(named: "file")
  98. }
  99. if collectionView.collectionViewLayout == listLayout {
  100. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell-list", for: indexPath) as! NCTrashListCell
  101. cell.delegate = self
  102. cell.fileID = tableTrash.fileID
  103. cell.labelTitle.text = tableTrash.trashbinFileName
  104. if tableTrash.directory {
  105. cell.imageItem.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
  106. cell.labelInfo.text = CCUtility.dateDiff(tableTrash.date as Date)
  107. } else {
  108. cell.imageItem.image = image
  109. cell.labelInfo.text = CCUtility.dateDiff(tableTrash.date as Date) + " " + CCUtility.transformedSize(tableTrash.size)
  110. }
  111. return cell
  112. } else {
  113. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell-grid", for: indexPath) as! NCTrashGridCell
  114. cell.delegate = self
  115. cell.fileID = tableTrash.fileID
  116. cell.labelTitle.text = tableTrash.trashbinFileName
  117. if tableTrash.directory {
  118. cell.imageItem.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
  119. } else {
  120. cell.imageItem.image = image
  121. }
  122. return cell
  123. }
  124. }
  125. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  126. let tableTrash = datasource[indexPath.item]
  127. if tableTrash.directory {
  128. let ncTrash:NCTrash = UIStoryboard(name: "NCTrash", bundle: nil).instantiateInitialViewController() as! NCTrash
  129. ncTrash.path = tableTrash.filePath + tableTrash.fileName
  130. ncTrash.titleCurrentFolder = tableTrash.trashbinFileName
  131. self.navigationController?.pushViewController(ncTrash, animated: true)
  132. }
  133. }
  134. }
  135. class ListLayout: UICollectionViewFlowLayout {
  136. var itemHeight: CGFloat = 60
  137. init(itemHeight: CGFloat) {
  138. super.init()
  139. self.itemHeight = itemHeight
  140. self.scrollDirection = .vertical
  141. self.sectionInset = UIEdgeInsets(top: 10, left: 0, bottom: 10, right: 0)
  142. }
  143. required init?(coder aDecoder: NSCoder) {
  144. fatalError("init(coder:) has not been implemented")
  145. }
  146. override var itemSize: CGSize {
  147. get {
  148. if let collectionView = collectionView {
  149. let itemWidth: CGFloat = collectionView.frame.width
  150. return CGSize(width: itemWidth, height: self.itemHeight)
  151. }
  152. // Default fallback
  153. return CGSize(width: 100, height: 100)
  154. }
  155. set {
  156. super.itemSize = newValue
  157. }
  158. }
  159. override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
  160. return proposedContentOffset
  161. }
  162. }
  163. class GridLayout: UICollectionViewFlowLayout {
  164. var numberOfColumns: Int = 5
  165. init(numberOfColumns: Int) {
  166. super.init()
  167. minimumInteritemSpacing = 10
  168. self.numberOfColumns = numberOfColumns
  169. self.scrollDirection = .vertical
  170. self.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
  171. }
  172. required init?(coder aDecoder: NSCoder) {
  173. fatalError("init(coder:) has not been implemented")
  174. }
  175. override var itemSize: CGSize {
  176. get {
  177. if let collectionView = collectionView {
  178. let itemWidth: CGFloat = (collectionView.frame.width/CGFloat(self.numberOfColumns)) - self.minimumInteritemSpacing
  179. let itemHeight: CGFloat = itemWidth + 40
  180. return CGSize(width: itemWidth, height: itemHeight)
  181. }
  182. // Default fallback
  183. return CGSize(width: 100, height: 100)
  184. }
  185. set {
  186. super.itemSize = newValue
  187. }
  188. }
  189. override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
  190. return proposedContentOffset
  191. }
  192. }