LeadKit/TIUIElements/Sources/Views/StatefulButton/StatefulButton+Appearance.s...

227 lines
8.7 KiB
Swift

//
// Copyright (c) 2023 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 TIUIKitCore
import UIKit
extension StatefulButton {
public typealias StateAppearance = UIButton.BaseAppearance<UIView.NoLayout, DefaultContentLayout>
public typealias StateAppearances = [StateKey: StateAppearance]
open class BaseAppearance<Layout: ViewLayout, ContentLayout: BaseContentLayout & ViewLayout>:
UIView.BaseAppearance<Layout> {
public static var defaultStateAppearances: StateAppearances {
[
.normal: UIButton.DefaultStateAppearance.defaultAppearance,
.highlighted: UIButton.DefaultStateAppearance.defaultAppearance,
.selected: UIButton.DefaultStateAppearance.defaultAppearance,
.disabled: UIButton.DefaultStateAppearance.defaultAppearance,
.loading: UIButton.DefaultStateAppearance.defaultAppearance
]
}
public var stateAppearances: StateAppearances
public init(layout: Layout = .defaultLayout,
background: UIViewBackground = UIViewColorBackground(color: .clear),
border: UIViewBorder = BaseUIViewBorder(),
shadow: UIViewShadow? = nil,
stateAppearances: StateAppearances = defaultStateAppearances) {
self.stateAppearances = stateAppearances
super.init(layout: layout,
background: background,
border: border,
shadow: shadow)
}
public func set(appearanceBuilder: (StateAppearance) -> Void, for states: [State]) {
for state in states {
stateAppearances[.init(controlState: state)] = DefaultStateAppearance.make(builder: appearanceBuilder)
}
}
}
open class BasePositionAppearance<Layout: ViewLayout, ContentLayout: BaseContentLayout & ViewLayout>:
BaseAppearance<Layout, ContentLayout> {
public var activityIndicatorPosition: ActivityIndicatorPosition
public init(layout: Layout = .defaultLayout,
background: UIViewBackground = UIViewColorBackground(color: .clear),
border: UIViewBorder = BaseUIViewBorder(),
shadow: UIViewShadow? = nil,
stateAppearances: StateAppearances = defaultStateAppearances,
activityIndicatorPosition: ActivityIndicatorPosition = .center) {
self.activityIndicatorPosition = activityIndicatorPosition
super.init(layout: layout,
background: background,
border: border,
shadow: shadow,
stateAppearances: stateAppearances)
}
}
public final class DefaultPositionAppearance: BasePositionAppearance<DefaultWrappedLayout, DefaultContentLayout>,
WrappedViewAppearance {
public static var defaultAppearance: Self {
Self()
}
}
@available(iOS 15.0, *)
open class BasePlacementAppearance<Layout: ViewLayout, ContentLayout: BaseContentLayout & ViewLayout>:
BaseAppearance<Layout, ContentLayout> {
public var activityIndicatorPlacement: ActivityIndicatorPlacement
public init(layout: Layout = .defaultLayout,
background: UIViewBackground = UIViewColorBackground(color: .clear),
border: UIViewBorder = BaseUIViewBorder(),
shadow: UIViewShadow? = nil,
stateAppearances: StateAppearances = defaultStateAppearances,
activityIndicatorPlacement: ActivityIndicatorPlacement = .center) {
self.activityIndicatorPlacement = activityIndicatorPlacement
super.init(layout: layout,
background: background,
border: border,
shadow: shadow,
stateAppearances: stateAppearances)
}
}
@available(iOS 15.0, *)
public final class DefaultPlacementAppearance: BasePlacementAppearance<DefaultWrappedLayout, DefaultContentLayout>,
WrappedViewAppearance {
public static var defaultAppearance: Self {
Self()
}
}
}
extension StatefulButton {
public func configureBaseStatefulButton(appearance: BaseAppearance<some ViewLayout, some BaseContentLayout>) {
onStateChanged = { [weak self] in
if let stateAppearance = appearance.stateAppearances[.init(controlState: $0)] {
self?.configureUIButton(appearance: stateAppearance, for: $0)
} else if $0 != .normal, let stateAppearance = appearance.stateAppearances[.normal] {
self?.configureUIButton(appearance: stateAppearance, for: .normal)
}
}
onStateChanged?(state)
}
public func configureStatefulButton(appearance: BasePositionAppearance<some ViewLayout, some BaseContentLayout>) {
configureBaseStatefulButton(appearance: appearance)
let baseOnStateChanged = onStateChanged
onStateChanged = { [weak self] in
baseOnStateChanged?($0)
self?.configureStatefulButtonActivityIndicator(appearance: appearance)
}
onStateChanged?(state)
}
private func configureStatefulButtonActivityIndicator(appearance: BasePositionAppearance<some ViewLayout,
some BaseContentLayout>) {
switch appearance.activityIndicatorPosition {
case .beforeTitle:
activityIndicatorShouldCenterInView = false
let transparentImage = UIGraphicsImageRenderer(size: activityIndicatorSize)
.image { _ in }
super.setImage(transparentImage, for: .loading)
case .center:
activityIndicatorShouldCenterInView = true
super.setImage(.init(), for: .loading)
}
if #available(iOS 15.0, *) {
var config = configuration ?? .plain()
if case let .beforeTitle(padding) = appearance.activityIndicatorPosition {
config.imagePlacement = .leading
config.imagePadding = padding
}
configuration = config
} else {
if case let .beforeTitle(padding) = appearance.activityIndicatorPosition {
imageEdgeInsets.right += padding
}
}
}
@available(iOS 15.0, *)
public func configureStatefulButton(appearance: BasePlacementAppearance<some ViewLayout, some BaseContentLayout>) {
configureBaseStatefulButton(appearance: appearance)
let baseOnStateChanged = onStateChanged
onStateChanged = { [weak self] in
baseOnStateChanged?($0)
self?.configureActivityIndicator(appearance: appearance)
}
onStateChanged?(state)
}
@available(iOS 15.0, *)
private func configureActivityIndicator(appearance: BasePlacementAppearance<some ViewLayout, some BaseContentLayout>) {
var config = configuration ?? .plain()
switch appearance.activityIndicatorPlacement {
case let .placement(imagePlacement, padding):
activityIndicatorShouldCenterInView = false
let transparentImage = UIGraphicsImageRenderer(size: activityIndicatorSize)
.image { _ in }
super.setImage(transparentImage, for: .loading)
config.imagePlacement = imagePlacement
config.imagePadding = padding
case .center:
activityIndicatorShouldCenterInView = true
super.setImage(.init(), for: .loading)
}
configuration = config
}
}