NCTrash.swift 10 KB

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