NCTrash.swift 9.2 KB

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