From 4778f2e70da1063e3f0b90e482efaf726ba34a6a Mon Sep 17 00:00:00 2001 From: Nikita Semenov Date: Fri, 18 Nov 2022 18:53:28 +0300 Subject: [PATCH] feat: code review notes --- TILogging/Sources/LoggingPresenter.swift | 7 ++-- .../LoggerList/LogsListViewController.swift | 10 +++-- .../LoggerWindow/LoggingTogglingWindow.swift | 16 ++------ .../UIViewController+TopVisible.swift | 40 +++++++++++++++++++ 4 files changed, 53 insertions(+), 20 deletions(-) create mode 100644 TIUIKitCore/Sources/Extensions/UIViewController/UIViewController+TopVisible.swift diff --git a/TILogging/Sources/LoggingPresenter.swift b/TILogging/Sources/LoggingPresenter.swift index a2c283ed..63884569 100644 --- a/TILogging/Sources/LoggingPresenter.swift +++ b/TILogging/Sources/LoggingPresenter.swift @@ -41,11 +41,12 @@ final public class LoggingPresenter { } /// Shows the UIWindow with a button that opens a logging list view. - public func addLogsButton(to scene: UIWindowScene? = nil) { - window = .init(frame: UIScreen.main.bounds) + public func addLogsButton(to scene: UIWindowScene? = nil, + windowRect rect: CGRect = UIScreen.main.bounds) { + + window = .init(frame: rect) showWindow(withScene: scene) - window?.setDefaultRootController() window?.set(isVisible: true) window?.set(isRegisteredForShakingEvent: false) } diff --git a/TILogging/Sources/Views/LoggerList/LogsListViewController.swift b/TILogging/Sources/Views/LoggerList/LogsListViewController.swift index 3c977dbb..7168311a 100644 --- a/TILogging/Sources/Views/LoggerList/LogsListViewController.swift +++ b/TILogging/Sources/Views/LoggerList/LogsListViewController.swift @@ -51,6 +51,7 @@ open class LogsListViewController: BaseInitializeableViewController, public typealias Snapshot = NSDiffableDataSourceSnapshot public let viewModel = LogsStorageViewModel() + public var logsFileExtension = "log" // MARK: - Life cycle @@ -117,7 +118,8 @@ open class LogsListViewController: BaseInitializeableViewController, viewModel.logsListView = self searchView.delegate = self - tableView.register(LogEntryTableViewCell.self, forCellReuseIdentifier: "identifier") + tableView.register(LogEntryTableViewCell.self, + forCellReuseIdentifier: LogEntryTableViewCell.reuseIdentifier) refreshControl.addTarget(self, action: #selector(reloadLogs), for: .valueChanged) shareButton.addTarget(self, action: #selector(shareLogs), for: .touchUpInside) } @@ -170,7 +172,7 @@ open class LogsListViewController: BaseInitializeableViewController, } timer?.invalidate() - timer = Timer.scheduledTimer(timeInterval: 3, + timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(filterLogsByText), userInfo: nil, @@ -182,14 +184,14 @@ open class LogsListViewController: BaseInitializeableViewController, } open func textFieldDidEndEditing(_ textField: UITextField) { - viewModel.fileCreator = .init(fileName: textField.text ?? "", fileExtension: "log") + viewModel.fileCreator = .init(fileName: textField.text ?? "", fileExtension: logsFileExtension) } // MARK: - Open methods open func createDataSource() -> DataSource { let cellProvider: DataSource.CellProvider = { collectionView, indexPath, itemIdentifier in - let cell = collectionView.dequeueReusableCell(withIdentifier: "identifier", + let cell = collectionView.dequeueReusableCell(withIdentifier: LogEntryTableViewCell.reuseIdentifier, for: indexPath) as? LogEntryTableViewCell cell?.configure(with: itemIdentifier) diff --git a/TILogging/Sources/Views/LoggerWindow/LoggingTogglingWindow.swift b/TILogging/Sources/Views/LoggerWindow/LoggingTogglingWindow.swift index d02514b8..92f66cbc 100644 --- a/TILogging/Sources/Views/LoggerWindow/LoggingTogglingWindow.swift +++ b/TILogging/Sources/Views/LoggerWindow/LoggingTogglingWindow.swift @@ -20,6 +20,7 @@ // THE SOFTWARE. // +import TIUIKitCore import UIKit @available(iOS 15, *) @@ -34,6 +35,7 @@ public final class LoggingTogglingWindow: UIWindow { override public init(frame: CGRect) { super.init(frame: frame) + rootViewController = loggingController windowLevel = .statusBar backgroundColor = .clear } @@ -70,10 +72,6 @@ public final class LoggingTogglingWindow: UIWindow { // MARK: - Public methdos - public func setDefaultRootController() { - rootViewController = loggingController - } - public func set(isRegisteredForShakingEvent: Bool) { loggingController.set(isRegisteredForShakingEvent: isRegisteredForShakingEvent) } @@ -97,7 +95,7 @@ public final class LoggingTogglingWindow: UIWindow { return } - let topViewController = getTopViewController(from: rootViewController) + let topViewController = rootViewController.topVisibleViewController topViewController.present(LogsListViewController(), animated: true, completion: { [weak self] in self?.loggingController.set(isLogsPresented: false) @@ -105,12 +103,4 @@ public final class LoggingTogglingWindow: UIWindow { loggingController.set(isLogsPresented: true) } - - private func getTopViewController(from viewController: UIViewController) -> UIViewController { - guard let presentedViewController = viewController.presentedViewController else { - return viewController - } - - return getTopViewController(from: presentedViewController) - } } diff --git a/TIUIKitCore/Sources/Extensions/UIViewController/UIViewController+TopVisible.swift b/TIUIKitCore/Sources/Extensions/UIViewController/UIViewController+TopVisible.swift new file mode 100644 index 00000000..3c8266e7 --- /dev/null +++ b/TIUIKitCore/Sources/Extensions/UIViewController/UIViewController+TopVisible.swift @@ -0,0 +1,40 @@ +// +// Copyright (c) 2022 Touch Instinct +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// + +import UIKit + +public extension UIViewController { + + /// Return top visible controller even if we have inner UI(Navigation/TabBar)Controller's inside + var topVisibleViewController: UIViewController { + switch self { + case let navController as UINavigationController: + return navController.visibleViewController?.topVisibleViewController ?? navController + + case let tabController as UITabBarController: + return tabController.selectedViewController?.topVisibleViewController ?? tabController + + default: + return self.presentedViewController?.topVisibleViewController ?? self + } + } +}