From 4011ea849ecb3b3d5dd3d41effc1554f38f6ad8c Mon Sep 17 00:00:00 2001 From: Diogo Autilio Date: Sat, 23 Mar 2024 08:22:16 -0300 Subject: [PATCH] Add PopoverMenuManager --- .../Presenter/UserCollectionPresenter.swift | 24 +++-------- .../Classes/Utils/PopoverMenuManager.swift | 41 +++++++++++++++++++ 2 files changed, 47 insertions(+), 18 deletions(-) create mode 100644 SWDestinyTrades/Classes/Utils/PopoverMenuManager.swift diff --git a/SWDestinyTrades/Classes/UserCollection/Presenter/UserCollectionPresenter.swift b/SWDestinyTrades/Classes/UserCollection/Presenter/UserCollectionPresenter.swift index f5e8ea6a..b1652040 100644 --- a/SWDestinyTrades/Classes/UserCollection/Presenter/UserCollectionPresenter.swift +++ b/SWDestinyTrades/Classes/UserCollection/Presenter/UserCollectionPresenter.swift @@ -7,7 +7,6 @@ // import Foundation -import FTPopOverMenu import UIKit protocol UserCollectionPresenterProtocol { @@ -69,16 +68,6 @@ final class UserCollectionPresenter: UserCollectionPresenterProtocol { try? database?.save(object: object) } - private func popOverMenuConfiguration() -> FTConfiguration { - let config = FTConfiguration() - config.backgoundTintColor = ColorPalette.appTheme - config.borderColor = ColorPalette.appTheme - config.menuSeparatorColor = .lightGray - config.textColor = .white - config.textAlignment = .center - return config - } - private func getUserCollection() -> UserCollectionDTO { var user = UserCollectionDTO() try? database?.fetch(UserCollectionDTO.self, predicate: nil, sorted: nil) { [weak self] results in @@ -125,13 +114,12 @@ final class UserCollectionPresenter: UserCollectionPresenterProtocol { @objc private func sort(_ sender: UIBarButtonItem, event: UIEvent) { - FTPopOverMenu.showForEvent(event: event, - with: [L10n.aToZ, L10n.cardNumber, L10n.color], - config: popOverMenuConfiguration(), - done: { [weak self] selectedIndex in - self?.controller?.sort(selectedIndex) - self?.currentSortIndex = selectedIndex - }, cancel: {}) + let manager = PopoverMenuManager() + manager.showPopoverMenu(forEvent: event, + with: [L10n.aToZ, L10n.cardNumber, L10n.color]) { [weak self] selectedIndex in + self?.controller?.sort(selectedIndex) + self?.currentSortIndex = selectedIndex + } } } diff --git a/SWDestinyTrades/Classes/Utils/PopoverMenuManager.swift b/SWDestinyTrades/Classes/Utils/PopoverMenuManager.swift new file mode 100644 index 00000000..906fb664 --- /dev/null +++ b/SWDestinyTrades/Classes/Utils/PopoverMenuManager.swift @@ -0,0 +1,41 @@ +// +// PopoverMenuManager.swift +// SWDestinyTrades +// +// Created by Diogo Autilio on 23/03/24. +// Copyright © 2024 Diogo Autilio. All rights reserved. +// + +import Foundation +import FTPopOverMenu +import UIKit + +protocol PopoverMenuManagerType { + func showPopoverMenu(forEvent event: UIEvent, with menuArray: [String], done: ((NSInteger) -> Void)?, cancel: (() -> Void)?) +} + +final class PopoverMenuManager: PopoverMenuManagerType { + + // Configures the appearance of the popover menu + private func appearance() -> FTConfiguration { + let config = FTConfiguration() + config.backgoundTintColor = ColorPalette.appTheme + config.borderColor = ColorPalette.appTheme + config.menuSeparatorColor = .lightGray + config.textColor = .white + config.textAlignment = .center + return config + } + + // Displays the popover menu for the given event + func showPopoverMenu(forEvent event: UIEvent, + with menuArray: [String], + done: ((NSInteger) -> Void)?, + cancel: (() -> Void)? = nil) { + FTPopOverMenu.showForEvent(event: event, + with: menuArray, + config: appearance(), + done: done, + cancel: cancel) + } +}