NCTrash.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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, NCTrashHeaderMenuDelegate {
  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 results = NCManageDatabase.sharedInstance.getTrash(filePath: path, sorted: "fileName", ascending: true)
  33. if (results != nil) {
  34. datasource = results!
  35. collectionView.reloadData()
  36. }
  37. let ocNetworking = OCnetworking.init(delegate: self, metadataNet: nil, withUser: appDelegate.activeUser, withUserID: appDelegate.activeUserID, withPassword: appDelegate.activePassword, withUrl: appDelegate.activeUrl)
  38. ocNetworking?.listingTrash(appDelegate.activeUrl, path:path, account: appDelegate.activeAccount, success: { (item) in
  39. NCManageDatabase.sharedInstance.deleteTrash(filePath: self.path)
  40. NCManageDatabase.sharedInstance.addTrashs(item as! [tableTrash])
  41. let results = NCManageDatabase.sharedInstance.getTrash(filePath: self.path, sorted: "fileName", ascending: true)
  42. if (results != nil) {
  43. self.datasource = results!
  44. self.collectionView.reloadData()
  45. }
  46. }, failure: { (message, errorCode) in
  47. print("error " + message!)
  48. })
  49. }
  50. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  51. super.viewWillTransition(to: size, with: coordinator)
  52. coordinator.animate(alongsideTransition: nil) { _ in
  53. self.collectionView.collectionViewLayout.invalidateLayout()
  54. }
  55. }
  56. // MARK: tap
  57. func tapRestoreItem(with fileID: String) {
  58. print("tap item restore")
  59. }
  60. func tapMoreItem(with fileID: String) {
  61. print("tap item more")
  62. }
  63. func tapSwitchHeaderMenu() {
  64. if collectionView.collectionViewLayout == gridLayout {
  65. // list layout
  66. UIView.animate(withDuration: 0.0, animations: {
  67. self.collectionView.collectionViewLayout.invalidateLayout()
  68. self.collectionView.setCollectionViewLayout(self.listLayout, animated: false, completion: { (_) in
  69. self.collectionView.reloadData()
  70. })
  71. })
  72. } else {
  73. // grid layout
  74. UIView.animate(withDuration: 0.0, animations: {
  75. self.collectionView.collectionViewLayout.invalidateLayout()
  76. self.collectionView.setCollectionViewLayout(self.gridLayout, animated: false, completion: { (_) in
  77. self.collectionView.reloadData()
  78. })
  79. })
  80. }
  81. }
  82. func tapMoreHeaderMenu() {
  83. print("tap header more")
  84. }
  85. // MARK: collectionView methods
  86. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  87. let trashHeader = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "headerMenu", for: indexPath) as! NCTrashHeaderMenu
  88. if collectionView.collectionViewLayout == gridLayout {
  89. trashHeader.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchList"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
  90. } else {
  91. trashHeader.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchGrid"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
  92. }
  93. trashHeader.delegate = self
  94. return trashHeader
  95. }
  96. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  97. return CGSize(width: collectionView.frame.width, height: 50)
  98. }
  99. func numberOfSections(in collectionView: UICollectionView) -> Int {
  100. return 1
  101. }
  102. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  103. return datasource.count
  104. }
  105. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  106. let tableTrash = datasource[indexPath.item]
  107. var image: UIImage?
  108. if tableTrash.iconName.count > 0 {
  109. image = UIImage.init(named: tableTrash.iconName)
  110. } else {
  111. image = UIImage.init(named: "file")
  112. }
  113. if collectionView.collectionViewLayout == listLayout {
  114. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell-list", for: indexPath) as! NCTrashListCell
  115. cell.delegate = self
  116. cell.fileID = tableTrash.fileID
  117. cell.labelTitle.text = tableTrash.trashbinFileName
  118. if tableTrash.directory {
  119. cell.imageItem.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
  120. cell.labelInfo.text = CCUtility.dateDiff(tableTrash.date as Date)
  121. } else {
  122. cell.imageItem.image = image
  123. cell.labelInfo.text = CCUtility.dateDiff(tableTrash.date as Date) + " " + CCUtility.transformedSize(tableTrash.size)
  124. }
  125. return cell
  126. } else {
  127. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell-grid", for: indexPath) as! NCTrashGridCell
  128. cell.delegate = self
  129. cell.fileID = tableTrash.fileID
  130. cell.labelTitle.text = tableTrash.trashbinFileName
  131. if tableTrash.directory {
  132. cell.imageItem.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
  133. } else {
  134. cell.imageItem.image = image
  135. }
  136. return cell
  137. }
  138. }
  139. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  140. let tableTrash = datasource[indexPath.item]
  141. if tableTrash.directory {
  142. let ncTrash:NCTrash = UIStoryboard(name: "NCTrash", bundle: nil).instantiateInitialViewController() as! NCTrash
  143. ncTrash.path = tableTrash.filePath + tableTrash.fileName
  144. ncTrash.titleCurrentFolder = tableTrash.trashbinFileName
  145. self.navigationController?.pushViewController(ncTrash, animated: true)
  146. }
  147. }
  148. }
  149. class ListLayout: UICollectionViewFlowLayout {
  150. var itemHeight: CGFloat = 60
  151. init(itemHeight: CGFloat) {
  152. super.init()
  153. self.itemHeight = itemHeight
  154. self.scrollDirection = .vertical
  155. self.sectionInset = UIEdgeInsets(top: 10, left: 0, bottom: 10, right: 0)
  156. }
  157. required init?(coder aDecoder: NSCoder) {
  158. fatalError("init(coder:) has not been implemented")
  159. }
  160. override var itemSize: CGSize {
  161. get {
  162. if let collectionView = collectionView {
  163. let itemWidth: CGFloat = collectionView.frame.width
  164. return CGSize(width: itemWidth, height: self.itemHeight)
  165. }
  166. // Default fallback
  167. return CGSize(width: 100, height: 100)
  168. }
  169. set {
  170. super.itemSize = newValue
  171. }
  172. }
  173. override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
  174. return proposedContentOffset
  175. }
  176. }
  177. class GridLayout: UICollectionViewFlowLayout {
  178. var numberOfColumns: Int = 5
  179. init(numberOfColumns: Int) {
  180. super.init()
  181. minimumInteritemSpacing = 10
  182. self.numberOfColumns = numberOfColumns
  183. self.scrollDirection = .vertical
  184. self.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
  185. }
  186. required init?(coder aDecoder: NSCoder) {
  187. fatalError("init(coder:) has not been implemented")
  188. }
  189. override var itemSize: CGSize {
  190. get {
  191. if let collectionView = collectionView {
  192. let itemWidth: CGFloat = (collectionView.frame.width/CGFloat(self.numberOfColumns)) - self.minimumInteritemSpacing
  193. let itemHeight: CGFloat = itemWidth + 34
  194. return CGSize(width: itemWidth, height: itemHeight)
  195. }
  196. // Default fallback
  197. return CGSize(width: 100, height: 100)
  198. }
  199. set {
  200. super.itemSize = newValue
  201. }
  202. }
  203. override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
  204. return proposedContentOffset
  205. }
  206. }