NCTrash.swift 37 KB

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