NCTrash.swift 13 KB

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