NCTrash.swift 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  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. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import Foundation
  24. import Sheeeeeeeeet
  25. class NCTrash: UIViewController, UIGestureRecognizerDelegate, NCTrashListCellDelegate, NCGridCellDelegate, NCTrashSectionHeaderMenuDelegate, DropdownMenuDelegate, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate {
  26. @IBOutlet fileprivate weak var collectionView: UICollectionView!
  27. var path = ""
  28. var titleCurrentFolder = NSLocalizedString("_trash_view_", comment: "")
  29. var scrollToFileID = ""
  30. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  31. private var isEditMode = false
  32. private var selectFileID = [String]()
  33. private var datasource = [tableTrash]()
  34. private var typeLayout = ""
  35. private var datasourceSorted = ""
  36. private var datasourceAscending = true
  37. private var datasourceGroupBy = "none"
  38. private var datasourceDirectoryOnTop = false
  39. private var listLayout: NCListLayout!
  40. private var gridLayout: NCGridLayout!
  41. private var actionSheet: ActionSheet?
  42. private let highHeader: CGFloat = 50
  43. private let refreshControl = UIRefreshControl()
  44. override func viewDidLoad() {
  45. super.viewDidLoad()
  46. // Cell
  47. collectionView.register(UINib.init(nibName: "NCTrashListCell", bundle: nil), forCellWithReuseIdentifier: "listCell")
  48. collectionView.register(UINib.init(nibName: "NCGridCell", bundle: nil), forCellWithReuseIdentifier: "gridCell")
  49. // Header - Footer
  50. collectionView.register(UINib.init(nibName: "NCTrashSectionHeaderMenu", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "sectionHeaderMenu")
  51. collectionView.register(UINib.init(nibName: "NCTrashSectionFooter", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "sectionFooter")
  52. collectionView.alwaysBounceVertical = true
  53. listLayout = NCListLayout()
  54. gridLayout = NCGridLayout()
  55. // Add Refresh Control
  56. collectionView.refreshControl = refreshControl
  57. // Configure Refresh Control
  58. refreshControl.tintColor = NCBrandColor.sharedInstance.brandText
  59. refreshControl.backgroundColor = NCBrandColor.sharedInstance.brand
  60. refreshControl.addTarget(self, action: #selector(loadListingTrash), for: .valueChanged)
  61. // empty Data Source
  62. self.collectionView.emptyDataSetDelegate = self;
  63. self.collectionView.emptyDataSetSource = self;
  64. }
  65. override func viewWillAppear(_ animated: Bool) {
  66. super.viewWillAppear(animated)
  67. // Color
  68. appDelegate.aspectNavigationControllerBar(self.navigationController?.navigationBar, online: appDelegate.reachability.isReachable(), hidden: false)
  69. appDelegate.aspectTabBar(self.tabBarController?.tabBar, hidden: false)
  70. self.navigationItem.title = titleCurrentFolder
  71. (typeLayout, datasourceSorted, datasourceAscending, datasourceGroupBy, datasourceDirectoryOnTop) = NCUtility.sharedInstance.getLayoutForView(key: k_layout_view_trash)
  72. if typeLayout == k_layout_list {
  73. collectionView.collectionViewLayout = listLayout
  74. } else {
  75. collectionView.collectionViewLayout = gridLayout
  76. }
  77. // Datasource & serverUrl
  78. if path == "" {
  79. let userID = (appDelegate.activeUserID as NSString).addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlFragmentAllowed)
  80. path = k_dav + "/trashbin/" + userID! + "/trash/"
  81. }
  82. if (datasource.count == 0) {
  83. loadDatasource()
  84. }
  85. loadListingTrash()
  86. }
  87. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  88. super.viewWillTransition(to: size, with: coordinator)
  89. coordinator.animate(alongsideTransition: nil) { _ in
  90. self.collectionView.collectionViewLayout.invalidateLayout()
  91. self.actionSheet?.viewDidLayoutSubviews()
  92. }
  93. }
  94. // MARK: DZNEmpty
  95. func backgroundColor(forEmptyDataSet scrollView: UIScrollView) -> UIColor? {
  96. return NCBrandColor.sharedInstance.backgroundView
  97. }
  98. func image(forEmptyDataSet scrollView: UIScrollView) -> UIImage? {
  99. return CCGraphics.changeThemingColorImage(UIImage.init(named: "trash"), width: 300, height: 300, color: NCBrandColor.sharedInstance.graySoft)
  100. }
  101. func title(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
  102. let text = "\n"+NSLocalizedString("_trash_no_trash_", comment: "")
  103. let attributes = [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 20), NSAttributedString.Key.foregroundColor: UIColor.lightGray]
  104. return NSAttributedString.init(string: text, attributes: attributes)
  105. }
  106. func description(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
  107. let text = "\n"+NSLocalizedString("_trash_no_trash_description_", comment: "")
  108. let attributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14), NSAttributedString.Key.foregroundColor: UIColor.lightGray]
  109. return NSAttributedString.init(string: text, attributes: attributes)
  110. }
  111. func emptyDataSetShouldAllowScroll(_ scrollView: UIScrollView) -> Bool {
  112. return true
  113. }
  114. // MARK: TAP EVENT
  115. func tapSwitchHeaderMenu(sender: Any) {
  116. if collectionView.collectionViewLayout == gridLayout {
  117. // list layout
  118. UIView.animate(withDuration: 0.0, animations: {
  119. self.collectionView.collectionViewLayout.invalidateLayout()
  120. self.collectionView.setCollectionViewLayout(self.listLayout, animated: false, completion: { (_) in
  121. self.collectionView.reloadData()
  122. self.collectionView.setContentOffset(CGPoint(x:0,y:0), animated: false)
  123. })
  124. })
  125. typeLayout = k_layout_list
  126. NCUtility.sharedInstance.setLayoutForView(key: k_layout_view_trash, layout: typeLayout, sort: datasourceSorted, ascending: datasourceAscending, groupBy: datasourceGroupBy, directoryOnTop: datasourceDirectoryOnTop)
  127. } else {
  128. // grid layout
  129. UIView.animate(withDuration: 0.0, animations: {
  130. self.collectionView.collectionViewLayout.invalidateLayout()
  131. self.collectionView.setCollectionViewLayout(self.gridLayout, animated: false, completion: { (_) in
  132. self.collectionView.reloadData()
  133. self.collectionView.setContentOffset(CGPoint(x:0,y:0), animated: false)
  134. })
  135. })
  136. typeLayout = k_layout_grid
  137. NCUtility.sharedInstance.setLayoutForView(key: k_layout_view_trash, layout: typeLayout, sort: datasourceSorted, ascending: datasourceAscending, groupBy: datasourceGroupBy, directoryOnTop: datasourceDirectoryOnTop)
  138. }
  139. }
  140. func tapOrderHeaderMenu(sender: Any) {
  141. var menuView: DropdownMenu?
  142. var selectedRow = 0
  143. let item1 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortFileNameAZ"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_name_a_z_", comment: ""))
  144. let item2 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortFileNameZA"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_name_z_a_", comment: ""))
  145. let item3 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortDateMoreRecent"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_date_more_recent_", comment: ""))
  146. let item4 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortDateLessRecent"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_date_less_recent_", comment: ""))
  147. let item5 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortSmallest"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_size_smallest_", comment: ""))
  148. let item6 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortLargest"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_size_largest_", comment: ""))
  149. switch datasourceSorted {
  150. case "fileName":
  151. if datasourceAscending == true { item1.style = .highlight; selectedRow = 0 }
  152. if datasourceAscending == false { item2.style = .highlight; selectedRow = 1 }
  153. case "date":
  154. if datasourceAscending == false { item3.style = .highlight; selectedRow = 2 }
  155. if datasourceAscending == true { item4.style = .highlight; selectedRow = 3 }
  156. case "size":
  157. if datasourceAscending == true { item5.style = .highlight; selectedRow = 4 }
  158. if datasourceAscending == false { item6.style = .highlight; selectedRow = 5 }
  159. default:
  160. print("")
  161. }
  162. menuView = DropdownMenu(navigationController: self.navigationController!, items: [item1, item2, item3, item4, item5, item6], selectedRow: selectedRow)
  163. menuView?.token = "tapOrderHeaderMenu"
  164. menuView?.delegate = self
  165. menuView?.rowHeight = 45
  166. menuView?.highlightColor = NCBrandColor.sharedInstance.brand
  167. menuView?.tableView.alwaysBounceVertical = false
  168. menuView?.tableViewBackgroundColor = UIColor.white
  169. let header = (sender as? UIButton)?.superview as! NCTrashSectionHeaderMenu
  170. let headerRect = self.collectionView.convert(header.bounds, from: self.view)
  171. let menuOffsetY = headerRect.height - headerRect.origin.y - 2
  172. menuView?.topOffsetY = CGFloat(menuOffsetY)
  173. menuView?.showMenu()
  174. }
  175. func tapMoreHeaderMenu(sender: Any) {
  176. var menuView: DropdownMenu?
  177. if isEditMode {
  178. //let item0 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "checkedNo"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_cancel_", comment: ""))
  179. //let item1 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "restore"), multiplier: 1, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_trash_restore_selected_", comment: ""))
  180. let item2 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "trash"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_trash_delete_selected_", comment: ""))
  181. menuView = DropdownMenu(navigationController: self.navigationController!, items: [item2], selectedRow: -1)
  182. menuView?.token = "tapMoreHeaderMenuSelect"
  183. } else {
  184. //let item0 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "select"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_select_", comment: ""))
  185. //let item1 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "restore"), multiplier: 1, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_trash_restore_all_", comment: ""))
  186. let item2 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "trash"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_trash_delete_all_", comment: ""))
  187. menuView = DropdownMenu(navigationController: self.navigationController!, items: [item2], selectedRow: -1)
  188. menuView?.token = "tapMoreHeaderMenu"
  189. }
  190. menuView?.delegate = self
  191. menuView?.rowHeight = 45
  192. menuView?.highlightColor = NCBrandColor.sharedInstance.brand
  193. menuView?.tableView.alwaysBounceVertical = false
  194. menuView?.tableViewBackgroundColor = UIColor.white
  195. let header = (sender as? UIButton)?.superview as! NCTrashSectionHeaderMenu
  196. let headerRect = self.collectionView.convert(header.bounds, from: self.view)
  197. let menuOffsetY = headerRect.height - headerRect.origin.y - 2
  198. menuView?.topOffsetY = CGFloat(menuOffsetY)
  199. menuView?.showMenu()
  200. }
  201. func tapRestoreListItem(with fileID: String, sender: Any) {
  202. if !isEditMode {
  203. restoreItem(with: fileID)
  204. } else {
  205. let buttonPosition:CGPoint = (sender as! UIButton).convert(CGPoint.zero, to:collectionView)
  206. let indexPath = collectionView.indexPathForItem(at: buttonPosition)
  207. collectionView(self.collectionView, didSelectItemAt: indexPath!)
  208. }
  209. }
  210. func tapMoreListItem(with fileID: String, sender: Any) {
  211. if !isEditMode {
  212. var items = [ActionSheetItem]()
  213. items.append(ActionSheetDangerButton(title: NSLocalizedString("_delete_", comment: "")))
  214. items.append(ActionSheetCancelButton(title: NSLocalizedString("_cancel_", comment: "")))
  215. actionSheet = ActionSheet(items: items) { sheet, item in
  216. if item is ActionSheetDangerButton { self.deleteItem(with: fileID) }
  217. if item is ActionSheetCancelButton { print("Cancel buttons has the value `true`") }
  218. }
  219. guard let tableTrash = NCManageDatabase.sharedInstance.getTrashItem(fileID: fileID, account: appDelegate.activeAccount) else {
  220. return
  221. }
  222. let headerView = NCActionSheetHeader.sharedInstance.actionSheetHeader(isDirectory: tableTrash.directory, iconName: tableTrash.iconName, fileID: tableTrash.fileID, fileNameView: tableTrash.fileName, text: tableTrash.trashbinFileName)
  223. actionSheet?.headerView = headerView
  224. actionSheet?.headerView?.frame.size.height = 50
  225. actionSheet?.present(in: self, from: sender as! UIButton)
  226. } else {
  227. let buttonPosition:CGPoint = (sender as! UIButton).convert(CGPoint.zero, to:collectionView)
  228. let indexPath = collectionView.indexPathForItem(at: buttonPosition)
  229. collectionView(self.collectionView, didSelectItemAt: indexPath!)
  230. }
  231. }
  232. func tapMoreGridItem(with fileID: String, sender: Any) {
  233. if !isEditMode {
  234. var items = [ActionSheetItem]()
  235. let appearanceDelete = ActionSheetItemAppearance.init()
  236. appearanceDelete.textColor = UIColor.red
  237. items.append(ActionSheetItem(title: NSLocalizedString("_restore_", comment: ""), value: 0, image: CCGraphics.changeThemingColorImage(UIImage.init(named: "restore"), multiplier: 1, color: NCBrandColor.sharedInstance.icon)))
  238. let itemDelete = ActionSheetItem(title: NSLocalizedString("_delete_", comment: ""), value: 1, image: CCGraphics.changeThemingColorImage(UIImage.init(named: "trash"), multiplier: 2, color: UIColor.red))
  239. itemDelete.customAppearance = appearanceDelete
  240. items.append(itemDelete)
  241. items.append(ActionSheetCancelButton(title: NSLocalizedString("_cancel_", comment: "")))
  242. actionSheet = ActionSheet(items: items) { sheet, item in
  243. if item.value as? Int == 0 { self.restoreItem(with: fileID) }
  244. if item.value as? Int == 1 { self.deleteItem(with: fileID) }
  245. if item is ActionSheetCancelButton { print("Cancel buttons has the value `true`") }
  246. }
  247. guard let tableTrash = NCManageDatabase.sharedInstance.getTrashItem(fileID: fileID, account: appDelegate.activeAccount) else {
  248. return
  249. }
  250. let headerView = NCActionSheetHeader.sharedInstance.actionSheetHeader(isDirectory: tableTrash.directory, iconName: tableTrash.iconName, fileID: tableTrash.fileID, fileNameView: tableTrash.fileName, text: tableTrash.trashbinFileName)
  251. actionSheet?.headerView = headerView
  252. actionSheet?.headerView?.frame.size.height = 50
  253. actionSheet?.present(in: self, from: sender as! UIButton)
  254. } else {
  255. let buttonPosition:CGPoint = (sender as! UIButton).convert(CGPoint.zero, to:collectionView)
  256. let indexPath = collectionView.indexPathForItem(at: buttonPosition)
  257. collectionView(self.collectionView, didSelectItemAt: indexPath!)
  258. }
  259. }
  260. // MARK: DROP-DOWN-MENU
  261. func dropdownMenu(_ dropdownMenu: DropdownMenu, didSelectRowAt indexPath: IndexPath) {
  262. if dropdownMenu.token == "tapOrderHeaderMenu" {
  263. switch indexPath.row {
  264. case 0: datasourceSorted = "fileName"; datasourceAscending = true
  265. case 1: datasourceSorted = "fileName"; datasourceAscending = false
  266. case 2: datasourceSorted = "date"; datasourceAscending = false
  267. case 3: datasourceSorted = "date"; datasourceAscending = true
  268. case 4: datasourceSorted = "size"; datasourceAscending = true
  269. case 5: datasourceSorted = "size"; datasourceAscending = false
  270. default: print("")
  271. }
  272. NCUtility.sharedInstance.setLayoutForView(key: k_layout_view_trash, layout: typeLayout, sort: datasourceSorted, ascending: datasourceAscending, groupBy: datasourceGroupBy, directoryOnTop: datasourceDirectoryOnTop)
  273. loadDatasource()
  274. }
  275. if dropdownMenu.token == "tapMoreHeaderMenu" {
  276. /*
  277. // Select
  278. if indexPath.row == 0 {
  279. isEditMode = true
  280. collectionView.reloadData()
  281. }
  282. // Restore ALL
  283. if indexPath.row == 1 {
  284. for record: tableTrash in self.datasource {
  285. restoreItem(with: record.fileID)
  286. }
  287. }
  288. */
  289. // Empty Trash
  290. if indexPath.row == 0 {
  291. var items = [ActionSheetItem]()
  292. items.append(ActionSheetTitle(title: NSLocalizedString("_trash_delete_all_", comment: "")))
  293. items.append(ActionSheetDangerButton(title: NSLocalizedString("_ok_", comment: "")))
  294. items.append(ActionSheetCancelButton(title: NSLocalizedString("_cancel_", comment: "")))
  295. actionSheet = ActionSheet(items: items) { sheet, item in
  296. if item is ActionSheetDangerButton {
  297. self.emptyTrash()
  298. //for record: tableTrash in self.datasource {
  299. // self.deleteItem(with: record.fileID)
  300. //}
  301. }
  302. if item is ActionSheetCancelButton { return }
  303. }
  304. actionSheet?.present(in: self, from: self.view)
  305. }
  306. }
  307. if dropdownMenu.token == "tapMoreHeaderMenuSelect" {
  308. // Cancel
  309. if indexPath.row == 0 {
  310. isEditMode = false
  311. selectFileID.removeAll()
  312. collectionView.reloadData()
  313. }
  314. // Restore selected files
  315. if indexPath.row == 1 {
  316. for fileID in selectFileID {
  317. restoreItem(with: fileID)
  318. }
  319. isEditMode = false
  320. selectFileID.removeAll()
  321. collectionView.reloadData()
  322. }
  323. // Delete selected files
  324. if indexPath.row == 2 {
  325. var items = [ActionSheetItem]()
  326. items.append(ActionSheetTitle(title: NSLocalizedString("_trash_delete_selected_", comment: "")))
  327. items.append(ActionSheetDangerButton(title: NSLocalizedString("_delete_", comment: "")))
  328. items.append(ActionSheetCancelButton(title: NSLocalizedString("_cancel_", comment: "")))
  329. actionSheet = ActionSheet(items: items) { sheet, item in
  330. if item is ActionSheetDangerButton {
  331. for fileID in self.selectFileID {
  332. self.deleteItem(with: fileID)
  333. }
  334. self.isEditMode = false
  335. self.selectFileID.removeAll()
  336. self.collectionView.reloadData()
  337. }
  338. if item is ActionSheetCancelButton { return }
  339. }
  340. actionSheet?.present(in: self, from: self.view)
  341. }
  342. }
  343. }
  344. /*
  345. func dropdownMenuWillDismiss(_ dropdownMenu: DropdownMenu) {
  346. if dropdownMenu.token == "tapOrderHeaderMenu" {
  347. let trashHeader = collectionView.supplementaryView(forElementKind: UICollectionView.elementKindSectionHeader, at: IndexPath(row: 0, section: 0)) as! NCTrashHeaderMenu
  348. let title = String(trashHeader.buttonOrder.title(for: .normal)!.dropLast()) + "▽"
  349. trashHeader.buttonOrder.setTitle(title, for: .normal)
  350. }
  351. }
  352. func dropdownMenuWillShow(_ dropdownMenu: DropdownMenu) {
  353. if dropdownMenu.token == "tapOrderHeaderMenu" {
  354. let trashHeader = collectionView.supplementaryView(forElementKind: UICollectionView.elementKindSectionHeader, at: IndexPath(row: 0, section: 0)) as! NCTrashHeaderMenu
  355. let title = String(trashHeader.buttonOrder.title(for: .normal)!.dropLast()) + "△"
  356. trashHeader.buttonOrder.setTitle(title, for: .normal)
  357. }
  358. }
  359. */
  360. }
  361. // MARK: - Collection View
  362. extension NCTrash: UICollectionViewDelegate {
  363. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  364. let tableTrash = datasource[indexPath.item]
  365. if isEditMode {
  366. if let index = selectFileID.firstIndex(of: tableTrash.fileID) {
  367. selectFileID.remove(at: index)
  368. } else {
  369. selectFileID.append(tableTrash.fileID)
  370. }
  371. collectionView.reloadItems(at: [indexPath])
  372. return
  373. }
  374. if tableTrash.directory {
  375. let ncTrash:NCTrash = UIStoryboard(name: "NCTrash", bundle: nil).instantiateInitialViewController() as! NCTrash
  376. ncTrash.path = tableTrash.filePath + tableTrash.fileName
  377. ncTrash.titleCurrentFolder = tableTrash.trashbinFileName
  378. self.navigationController?.pushViewController(ncTrash, animated: true)
  379. }
  380. }
  381. }
  382. extension NCTrash: UICollectionViewDataSource {
  383. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  384. if kind == UICollectionView.elementKindSectionHeader {
  385. let trashHeader = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionHeaderMenu", for: indexPath) as! NCTrashSectionHeaderMenu
  386. if collectionView.collectionViewLayout == gridLayout {
  387. trashHeader.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchList"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
  388. } else {
  389. trashHeader.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchGrid"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
  390. }
  391. trashHeader.delegate = self
  392. trashHeader.setStatusButton(datasource: datasource)
  393. trashHeader.setTitleOrder(datasourceSorted: datasourceSorted, datasourceAscending: datasourceAscending)
  394. return trashHeader
  395. } else {
  396. let trashFooter = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFooter", for: indexPath) as! NCTrashSectionFooter
  397. trashFooter.setTitleLabelFooter(datasource: datasource)
  398. return trashFooter
  399. }
  400. }
  401. func numberOfSections(in collectionView: UICollectionView) -> Int {
  402. return 1
  403. }
  404. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  405. return datasource.count
  406. }
  407. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  408. let tableTrash = datasource[indexPath.item]
  409. var image: UIImage?
  410. if tableTrash.iconName.count > 0 {
  411. image = UIImage.init(named: tableTrash.iconName)
  412. } else {
  413. image = UIImage.init(named: "file")
  414. }
  415. if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconFileID(tableTrash.fileID, fileNameView: tableTrash.fileName)) {
  416. image = UIImage.init(contentsOfFile: CCUtility.getDirectoryProviderStorageIconFileID(tableTrash.fileID, fileNameView: tableTrash.fileName))
  417. } else {
  418. if tableTrash.hasPreview == 1 && !CCUtility.fileProviderStorageIconExists(tableTrash.fileID, fileNameView: tableTrash.fileName) {
  419. downloadThumbnail(with: tableTrash, indexPath: indexPath)
  420. }
  421. }
  422. if collectionView.collectionViewLayout == listLayout {
  423. // LIST
  424. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as! NCTrashListCell
  425. cell.delegate = self
  426. cell.fileID = tableTrash.fileID
  427. cell.indexPath = indexPath
  428. cell.labelTitle.text = tableTrash.trashbinFileName
  429. if tableTrash.directory {
  430. cell.imageItem.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
  431. cell.labelInfo.text = CCUtility.dateDiff(tableTrash.date as Date)
  432. } else {
  433. cell.imageItem.image = image
  434. cell.labelInfo.text = CCUtility.dateDiff(tableTrash.date as Date) + ", " + CCUtility.transformedSize(tableTrash.size)
  435. }
  436. if isEditMode {
  437. cell.imageItemLeftConstraint.constant = 45
  438. cell.imageSelect.isHidden = false
  439. if selectFileID.contains(tableTrash.fileID) {
  440. cell.imageSelect.image = CCGraphics.scale(UIImage.init(named: "checkedYes"), to: CGSize(width: 50, height: 50), isAspectRation: true)
  441. cell.backgroundView = NCUtility.sharedInstance.cellBlurEffect(with: cell.bounds)
  442. } else {
  443. cell.imageSelect.image = CCGraphics.scale(UIImage.init(named: "checkedNo"), to: CGSize(width: 50, height: 50), isAspectRation: true)
  444. cell.backgroundView = nil
  445. }
  446. } else {
  447. cell.imageItemLeftConstraint.constant = 10
  448. cell.imageSelect.isHidden = true
  449. cell.backgroundView = nil
  450. }
  451. return cell
  452. } else {
  453. // GRID
  454. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as! NCGridCell
  455. cell.delegate = self
  456. cell.fileID = tableTrash.fileID
  457. cell.indexPath = indexPath
  458. cell.labelTitle.text = tableTrash.trashbinFileName
  459. if tableTrash.directory {
  460. cell.imageItem.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
  461. } else {
  462. cell.imageItem.image = image
  463. }
  464. if isEditMode {
  465. cell.imageSelect.isHidden = false
  466. if selectFileID.contains(tableTrash.fileID) {
  467. cell.imageSelect.image = CCGraphics.scale(UIImage.init(named: "checkedYes"), to: CGSize(width: 50, height: 50), isAspectRation: true)
  468. cell.backgroundView = NCUtility.sharedInstance.cellBlurEffect(with: cell.bounds)
  469. } else {
  470. cell.imageSelect.isHidden = true
  471. cell.backgroundView = nil
  472. }
  473. } else {
  474. cell.imageSelect.isHidden = true
  475. cell.backgroundView = nil
  476. }
  477. return cell
  478. }
  479. }
  480. }
  481. extension NCTrash: UICollectionViewDelegateFlowLayout {
  482. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  483. return CGSize(width: collectionView.frame.width, height: highHeader)
  484. }
  485. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
  486. return CGSize(width: collectionView.frame.width, height: highHeader)
  487. }
  488. }
  489. // MARK: - NC API & Algorithm
  490. extension NCTrash {
  491. @objc func loadDatasource() {
  492. datasource.removeAll()
  493. guard let tashItems = NCManageDatabase.sharedInstance.getTrash(filePath: path, sorted: datasourceSorted, ascending: datasourceAscending, account: appDelegate.activeAccount) else {
  494. return
  495. }
  496. datasource = tashItems
  497. reloadDataThenPerform {
  498. // GoTo FileID
  499. if self.scrollToFileID != "" {
  500. for item in 0...self.datasource.count-1 {
  501. if self.datasource[item].fileID.contains(self.scrollToFileID) {
  502. let indexPath = IndexPath(item: item, section: 0)
  503. UIView.animate(withDuration: 0.5, animations: {
  504. self.collectionView.scrollToItem(at: indexPath, at: .top, animated: true)
  505. }, completion: { (finished) in
  506. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
  507. NCUtility.sharedInstance.blink(cell: self.collectionView.cellForItem(at: indexPath))
  508. }
  509. })
  510. self.scrollToFileID = ""
  511. }
  512. }
  513. }
  514. }
  515. }
  516. @objc func loadListingTrash() {
  517. OCNetworking.sharedManager().listingTrash(withAccount: appDelegate.activeAccount, path: path, serverUrl: appDelegate.activeUrl, depth: "1", completion: { (account, item, message, errorCode) in
  518. self.refreshControl.endRefreshing()
  519. if errorCode == 0 && account == self.appDelegate.activeAccount {
  520. NCManageDatabase.sharedInstance.deleteTrash(filePath: self.path, account: self.appDelegate.activeAccount)
  521. NCManageDatabase.sharedInstance.addTrashs(item as! [tableTrash])
  522. } else if errorCode != 0 {
  523. self.appDelegate.messageNotification("_error_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  524. } else {
  525. print("[LOG] It has been changed user during networking process, error.")
  526. }
  527. self.loadDatasource()
  528. })
  529. }
  530. func reloadDataThenPerform(_ closure: @escaping (() -> Void)) {
  531. CATransaction.begin()
  532. CATransaction.setCompletionBlock(closure)
  533. collectionView.reloadData()
  534. CATransaction.commit()
  535. }
  536. func restoreItem(with fileID: String) {
  537. guard let tableTrash = NCManageDatabase.sharedInstance.getTrashItem(fileID: fileID, account: appDelegate.activeAccount) else {
  538. return
  539. }
  540. let fileName = appDelegate.activeUrl + tableTrash.filePath + tableTrash.fileName
  541. let fileNameTo = appDelegate.activeUrl + k_dav + "/trashbin/" + appDelegate.activeUserID + "/restore/" + tableTrash.fileName
  542. OCNetworking.sharedManager().moveFileOrFolder(withAccount: appDelegate.activeAccount, fileName: fileName, fileNameTo: fileNameTo, completion: { (account, message, errorCode) in
  543. if errorCode == 0 && account == self.appDelegate.activeAccount {
  544. NCManageDatabase.sharedInstance.deleteTrash(fileID: fileID, account: account!)
  545. self.loadDatasource()
  546. } else if errorCode != 0 {
  547. self.appDelegate.messageNotification("_error_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  548. } else {
  549. print("[LOG] It has been changed user during networking process, error.")
  550. }
  551. })
  552. }
  553. func emptyTrash() {
  554. OCNetworking.sharedManager().emptyTrash(withAccount: appDelegate.activeAccount, completion: { (account, message, errorCode) in
  555. if errorCode == 0 && account == self.appDelegate.activeAccount {
  556. NCManageDatabase.sharedInstance.deleteTrash(fileID: nil, account: self.appDelegate.activeAccount)
  557. } else if errorCode != 0 {
  558. self.appDelegate.messageNotification("_error_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  559. } else {
  560. print("[LOG] It has been changed user during networking process, error.")
  561. }
  562. self.loadDatasource()
  563. })
  564. }
  565. func deleteItem(with fileID: String) {
  566. guard let tableTrash = NCManageDatabase.sharedInstance.getTrashItem(fileID: fileID, account: appDelegate.activeAccount) else {
  567. return
  568. }
  569. let path = appDelegate.activeUrl + tableTrash.filePath + tableTrash.fileName
  570. OCNetworking.sharedManager().deleteFileOrFolder(withAccount: appDelegate.activeAccount, path: path, completion: { (account, message, errorCode) in
  571. if errorCode == 0 && account == self.appDelegate.activeAccount {
  572. NCManageDatabase.sharedInstance.deleteTrash(fileID: fileID, account: account!)
  573. self.loadDatasource()
  574. } else if errorCode != 0 {
  575. self.appDelegate.messageNotification("_error_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  576. } else {
  577. print("[LOG] It has been changed user during networking process, error.")
  578. }
  579. })
  580. }
  581. func downloadThumbnail(with tableTrash: tableTrash, indexPath: IndexPath) {
  582. OCNetworking.sharedManager().downloadPreviewTrash(withAccount: appDelegate.activeAccount, fileID: tableTrash.fileID, fileName: tableTrash.fileName, completion: { (account, image, message, errorCode) in
  583. if errorCode == 0 && account == self.appDelegate.activeAccount {
  584. if let cell = self.collectionView.cellForItem(at: indexPath) {
  585. if cell is NCTrashListCell {
  586. (cell as! NCTrashListCell).imageItem.image = image
  587. } else if cell is NCGridCell {
  588. (cell as! NCGridCell).imageItem.image = image
  589. }
  590. }
  591. }
  592. })
  593. }
  594. }