// // NCSortMenu.swift // Nextcloud // // Created by Marino Faggiana on 27/08/2020. // Copyright © 2020 Marino Faggiana. All rights reserved. // // Author Marino Faggiana // // 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 . // import UIKit import FloatingPanel import NCCommunication class NCSortMenu: NSObject { private var sortButton: UIButton? private var serverUrl: String = "" private var hideDirectoryOnTop: Bool? private var key = "" func toggleMenu(viewController: UIViewController, key: String, sortButton: UIButton?, serverUrl: String, hideDirectoryOnTop: Bool = false) { self.key = key self.sortButton = sortButton self.serverUrl = serverUrl self.hideDirectoryOnTop = hideDirectoryOnTop var layoutForView = NCUtility.shared.getLayoutForView(key: key, serverUrl: serverUrl) let menuViewController = UIStoryboard.init(name: "NCMenu", bundle: nil).instantiateInitialViewController() as! NCMenu var actions = [NCMenuAction]() var title = "" var icon = UIImage() if layoutForView.ascending { title = NSLocalizedString("_order_by_name_z_a_", comment: "") icon = UIImage(named: "sortFileNameZA")!.image(color: NCBrandColor.shared.gray, size: 50) } else { title = NSLocalizedString("_order_by_name_a_z_", comment: "") icon = UIImage(named: "sortFileNameAZ")!.image(color: NCBrandColor.shared.gray, size: 50) } actions.append( NCMenuAction( title: title, icon: icon, selected: layoutForView.sort == "fileName", on: layoutForView.sort == "fileName", action: { menuAction in layoutForView.sort = "fileName" layoutForView.ascending = !layoutForView.ascending self.actionMenu(layoutForView: layoutForView) } ) ) if layoutForView.ascending { title = NSLocalizedString("_order_by_date_more_recent_", comment: "") icon = UIImage(named: "sortDateMoreRecent")!.image(color: NCBrandColor.shared.gray, size: 50) } else { title = NSLocalizedString("_order_by_date_less_recent_", comment: "") icon = UIImage(named: "sortDateLessRecent")!.image(color: NCBrandColor.shared.gray, size: 50) } actions.append( NCMenuAction( title: title, icon: icon, selected: layoutForView.sort == "date", on: layoutForView.sort == "date", action: { menuAction in layoutForView.sort = "date" layoutForView.ascending = !layoutForView.ascending self.actionMenu(layoutForView: layoutForView) } ) ) if layoutForView.ascending { title = NSLocalizedString("_order_by_size_largest_", comment: "") icon = UIImage(named: "sortLargest")!.image(color: NCBrandColor.shared.gray, size: 50) } else { title = NSLocalizedString("_order_by_size_smallest_", comment: "") icon = UIImage(named: "sortSmallest")!.image(color: NCBrandColor.shared.gray, size: 50) } actions.append( NCMenuAction( title: title, icon: icon, selected: layoutForView.sort == "size", on: layoutForView.sort == "size", action: { menuAction in layoutForView.sort = "size" layoutForView.ascending = !layoutForView.ascending self.actionMenu(layoutForView: layoutForView) } ) ) if !hideDirectoryOnTop { actions.append( NCMenuAction( title: NSLocalizedString("_directory_on_top_no_", comment: ""), icon: UIImage(named: "foldersOnTop")!.image(color: NCBrandColor.shared.gray, size: 50), selected: layoutForView.directoryOnTop, on: layoutForView.directoryOnTop, action: { menuAction in layoutForView.directoryOnTop = !layoutForView.directoryOnTop self.actionMenu(layoutForView: layoutForView) } ) ) } menuViewController.actions = actions let menuPanelController = NCMenuPanelController() menuPanelController.parentPresenter = viewController menuPanelController.delegate = menuViewController menuPanelController.set(contentViewController: menuViewController) menuPanelController.track(scrollView: menuViewController.tableView) viewController.present(menuPanelController, animated: true, completion: nil) } func actionMenu(layoutForView: NCGlobal.layoutForViewType) { var layoutForView = layoutForView switch layoutForView.sort { case "fileName": layoutForView.titleButtonHeader = layoutForView.ascending ? "_sorted_by_name_a_z_" : "_sorted_by_name_z_a_" case "date": layoutForView.titleButtonHeader = layoutForView.ascending ? "_sorted_by_date_less_recent_" : "_sorted_by_date_more_recent_" case "size": layoutForView.titleButtonHeader = layoutForView.ascending ? "_sorted_by_size_smallest_" : "_sorted_by_size_largest_" default: break } self.sortButton?.setTitle(NSLocalizedString(layoutForView.titleButtonHeader, comment: ""), for: .normal) NCUtility.shared.setLayoutForView(key: key, serverUrl: serverUrl, layoutForView: layoutForView) NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataSource, userInfo: ["serverUrl":self.serverUrl]) } }