Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Visual Effects on tvOS #574

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,30 @@

import UIKit

extension UIView {
var allSubviews: [UIView] {
subviews.reduce(into: [self]) { array, subview in
array += subview.allSubviews
}
/// A button that enforces clear background colors.
/// To style the background, use ``UIButton.setBackgroundImage(_ image:for:)``.
/// This suppresses unwanted background and border effects automatically added when using
/// the system button type while keeping the default appearance, drop shadow, and scaling effect.
final class SPAppleTVButton: UIButton {
override func layoutSubviews() {
super.layoutSubviews()
clearBackgroundColors()
}
}

class SPAppleTVButton: UIButton {
var viewBeforeUITitleView: UIView? { allSubviews.dropFirst(allSubviews.count - 2).first }
extension UIView {

/// Sets the background colors of all the view's subviews to ``UIColor.clear``.
/// - Parameter ignoredColor: Exception that is not overridden.
func clearBackgroundColors(ignoring ignoredColor: UIColor? = nil) {
for subview in subviews {
subview.clearBackgroundColors(ignoring: ignoredColor)

var onFocusBackgroundColor: UIColor?
var onUnfocusBackgroundColor: UIColor? {
didSet {
viewBeforeUITitleView?.backgroundColor = onUnfocusBackgroundColor
}
}
if let ignoredColor, subview.backgroundColor == ignoredColor {
continue
}

override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
super.didUpdateFocus(in: context, with: coordinator)
if isFocused {
backgroundColor = onFocusBackgroundColor
coordinator.addCoordinatedAnimations({ [weak self] in
self?.viewBeforeUITitleView?.backgroundColor = self?.onFocusBackgroundColor
})
} else {
backgroundColor = onUnfocusBackgroundColor
viewBeforeUITitleView?.backgroundColor = onUnfocusBackgroundColor
subview.backgroundColor = .clear
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,13 @@ class FocusGuideDebugView: UIView {
button.setTitle(action.settings.text, for: .normal)
button.setTitleColor(UIColor(hexString: style.onUnfocusTextColor), for: .normal)
button.setTitleColor(UIColor(hexString: style.onFocusTextColor), for: .focused)
button.backgroundColor = UIColor(hexString: style.onUnfocusBackgroundColor)
button.onUnfocusBackgroundColor = button.backgroundColor
button.onFocusBackgroundColor = UIColor(hexString: style.onFocusBackgroundColor)
button.titleLabel?.font = UIFont(from: style.font)
button.layer.cornerRadius = 12
UIColor(hexString: style.onUnfocusBackgroundColor)
.map { getImageWithColor(color: $0) }
.map { button.setBackgroundImage($0, for: .normal) }
UIColor(hexString: style.onFocusBackgroundColor)
.map { getImageWithColor(color: $0) }
.map { button.setBackgroundImage($0, for: .focused) }
} else {
button.isHidden = true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ protocol SPNativePrivacyManagerHome {
setFocusGuidesForButtons()
categoryTableView.accessibilityIdentifier = "Categories List"
categoryTableView.allowsSelection = false
categoryTableView.register(UITableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)
if #available(tvOS 14.0, *) {
categoryTableView.register(CategoryCellView.self, forCellReuseIdentifier: cellReuseIdentifier)
} else {
categoryTableView.register(UITableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)
}
categoryTableView.delegate = self
categoryTableView.dataSource = self
disableMenuButton()
Expand Down Expand Up @@ -296,6 +300,7 @@ extension SPGDPRNativePrivacyManagerViewController: UITableViewDataSource {
let cell = categoryTableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier, for: indexPath)
cell.textLabel?.text = categories[indexPath.row].name.trimmingCharacters(in: .whitespacesAndNewlines)
cell.textLabel?.setDefaultTextColorForDarkMode()
cell.textLabel?.font = .preferredFont(forTextStyle: .footnote)
return cell
}
}
Expand All @@ -317,4 +322,32 @@ class FocusableScrollView: UIScrollView {
}
}

/// A table view cell with a custom background configuration.
/// It suppresses unwanted background and border effects that are automatically added by the system
/// while while keeping the drop shadow and scaling effect when being focused.
@available(tvOS 14.0, *)
class CategoryCellView: UITableViewCell {

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupBackground()
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

private func setupBackground() {
var backgroundConfiguration = UIBackgroundConfiguration.listPlainCell()
backgroundConfiguration.backgroundColor = .darkGray
self.backgroundConfiguration = backgroundConfiguration
}

override func updateConfiguration(using state: UICellConfigurationState) {
super.updateConfiguration(using: state)
textLabel?.textColor = .white
clearBackgroundColors(ignoring: backgroundConfiguration?.backgroundColor)
}
}

// swiftlint:enable function_body_length