123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761 |
- //
- // NCTrash.swift
- // Nextcloud
- //
- // Created by Marino Faggiana on 02/10/2018.
- // Copyright © 2018 Marino Faggiana. All rights reserved.
- //
- // Author Marino Faggiana <marino.faggiana@nextcloud.com>
- //
- // This program is free software: you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with this program. If not, see <http://www.gnu.org/licenses/>.
- //
- import Foundation
- import NCCommunication
- class NCTrash: UIViewController, UIGestureRecognizerDelegate, NCTrashListCellDelegate, NCGridCellDelegate, NCTrashSectionHeaderMenuDelegate, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate {
-
- @IBOutlet fileprivate weak var collectionView: UICollectionView!
- var path = ""
- var titleCurrentFolder = NSLocalizedString("_trash_view_", comment: "")
- var blinkocId: String?
-
- private let appDelegate = UIApplication.shared.delegate as! AppDelegate
-
- private var isEditMode = false
- private var selectocId = [String]()
-
- private var datasource = [tableTrash]()
-
- private var typeLayout = ""
- private var datasourceSorted = ""
- private var datasourceAscending = true
- private var datasourceGroupBy = "none"
- private var datasourceDirectoryOnTop = false
-
- private var listLayout: NCListLayout!
- private var gridLayout: NCGridLayout!
-
- private let highHeader: CGFloat = 50
-
- private let refreshControl = UIRefreshControl()
- override func viewDidLoad() {
- super.viewDidLoad()
-
- // Cell
- collectionView.register(UINib.init(nibName: "NCTrashListCell", bundle: nil), forCellWithReuseIdentifier: "listCell")
- collectionView.register(UINib.init(nibName: "NCGridCell", bundle: nil), forCellWithReuseIdentifier: "gridCell")
-
- // Header - Footer
- collectionView.register(UINib.init(nibName: "NCTrashSectionHeaderMenu", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "sectionHeaderMenu")
- collectionView.register(UINib.init(nibName: "NCTrashSectionFooter", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "sectionFooter")
- collectionView.alwaysBounceVertical = true
- listLayout = NCListLayout()
- gridLayout = NCGridLayout()
-
- // Add Refresh Control
- collectionView.refreshControl = refreshControl
-
- // Configure Refresh Control
- refreshControl.addTarget(self, action: #selector(loadListingTrash), for: .valueChanged)
-
- // empty Data Source
- self.collectionView.emptyDataSetDelegate = self;
- self.collectionView.emptyDataSetSource = self;
-
- // changeTheming
- NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: k_notificationCenter_changeTheming), object: nil)
- changeTheming()
- }
-
- override func viewWillAppear(_ animated: Bool) {
- super.viewWillAppear(animated)
-
- self.navigationItem.title = titleCurrentFolder
- (typeLayout, datasourceSorted, datasourceAscending, datasourceGroupBy, datasourceDirectoryOnTop) = NCUtility.sharedInstance.getLayoutForView(key: k_layout_view_trash)
- if typeLayout == k_layout_list {
- collectionView.collectionViewLayout = listLayout
- } else {
- collectionView.collectionViewLayout = gridLayout
- }
-
- // Datasource & serverUrl
-
- if path == "" {
- let userID = (appDelegate.activeUserID as NSString).addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlFragmentAllowed)
- path = k_dav + "/trashbin/" + userID! + "/trash/"
- }
-
- if (datasource.count == 0) {
- loadDatasource()
- }
-
- loadListingTrash()
- }
-
- override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
- super.viewWillTransition(to: size, with: coordinator)
-
- coordinator.animate(alongsideTransition: nil) { _ in
- self.collectionView.collectionViewLayout.invalidateLayout()
- }
- }
-
- @objc func changeTheming() {
- appDelegate.changeTheming(self, tableView: nil, collectionView: collectionView, form: false)
-
- refreshControl.tintColor = NCBrandColor.sharedInstance.brandText
- refreshControl.backgroundColor = NCBrandColor.sharedInstance.brand
- }
-
- // MARK: DZNEmpty
-
- func backgroundColor(forEmptyDataSet scrollView: UIScrollView) -> UIColor? {
- return NCBrandColor.sharedInstance.backgroundView
- }
-
- func image(forEmptyDataSet scrollView: UIScrollView) -> UIImage? {
- return CCGraphics.changeThemingColorImage(UIImage.init(named: "trash"), width: 300, height: 300, color: NCBrandColor.sharedInstance.graySoft)
- }
-
- func title(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
- let text = "\n"+NSLocalizedString("_trash_no_trash_", comment: "")
- let attributes = [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 20), NSAttributedString.Key.foregroundColor: UIColor.lightGray]
- return NSAttributedString.init(string: text, attributes: attributes)
- }
-
- func description(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
- let text = "\n"+NSLocalizedString("_trash_no_trash_description_", comment: "")
- let attributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14), NSAttributedString.Key.foregroundColor: UIColor.lightGray]
- return NSAttributedString.init(string: text, attributes: attributes)
- }
-
- func emptyDataSetShouldAllowScroll(_ scrollView: UIScrollView) -> Bool {
- return true
- }
-
- func reloadSortOrder(){
- NCUtility.sharedInstance.setLayoutForView(
- key: k_layout_view_trash,
- layout: self.typeLayout,
- sort: self.datasourceSorted,
- ascending: self.datasourceAscending,
- groupBy: self.datasourceGroupBy,
- directoryOnTop: self.datasourceDirectoryOnTop)
- self.loadDatasource()
- }
- // MARK: TAP EVENT
-
- func tapSwitchHeaderMenu(sender: Any) {
-
- if collectionView.collectionViewLayout == gridLayout {
- // list layout
- UIView.animate(withDuration: 0.0, animations: {
- self.collectionView.collectionViewLayout.invalidateLayout()
- self.collectionView.setCollectionViewLayout(self.listLayout, animated: false, completion: { (_) in
- self.collectionView.reloadData()
- self.collectionView.setContentOffset(CGPoint(x:0,y:0), animated: false)
- })
- })
- typeLayout = k_layout_list
- NCUtility.sharedInstance.setLayoutForView(key: k_layout_view_trash, layout: typeLayout, sort: datasourceSorted, ascending: datasourceAscending, groupBy: datasourceGroupBy, directoryOnTop: datasourceDirectoryOnTop)
- } else {
- // grid layout
- UIView.animate(withDuration: 0.0, animations: {
- self.collectionView.collectionViewLayout.invalidateLayout()
- self.collectionView.setCollectionViewLayout(self.gridLayout, animated: false, completion: { (_) in
- self.collectionView.reloadData()
- self.collectionView.setContentOffset(CGPoint(x:0,y:0), animated: false)
- })
- })
- typeLayout = k_layout_grid
- NCUtility.sharedInstance.setLayoutForView(key: k_layout_view_trash, layout: typeLayout, sort: datasourceSorted, ascending: datasourceAscending, groupBy: datasourceGroupBy, directoryOnTop: datasourceDirectoryOnTop)
- }
- }
-
- func tapOrderHeaderMenu(sender: Any) {
- let mainMenuViewController = UIStoryboard.init(name: "NCMenu", bundle: nil).instantiateViewController(withIdentifier: "NCMainMenuTableViewController") as! NCMainMenuTableViewController
-
- var actions = [NCMenuAction]()
-
- actions.append(
- NCMenuAction(
- title: NSLocalizedString("_order_by_name_a_z_", comment: ""),
- icon: CCGraphics.changeThemingColorImage(UIImage(named: "sortFileNameAZ"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
- onTitle: NSLocalizedString("_order_by_name_z_a_", comment: ""),
- onIcon: CCGraphics.changeThemingColorImage(UIImage(named: "sortFileNameZA"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
- selected: datasourceSorted == "fileName",
- on: datasourceAscending,
- action: { menuAction in
- if(self.datasourceSorted == "fileName"){
- self.datasourceAscending = !self.datasourceAscending
- } else {
- self.datasourceSorted = "fileName"
- }
- self.reloadSortOrder()
- }
- )
- )
-
- actions.append(
- NCMenuAction(
- title: NSLocalizedString("_order_by_date_more_recent_", comment: ""),
- icon: CCGraphics.changeThemingColorImage(UIImage(named: "sortDateMoreRecent"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
- onTitle: NSLocalizedString("_order_by_date_less_recent_", comment: ""),
- onIcon: CCGraphics.changeThemingColorImage(UIImage(named: "sortDateLessRecent"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
- selected: datasourceSorted == "date",
- on: datasourceAscending,
- action: { menuAction in
- if(self.datasourceSorted == "date"){
- self.datasourceAscending = !self.datasourceAscending
- } else {
- self.datasourceSorted = "date"
- }
- self.reloadSortOrder()
- }
- )
- )
- actions.append(
- NCMenuAction(
- title: NSLocalizedString("_order_by_size_smallest_", comment: ""),
- icon: CCGraphics.changeThemingColorImage(UIImage(named: "sortSmallest"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
- onTitle: NSLocalizedString("_order_by_size_largest_", comment: ""),
- onIcon: CCGraphics.changeThemingColorImage(UIImage(named: "sortLargest"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
- selected: datasourceSorted == "size",
- on: datasourceAscending,
- action: { menuAction in
- if(self.datasourceSorted == "size"){
- self.datasourceAscending = !self.datasourceAscending
- } else {
- self.datasourceSorted = "size"
- }
- self.reloadSortOrder()
- }
- )
- )
-
- mainMenuViewController.actions = actions
-
- let menuPanelController = NCMenuPanelController()
- menuPanelController.parentPresenter = self
- menuPanelController.delegate = mainMenuViewController
- menuPanelController.set(contentViewController: mainMenuViewController)
- menuPanelController.track(scrollView: mainMenuViewController.tableView)
- self.present(menuPanelController, animated: true, completion: nil)
- }
-
- func tapMoreHeaderMenu(sender: Any) {
- let mainMenuViewController = UIStoryboard.init(name: "NCMenu", bundle: nil).instantiateViewController(withIdentifier: "NCMainMenuTableViewController") as! NCMainMenuTableViewController
-
- var actions = [NCMenuAction]()
-
- if isEditMode {
- actions.append(
- NCMenuAction(
- title: NSLocalizedString("_trash_delete_selected_", comment: ""),
- icon: CCGraphics.changeThemingColorImage(UIImage(named: "trash"), width: 50, height: 50, color: .red),
- action: { menuAction in
- let alert = UIAlertController(title: NSLocalizedString("_trash_delete_selected_", comment: ""), message: "", preferredStyle: .alert)
- alert.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .destructive, handler: { _ in
- for ocId in self.selectocId {
- self.deleteItem(with: ocId)
- }
- self.isEditMode = false
- self.selectocId.removeAll()
- self.collectionView.reloadData()
- }))
- alert.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: { _ in
- }))
- self.present(alert, animated: true, completion: nil)
- }
- )
- )
- } else {
- actions.append(
- NCMenuAction(
- title: NSLocalizedString("_trash_delete_all_", comment: ""),
- icon: CCGraphics.changeThemingColorImage(UIImage(named: "trash"), width: 50, height: 50, color: .red),
- action: { menuAction in
- let alert = UIAlertController(title: NSLocalizedString("_trash_delete_all_", comment: ""), message: "", preferredStyle: .alert)
- alert.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .destructive, handler: { _ in
- self.emptyTrash()
- }))
- alert.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: { _ in
- }))
- self.present(alert, animated: true, completion: nil)
- }
- )
- )
- }
-
- mainMenuViewController.actions = actions
-
- let menuPanelController = NCMenuPanelController()
- menuPanelController.parentPresenter = self
- menuPanelController.delegate = mainMenuViewController
- menuPanelController.set(contentViewController: mainMenuViewController)
- menuPanelController.track(scrollView: mainMenuViewController.tableView)
- self.present(menuPanelController, animated: true, completion: nil)
- }
-
- func tapRestoreListItem(with ocId: String, sender: Any) {
-
- if !isEditMode {
- restoreItem(with: ocId)
- } else {
- let buttonPosition:CGPoint = (sender as! UIButton).convert(CGPoint.zero, to:collectionView)
- let indexPath = collectionView.indexPathForItem(at: buttonPosition)
- collectionView(self.collectionView, didSelectItemAt: indexPath!)
- }
- }
-
- func tapMoreListItem(with objectId: String, sender: Any) {
- if !isEditMode {
- let mainMenuViewController = UIStoryboard.init(name: "NCMenu", bundle: nil).instantiateViewController(withIdentifier: "NCMainMenuTableViewController") as! NCMainMenuTableViewController
- var actions = [NCMenuAction]()
- guard let tableTrash = NCManageDatabase.sharedInstance.getTrashItem(fileId: objectId, account: appDelegate.activeAccount) else {
- return
- }
- var iconHeader: UIImage!
- if let icon = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(tableTrash.fileId, fileNameView: tableTrash.fileName)) {
- iconHeader = icon
- } else {
- if(tableTrash.directory) {
- iconHeader = CCGraphics.changeThemingColorImage(UIImage(named: "folder"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon)
- } else {
- iconHeader = UIImage(named: tableTrash.iconName)
- }
- }
- actions.append(
- NCMenuAction(
- title: tableTrash.trashbinFileName,
- icon: iconHeader,
- action: nil
- )
- )
- actions.append(
- NCMenuAction(
- title: NSLocalizedString("_delete_", comment: ""),
- icon: CCGraphics.changeThemingColorImage(UIImage(named: "trash"), width: 50, height: 50, color: .red),
- action: { menuAction in
- self.deleteItem(with: objectId)
- }
- )
- )
- mainMenuViewController.actions = actions
- let menuPanelController = NCMenuPanelController()
- menuPanelController.parentPresenter = self
- menuPanelController.delegate = mainMenuViewController
- menuPanelController.set(contentViewController: mainMenuViewController)
- menuPanelController.track(scrollView: mainMenuViewController.tableView)
- self.present(menuPanelController, animated: true, completion: nil)
- } else {
- let buttonPosition: CGPoint = (sender as! UIButton).convert(CGPoint.zero, to: collectionView)
- let indexPath = collectionView.indexPathForItem(at: buttonPosition)
- collectionView(self.collectionView, didSelectItemAt: indexPath!)
- }
- }
-
- func tapMoreGridItem(with objectId: String, sender: Any) {
- if !isEditMode {
- let mainMenuViewController = UIStoryboard.init(name: "NCMenu", bundle: nil).instantiateViewController(withIdentifier: "NCMainMenuTableViewController") as! NCMainMenuTableViewController
- var actions = [NCMenuAction]()
- guard let tableTrash = NCManageDatabase.sharedInstance.getTrashItem(fileId: objectId, account: appDelegate.activeAccount) else {
- return
- }
- var iconHeader: UIImage!
- if let icon = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(tableTrash.fileId, fileNameView: tableTrash.fileName)) {
- iconHeader = icon
- } else {
- if(tableTrash.directory) {
- iconHeader = CCGraphics.changeThemingColorImage(UIImage(named: "folder"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon)
- } else {
- iconHeader = UIImage(named: tableTrash.iconName)
- }
- }
- actions.append(
- NCMenuAction(
- title: tableTrash.trashbinFileName,
- icon: iconHeader,
- action: nil
- )
- )
- actions.append(
- NCMenuAction(
- title: NSLocalizedString("_restore_", comment: ""),
- icon: CCGraphics.changeThemingColorImage(UIImage(named: "restore"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
- action: { menuAction in
- self.restoreItem(with: objectId)
- }
- )
- )
- actions.append(
- NCMenuAction(
- title: NSLocalizedString("_delete_", comment: ""),
- icon: CCGraphics.changeThemingColorImage(UIImage(named: "trash"), width: 50, height: 50, color: .red),
- action: { menuAction in
- self.deleteItem(with: objectId)
- }
- )
- )
- mainMenuViewController.actions = actions
- let menuPanelController = NCMenuPanelController()
- menuPanelController.parentPresenter = self
- menuPanelController.delegate = mainMenuViewController
- menuPanelController.set(contentViewController: mainMenuViewController)
- menuPanelController.track(scrollView: mainMenuViewController.tableView)
- self.present(menuPanelController, animated: true, completion: nil)
- } else {
- let buttonPosition: CGPoint = (sender as! UIButton).convert(CGPoint.zero, to: collectionView)
- let indexPath = collectionView.indexPathForItem(at: buttonPosition)
- collectionView(self.collectionView, didSelectItemAt: indexPath!)
- }
- }
-
- }
- // MARK: - Collection View
- extension NCTrash: UICollectionViewDelegate {
- func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
-
- let tableTrash = datasource[indexPath.item]
-
- if isEditMode {
- if let index = selectocId.firstIndex(of: tableTrash.fileId) {
- selectocId.remove(at: index)
- } else {
- selectocId.append(tableTrash.fileId)
- }
- collectionView.reloadItems(at: [indexPath])
- return
- }
-
- if tableTrash.directory {
-
- let ncTrash:NCTrash = UIStoryboard(name: "NCTrash", bundle: nil).instantiateInitialViewController() as! NCTrash
-
- ncTrash.path = tableTrash.filePath + tableTrash.fileName
- ncTrash.titleCurrentFolder = tableTrash.trashbinFileName
-
- self.navigationController?.pushViewController(ncTrash, animated: true)
- }
- }
- }
- extension NCTrash: UICollectionViewDataSource {
- func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
-
- if kind == UICollectionView.elementKindSectionHeader {
-
- let trashHeader = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionHeaderMenu", for: indexPath) as! NCTrashSectionHeaderMenu
-
- if collectionView.collectionViewLayout == gridLayout {
- trashHeader.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchList"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
- } else {
- trashHeader.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchGrid"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
- }
-
- trashHeader.delegate = self
- trashHeader.backgroundColor = NCBrandColor.sharedInstance.backgroundView
- trashHeader.separator.backgroundColor = NCBrandColor.sharedInstance.separator
- trashHeader.setStatusButton(datasource: datasource)
- trashHeader.setTitleOrder(datasourceSorted: datasourceSorted, datasourceAscending: datasourceAscending)
-
-
- return trashHeader
-
- } else {
-
- let trashFooter = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFooter", for: indexPath) as! NCTrashSectionFooter
-
- trashFooter.setTitleLabelFooter(datasource: datasource)
-
- return trashFooter
- }
- }
-
- func numberOfSections(in collectionView: UICollectionView) -> Int {
- return 1
- }
-
- func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
- return datasource.count
- }
-
- func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
-
- let tableTrash = datasource[indexPath.item]
- var image: UIImage?
-
- if tableTrash.iconName.count > 0 {
- image = UIImage.init(named: tableTrash.iconName)
- } else {
- image = UIImage.init(named: "file")
- }
-
- if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconOcId(tableTrash.fileId, fileNameView: tableTrash.fileName)) {
- image = UIImage.init(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(tableTrash.fileId, fileNameView: tableTrash.fileName))
- } else {
- if tableTrash.hasPreview && !CCUtility.fileProviderStorageIconExists(tableTrash.fileId, fileNameView: tableTrash.fileName) {
- downloadThumbnail(with: tableTrash, indexPath: indexPath)
- }
- }
-
- if collectionView.collectionViewLayout == listLayout {
-
- // LIST
- let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as! NCTrashListCell
- cell.delegate = self
-
- cell.objectId = tableTrash.fileId
- cell.indexPath = indexPath
- cell.labelTitle.text = tableTrash.trashbinFileName
- cell.labelTitle.textColor = NCBrandColor.sharedInstance.textView
- cell.separator.backgroundColor = NCBrandColor.sharedInstance.separator
- if tableTrash.directory {
- cell.imageItem.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
- cell.labelInfo.text = CCUtility.dateDiff(tableTrash.date as Date)
- } else {
- cell.imageItem.image = image
- cell.labelInfo.text = CCUtility.dateDiff(tableTrash.date as Date) + ", " + CCUtility.transformedSize(tableTrash.size)
- }
-
- if isEditMode {
- cell.imageItemLeftConstraint.constant = 45
- cell.imageSelect.isHidden = false
-
- if selectocId.contains(tableTrash.fileId) {
- cell.imageSelect.image = CCGraphics.scale(UIImage.init(named: "checkedYes"), to: CGSize(width: 50, height: 50), isAspectRation: true)
- cell.backgroundView = NCUtility.sharedInstance.cellBlurEffect(with: cell.bounds)
- } else {
- cell.imageSelect.image = CCGraphics.scale(UIImage.init(named: "checkedNo"), to: CGSize(width: 50, height: 50), isAspectRation: true)
- cell.backgroundView = nil
- }
- } else {
- cell.imageItemLeftConstraint.constant = 10
- cell.imageSelect.isHidden = true
- cell.backgroundView = nil
- }
-
- return cell
-
- } else {
-
- // GRID
- let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as! NCGridCell
- cell.delegate = self
-
- cell.objectId = tableTrash.fileId
- cell.indexPath = indexPath
- cell.labelTitle.text = tableTrash.trashbinFileName
- cell.labelTitle.textColor = NCBrandColor.sharedInstance.textView
-
- if tableTrash.directory {
- cell.imageItem.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
- } else {
- cell.imageItem.image = image
- }
-
- if isEditMode {
- cell.imageSelect.isHidden = false
- if selectocId.contains(tableTrash.fileId) {
- cell.imageSelect.image = CCGraphics.scale(UIImage.init(named: "checkedYes"), to: CGSize(width: 50, height: 50), isAspectRation: true)
- cell.backgroundView = NCUtility.sharedInstance.cellBlurEffect(with: cell.bounds)
- } else {
- cell.imageSelect.isHidden = true
- cell.backgroundView = nil
- }
- } else {
- cell.imageSelect.isHidden = true
- cell.backgroundView = nil
- }
- return cell
- }
- }
- }
- extension NCTrash: UICollectionViewDelegateFlowLayout {
- func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
- return CGSize(width: collectionView.frame.width, height: highHeader)
- }
-
- func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
- return CGSize(width: collectionView.frame.width, height: highHeader)
- }
- }
- // MARK: - NC API & Algorithm
- extension NCTrash {
- @objc func loadDatasource() {
-
- datasource.removeAll()
-
- guard let tashItems = NCManageDatabase.sharedInstance.getTrash(filePath: path, sorted: datasourceSorted, ascending: datasourceAscending, account: appDelegate.activeAccount) else {
- return
- }
-
- datasource = tashItems
-
- reloadDataThenPerform {
- // GoTo ocId
- if self.blinkocId != nil {
- for item in 0...self.datasource.count-1 {
- if self.datasource[item].fileId.contains(self.blinkocId!) {
- let indexPath = IndexPath(item: item, section: 0)
- self.collectionView.scrollToItem(at: indexPath, at: .top, animated: false)
- DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
- self.blinkocId = nil
- NCUtility.sharedInstance.blink(cell: self.collectionView.cellForItem(at: indexPath))
- }
- }
- }
- }
- }
- }
-
- @objc func loadListingTrash() {
-
- NCCommunication.shared.listingTrash(showHiddenFiles: false) { (account, items, errorCode, errorDescription) in
- self.refreshControl.endRefreshing()
-
- if errorCode == 0 && account == self.appDelegate.activeAccount && items != nil {
- NCManageDatabase.sharedInstance.deleteTrash(filePath: self.path, account: self.appDelegate.activeAccount)
- NCManageDatabase.sharedInstance.addTrash(account: account, items: items!)
- } else if errorCode != 0 {
- NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
- } else {
- print("[LOG] It has been changed user during networking process, error.")
- }
-
- self.loadDatasource()
- }
- }
-
- func reloadDataThenPerform(_ closure: @escaping (() -> Void)) {
- CATransaction.begin()
- CATransaction.setCompletionBlock(closure)
- collectionView.reloadData()
- CATransaction.commit()
- }
-
- func restoreItem(with fileId: String) {
-
- guard let tableTrash = NCManageDatabase.sharedInstance.getTrashItem(fileId: fileId, account: appDelegate.activeAccount) else {
- return
- }
-
- let fileNameFrom = appDelegate.activeUrl + tableTrash.filePath + tableTrash.fileName
- let fileNameTo = appDelegate.activeUrl + k_dav + "/trashbin/" + appDelegate.activeUserID + "/restore/" + tableTrash.fileName
-
- NCCommunication.shared.moveFileOrFolder(serverUrlFileNameSource: fileNameFrom, serverUrlFileNameDestination: fileNameTo, overwrite: true) { (account, errorCode, errorDescription) in
- if errorCode == 0 && account == self.appDelegate.activeAccount {
- NCManageDatabase.sharedInstance.deleteTrash(fileId: fileId, account: account)
- self.loadDatasource()
- } else if errorCode != 0 {
- NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
- } else {
- print("[LOG] It has been changed user during networking process, error.")
- }
- }
- }
-
- func emptyTrash() {
-
- let serverUrlFileName = appDelegate.activeUrl + "/remote.php/dav/trashbin/" + appDelegate.activeUserID + "/trash"
- NCCommunication.shared.deleteFileOrFolder(serverUrlFileName) { (account, errorCode, errorDescription) in
- if errorCode == 0 && account == self.appDelegate.activeAccount {
- NCManageDatabase.sharedInstance.deleteTrash(fileId: nil, account: self.appDelegate.activeAccount)
- } else if errorCode != 0 {
- NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
- } else {
- print("[LOG] It has been changed user during networking process, error.")
- }
- self.loadDatasource()
- }
- }
-
- func deleteItem(with fileId: String) {
-
- guard let tableTrash = NCManageDatabase.sharedInstance.getTrashItem(fileId: fileId, account: appDelegate.activeAccount) else {
- return
- }
-
- let serverUrlFileName = appDelegate.activeUrl + tableTrash.filePath + tableTrash.fileName
-
- NCCommunication.shared.deleteFileOrFolder(serverUrlFileName) { (account, errorCode, errorDescription) in
- if errorCode == 0 && account == self.appDelegate.activeAccount {
- NCManageDatabase.sharedInstance.deleteTrash(fileId: fileId, account: account)
- self.loadDatasource()
- } else if errorCode != 0 {
- NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
- } else {
- print("[LOG] It has been changed user during networking process, error.")
- }
- }
- }
-
- func downloadThumbnail(with tableTrash: tableTrash, indexPath: IndexPath) {
-
- let fileNameLocalPath = CCUtility.getDirectoryProviderStorageIconOcId(tableTrash.fileId, fileNameView: tableTrash.fileName)!
-
- NCCommunication.shared.downloadPreview(fileNamePathOrFileId: tableTrash.fileId, fileNameLocalPath: fileNameLocalPath, width: Int(k_sizePreview), height: Int(k_sizePreview), downloadFromTrash: true) { (account, data, errorCode, errorDescription) in
-
- if errorCode == 0 && data != nil && account == self.appDelegate.activeAccount {
- if let cell = self.collectionView.cellForItem(at: indexPath), let image = UIImage.init(data: data!) {
- if cell is NCTrashListCell {
- (cell as! NCTrashListCell).imageItem.image = image
- } else if cell is NCGridCell {
- (cell as! NCGridCell).imageItem.image = image
- }
- }
- }
- }
- }
- }
|