mirror of
https://github.com/divkit/divkit.git
synced 2026-06-06 20:07:59 +00:00
57 lines
1.2 KiB
Swift
57 lines
1.2 KiB
Swift
import UIKit
|
|
|
|
import Base
|
|
|
|
public enum ImageViewBackgroundModel {
|
|
case color(Color)
|
|
case view(UIView)
|
|
}
|
|
|
|
extension ImageViewBackgroundModel {
|
|
public init?(placeholder: ImagePlaceholder) {
|
|
switch placeholder {
|
|
case let .color(color):
|
|
self = .color(color)
|
|
case let .view(view):
|
|
self = .view(view)
|
|
case .image:
|
|
return nil
|
|
@unknown default:
|
|
return nil
|
|
}
|
|
}
|
|
}
|
|
|
|
extension Optional where Wrapped == ImageViewBackgroundModel {
|
|
public func applyTo(_ view: UIView, oldValue: Self) {
|
|
view.subviews.filter { $0 == oldValue?.view }.forEach { $0.removeFromSuperview() }
|
|
view.backgroundColor = self?.color
|
|
if let backgroundView = self?.view {
|
|
backgroundView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
|
|
view.addSubview(backgroundView)
|
|
}
|
|
}
|
|
}
|
|
|
|
extension ImageViewBackgroundModel {
|
|
fileprivate var view: UIView? {
|
|
switch self {
|
|
case .color:
|
|
return nil
|
|
case let .view(view):
|
|
return view
|
|
}
|
|
}
|
|
}
|
|
|
|
extension ImageViewBackgroundModel {
|
|
fileprivate var color: UIColor? {
|
|
switch self {
|
|
case let .color(color):
|
|
return color.systemColor
|
|
case .view:
|
|
return nil
|
|
}
|
|
}
|
|
}
|