Fix to only animate the disclosure chevron with button presses

This commit is contained in:
Maurice Parker 2019-09-27 11:54:50 -05:00
parent 9da949114f
commit 08f7e6d1a4
2 changed files with 31 additions and 27 deletions

View File

@ -18,8 +18,7 @@ protocol MasterFeedTableViewCellDelegate: class {
class MasterFeedTableViewCell : NNWTableViewCell { class MasterFeedTableViewCell : NNWTableViewCell {
weak var delegate: MasterFeedTableViewCellDelegate? weak var delegate: MasterFeedTableViewCellDelegate?
var isDisclosureAvailable = false
override var accessibilityLabel: String? { override var accessibilityLabel: String? {
set {} set {}
get { get {
@ -32,18 +31,20 @@ class MasterFeedTableViewCell : NNWTableViewCell {
} }
} }
var disclosureExpanded = false {
didSet {
updateDisclosureImage()
}
}
var faviconImage: UIImage? { var faviconImage: UIImage? {
didSet { didSet {
faviconImageView.image = faviconImage faviconImageView.image = faviconImage
} }
} }
var isDisclosureAvailable = false {
didSet {
if isDisclosureAvailable != oldValue {
setNeedsLayout()
}
}
}
var unreadCount: Int { var unreadCount: Int {
get { get {
return unreadCountView.unreadCount return unreadCountView.unreadCount
@ -85,15 +86,29 @@ class MasterFeedTableViewCell : NNWTableViewCell {
return imageView return imageView
}() }()
private var isDisclosureExpanded = false
private var disclosureButton: UIButton?
private var unreadCountView = MasterFeedUnreadCountView(frame: CGRect.zero) private var unreadCountView = MasterFeedUnreadCountView(frame: CGRect.zero)
private var showingEditControl = false private var isShowingEditControl = false
var disclosureButton: UIButton?
required init?(coder: NSCoder) { required init?(coder: NSCoder) {
super.init(coder: coder) super.init(coder: coder)
commonInit() commonInit()
} }
func setDisclosure(isExpanded: Bool, animated: Bool) {
isDisclosureExpanded = isExpanded
let duration = animated ? 0.3 : 0.0
UIView.animate(withDuration: duration) {
if self.isDisclosureExpanded {
self.disclosureButton?.imageView?.transform = CGAffineTransform(rotationAngle: 1.570796)
} else {
self.disclosureButton?.imageView?.transform = CGAffineTransform(rotationAngle: 0)
}
}
}
override func applyThemeProperties() { override func applyThemeProperties() {
super.applyThemeProperties() super.applyThemeProperties()
titleView.highlightedTextColor = AppAssets.tableViewCellHighlightedTextColor titleView.highlightedTextColor = AppAssets.tableViewCellHighlightedTextColor
@ -117,24 +132,24 @@ class MasterFeedTableViewCell : NNWTableViewCell {
override func willTransition(to state: UITableViewCell.StateMask) { override func willTransition(to state: UITableViewCell.StateMask) {
super.willTransition(to: state) super.willTransition(to: state)
showingEditControl = state.contains(.showingEditControl) isShowingEditControl = state.contains(.showingEditControl)
} }
override func sizeThatFits(_ size: CGSize) -> CGSize { override func sizeThatFits(_ size: CGSize) -> CGSize {
let layout = MasterFeedTableViewCellLayout(cellWidth: bounds.size.width, insets: safeAreaInsets, label: titleView, unreadCountView: unreadCountView, showingEditingControl: showingEditControl, indent: indentationLevel == 1, shouldShowDisclosure: isDisclosureAvailable) let layout = MasterFeedTableViewCellLayout(cellWidth: bounds.size.width, insets: safeAreaInsets, label: titleView, unreadCountView: unreadCountView, showingEditingControl: isShowingEditControl, indent: indentationLevel == 1, shouldShowDisclosure: isDisclosureAvailable)
return CGSize(width: bounds.width, height: layout.height) return CGSize(width: bounds.width, height: layout.height)
} }
override func layoutSubviews() { override func layoutSubviews() {
super.layoutSubviews() super.layoutSubviews()
let layout = MasterFeedTableViewCellLayout(cellWidth: bounds.size.width, insets: safeAreaInsets, label: titleView, unreadCountView: unreadCountView, showingEditingControl: showingEditControl, indent: indentationLevel == 1, shouldShowDisclosure: isDisclosureAvailable) let layout = MasterFeedTableViewCellLayout(cellWidth: bounds.size.width, insets: safeAreaInsets, label: titleView, unreadCountView: unreadCountView, showingEditingControl: isShowingEditControl, indent: indentationLevel == 1, shouldShowDisclosure: isDisclosureAvailable)
layoutWith(layout) layoutWith(layout)
} }
@objc func buttonPressed(_ sender: UIButton) { @objc func buttonPressed(_ sender: UIButton) {
if isDisclosureAvailable { if isDisclosureAvailable {
disclosureExpanded = !disclosureExpanded setDisclosure(isExpanded: !isDisclosureExpanded, animated: true)
delegate?.disclosureSelected(self, expanding: disclosureExpanded) delegate?.disclosureSelected(self, expanding: isDisclosureExpanded)
} }
} }
@ -153,20 +168,9 @@ private extension MasterFeedTableViewCell {
disclosureButton = NonIntrinsicButton(type: .roundedRect) disclosureButton = NonIntrinsicButton(type: .roundedRect)
disclosureButton!.addTarget(self, action: #selector(buttonPressed(_:)), for: UIControl.Event.touchUpInside) disclosureButton!.addTarget(self, action: #selector(buttonPressed(_:)), for: UIControl.Event.touchUpInside)
disclosureButton?.setImage(AppAssets.chevronBaseImage, for: .normal) disclosureButton?.setImage(AppAssets.chevronBaseImage, for: .normal)
updateDisclosureImage()
addSubviewAtInit(disclosureButton!) addSubviewAtInit(disclosureButton!)
} }
func updateDisclosureImage() {
UIView.animate(withDuration: 0.3) {
if self.disclosureExpanded {
self.disclosureButton?.imageView?.transform = CGAffineTransform(rotationAngle: 1.570796)
} else {
self.disclosureButton?.imageView?.transform = CGAffineTransform(rotationAngle: 0)
}
}
}
func addSubviewAtInit(_ view: UIView) { func addSubviewAtInit(_ view: UIView) {
addSubview(view) addSubview(view)
view.translatesAutoresizingMaskIntoConstraints = false view.translatesAutoresizingMaskIntoConstraints = false

View File

@ -600,7 +600,7 @@ private extension MasterFeedViewController {
} else { } else {
cell.indentationLevel = 0 cell.indentationLevel = 0
} }
cell.disclosureExpanded = node.isExpanded cell.setDisclosure(isExpanded: node.isExpanded, animated: false)
cell.isDisclosureAvailable = node.canHaveChildNodes cell.isDisclosureAvailable = node.canHaveChildNodes
cell.name = nameFor(node) cell.name = nameFor(node)