session-ios/Session/Conversations/Input View/MentionSelectionView.swift

180 lines
7.1 KiB
Swift
Raw Normal View History

2021-02-17 04:26:43 +01:00
final class MentionSelectionView : UIView, UITableViewDataSource, UITableViewDelegate {
2021-02-17 05:57:07 +01:00
var candidates: [Mention] = [] {
didSet {
tableView.isScrollEnabled = (candidates.count > 4)
tableView.reloadData()
}
2021-02-17 04:26:43 +01:00
}
2021-02-17 05:57:07 +01:00
var openGroupServer: String?
var openGroupChannel: UInt64?
2021-03-24 04:36:26 +01:00
var openGroupRoom: String?
2021-04-01 05:24:10 +02:00
weak var delegate: MentionSelectionViewDelegate?
2021-02-17 04:26:43 +01:00
// MARK: Components
2021-02-17 05:57:07 +01:00
lazy var tableView: UITableView = { // TODO: Make this private
2021-02-17 04:26:43 +01:00
let result = UITableView()
result.dataSource = self
result.delegate = self
result.register(Cell.self, forCellReuseIdentifier: "Cell")
result.separatorStyle = .none
result.backgroundColor = .clear
result.showsVerticalScrollIndicator = false
return result
}()
// MARK: Initialization
override init(frame: CGRect) {
super.init(frame: frame)
setUpViewHierarchy()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setUpViewHierarchy()
}
private func setUpViewHierarchy() {
2021-02-17 05:57:07 +01:00
// Table view
2021-02-17 04:26:43 +01:00
addSubview(tableView)
tableView.pin(to: self)
2021-02-17 05:57:07 +01:00
// Top separator
2021-02-17 04:26:43 +01:00
let topSeparator = UIView()
topSeparator.backgroundColor = Colors.separator
topSeparator.set(.height, to: Values.separatorThickness)
addSubview(topSeparator)
topSeparator.pin(.leading, to: .leading, of: self)
topSeparator.pin(.top, to: .top, of: self)
topSeparator.pin(.trailing, to: .trailing, of: self)
2021-02-17 05:57:07 +01:00
// Bottom separator
2021-02-17 04:26:43 +01:00
let bottomSeparator = UIView()
bottomSeparator.backgroundColor = Colors.separator
bottomSeparator.set(.height, to: Values.separatorThickness)
addSubview(bottomSeparator)
bottomSeparator.pin(.leading, to: .leading, of: self)
bottomSeparator.pin(.trailing, to: .trailing, of: self)
bottomSeparator.pin(.bottom, to: .bottom, of: self)
}
// MARK: Data
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
2021-02-17 05:57:07 +01:00
return candidates.count
2021-02-17 04:26:43 +01:00
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! Cell
2021-02-17 05:57:07 +01:00
let mentionCandidate = candidates[indexPath.row]
2021-02-17 04:26:43 +01:00
cell.mentionCandidate = mentionCandidate
2021-02-17 05:57:07 +01:00
cell.openGroupServer = openGroupServer
cell.openGroupChannel = openGroupChannel
2021-03-24 04:36:26 +01:00
cell.openGroupRoom = openGroupRoom
2021-02-17 05:57:07 +01:00
cell.separator.isHidden = (indexPath.row == (candidates.count - 1))
2021-02-17 04:26:43 +01:00
return cell
}
// MARK: Interaction
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
2021-02-17 05:57:07 +01:00
let mentionCandidate = candidates[indexPath.row]
2021-02-17 04:26:43 +01:00
delegate?.handleMentionSelected(mentionCandidate, from: self)
}
}
// MARK: - Cell
private extension MentionSelectionView {
final class Cell : UITableViewCell {
var mentionCandidate = Mention(publicKey: "", displayName: "") { didSet { update() } }
2021-02-17 05:57:07 +01:00
var openGroupServer: String?
var openGroupChannel: UInt64?
2021-03-24 04:36:26 +01:00
var openGroupRoom: String?
2021-02-17 04:26:43 +01:00
// MARK: Components
private lazy var profilePictureView = ProfilePictureView()
2021-02-19 04:30:16 +01:00
private lazy var moderatorIconImageView = UIImageView(image: #imageLiteral(resourceName: "Crown"))
2021-02-17 04:26:43 +01:00
private lazy var displayNameLabel: UILabel = {
let result = UILabel()
result.textColor = Colors.text
result.font = .systemFont(ofSize: Values.smallFontSize)
result.lineBreakMode = .byTruncatingTail
return result
}()
lazy var separator: UIView = {
let result = UIView()
result.backgroundColor = Colors.separator
result.set(.height, to: Values.separatorThickness)
return result
}()
// MARK: Initialization
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setUpViewHierarchy()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setUpViewHierarchy()
}
private func setUpViewHierarchy() {
2021-02-17 05:57:07 +01:00
// Cell background color
backgroundColor = .clear
// Highlight color
2021-02-17 04:26:43 +01:00
let selectedBackgroundView = UIView()
2021-02-17 05:57:07 +01:00
selectedBackgroundView.backgroundColor = .clear
2021-02-17 04:26:43 +01:00
self.selectedBackgroundView = selectedBackgroundView
2021-02-17 05:57:07 +01:00
// Profile picture image view
let profilePictureViewSize = Values.smallProfilePictureSize
2021-02-17 04:26:43 +01:00
profilePictureView.set(.width, to: profilePictureViewSize)
profilePictureView.set(.height, to: profilePictureViewSize)
profilePictureView.size = profilePictureViewSize
2021-02-17 05:57:07 +01:00
// Main stack view
let mainStackView = UIStackView(arrangedSubviews: [ profilePictureView, displayNameLabel ])
mainStackView.axis = .horizontal
mainStackView.alignment = .center
mainStackView.spacing = Values.mediumSpacing
mainStackView.set(.height, to: profilePictureViewSize)
contentView.addSubview(mainStackView)
mainStackView.pin(.leading, to: .leading, of: contentView, withInset: Values.mediumSpacing)
mainStackView.pin(.top, to: .top, of: contentView, withInset: Values.smallSpacing)
contentView.pin(.trailing, to: .trailing, of: mainStackView, withInset: Values.mediumSpacing)
contentView.pin(.bottom, to: .bottom, of: mainStackView, withInset: Values.smallSpacing)
mainStackView.set(.width, to: UIScreen.main.bounds.width - 2 * Values.mediumSpacing)
// Moderator icon image view
2021-02-17 04:26:43 +01:00
moderatorIconImageView.set(.width, to: 20)
moderatorIconImageView.set(.height, to: 20)
contentView.addSubview(moderatorIconImageView)
2021-02-17 05:57:07 +01:00
moderatorIconImageView.pin(.trailing, to: .trailing, of: profilePictureView, withInset: 1)
moderatorIconImageView.pin(.bottom, to: .bottom, of: profilePictureView, withInset: 4.5)
// Separator
2021-02-17 04:26:43 +01:00
addSubview(separator)
separator.pin(.leading, to: .leading, of: self)
separator.pin(.trailing, to: .trailing, of: self)
separator.pin(.bottom, to: .bottom, of: self)
}
// MARK: Updating
private func update() {
displayNameLabel.text = mentionCandidate.displayName
2021-02-26 04:42:46 +01:00
profilePictureView.publicKey = mentionCandidate.publicKey
2021-02-17 04:26:43 +01:00
profilePictureView.update()
2021-03-24 04:36:26 +01:00
if let server = openGroupServer, let room = openGroupRoom {
let isUserModerator = OpenGroupAPIV2.isUserModerator(mentionCandidate.publicKey, for: room, on: server)
moderatorIconImageView.isHidden = !isUserModerator
2021-02-17 04:26:43 +01:00
} else {
moderatorIconImageView.isHidden = true
}
}
}
}
// MARK: - Delegate
protocol MentionSelectionViewDelegate: AnyObject {
2021-02-17 04:26:43 +01:00
func handleMentionSelected(_ mention: Mention, from view: MentionSelectionView)
}