Skip to content
Snippets Groups Projects
Commit 75594db5 authored by Bruno Muniz's avatar Bruno Muniz :apple:
Browse files

Refactored avatar cell

parent 88a60d85
No related branches found
No related tags found
1 merge request!58Migrating lists to compositional layout
...@@ -49,7 +49,7 @@ final class ContactListTableController: UITableViewController { ...@@ -49,7 +49,7 @@ final class ContactListTableController: UITableViewController {
let contact = sections[indexPath.section][indexPath.row] let contact = sections[indexPath.section][indexPath.row]
let name = (contact.nickname ?? contact.username) ?? "Fetching username..." let name = (contact.nickname ?? contact.username) ?? "Fetching username..."
cell.setup(title: name, image: contact.photo) cell.set(image: contact.photo, h1Text: name)
return cell return cell
} }
......
...@@ -87,7 +87,7 @@ public final class CreateGroupController: UIViewController { ...@@ -87,7 +87,7 @@ public final class CreateGroupController: UIViewController {
let cell = tableView.dequeueReusableCell(forIndexPath: indexPath, ofType: AvatarCell.self) let cell = tableView.dequeueReusableCell(forIndexPath: indexPath, ofType: AvatarCell.self)
let title = (contact.nickname ?? contact.username) ?? "" let title = (contact.nickname ?? contact.username) ?? ""
cell.setup(title: title, image: contact.photo) cell.set(image: contact.photo, h1Text: title)
if let selectedElements = self?.selectedElements, selectedElements.contains(contact) { if let selectedElements = self?.selectedElements, selectedElements.contains(contact) {
tableView.selectRow(at: indexPath, animated: true, scrollPosition: .none) tableView.selectRow(at: indexPath, animated: true, scrollPosition: .none)
......
...@@ -77,21 +77,37 @@ final class SearchLeftController: UIViewController { ...@@ -77,21 +77,37 @@ final class SearchLeftController: UIViewController {
} }
} }
cell.setup( var action: AvatarCell.Action?
title: h1Text,
image: contact.photo, if contact.authStatus == .requested {
firstSubtitle: h2Text, action = .init(
secondSubtitle: contact.email, title: Localized.Requests.Cell.requested,
thirdSubtitle: contact.phone, color: Asset.brandPrimary.color,
showSeparator: false, image: Asset.requestsResend.image,
sent: contact.authStatus == .requested action: { [weak self] in
)
cell.didTapStateButton = { [weak self] in
guard let self = self else { return } guard let self = self else { return }
self.viewModel.didTapResend(contact: contact) self.viewModel.didTapResend(contact: contact)
cell.updateToResent()
cell.update(action: .init(
title: Localized.Requests.Cell.resent,
color: Asset.neutralWeak.color,
image: Asset.requestsResent.image,
action: {}
))
} }
)
}
cell.set(
image: contact.photo,
h1Text: h1Text,
h2Text: h2Text,
h3Text: contact.email,
h4Text: contact.phone,
showSeparator: false,
action: action
)
return cell return cell
} }
......
import UIKit import UIKit
import Combine import Combine
final class AvatarCellButton: UIControl { public final class AvatarCell: UITableViewCell {
let titleLabel = UILabel() public struct Action {
let imageView = UIImageView() public var title: String
public var color: UIColor
init() { public var image: UIImage?
super.init(frame: .zero) public var action: () -> Void
titleLabel.numberOfLines = 0
titleLabel.textAlignment = .right
titleLabel.font = Fonts.Mulish.semiBold.font(size: 13.0)
addSubview(imageView)
addSubview(titleLabel)
imageView.snp.makeConstraints {
$0.top.greaterThanOrEqualToSuperview()
$0.left.equalToSuperview()
$0.centerY.equalToSuperview()
$0.bottom.lessThanOrEqualToSuperview()
}
titleLabel.snp.makeConstraints { public init(
$0.top.greaterThanOrEqualToSuperview() title: String,
$0.left.equalTo(imageView.snp.right).offset(5) color: UIColor,
$0.centerY.equalToSuperview() image: UIImage? = nil,
$0.right.equalToSuperview() action: @escaping () -> Void
$0.width.equalTo(60) ) {
$0.bottom.lessThanOrEqualToSuperview() self.title = title
} self.color = color
self.image = image
self.action = action
} }
required init?(coder: NSCoder) { nil }
} }
public final class AvatarCell: UITableViewCell { private let h1Label = UILabel()
let h1Label = UILabel() private let h2Label = UILabel()
let h2Label = UILabel() private let h3Label = UILabel()
let h3Label = UILabel() private let h4Label = UILabel()
let h4Label = UILabel() private let separatorView = UIView()
let separatorView = UIView() private let avatarView = AvatarView()
let avatarView = AvatarView() private let stackView = UIStackView()
let stackView = UIStackView() private var didTapAction: (() -> Void)?
let stateButton = AvatarCellButton() private let actionButton = AvatarCellButton()
private var cancellables = Set<AnyCancellable>()
var cancellables = Set<AnyCancellable>()
public var didTapStateButton: (() -> Void)!
public override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { public override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier) super.init(style: style, reuseIdentifier: reuseIdentifier)
selectedBackgroundView = UIView()
multipleSelectionBackgroundView = UIView()
backgroundColor = Asset.neutralWhite.color backgroundColor = Asset.neutralWhite.color
separatorView.backgroundColor = Asset.neutralLine.color
h1Label.textColor = Asset.neutralActive.color h1Label.textColor = Asset.neutralActive.color
h2Label.textColor = Asset.neutralSecondaryAlternative.color h2Label.textColor = Asset.neutralSecondaryAlternative.color
...@@ -66,17 +50,14 @@ public final class AvatarCell: UITableViewCell { ...@@ -66,17 +50,14 @@ public final class AvatarCell: UITableViewCell {
stackView.spacing = 4 stackView.spacing = 4
stackView.axis = .vertical stackView.axis = .vertical
stackView.addArrangedSubview(h1Label) stackView.addArrangedSubview(h1Label)
stackView.addArrangedSubview(h2Label) stackView.addArrangedSubview(h2Label)
stackView.addArrangedSubview(h3Label) stackView.addArrangedSubview(h3Label)
stackView.addArrangedSubview(h4Label) stackView.addArrangedSubview(h4Label)
separatorView.backgroundColor = Asset.neutralLine.color
contentView.addSubview(stackView) contentView.addSubview(stackView)
contentView.addSubview(avatarView) contentView.addSubview(avatarView)
contentView.addSubview(stateButton) contentView.addSubview(actionButton)
contentView.addSubview(separatorView) contentView.addSubview(separatorView)
setupConstraints() setupConstraints()
...@@ -90,70 +71,73 @@ public final class AvatarCell: UITableViewCell { ...@@ -90,70 +71,73 @@ public final class AvatarCell: UITableViewCell {
h2Label.text = nil h2Label.text = nil
h3Label.text = nil h3Label.text = nil
h4Label.text = nil h4Label.text = nil
didTapAction = nil
stateButton.imageView.image = nil
stateButton.titleLabel.text = nil
avatarView.prepareForReuse() avatarView.prepareForReuse()
actionButton.prepareForReuse()
cancellables.forEach { $0.cancel() }
cancellables.removeAll() cancellables.removeAll()
} }
public func setup( public func set(
title: String,
image: Data?, image: Data?,
firstSubtitle: String? = nil, h1Text: String,
secondSubtitle: String? = nil, h2Text: String? = nil,
thirdSubtitle: String? = nil, h3Text: String? = nil,
h4Text: String? = nil,
showSeparator: Bool = true, showSeparator: Bool = true,
sent: Bool = false action: Action? = nil
) { ) {
h1Label.text = title avatarView.setupProfile(
title: h1Text,
if let firstSubtitle = firstSubtitle { image: image,
h2Label.isHidden = false size: .medium
h2Label.text = firstSubtitle )
} else {
h2Label.isHidden = true
}
if let secondSubtitle = secondSubtitle { h1Label.text = h1Text
h3Label.isHidden = false h2Label.text = h2Text
h3Label.text = secondSubtitle h3Label.text = h3Text
} else { h4Label.text = h4Text
h3Label.isHidden = true
}
if let thirdSubtitle = thirdSubtitle { h2Label.isHidden = h2Text == nil
h4Label.isHidden = false h3Label.isHidden = h3Text == nil
h4Label.text = thirdSubtitle h4Label.isHidden = h4Text == nil
} else {
h4Label.isHidden = true
}
avatarView.setupProfile(title: title, image: image, size: .medium) separatorView.isHidden = !showSeparator
separatorView.alpha = showSeparator ? 1.0 : 0.0
cancellables.removeAll() if let action = action {
actionButton.set(
image: action.image,
title: action.title,
titleColor: action.color
)
if sent { didTapAction = action.action
stateButton.imageView.image = Asset.requestsResend.image
stateButton.titleLabel.text = Localized.Requests.Cell.requested
stateButton.titleLabel.textColor = Asset.brandPrimary.color
stateButton actionButton
.publisher(for: .touchUpInside) .publisher(for: .touchUpInside)
.sink { [unowned self] in didTapStateButton() } .sink { [unowned self] in didTapAction?() }
.store(in: &cancellables) .store(in: &cancellables)
} }
} }
public func updateToResent() { public func update(action: Action) {
stateButton.imageView.image = Asset.requestsResent.image
stateButton.titleLabel.text = Localized.Requests.Cell.resent
stateButton.titleLabel.textColor = Asset.neutralWeak.color
cancellables.forEach { $0.cancel() } cancellables.forEach { $0.cancel() }
cancellables.removeAll() cancellables.removeAll()
actionButton.set(
image: action.image,
title: action.title,
titleColor: action.color
)
didTapAction = action.action
actionButton
.publisher(for: .touchUpInside)
.sink { [unowned self] in didTapAction?() }
.store(in: &cancellables)
} }
private func setupConstraints() { private func setupConstraints() {
...@@ -179,7 +163,7 @@ public final class AvatarCell: UITableViewCell { ...@@ -179,7 +163,7 @@ public final class AvatarCell: UITableViewCell {
$0.bottom.equalToSuperview() $0.bottom.equalToSuperview()
} }
stateButton.snp.makeConstraints { actionButton.snp.makeConstraints {
$0.centerY.equalTo(stackView) $0.centerY.equalTo(stackView)
$0.right.equalToSuperview().offset(-24) $0.right.equalToSuperview().offset(-24)
} }
......
import UIKit
final class AvatarCellButton: UIControl {
private let titleLabel = UILabel()
private let imageView = UIImageView()
init() {
super.init(frame: .zero)
titleLabel.numberOfLines = 0
titleLabel.textAlignment = .right
titleLabel.font = Fonts.Mulish.semiBold.font(size: 13.0)
addSubview(imageView)
addSubview(titleLabel)
setupConstraints()
}
required init?(coder: NSCoder) { nil }
func prepareForReuse() {
titleLabel.text = nil
imageView.image = nil
}
func set(
image: UIImage?,
title: String,
titleColor: UIColor
) {
imageView.image = image
titleLabel.text = title
titleLabel.textColor = titleColor
}
private func setupConstraints() {
imageView.snp.makeConstraints {
$0.top.greaterThanOrEqualToSuperview()
$0.left.equalToSuperview()
$0.centerY.equalToSuperview()
$0.bottom.lessThanOrEqualToSuperview()
}
titleLabel.snp.makeConstraints {
$0.top.greaterThanOrEqualToSuperview()
$0.left.equalTo(imageView.snp.right).offset(5)
$0.centerY.equalToSuperview()
$0.right.equalToSuperview()
$0.width.equalTo(60)
$0.bottom.lessThanOrEqualToSuperview()
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment