NCTrash.swift 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  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, online: appDelegate.reachability.isReachable(), hidden: false)
  69. appDelegate.aspectTabBar(self.tabBarController?.tabBar, hidden: false)
  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. //let appearanceDelete = ActionSheetItemAppearance.init()
  246. //appearanceDelete.textColor = UIColor.red
  247. items.append(ActionSheetItem(title: NSLocalizedString("_restore_", comment: ""), value: 0, image: CCGraphics.changeThemingColorImage(UIImage.init(named: "restore"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon)))
  248. let itemDelete = ActionSheetItem(title: NSLocalizedString("_delete_", comment: ""), value: 1, image: CCGraphics.changeThemingColorImage(UIImage.init(named: "trash"), width: 50, height: 50, color: UIColor.red))
  249. // itemDelete.customAppearance = appearanceDelete
  250. items.append(itemDelete)
  251. items.append(ActionSheetCancelButton(title: NSLocalizedString("_cancel_", comment: "")))
  252. actionSheet = ActionSheet(items: items) { sheet, item in
  253. if item.value as? Int == 0 { self.restoreItem(with: objectId) }
  254. if item.value as? Int == 1 { self.deleteItem(with: objectId) }
  255. if item is ActionSheetCancelButton { print("Cancel buttons has the value `true`") }
  256. }
  257. guard let tableTrash = NCManageDatabase.sharedInstance.getTrashItem(fileId: objectId, account: appDelegate.activeAccount) else {
  258. return
  259. }
  260. let headerView = NCActionSheetHeader.sharedInstance.actionSheetHeader(isDirectory: tableTrash.directory, iconName: tableTrash.iconName, ocId: tableTrash.fileId, fileNameView: tableTrash.fileName, text: tableTrash.trashbinFileName)
  261. actionSheet?.headerView = headerView
  262. actionSheet?.headerView?.frame.size.height = 50
  263. ActionSheetTableView.appearance().backgroundColor = NCBrandColor.sharedInstance.backgroundForm
  264. ActionSheetTableView.appearance().separatorColor = NCBrandColor.sharedInstance.separator
  265. ActionSheetItemCell.appearance().backgroundColor = NCBrandColor.sharedInstance.backgroundForm
  266. ActionSheetItemCell.appearance().titleColor = NCBrandColor.sharedInstance.textView
  267. actionSheet?.present(in: self, from: sender as! UIButton)
  268. } else {
  269. let buttonPosition:CGPoint = (sender as! UIButton).convert(CGPoint.zero, to:collectionView)
  270. let indexPath = collectionView.indexPathForItem(at: buttonPosition)
  271. collectionView(self.collectionView, didSelectItemAt: indexPath!)
  272. }
  273. }
  274. // MARK: DROP-DOWN-MENU
  275. func dropdownMenu(_ dropdownMenu: DropdownMenu, didSelectRowAt indexPath: IndexPath) {
  276. if dropdownMenu.token == "tapOrderHeaderMenu" {
  277. switch indexPath.row {
  278. case 0: datasourceSorted = "fileName"; datasourceAscending = true
  279. case 1: datasourceSorted = "fileName"; datasourceAscending = false
  280. case 2: datasourceSorted = "date"; datasourceAscending = false
  281. case 3: datasourceSorted = "date"; datasourceAscending = true
  282. case 4: datasourceSorted = "size"; datasourceAscending = true
  283. case 5: datasourceSorted = "size"; datasourceAscending = false
  284. default: print("")
  285. }
  286. NCUtility.sharedInstance.setLayoutForView(key: k_layout_view_trash, layout: typeLayout, sort: datasourceSorted, ascending: datasourceAscending, groupBy: datasourceGroupBy, directoryOnTop: datasourceDirectoryOnTop)
  287. loadDatasource()
  288. }
  289. if dropdownMenu.token == "tapMoreHeaderMenu" {
  290. /*
  291. // Select
  292. if indexPath.row == 0 {
  293. isEditMode = true
  294. collectionView.reloadData()
  295. }
  296. // Restore ALL
  297. if indexPath.row == 1 {
  298. for record: tableTrash in self.datasource {
  299. restoreItem(with: record.ocId)
  300. }
  301. }
  302. */
  303. // Empty Trash
  304. if indexPath.row == 0 {
  305. var items = [ActionSheetItem]()
  306. items.append(ActionSheetTitle(title: NSLocalizedString("_trash_delete_all_", comment: "")))
  307. items.append(ActionSheetDangerButton(title: NSLocalizedString("_ok_", comment: "")))
  308. items.append(ActionSheetCancelButton(title: NSLocalizedString("_cancel_", comment: "")))
  309. actionSheet = ActionSheet(items: items) { sheet, item in
  310. if item is ActionSheetDangerButton {
  311. self.emptyTrash()
  312. //for record: tableTrash in self.datasource {
  313. // self.deleteItem(with: record.ocId)
  314. //}
  315. }
  316. if item is ActionSheetCancelButton { return }
  317. }
  318. ActionSheetTableView.appearance().backgroundColor = NCBrandColor.sharedInstance.backgroundForm
  319. ActionSheetTableView.appearance().separatorColor = NCBrandColor.sharedInstance.separator
  320. ActionSheetItemCell.appearance().backgroundColor = NCBrandColor.sharedInstance.backgroundForm
  321. ActionSheetItemCell.appearance().titleColor = NCBrandColor.sharedInstance.textView
  322. actionSheet?.present(in: self, from: self.view)
  323. }
  324. }
  325. if dropdownMenu.token == "tapMoreHeaderMenuSelect" {
  326. // Cancel
  327. if indexPath.row == 0 {
  328. isEditMode = false
  329. selectocId.removeAll()
  330. collectionView.reloadData()
  331. }
  332. // Restore selected files
  333. if indexPath.row == 1 {
  334. for ocId in selectocId {
  335. restoreItem(with: ocId)
  336. }
  337. isEditMode = false
  338. selectocId.removeAll()
  339. collectionView.reloadData()
  340. }
  341. // Delete selected files
  342. if indexPath.row == 2 {
  343. var items = [ActionSheetItem]()
  344. items.append(ActionSheetTitle(title: NSLocalizedString("_trash_delete_selected_", comment: "")))
  345. items.append(ActionSheetDangerButton(title: NSLocalizedString("_delete_", comment: "")))
  346. items.append(ActionSheetCancelButton(title: NSLocalizedString("_cancel_", comment: "")))
  347. actionSheet = ActionSheet(items: items) { sheet, item in
  348. if item is ActionSheetDangerButton {
  349. for ocId in self.selectocId {
  350. self.deleteItem(with: ocId)
  351. }
  352. self.isEditMode = false
  353. self.selectocId.removeAll()
  354. self.collectionView.reloadData()
  355. }
  356. if item is ActionSheetCancelButton { return }
  357. }
  358. ActionSheetTableView.appearance().backgroundColor = NCBrandColor.sharedInstance.backgroundForm
  359. ActionSheetTableView.appearance().separatorColor = NCBrandColor.sharedInstance.separator
  360. ActionSheetItemCell.appearance().backgroundColor = NCBrandColor.sharedInstance.backgroundForm
  361. ActionSheetItemCell.appearance().titleColor = NCBrandColor.sharedInstance.textView
  362. actionSheet?.present(in: self, from: self.view)
  363. }
  364. }
  365. }
  366. /*
  367. func dropdownMenuWillDismiss(_ dropdownMenu: DropdownMenu) {
  368. if dropdownMenu.token == "tapOrderHeaderMenu" {
  369. let trashHeader = collectionView.supplementaryView(forElementKind: UICollectionView.elementKindSectionHeader, at: IndexPath(row: 0, section: 0)) as! NCTrashHeaderMenu
  370. let title = String(trashHeader.buttonOrder.title(for: .normal)!.dropLast()) + "▽"
  371. trashHeader.buttonOrder.setTitle(title, for: .normal)
  372. }
  373. }
  374. func dropdownMenuWillShow(_ dropdownMenu: DropdownMenu) {
  375. if dropdownMenu.token == "tapOrderHeaderMenu" {
  376. let trashHeader = collectionView.supplementaryView(forElementKind: UICollectionView.elementKindSectionHeader, at: IndexPath(row: 0, section: 0)) as! NCTrashHeaderMenu
  377. let title = String(trashHeader.buttonOrder.title(for: .normal)!.dropLast()) + "△"
  378. trashHeader.buttonOrder.setTitle(title, for: .normal)
  379. }
  380. }
  381. */
  382. }
  383. // MARK: - Collection View
  384. extension NCTrash: UICollectionViewDelegate {
  385. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  386. let tableTrash = datasource[indexPath.item]
  387. if isEditMode {
  388. if let index = selectocId.firstIndex(of: tableTrash.fileId) {
  389. selectocId.remove(at: index)
  390. } else {
  391. selectocId.append(tableTrash.fileId)
  392. }
  393. collectionView.reloadItems(at: [indexPath])
  394. return
  395. }
  396. if tableTrash.directory {
  397. let ncTrash:NCTrash = UIStoryboard(name: "NCTrash", bundle: nil).instantiateInitialViewController() as! NCTrash
  398. ncTrash.path = tableTrash.filePath + tableTrash.fileName
  399. ncTrash.titleCurrentFolder = tableTrash.trashbinFileName
  400. self.navigationController?.pushViewController(ncTrash, animated: true)
  401. }
  402. }
  403. }
  404. extension NCTrash: UICollectionViewDataSource {
  405. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  406. if kind == UICollectionView.elementKindSectionHeader {
  407. let trashHeader = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionHeaderMenu", for: indexPath) as! NCTrashSectionHeaderMenu
  408. if collectionView.collectionViewLayout == gridLayout {
  409. trashHeader.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchList"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
  410. } else {
  411. trashHeader.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchGrid"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
  412. }
  413. trashHeader.delegate = self
  414. trashHeader.setStatusButton(datasource: datasource)
  415. trashHeader.setTitleOrder(datasourceSorted: datasourceSorted, datasourceAscending: datasourceAscending)
  416. return trashHeader
  417. } else {
  418. let trashFooter = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFooter", for: indexPath) as! NCTrashSectionFooter
  419. trashFooter.setTitleLabelFooter(datasource: datasource)
  420. return trashFooter
  421. }
  422. }
  423. func numberOfSections(in collectionView: UICollectionView) -> Int {
  424. return 1
  425. }
  426. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  427. return datasource.count
  428. }
  429. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  430. let tableTrash = datasource[indexPath.item]
  431. var image: UIImage?
  432. if tableTrash.iconName.count > 0 {
  433. image = UIImage.init(named: tableTrash.iconName)
  434. } else {
  435. image = UIImage.init(named: "file")
  436. }
  437. if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconOcId(tableTrash.fileId, fileNameView: tableTrash.fileName)) {
  438. image = UIImage.init(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(tableTrash.fileId, fileNameView: tableTrash.fileName))
  439. } else {
  440. if tableTrash.hasPreview && !CCUtility.fileProviderStorageIconExists(tableTrash.fileId, fileNameView: tableTrash.fileName) {
  441. downloadThumbnail(with: tableTrash, indexPath: indexPath)
  442. }
  443. }
  444. if collectionView.collectionViewLayout == listLayout {
  445. // LIST
  446. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as! NCTrashListCell
  447. cell.delegate = self
  448. cell.objectId = tableTrash.fileId
  449. cell.indexPath = indexPath
  450. cell.labelTitle.text = tableTrash.trashbinFileName
  451. cell.labelTitle.textColor = NCBrandColor.sharedInstance.textView
  452. if tableTrash.directory {
  453. cell.imageItem.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
  454. cell.labelInfo.text = CCUtility.dateDiff(tableTrash.date as Date)
  455. } else {
  456. cell.imageItem.image = image
  457. cell.labelInfo.text = CCUtility.dateDiff(tableTrash.date as Date) + ", " + CCUtility.transformedSize(tableTrash.size)
  458. }
  459. if isEditMode {
  460. cell.imageItemLeftConstraint.constant = 45
  461. cell.imageSelect.isHidden = false
  462. if selectocId.contains(tableTrash.fileId) {
  463. cell.imageSelect.image = CCGraphics.scale(UIImage.init(named: "checkedYes"), to: CGSize(width: 50, height: 50), isAspectRation: true)
  464. cell.backgroundView = NCUtility.sharedInstance.cellBlurEffect(with: cell.bounds)
  465. } else {
  466. cell.imageSelect.image = CCGraphics.scale(UIImage.init(named: "checkedNo"), to: CGSize(width: 50, height: 50), isAspectRation: true)
  467. cell.backgroundView = nil
  468. }
  469. } else {
  470. cell.imageItemLeftConstraint.constant = 10
  471. cell.imageSelect.isHidden = true
  472. cell.backgroundView = nil
  473. }
  474. return cell
  475. } else {
  476. // GRID
  477. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as! NCGridCell
  478. cell.delegate = self
  479. cell.objectId = tableTrash.fileId
  480. cell.indexPath = indexPath
  481. cell.labelTitle.text = tableTrash.trashbinFileName
  482. cell.labelTitle.textColor = NCBrandColor.sharedInstance.textView
  483. if tableTrash.directory {
  484. cell.imageItem.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
  485. } else {
  486. cell.imageItem.image = image
  487. }
  488. if isEditMode {
  489. cell.imageSelect.isHidden = false
  490. if selectocId.contains(tableTrash.fileId) {
  491. cell.imageSelect.image = CCGraphics.scale(UIImage.init(named: "checkedYes"), to: CGSize(width: 50, height: 50), isAspectRation: true)
  492. cell.backgroundView = NCUtility.sharedInstance.cellBlurEffect(with: cell.bounds)
  493. } else {
  494. cell.imageSelect.isHidden = true
  495. cell.backgroundView = nil
  496. }
  497. } else {
  498. cell.imageSelect.isHidden = true
  499. cell.backgroundView = nil
  500. }
  501. return cell
  502. }
  503. }
  504. }
  505. extension NCTrash: UICollectionViewDelegateFlowLayout {
  506. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  507. return CGSize(width: collectionView.frame.width, height: highHeader)
  508. }
  509. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
  510. return CGSize(width: collectionView.frame.width, height: highHeader)
  511. }
  512. }
  513. // MARK: - NC API & Algorithm
  514. extension NCTrash {
  515. @objc func loadDatasource() {
  516. datasource.removeAll()
  517. guard let tashItems = NCManageDatabase.sharedInstance.getTrash(filePath: path, sorted: datasourceSorted, ascending: datasourceAscending, account: appDelegate.activeAccount) else {
  518. return
  519. }
  520. datasource = tashItems
  521. reloadDataThenPerform {
  522. // GoTo ocId
  523. if self.blinkocId != nil {
  524. for item in 0...self.datasource.count-1 {
  525. if self.datasource[item].fileId.contains(self.blinkocId!) {
  526. let indexPath = IndexPath(item: item, section: 0)
  527. self.collectionView.scrollToItem(at: indexPath, at: .top, animated: false)
  528. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
  529. self.blinkocId = nil
  530. NCUtility.sharedInstance.blink(cell: self.collectionView.cellForItem(at: indexPath))
  531. }
  532. }
  533. }
  534. }
  535. }
  536. }
  537. @objc func loadListingTrash() {
  538. OCNetworking.sharedManager().listingTrash(withAccount: appDelegate.activeAccount, path: path, serverUrl: appDelegate.activeUrl, depth: "1", completion: { (account, item, message, errorCode) in
  539. self.refreshControl.endRefreshing()
  540. if errorCode == 0 && account == self.appDelegate.activeAccount {
  541. NCManageDatabase.sharedInstance.deleteTrash(filePath: self.path, account: self.appDelegate.activeAccount)
  542. NCManageDatabase.sharedInstance.addTrashs(item as! [tableTrash])
  543. } else if errorCode != 0 {
  544. self.appDelegate.messageNotification("_error_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  545. } else {
  546. print("[LOG] It has been changed user during networking process, error.")
  547. }
  548. self.loadDatasource()
  549. })
  550. }
  551. func reloadDataThenPerform(_ closure: @escaping (() -> Void)) {
  552. CATransaction.begin()
  553. CATransaction.setCompletionBlock(closure)
  554. collectionView.reloadData()
  555. CATransaction.commit()
  556. }
  557. func restoreItem(with fileId: String) {
  558. guard let tableTrash = NCManageDatabase.sharedInstance.getTrashItem(fileId: fileId, account: appDelegate.activeAccount) else {
  559. return
  560. }
  561. let fileName = appDelegate.activeUrl + tableTrash.filePath + tableTrash.fileName
  562. let fileNameTo = appDelegate.activeUrl + k_dav + "/trashbin/" + appDelegate.activeUserID + "/restore/" + tableTrash.fileName
  563. OCNetworking.sharedManager().moveFileOrFolder(withAccount: appDelegate.activeAccount, fileName: fileName, fileNameTo: fileNameTo, completion: { (account, message, errorCode) in
  564. if errorCode == 0 && account == self.appDelegate.activeAccount {
  565. NCManageDatabase.sharedInstance.deleteTrash(fileId: fileId, account: account!)
  566. self.loadDatasource()
  567. } else if errorCode != 0 {
  568. self.appDelegate.messageNotification("_error_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  569. } else {
  570. print("[LOG] It has been changed user during networking process, error.")
  571. }
  572. })
  573. }
  574. func emptyTrash() {
  575. OCNetworking.sharedManager().emptyTrash(withAccount: appDelegate.activeAccount, completion: { (account, message, errorCode) in
  576. if errorCode == 0 && account == self.appDelegate.activeAccount {
  577. NCManageDatabase.sharedInstance.deleteTrash(fileId: nil, account: self.appDelegate.activeAccount)
  578. } else if errorCode != 0 {
  579. self.appDelegate.messageNotification("_error_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  580. } else {
  581. print("[LOG] It has been changed user during networking process, error.")
  582. }
  583. self.loadDatasource()
  584. })
  585. }
  586. func deleteItem(with fileId: String) {
  587. guard let tableTrash = NCManageDatabase.sharedInstance.getTrashItem(fileId: fileId, account: appDelegate.activeAccount) else {
  588. return
  589. }
  590. let path = appDelegate.activeUrl + tableTrash.filePath + tableTrash.fileName
  591. OCNetworking.sharedManager().deleteFileOrFolder(withAccount: appDelegate.activeAccount, path: path, completion: { (account, message, errorCode) in
  592. if errorCode == 0 && account == self.appDelegate.activeAccount {
  593. NCManageDatabase.sharedInstance.deleteTrash(fileId: fileId, account: account!)
  594. self.loadDatasource()
  595. } else if errorCode != 0 {
  596. self.appDelegate.messageNotification("_error_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  597. } else {
  598. print("[LOG] It has been changed user during networking process, error.")
  599. }
  600. })
  601. }
  602. func downloadThumbnail(with tableTrash: tableTrash, indexPath: IndexPath) {
  603. OCNetworking.sharedManager().downloadPreviewTrash(withAccount: appDelegate.activeAccount, fileId: tableTrash.fileId, size: "128", fileName: tableTrash.fileName, completion: { (account, image, message, errorCode) in
  604. if errorCode == 0 && account == self.appDelegate.activeAccount {
  605. if let cell = self.collectionView.cellForItem(at: indexPath) {
  606. if cell is NCTrashListCell {
  607. (cell as! NCTrashListCell).imageItem.image = image
  608. } else if cell is NCGridCell {
  609. (cell as! NCGridCell).imageItem.image = image
  610. }
  611. }
  612. }
  613. })
  614. }
  615. }