123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- //
- // NCSortMenu.swift
- // Nextcloud
- //
- // Created by Marino Faggiana on 27/08/2020.
- // Copyright © 2020 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 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]()
- actions.append(
- NCMenuAction(
- title: NSLocalizedString("_order_by_name_a_z_", comment: ""),
- icon: UIImage(named: "sortFileNameAZ")!.image(color: NCBrandColor.shared.gray, size: 50),
- onTitle: NSLocalizedString("_order_by_name_z_a_", comment: ""),
- onIcon: UIImage(named: "sortFileNameZA")!.image(color: NCBrandColor.shared.gray, size: 50),
- selected: layoutForView.sort == "fileName",
- on: layoutForView.sort == "fileName",
- action: { menuAction in
- if layoutForView.sort == "fileName" {
- layoutForView.ascending = !layoutForView.ascending
- } else {
- layoutForView.sort = "fileName"
- }
- self.actionMenu(layoutForView: layoutForView)
- }
- )
- )
- actions.append(
- NCMenuAction(
- title: NSLocalizedString("_order_by_date_more_recent_", comment: ""),
- icon: UIImage(named: "sortDateMoreRecent")!.image(color: NCBrandColor.shared.gray, size: 50),
- onTitle: NSLocalizedString("_order_by_date_less_recent_", comment: ""),
- onIcon: UIImage(named: "sortDateLessRecent")!.image(color: NCBrandColor.shared.gray, size: 50),
- selected: layoutForView.sort == "date",
- on: layoutForView.sort == "date",
- action: { menuAction in
- if layoutForView.sort == "date" {
- layoutForView.ascending = !layoutForView.ascending
- } else {
- layoutForView.sort = "date"
- }
- self.actionMenu(layoutForView: layoutForView)
- }
- )
- )
- actions.append(
- NCMenuAction(
- title: NSLocalizedString("_order_by_size_smallest_", comment: ""),
- icon: UIImage(named: "sortSmallest")!.image(color: NCBrandColor.shared.gray, size: 50),
- onTitle: NSLocalizedString("_order_by_size_largest_", comment: ""),
- onIcon: UIImage(named: "sortLargest")!.image(color: NCBrandColor.shared.gray, size: 50),
- selected: layoutForView.sort == "size",
- on: layoutForView.sort == "size",
- action: { menuAction in
- if layoutForView.sort == "size" {
- layoutForView.ascending = !layoutForView.ascending
- } else {
- layoutForView.sort = "size"
- }
- 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])
- }
- }
|