fix: code review notes
This commit is contained in:
parent
19bb08aa66
commit
2250b1b4d9
|
|
@ -75,10 +75,10 @@ extension UIView {
|
|||
return .imageView(imageView)
|
||||
|
||||
} else if self.isSkeletonsContainer {
|
||||
return .container(self)
|
||||
return .parentView(self)
|
||||
|
||||
} else {
|
||||
return .generic(self)
|
||||
return .anyFinalView(self)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -86,23 +86,23 @@ extension UIView {
|
|||
attributedText: NSAttributedString?,
|
||||
font: UIFont?,
|
||||
textAlignment: NSTextAlignment) -> Bool {
|
||||
let _text: String
|
||||
let _font: UIFont
|
||||
let finalText: String
|
||||
let finalFont: UIFont
|
||||
|
||||
if let attributedText = attributedText, let maxFont = attributedText.getMaxFont() {
|
||||
_text = attributedText.string
|
||||
_font = maxFont
|
||||
finalText = attributedText.string
|
||||
finalFont = maxFont
|
||||
|
||||
} else if let text = text, let font = font {
|
||||
_text = text
|
||||
_font = font
|
||||
finalText = text
|
||||
finalFont = font
|
||||
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
|
||||
let textAttributes = BaseTextAttributes(font: _font, color: .black, alignment: textAlignment, isMultiline: true)
|
||||
let labelTextSize = textAttributes.size(of: _text, with: .zero)
|
||||
let textAttributes = BaseTextAttributes(font: finalFont, color: .black, alignment: textAlignment, isMultiline: true)
|
||||
let labelTextSize = textAttributes.size(of: finalText, with: .zero)
|
||||
|
||||
return labelTextSize.width > bounds.width
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,19 +31,19 @@ open class SkeletonLayer: CAShapeLayer {
|
|||
}
|
||||
|
||||
public enum ViewType {
|
||||
case base(UIView)
|
||||
case generic(UIView)
|
||||
case container(UIView)
|
||||
case label(UILabel)
|
||||
case textView(UITextView)
|
||||
case skeletonsHolderView(UIView)
|
||||
case parentView(UIView)
|
||||
case imageView(UIImageView)
|
||||
case textView(UITextView)
|
||||
case label(UILabel)
|
||||
case anyFinalView(UIView)
|
||||
|
||||
public var view: UIView {
|
||||
switch self {
|
||||
case let .imageView(imageView):
|
||||
return imageView
|
||||
|
||||
case let .container(containerView):
|
||||
case let .parentView(containerView):
|
||||
return containerView
|
||||
|
||||
case let .label(labelView):
|
||||
|
|
@ -52,10 +52,10 @@ open class SkeletonLayer: CAShapeLayer {
|
|||
case let .textView(textView):
|
||||
return textView
|
||||
|
||||
case let .generic(view):
|
||||
case let .anyFinalView(view):
|
||||
return view
|
||||
|
||||
case let .base(view):
|
||||
case let .skeletonsHolderView(view):
|
||||
return view
|
||||
}
|
||||
}
|
||||
|
|
@ -63,8 +63,10 @@ open class SkeletonLayer: CAShapeLayer {
|
|||
|
||||
private var animationLayer = CAGradientLayer()
|
||||
private var viewBoundsObservation: NSKeyValueObservation?
|
||||
private var applicationStateObservation: NSObjectProtocol?
|
||||
|
||||
public var configuration: SkeletonsConfiguration
|
||||
public var isSkeletonsHolder: Bool = false
|
||||
public weak var baseView: UIView?
|
||||
|
||||
public var isAnimating: Bool {
|
||||
|
|
@ -93,10 +95,6 @@ open class SkeletonLayer: CAShapeLayer {
|
|||
super.init(coder: coder)
|
||||
}
|
||||
|
||||
deinit {
|
||||
NotificationCenter.default.removeObserver(self)
|
||||
}
|
||||
|
||||
// MARK: - Open methods
|
||||
|
||||
open func bind(to viewType: ViewType) {
|
||||
|
|
@ -108,10 +106,13 @@ open class SkeletonLayer: CAShapeLayer {
|
|||
}
|
||||
|
||||
if let _ = configuration.animation?(self) {
|
||||
NotificationCenter.default.addObserver(self,
|
||||
selector: #selector(SkeletonLayer.restartAnimationIfNeeded),
|
||||
name: UIApplication.willEnterForegroundNotification,
|
||||
object: nil)
|
||||
applicationStateObservation = NotificationCenter.default
|
||||
.addObserver(forName: UIApplication.willEnterForegroundNotification,
|
||||
object: nil,
|
||||
queue: .main) { [weak self] _ in
|
||||
self?.stopAnimation()
|
||||
self?.startAnimation()
|
||||
}
|
||||
}
|
||||
|
||||
configuration.configurationDelegate?.layerDidConfigured(forViewType: viewType, layer: self)
|
||||
|
|
@ -121,10 +122,13 @@ open class SkeletonLayer: CAShapeLayer {
|
|||
stopAnimation()
|
||||
removeFromSuperlayer()
|
||||
viewBoundsObservation = nil
|
||||
applicationStateObservation = nil
|
||||
}
|
||||
|
||||
open func startAnimation() {
|
||||
guard !isAnimating, let animation = configuration.animation?(self) else {
|
||||
guard !isAnimating,
|
||||
!isSkeletonsHolder,
|
||||
let animation = configuration.animation?(self) else {
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -141,10 +145,11 @@ open class SkeletonLayer: CAShapeLayer {
|
|||
|
||||
private func configureAppearance(_ type: ViewType) {
|
||||
switch type {
|
||||
case .container(_):
|
||||
case .parentView(_):
|
||||
configuration.configureContainerAppearance(layer: self)
|
||||
|
||||
case .base(_):
|
||||
case .skeletonsHolderView(_):
|
||||
isSkeletonsHolder = true
|
||||
configuration.configureBaseViewAppearance(layer: self, view: type.view)
|
||||
|
||||
default:
|
||||
|
|
@ -176,13 +181,13 @@ open class SkeletonLayer: CAShapeLayer {
|
|||
let viewFrame = CGRect(origin: rect.origin, size: path?.boundingBox.size ?? rect.size)
|
||||
frame = configuration.imageViewConfiguration.applyPadding(viewFrame: viewFrame)
|
||||
|
||||
case .container(_):
|
||||
case .parentView(_):
|
||||
path = configuration.containerViewConfiguration.drawPath(rect: viewType.view.bounds)
|
||||
|
||||
let viewFrame = CGRect(origin: rect.origin, size: path?.boundingBox.size ?? rect.size)
|
||||
frame = configuration.containerViewConfiguration.applyPadding(viewFrame: viewFrame)
|
||||
|
||||
case .generic(_):
|
||||
case .anyFinalView(_):
|
||||
path = configuration.viewConfiguration.drawPath(rect: viewType.view.bounds)
|
||||
|
||||
let viewFrame = CGRect(origin: rect.origin, size: path?.boundingBox.size ?? rect.size)
|
||||
|
|
@ -195,9 +200,4 @@ open class SkeletonLayer: CAShapeLayer {
|
|||
|
||||
animationLayer.frame = bounds
|
||||
}
|
||||
|
||||
@objc private func restartAnimationIfNeeded() {
|
||||
stopAnimation()
|
||||
startAnimation()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -85,7 +85,7 @@ extension UIView {
|
|||
|
||||
if view.isSkeletonsContainer {
|
||||
if !conf.isContainersHidden, !forceNoContainers {
|
||||
skeletonLayer.bind(to: .container(view))
|
||||
skeletonLayer.bind(to: .parentView(view))
|
||||
}
|
||||
|
||||
subviewSkeletonLayers = view.skeletonableViews
|
||||
|
|
@ -102,7 +102,7 @@ extension UIView {
|
|||
private func configureBaseLayer(withConfiguration conf: SkeletonsConfiguration) {
|
||||
let skeletonLayer = conf.createSkeletonLayer(for: self)
|
||||
|
||||
skeletonLayer.bind(to: .base(self))
|
||||
skeletonLayer.bind(to: .skeletonsHolderView(self))
|
||||
layer.insertSublayer(skeletonLayer, at: .max)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue