From f2ddb604ce66f42302c20ba402e6488fe1c03967 Mon Sep 17 00:00:00 2001 From: An Tran Date: Sun, 14 Nov 2021 11:44:44 +0800 Subject: [PATCH] feat: Add reset and reload button to UserInterfaceToogleableNavigationAppController --- .../Scenarios/TypographyScenario.swift | 2 +- .../NavigationAppController.swift | 25 +++++++------ ...UserInterfaceToogleableAppController.swift | 36 +++++++++++++------ 3 files changed, 41 insertions(+), 22 deletions(-) diff --git a/Sample/Sample/Sources/Internal/Scenarios/TypographyScenario.swift b/Sample/Sample/Sources/Internal/Scenarios/TypographyScenario.swift index 8f206fc..0e001b7 100644 --- a/Sample/Sample/Sources/Internal/Scenarios/TypographyScenario.swift +++ b/Sample/Sample/Sources/Internal/Scenarios/TypographyScenario.swift @@ -11,7 +11,7 @@ public class TypographyScenario: AudienceTargetableScenario { public static let kind = ScenarioKind.designSystem public static var rootViewProvider: RootViewProviding { - UserInterfaceToogleableNavigationAppController { _ in + UserInterfaceToogleableNavigationAppController(withResetButton: true) { _ in ReloadableHostingViewController(rootView: ContentView()) } } diff --git a/Sources/Scenarios/AppController/NavigationAppController.swift b/Sources/Scenarios/AppController/NavigationAppController.swift index f16f9cd..d591212 100644 --- a/Sources/Scenarios/AppController/NavigationAppController.swift +++ b/Sources/Scenarios/AppController/NavigationAppController.swift @@ -7,20 +7,25 @@ import UIKit public class NavigationAppController: RootViewProviding { public var rootViewController: UIViewController - public init( + public convenience init( withResetButton: Bool = false, withRefreshButton: Bool = false, makeChild: (UINavigationController) -> UIViewController ) { - var navigationController: UINavigationController - if withResetButton || withRefreshButton { - navigationController = ResetableRefreshableNavigationController( - hasResetButton: withResetButton, - hasRefreshButton: withRefreshButton - ) - } else { - navigationController = UINavigationController() - } + let navigationController = ResetableRefreshableNavigationController( + hasResetButton: withResetButton, + hasRefreshButton: withRefreshButton + ) + self.init( + navigationController: navigationController, + makeChild: makeChild + ) + } + + public init( + navigationController: UINavigationController, + makeChild: (UINavigationController) -> UIViewController + ) { navigationController.pushViewController(makeChild(navigationController), animated: false) rootViewController = navigationController } diff --git a/Sources/Scenarios/AppController/UserInterfaceToogleableAppController.swift b/Sources/Scenarios/AppController/UserInterfaceToogleableAppController.swift index 7b7a96f..527c898 100644 --- a/Sources/Scenarios/AppController/UserInterfaceToogleableAppController.swift +++ b/Sources/Scenarios/AppController/UserInterfaceToogleableAppController.swift @@ -7,20 +7,26 @@ import UIKit // swiftlint:disable type_name @available(iOS 13.0, *) -public struct UserInterfaceToogleableNavigationAppController: RootViewProviding { - public var rootViewController: UIViewController +public class UserInterfaceToogleableNavigationAppController: NavigationAppController { public init( + withResetButton: Bool = false, + withRefreshButton: Bool = false, makeChild: (UINavigationController) -> ReloadableViewController ) { - let navigationController = UserInterfaceToogleableNavigationController() - navigationController.pushViewController(makeChild(navigationController), animated: false) - rootViewController = navigationController + let navigationController = UserInterfaceToogleableNavigationController( + hasResetButton: withResetButton, + hasRefreshButton: withResetButton + ) + super.init( + navigationController: navigationController, + makeChild: makeChild + ) } } @available(iOS 13.0, *) -class UserInterfaceToogleableNavigationController: UINavigationController, UINavigationControllerDelegate { +class UserInterfaceToogleableNavigationController: ResetableRefreshableNavigationController { private lazy var toggleInterfaceStyleButton: UIBarButtonItem = { UIBarButtonItem( @@ -28,11 +34,14 @@ class UserInterfaceToogleableNavigationController: UINavigationController, UINav style: .plain, target: self, action: #selector(self.didToggleAppearanceMode) ) }() - - init() { - super.init(nibName: nil, bundle: nil) + + override init( + hasResetButton: Bool = false, + hasRefreshButton: Bool = false + ) { + super.init(hasResetButton: hasResetButton, hasRefreshButton: hasRefreshButton) } - + @available(*, unavailable) required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") @@ -43,7 +52,12 @@ class UserInterfaceToogleableNavigationController: UINavigationController, UINav delegate = self } - func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) { + override func navigationController( + _ navigationController: UINavigationController, + willShow viewController: UIViewController, + animated: Bool + ) { + super.navigationController(navigationController, willShow: viewController, animated: animated) viewController.navigationItem.rightBarButtonItem = toggleInterfaceStyleButton }