NCTrash.swift 38 KB

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