123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730 |
- //
- // NCOffline.swift
- // Nextcloud
- //
- // Created by Marino Faggiana on 24/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
- class NCOffline: UIViewController, UIGestureRecognizerDelegate, NCListCellDelegate, NCGridCellDelegate, NCSectionHeaderMenuDelegate, DropdownMenuDelegate, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate {
-
- @IBOutlet fileprivate weak var collectionView: UICollectionView!
- var titleCurrentFolder = NSLocalizedString("_manage_file_offline_", comment: "")
- var serverUrl = ""
-
- private let appDelegate = UIApplication.shared.delegate as! AppDelegate
-
- private var metadataPush: tableMetadata?
- private var isEditMode = false
- private var selectocId = [String]()
-
- private var sectionDatasource = CCSectionDataSourceMetadata()
-
- private var typeLayout = ""
- private var datasourceSorted = ""
- private var datasourceAscending = true
- private var datasourceGroupBy = ""
- private var datasourceDirectoryOnTop = false
-
- private var autoUploadFileName = ""
- private var autoUploadDirectory = ""
-
- private var listLayout: NCListLayout!
- private var gridLayout: NCGridLayout!
-
- private let headerMenuHeight: CGFloat = 50
- private let sectionHeaderHeight: CGFloat = 20
- private let footerHeight: CGFloat = 50
- private let refreshControl = UIRefreshControl()
-
- required init?(coder aDecoder: NSCoder) {
- super.init(coder: aDecoder)
-
- appDelegate.activeOffline = self
- }
-
- override func viewDidLoad() {
- super.viewDidLoad()
-
- // Cell
- collectionView.register(UINib.init(nibName: "NCListCell", bundle: nil), forCellWithReuseIdentifier: "listCell")
- collectionView.register(UINib.init(nibName: "NCGridCell", bundle: nil), forCellWithReuseIdentifier: "gridCell")
-
- // Header
- collectionView.register(UINib.init(nibName: "NCSectionHeaderMenu", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "sectionHeaderMenu")
- collectionView.register(UINib.init(nibName: "NCSectionHeader", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "sectionHeader")
-
- // Footer
- collectionView.register(UINib.init(nibName: "NCSectionFooter", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "sectionFooter")
-
- collectionView.alwaysBounceVertical = true
- listLayout = NCListLayout()
- gridLayout = NCGridLayout()
-
- // Refresh Control
- collectionView.addSubview(refreshControl)
-
- // Configure Refresh Control
- refreshControl.tintColor = NCBrandColor.sharedInstance.brandText
- refreshControl.backgroundColor = NCBrandColor.sharedInstance.brand
- refreshControl.addTarget(self, action: #selector(loadDatasource), for: .valueChanged)
-
- // empty Data Source
- self.collectionView.emptyDataSetDelegate = self
- self.collectionView.emptyDataSetSource = self
-
- // 3D Touch peek and pop
- if traitCollection.forceTouchCapability == .available {
- registerForPreviewing(with: self, sourceView: view)
- }
-
- NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: k_notificationCenter_changeTheming), object: nil)
- NotificationCenter.default.addObserver(self, selector: #selector(deleteFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_deleteFile), 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_offline)
-
- // get auto upload folder
- autoUploadFileName = NCManageDatabase.sharedInstance.getAccountAutoUploadFileName()
- autoUploadDirectory = NCManageDatabase.sharedInstance.getAccountAutoUploadDirectory(appDelegate.activeUrl)
-
- if typeLayout == k_layout_list {
- collectionView.collectionViewLayout = listLayout
- } else {
- collectionView.collectionViewLayout = gridLayout
- }
-
- loadDatasource()
- }
-
- override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
- super.viewWillTransition(to: size, with: coordinator)
-
- coordinator.animate(alongsideTransition: nil) { _ in
- self.collectionView.collectionViewLayout.invalidateLayout()
- }
- }
-
- //MARK: - NotificationCenter
- @objc func deleteFile(_ notification: NSNotification) {
- if self.view?.window == nil { return }
-
- if let userInfo = notification.userInfo as NSDictionary? {
- if let errorCode = userInfo["errorCode"] as? Int, let errorDescription = userInfo["errorDescription"] as? String {
- if errorCode == 0 {
- self.loadDatasource()
- } else {
- NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
- }
- }
- }
- }
-
- @objc func changeTheming() {
- appDelegate.changeTheming(self, tableView: nil, collectionView: collectionView, form: false)
- }
-
- // MARK: DZNEmpty
-
- func backgroundColor(forEmptyDataSet scrollView: UIScrollView) -> UIColor? {
- return NCBrandColor.sharedInstance.backgroundView
- }
-
- func image(forEmptyDataSet scrollView: UIScrollView) -> UIImage? {
- return CCGraphics.changeThemingColorImage(UIImage.init(named: "folder"), width: 300, height: 300, color: NCBrandColor.sharedInstance.brandElement)
- }
-
- func title(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
- let text = "\n"+NSLocalizedString("_files_no_files_", comment: "")
- let attributes = [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 20), NSAttributedString.Key.foregroundColor: UIColor.lightGray]
- return NSAttributedString.init(string: text, attributes: attributes)
- }
-
- func emptyDataSetShouldAllowScroll(_ scrollView: UIScrollView) -> Bool {
- return true
- }
-
- // MARK: TAP EVENT
-
- func tapSwitchHeader(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_offline, 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_offline, layout: typeLayout, sort: datasourceSorted, ascending: datasourceAscending, groupBy: datasourceGroupBy, directoryOnTop: datasourceDirectoryOnTop)
- }
- }
-
- func tapOrderHeader(sender: Any) {
-
- var menuView: DropdownMenu?
- var selectedIndexPath = [IndexPath()]
-
- let item1 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortFileNameAZ"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_name_a_z_", comment: ""))
- let item2 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortFileNameZA"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_name_z_a_", comment: ""))
- let item3 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortDateMoreRecent"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_date_more_recent_", comment: ""))
- let item4 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortDateLessRecent"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_date_less_recent_", comment: ""))
- let item5 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortSmallest"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_size_smallest_", comment: ""))
- let item6 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortLargest"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_size_largest_", comment: ""))
-
- switch datasourceSorted {
- case "fileName":
- if datasourceAscending == true { item1.style = .highlight; selectedIndexPath.append(IndexPath(row: 0, section: 0)) }
- if datasourceAscending == false { item2.style = .highlight; selectedIndexPath.append(IndexPath(row: 1, section: 0)) }
- case "date":
- if datasourceAscending == false { item3.style = .highlight; selectedIndexPath.append(IndexPath(row: 2, section: 0)) }
- if datasourceAscending == true { item4.style = .highlight; selectedIndexPath.append(IndexPath(row: 3, section: 0)) }
- case "size":
- if datasourceAscending == true { item5.style = .highlight; selectedIndexPath.append(IndexPath(row: 4, section: 0)) }
- if datasourceAscending == false { item6.style = .highlight; selectedIndexPath.append(IndexPath(row: 5, section: 0)) }
- default:
- ()
- }
-
- let item7 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "MenuGroupByAlphabetic"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_group_alphabetic_no_", comment: ""))
- let item8 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "MenuGroupByFile"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_group_typefile_no_", comment: ""))
- let item9 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "MenuGroupByDate"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_group_date_no_", comment: ""))
-
- switch datasourceGroupBy {
- case "alphabetic":
- item7.style = .highlight; selectedIndexPath.append(IndexPath(row: 0, section: 1))
- case "typefile":
- item8.style = .highlight; selectedIndexPath.append(IndexPath(row: 1, section: 1))
- case "date":
- item9.style = .highlight; selectedIndexPath.append(IndexPath(row: 2, section: 1))
- default:
- ()
- }
-
- let item10 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "foldersOnTop"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_directory_on_top_no_", comment: ""))
-
- if datasourceDirectoryOnTop {
- item10.style = .highlight; selectedIndexPath.append(IndexPath(row: 0, section: 2))
- }
-
- let sectionOrder = DropdownSection(sectionIdentifier: "", items: [item1, item2, item3, item4, item5, item6])
- let sectionGroupBy = DropdownSection(sectionIdentifier: "", items: [item7, item8, item9])
- let sectionFolderOnTop = DropdownSection(sectionIdentifier: "", items: [item10])
-
- menuView = DropdownMenu(navigationController: self.navigationController!, sections: [sectionOrder, sectionGroupBy, sectionFolderOnTop], selectedIndexPath: selectedIndexPath)
- menuView?.token = "tapOrderHeaderMenu"
- menuView?.delegate = self
- menuView?.rowHeight = 45
- menuView?.sectionHeaderHeight = 0.3
- menuView?.highlightColor = NCBrandColor.sharedInstance.brand
- menuView?.tableView.alwaysBounceVertical = false
- menuView?.tableViewSeperatorColor = NCBrandColor.sharedInstance.separator
- menuView?.tableViewBackgroundColor = NCBrandColor.sharedInstance.backgroundForm
- menuView?.cellBackgroundColor = NCBrandColor.sharedInstance.backgroundForm
- menuView?.textColor = NCBrandColor.sharedInstance.textView
-
- let header = (sender as? UIButton)?.superview
- let headerRect = self.collectionView.convert(header!.bounds, from: self.view)
- let menuOffsetY = headerRect.height - headerRect.origin.y - 2
- menuView?.topOffsetY = CGFloat(menuOffsetY)
-
- menuView?.showMenu()
- }
-
- func tapMoreHeader(sender: Any) {
-
- }
-
- func tapMoreListItem(with objectId: String, sender: Any) {
- tapMoreGridItem(with: objectId, sender: sender)
- }
-
- func tapShareListItem(with objectId: String, sender: Any) {
-
- guard let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "ocId == %@", objectId)) else {
- return
- }
-
- NCMainCommon.sharedInstance.openShare(ViewController: self, metadata: metadata, indexPage: 2)
- }
-
- func tapMoreGridItem(with objectId: String, sender: Any) {
-
- guard let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "ocId == %@", objectId)) else {
- return
- }
-
- if !isEditMode {
- let mainMenuViewController = UIStoryboard.init(name: "NCMenu", bundle: nil).instantiateViewController(withIdentifier: "NCMainMenuTableViewController") as! NCMainMenuTableViewController
- var actions = [NCMenuAction]()
- var iconHeader: UIImage!
- if let icon = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, fileNameView: metadata.fileNameView)) {
- iconHeader = icon
- } else {
- iconHeader = UIImage(named: metadata.iconName)
- }
- actions.append(
- NCMenuAction(
- title: metadata.fileNameView,
- icon: iconHeader,
- action: nil
- )
- )
- actions.append(
- NCMenuAction(
- title: NSLocalizedString("_remove_available_offline_", comment: ""),
- icon: CCGraphics.changeThemingColorImage(UIImage(named: "offline"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
- action: { menuAction in
- if metadata.directory {
- NCManageDatabase.sharedInstance.setDirectory(serverUrl: CCUtility.stringAppendServerUrl(metadata.serverUrl, addFileName: metadata.fileName)!, offline: false, account: self.appDelegate.activeAccount)
- } else {
- NCManageDatabase.sharedInstance.setLocalFile(ocId: metadata.ocId, offline: false)
- }
- self.loadDatasource()
- }
- )
- )
- actions.append(
- NCMenuAction(
- title: NSLocalizedString("_details_", comment: ""),
- icon: CCGraphics.changeThemingColorImage(UIImage(named: "details"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
- action: { menuAction in
- NCMainCommon.sharedInstance.openShare(ViewController: self, metadata: metadata, indexPage: 0)
- }
- )
- )
- actions.append(
- NCMenuAction(
- title: NSLocalizedString("_delete_", comment: ""),
- icon: CCGraphics.changeThemingColorImage(UIImage(named: "trash"), width: 50, height: 50, color: .red),
- action: { menuAction in
- NCNetworking.shared.deleteMetadata(metadata, account: self.appDelegate.activeAccount, url: self.appDelegate.activeUrl) { (errorCode, errorDescription) in }
- }
- )
- )
- 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: DROP-DOWN-MENU
- func dropdownMenu(_ dropdownMenu: DropdownMenu, didSelectRowAt indexPath: IndexPath) {
-
- if dropdownMenu.token == "tapOrderHeaderMenu" {
-
- switch indexPath.section {
-
- case 0: switch indexPath.row {
-
- case 0: datasourceSorted = "fileName"; datasourceAscending = true
- case 1: datasourceSorted = "fileName"; datasourceAscending = false
-
- case 2: datasourceSorted = "date"; datasourceAscending = false
- case 3: datasourceSorted = "date"; datasourceAscending = true
-
- case 4: datasourceSorted = "size"; datasourceAscending = true
- case 5: datasourceSorted = "size"; datasourceAscending = false
-
- default: ()
- }
-
- case 1: switch indexPath.row {
-
- case 0:
- if datasourceGroupBy == "alphabetic" {
- datasourceGroupBy = "none"
- } else {
- datasourceGroupBy = "alphabetic"
- }
- case 1:
- if datasourceGroupBy == "typefile" {
- datasourceGroupBy = "none"
- } else {
- datasourceGroupBy = "typefile"
- }
- case 2:
- if datasourceGroupBy == "date" {
- datasourceGroupBy = "none"
- } else {
- datasourceGroupBy = "date"
- }
-
- default: ()
- }
- case 2:
- if datasourceDirectoryOnTop {
- datasourceDirectoryOnTop = false
- } else {
- datasourceDirectoryOnTop = true
- }
- default: ()
- }
-
- NCUtility.sharedInstance.setLayoutForView(key: k_layout_view_offline, layout: typeLayout, sort: datasourceSorted, ascending: datasourceAscending, groupBy: datasourceGroupBy, directoryOnTop: datasourceDirectoryOnTop)
- loadDatasource()
- }
-
- if dropdownMenu.token == "tapMoreHeaderMenu" {
-
- }
-
- if dropdownMenu.token == "tapMoreHeaderMenuSelect" {
-
- }
- }
-
- // MARK: SEGUE
-
- override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
-
- let photoDataSource: NSMutableArray = []
-
- for ocId: String in sectionDatasource.allOcId as! [String] {
- let metadata = sectionDatasource.allRecordsDataSource.object(forKey: ocId) as! tableMetadata
- if metadata.typeFile == k_metadataTypeFile_image {
- photoDataSource.add(metadata)
- }
- }
-
- if let segueNavigationController = segue.destination as? UINavigationController {
- if let segueViewController = segueNavigationController.topViewController as? NCDetailViewController {
-
- segueViewController.metadata = metadataPush
- segueViewController.offlineFilterImage = true
- }
- }
- }
- }
- // MARK: - 3D Touch peek and pop
- extension NCOffline: UIViewControllerPreviewingDelegate {
-
- func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
-
- guard let point = collectionView?.convert(location, from: collectionView?.superview) else { return nil }
- guard let indexPath = collectionView?.indexPathForItem(at: point) else { return nil }
- guard let metadata = NCMainCommon.sharedInstance.getMetadataFromSectionDataSourceIndexPath(indexPath, sectionDataSource: sectionDatasource) else { return nil }
- guard let viewController = UIStoryboard(name: "CCPeekPop", bundle: nil).instantiateViewController(withIdentifier: "PeekPopImagePreview") as? CCPeekPop else { return nil }
- viewController.metadata = metadata
- if typeLayout == k_layout_grid {
- guard let cell = collectionView?.cellForItem(at: indexPath) as? NCGridCell else { return nil }
- previewingContext.sourceRect = cell.frame
- viewController.imageFile = cell.imageItem.image
- } else {
- guard let cell = collectionView?.cellForItem(at: indexPath) as? NCListCell else { return nil }
- previewingContext.sourceRect = cell.frame
- viewController.imageFile = cell.imageItem.image
- }
-
- viewController.showOpenIn = true
- viewController.showOpenQuickLook = NCUtility.sharedInstance.isQuickLookDisplayable(metadata: metadata)
- viewController.showShare = false
-
- return viewController
- }
-
- func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {
-
- guard let indexPath = collectionView?.indexPathForItem(at: previewingContext.sourceRect.origin) else { return }
-
- collectionView(collectionView, didSelectItemAt: indexPath)
- }
- }
- // MARK: - Collection View
- extension NCOffline: UICollectionViewDelegate {
- func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
-
- guard let metadata = NCMainCommon.sharedInstance.getMetadataFromSectionDataSourceIndexPath(indexPath, sectionDataSource: sectionDatasource) else {
- return
- }
- metadataPush = metadata
-
- if isEditMode {
- if let index = selectocId.firstIndex(of: metadata.ocId) {
- selectocId.remove(at: index)
- } else {
- selectocId.append(metadata.ocId)
- }
- collectionView.reloadItems(at: [indexPath])
- return
- }
-
- if metadata.directory {
-
- guard let serverUrlPush = CCUtility.stringAppendServerUrl(metadataPush!.serverUrl, addFileName: metadataPush!.fileName) else { return }
- let ncOffline:NCOffline = UIStoryboard(name: "NCOffline", bundle: nil).instantiateInitialViewController() as! NCOffline
-
- ncOffline.serverUrl = serverUrlPush
- ncOffline.titleCurrentFolder = metadataPush!.fileNameView
-
- self.navigationController?.pushViewController(ncOffline, animated: true)
-
- } else {
-
- performSegue(withIdentifier: "segueDetail", sender: self)
- }
- }
- }
- extension NCOffline: UICollectionViewDataSource {
- func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
-
- if (indexPath.section == 0) {
-
- if kind == UICollectionView.elementKindSectionHeader {
-
- let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionHeaderMenu", for: indexPath) as! NCSectionHeaderMenu
-
- if collectionView.collectionViewLayout == gridLayout {
- header.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchList"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
- } else {
- header.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchGrid"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
- }
-
- header.delegate = self
- header.backgroundColor = NCBrandColor.sharedInstance.backgroundView
- header.separator.backgroundColor = NCBrandColor.sharedInstance.separator
- header.setStatusButton(count: sectionDatasource.allOcId.count)
- header.setTitleOrder(datasourceSorted: datasourceSorted, datasourceAscending: datasourceAscending)
-
- if datasourceGroupBy == "none" {
- header.labelSection.isHidden = true
- header.labelSectionHeightConstraint.constant = 0
- } else {
- header.labelSection.isHidden = false
- header.setTitleLabel(sectionDatasource: sectionDatasource, section: indexPath.section)
- header.labelSectionHeightConstraint.constant = sectionHeaderHeight
- }
-
- return header
-
- } else {
-
- let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFooter", for: indexPath) as! NCSectionFooter
-
- footer.setTitleLabel(sectionDatasource: sectionDatasource)
-
- return footer
- }
-
- } else {
-
- if kind == UICollectionView.elementKindSectionHeader {
-
- let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionHeader", for: indexPath) as! NCSectionHeader
-
- header.setTitleLabel(sectionDatasource: sectionDatasource, section: indexPath.section)
-
- return header
-
- } else {
-
- let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFooter", for: indexPath) as! NCSectionFooter
-
- footer.setTitleLabel(sectionDatasource: sectionDatasource)
-
- return footer
- }
- }
- }
-
- func numberOfSections(in collectionView: UICollectionView) -> Int {
- let sections = sectionDatasource.sectionArrayRow.allKeys.count
- return sections
- }
-
- func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
- let key = sectionDatasource.sections.object(at: section)
- let datasource = sectionDatasource.sectionArrayRow.object(forKey: key) as! [tableMetadata]
- return datasource.count
- }
-
- func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
-
- let cell: UICollectionViewCell
-
- guard let metadata = NCMainCommon.sharedInstance.getMetadataFromSectionDataSourceIndexPath(indexPath, sectionDataSource: sectionDatasource) else {
- return collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as! NCListCell
- }
-
- if typeLayout == k_layout_grid {
- cell = collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as! NCGridCell
- } else {
- cell = collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as! NCListCell
- (cell as! NCListCell).separator.backgroundColor = NCBrandColor.sharedInstance.separator
- }
-
- let shares = NCManageDatabase.sharedInstance.getTableShares(account: metadata.account, serverUrl: metadata.serverUrl, fileName: metadata.fileName)
-
- NCMainCommon.sharedInstance.collectionViewCellForItemAt(indexPath, collectionView: collectionView, cell: cell, metadata: metadata, metadataFolder: nil, serverUrl: metadata.serverUrl, isEditMode: isEditMode, selectocId: selectocId, autoUploadFileName: autoUploadFileName, autoUploadDirectory: autoUploadDirectory, hideButtonMore: false, downloadThumbnail: true, shares: shares, source: self)
-
- return cell
- }
- }
- extension NCOffline: UICollectionViewDelegateFlowLayout {
- func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
- if section == 0 {
- if datasourceGroupBy == "none" {
- return CGSize(width: collectionView.frame.width, height: headerMenuHeight)
- } else {
- return CGSize(width: collectionView.frame.width, height: headerMenuHeight + sectionHeaderHeight)
- }
- } else {
- return CGSize(width: collectionView.frame.width, height: sectionHeaderHeight)
- }
- }
-
- func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
- let sections = sectionDatasource.sectionArrayRow.allKeys.count
- if (section == sections - 1) {
- return CGSize(width: collectionView.frame.width, height: footerHeight)
- } else {
- return CGSize(width: collectionView.frame.width, height: 0)
- }
- }
- }
- // MARK: - NC API & Algorithm
- extension NCOffline {
- @objc func loadDatasource() {
-
- var ocIds = [String]()
- sectionDatasource = CCSectionDataSourceMetadata()
-
- if serverUrl == "" {
-
- if let directories = NCManageDatabase.sharedInstance.getTablesDirectory(predicate: NSPredicate(format: "account == %@ AND offline == true", appDelegate.activeAccount), sorted: "serverUrl", ascending: true) {
- for directory: tableDirectory in directories {
- ocIds.append(directory.ocId)
- }
- }
-
- if let files = NCManageDatabase.sharedInstance.getTableLocalFiles(predicate: NSPredicate(format: "account == %@ AND offline == true", appDelegate.activeAccount), sorted: "fileName", ascending: true) {
- for file: tableLocalFile in files {
- ocIds.append(file.ocId)
- }
- }
-
- if let metadatas = NCManageDatabase.sharedInstance.getMetadatas(predicate: NSPredicate(format: "account == %@ AND ocId IN %@", appDelegate.activeAccount, ocIds), sorted: nil, ascending: false) {
-
- sectionDatasource = CCSectionMetadata.creataDataSourseSectionMetadata(metadatas, listProgressMetadata: nil, groupByField: datasourceGroupBy, filterTypeFileImage: false, filterTypeFileVideo: false, filterLivePhoto: true, sorted: datasourceSorted, ascending: datasourceAscending, activeAccount: appDelegate.activeAccount)
- }
-
- } else {
-
- if let metadatas = NCManageDatabase.sharedInstance.getMetadatas(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.activeAccount, serverUrl), sorted: nil, ascending: false) {
-
- sectionDatasource = CCSectionMetadata.creataDataSourseSectionMetadata(metadatas, listProgressMetadata: nil, groupByField: datasourceGroupBy, filterTypeFileImage: false, filterTypeFileVideo: false, filterLivePhoto: true, sorted: datasourceSorted, ascending: datasourceAscending, activeAccount: appDelegate.activeAccount)
- }
- }
-
- self.refreshControl.endRefreshing()
-
- collectionView.reloadData()
- }
-
- /*
- func deleteItem(with metadata: tableMetadata, sender: Any) {
- let mainMenuViewController = UIStoryboard.init(name: "NCMenu", bundle: nil).instantiateViewController(withIdentifier: "NCMainMenuTableViewController") as! NCMainMenuTableViewController
- var actions = [NCMenuAction]()
- actions.append(
- NCMenuAction(
- title: NSLocalizedString("_delete_", comment: ""),
- icon: CCGraphics.changeThemingColorImage(UIImage(named: "trash"), width: 50, height: 50, color: .red),
- action: { menuAction in
- NCNetworking.sharedInstance.deleteMetadata(metadata, user: self.appDelegate.activeUser, userID: self.appDelegate.activeUserID, password: self.appDelegate.activePassword, url: self.appDelegate.activeUrl) { (errorCode, errorDescription) in }
- }
- )
- )
- actions.append(
- NCMenuAction(
- title: NSLocalizedString("_cancel_", comment: ""),
- icon: CCGraphics.changeThemingColorImage(UIImage(named: "cancel"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
- action: { menuAction in }
- )
- )
- mainMenuViewController.actions = actions
- let menuPanelController = NCMenuPanelController()
- menuPanelController.parentPresenter = self
- menuPanelController.delegate = mainMenuViewController
- menuPanelController.set(contentViewController: mainMenuViewController)
- menuPanelController.track(scrollView: mainMenuViewController.tableView)
- }
- */
- }
|