Skip to content

Commit

Permalink
revert some deprecation and fix default config not applied to tappabl…
Browse files Browse the repository at this point in the history
…eView subclass
  • Loading branch information
lkzhao committed Mar 11, 2024
1 parent e7ab6ee commit 711ced1
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 28 deletions.
15 changes: 3 additions & 12 deletions Sources/UIComponent/Components/View/PrimaryMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,7 @@ import UIKit
@available(iOS 14.0, *)
public struct PrimaryMenuConfig {
/// The default configuration for all PrimaryMenu instances.
/// This static property is deprecated and you should use `PrimaryMenuConfigEnvironmentKey.defaultValue` instead.
@available(*, deprecated, message: "Use PrimaryMenuConfigEnvironmentKey.defaultValue instead")
public static var `default`: PrimaryMenuConfig {
get {
PrimaryMenuConfigEnvironmentKey.defaultValue
}
set {
PrimaryMenuConfigEnvironmentKey.defaultValue = newValue
}
}
public static var `default`: PrimaryMenuConfig = PrimaryMenuConfig(onHighlightChanged: nil, didTap: nil)

/// Closure to apply highlight state or animation to the TappableView.
public var onHighlightChanged: ((PrimaryMenu, Bool) -> Void)?
Expand Down Expand Up @@ -109,7 +100,7 @@ public class PrimaryMenu: UIControl {
public private(set) var isPressed: Bool = false {
didSet {
guard isPressed != oldValue else { return }
config?.onHighlightChanged?(self, isPressed)
(config ?? .default).onHighlightChanged?(self, isPressed)
}
}

Expand Down Expand Up @@ -157,7 +148,7 @@ public class PrimaryMenu: UIControl {
// MARK: - UIContextMenuInteractionDelegate

public override func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
config?.didTap?(self)
(config ?? .default).didTap?(self)
let menuConfiguration = UIContextMenuConfiguration(actionProvider: { [menu] suggested in
return menu
})
Expand Down
10 changes: 8 additions & 2 deletions Sources/UIComponent/Components/View/PrimaryMenuComponent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,14 @@ public struct PrimaryMenuRenderNode: RenderNode {
/// The key for accessing `PrimaryMenuConfig` values within the environment.
@available(iOS 14.0, *)
public struct PrimaryMenuConfigEnvironmentKey: EnvironmentKey {
/// The default value for `PrimaryMenuConfig`.
public static var defaultValue: PrimaryMenuConfig = PrimaryMenuConfig(onHighlightChanged: nil, didTap: nil)
public static var defaultValue: PrimaryMenuConfig {
get {
PrimaryMenuConfig.default
}
set {
PrimaryMenuConfig.default = newValue
}
}
}

/// An extension to provide easy access and modification of `PrimaryMenuConfig` within `EnvironmentValues`.
Expand Down
15 changes: 3 additions & 12 deletions Sources/UIComponent/Components/View/TappableView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,7 @@ import UIKit
/// It contains closures that can be used to customize the behavior of the view when it is tapped or highlighted.
public struct TappableViewConfig {
/// The default configuration for all TappableView instances.
/// This static property is deprecated and you should use `TappableViewConfigEnvironmentKey.defaultValue` instead.
@available(*, deprecated, message: "Use TappableViewConfigEnvironmentKey.defaultValue instead")
public static var `default`: TappableViewConfig {
get {
TappableViewConfigEnvironmentKey.defaultValue
}
set {
TappableViewConfigEnvironmentKey.defaultValue = newValue
}
}
public static var `default`: TappableViewConfig = TappableViewConfig(onHighlightChanged: nil, didTap: nil)

/// Closure to apply highlight state or animation to the TappableView.
public var onHighlightChanged: ((TappableView, Bool) -> Void)?
Expand Down Expand Up @@ -206,7 +197,7 @@ open class TappableView: ComponentView {
open var isHighlighted: Bool = false {
didSet {
guard isHighlighted != oldValue else { return }
config?.onHighlightChanged?(self, isHighlighted)
(config ?? .default).onHighlightChanged?(self, isHighlighted)
}
}

Expand Down Expand Up @@ -241,7 +232,7 @@ open class TappableView: ComponentView {

/// Called when a tap is recognized.
@objc open func didTap() {
config?.didTap?(self)
(config ?? .default).didTap?(self)
onTap?(self)
}

Expand Down
10 changes: 8 additions & 2 deletions Sources/UIComponent/Components/View/TappableViewComponent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,14 @@ public struct TappableViewRenderNode: RenderNode {

/// The key for accessing `TappableViewConfig` values within the environment.
public struct TappableViewConfigEnvironmentKey: EnvironmentKey {
/// The default value for `TappableViewConfig`.
public static var defaultValue: TappableViewConfig = TappableViewConfig(onHighlightChanged: nil, didTap: nil)
public static var defaultValue: TappableViewConfig {
get {
TappableViewConfig.default
}
set {
TappableViewConfig.default = newValue
}
}
}

/// An extension to provide easy access and modification of `TappableViewConfig` within `EnvironmentValues`.
Expand Down

0 comments on commit 711ced1

Please sign in to comment.