|
| 1 | +// The MIT License (MIT) |
| 2 | +// Copyright © 2021 Ivan Vorobei (hello@ivanvorobei.by) |
| 3 | +// |
| 4 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +// of this software and associated documentation files (the "Software"), to deal |
| 6 | +// in the Software without restriction, including without limitation the rights |
| 7 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +// copies of the Software, and to permit persons to whom the Software is |
| 9 | +// furnished to do so, subject to the following conditions: |
| 10 | +// |
| 11 | +// The above copyright notice and this permission notice shall be included in all |
| 12 | +// copies or substantial portions of the Software. |
| 13 | +// |
| 14 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | +// SOFTWARE. |
| 21 | + |
| 22 | +#if canImport(UIKit) && (os(iOS)) |
| 23 | +import UIKit |
| 24 | +import SparrowKit |
| 25 | + |
| 26 | +open class NativeEmptyTableViewCell: SPTableViewCell { |
| 27 | + |
| 28 | + // MARK: - Data |
| 29 | + |
| 30 | + open var verticalMargins: Margins = .medium { |
| 31 | + didSet { |
| 32 | + contentView.layoutMargins.top = self.verticalMargins.value |
| 33 | + contentView.layoutMargins.bottom = self.verticalMargins.value |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + // MARK: - Views |
| 38 | + |
| 39 | + public let placeholderView = NativePlaceholderView().do { |
| 40 | + $0.isUserInteractionEnabled = false |
| 41 | + } |
| 42 | + |
| 43 | + // MARK: - Init |
| 44 | + |
| 45 | + open override func commonInit() { |
| 46 | + super.commonInit() |
| 47 | + selectionStyle = .none |
| 48 | + contentView.addSubview(placeholderView) |
| 49 | + updateBackgroundColorByParent() |
| 50 | + } |
| 51 | + |
| 52 | + // MARK: - Lifecycle |
| 53 | + |
| 54 | + open override func tintColorDidChange() { |
| 55 | + super.tintColorDidChange() |
| 56 | + updateBackgroundColorByParent() |
| 57 | + } |
| 58 | + |
| 59 | + // MARK: - Layout |
| 60 | + |
| 61 | + open override func layoutSubviews() { |
| 62 | + super.layoutSubviews() |
| 63 | + placeholderView.frame.origin.y = contentView.layoutMargins.top |
| 64 | + placeholderView.setWidthAndFit(width: contentView.layoutWidth) |
| 65 | + placeholderView.setXCenter() |
| 66 | + } |
| 67 | + |
| 68 | + open override func sizeThatFits(_ size: CGSize) -> CGSize { |
| 69 | + let superSize = super.sizeThatFits(size) |
| 70 | + layoutSubviews() |
| 71 | + let calculatedHeight = placeholderView.frame.maxY + contentView.layoutMargins.bottom |
| 72 | + let bottomInset = calculatedHeight * (1 - 0.94) |
| 73 | + return .init(width: superSize.width, height: calculatedHeight + bottomInset) |
| 74 | + } |
| 75 | + |
| 76 | + // MARK: - Actions |
| 77 | + |
| 78 | + private func updateBackgroundColorByParent() { |
| 79 | + if #available(iOS 13.0, *) { |
| 80 | + backgroundColor = UIColor.systemDownedGroupedBackground |
| 81 | + } |
| 82 | + /*if let superView = self.superview?.superview { |
| 83 | + let superViewBackgroundColor = superView.backgroundColor ?? .clear |
| 84 | + if superViewBackgroundColor == .systemGroupedBackground { |
| 85 | + backgroundColor = UIColor.init( |
| 86 | + light: superViewBackgroundColor.mixWithColor(.darkGray, amount: 0.09).mixWithColor(.systemBlue, amount: 0.01), |
| 87 | + dark: .secondarySystemGroupedBackground.alpha(0.7) |
| 88 | + ) |
| 89 | + } else { |
| 90 | + // Here not implemented becouse not using never for now. |
| 91 | + backgroundColor = .red |
| 92 | + } |
| 93 | + }*/ |
| 94 | + } |
| 95 | + |
| 96 | + // MARK: - Models |
| 97 | + |
| 98 | + public enum Margins { |
| 99 | + |
| 100 | + case small |
| 101 | + case medium |
| 102 | + case large |
| 103 | + |
| 104 | + var value: CGFloat { |
| 105 | + switch self { |
| 106 | + case .small: return 16 |
| 107 | + case .medium: return 32 |
| 108 | + case .large: return 48 |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | +} |
| 113 | +#endif |
0 commit comments