mirror of https://github.com/oxen-io/session-ios
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
142 lines
5.7 KiB
Swift
142 lines
5.7 KiB
Swift
|
|
// MARK: - User Selection View
|
|
|
|
@objc(LKMentionCandidateSelectionView)
|
|
final class MentionCandidateSelectionView : UIView, UITableViewDataSource, UITableViewDelegate {
|
|
@objc var mentionCandidates: [Mention] = [] { didSet { tableView.reloadData() } }
|
|
@objc var publicChatServer: String?
|
|
var publicChatServerID: UInt64?
|
|
@objc var delegate: MentionCandidateSelectionViewDelegate?
|
|
|
|
// MARK: Convenience
|
|
@objc(setPublicChatServerID:)
|
|
func setPublicChatServerID(to publicChatServerID: UInt64) {
|
|
self.publicChatServerID = publicChatServerID != 0 ? publicChatServerID : nil
|
|
}
|
|
|
|
// MARK: Components
|
|
@objc lazy var tableView: UITableView = { // TODO: Make this private
|
|
let result = UITableView()
|
|
result.dataSource = self
|
|
result.delegate = self
|
|
result.register(Cell.self, forCellReuseIdentifier: "Cell")
|
|
result.separatorStyle = .none
|
|
result.backgroundColor = .clear
|
|
result.contentInset = UIEdgeInsets(top: 6, leading: 0, bottom: 0, trailing: 0)
|
|
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() {
|
|
addSubview(tableView)
|
|
tableView.pin(to: self)
|
|
}
|
|
|
|
// MARK: Data
|
|
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
|
return mentionCandidates.count
|
|
}
|
|
|
|
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! Cell
|
|
let mentionCandidate = mentionCandidates[indexPath.row]
|
|
cell.mentionCandidate = mentionCandidate
|
|
cell.publicChatServer = publicChatServer
|
|
cell.publicChatServerID = publicChatServerID
|
|
return cell
|
|
}
|
|
|
|
// MARK: Interaction
|
|
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
|
let mentionCandidate = mentionCandidates[indexPath.row]
|
|
delegate?.handleMentionCandidateSelected(mentionCandidate, from: self)
|
|
}
|
|
}
|
|
|
|
// MARK: - Cell
|
|
|
|
private extension MentionCandidateSelectionView {
|
|
|
|
final class Cell : UITableViewCell {
|
|
var mentionCandidate = Mention(hexEncodedPublicKey: "", displayName: "") { didSet { update() } }
|
|
var publicChatServer: String?
|
|
var publicChatServerID: UInt64?
|
|
|
|
// MARK: Components
|
|
private lazy var profilePictureImageView = AvatarImageView()
|
|
|
|
private lazy var moderatorIconImageView: UIImageView = {
|
|
let result = UIImageView(image: #imageLiteral(resourceName: "Crown"))
|
|
return result
|
|
}()
|
|
|
|
private lazy var displayNameLabel: UILabel = {
|
|
let result = UILabel()
|
|
result.textColor = Theme.primaryColor
|
|
result.font = UIFont.ows_dynamicTypeSubheadlineClamped
|
|
result.lineBreakMode = .byTruncatingTail
|
|
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() {
|
|
// Make the cell transparent
|
|
backgroundColor = .clear
|
|
// Set up the profile picture image view
|
|
profilePictureImageView.set(.width, to: 36)
|
|
profilePictureImageView.set(.height, to: 36)
|
|
// Set up the main stack view
|
|
let stackView = UIStackView(arrangedSubviews: [ profilePictureImageView, displayNameLabel ])
|
|
stackView.axis = .horizontal
|
|
stackView.alignment = .center
|
|
stackView.spacing = 16
|
|
stackView.set(.height, to: 36)
|
|
contentView.addSubview(stackView)
|
|
stackView.pin(.leading, to: .leading, of: contentView, withInset: 16)
|
|
stackView.pin(.top, to: .top, of: contentView, withInset: 8)
|
|
contentView.pin(.trailing, to: .trailing, of: stackView, withInset: 16)
|
|
contentView.pin(.bottom, to: .bottom, of: stackView, withInset: 8)
|
|
stackView.set(.width, to: UIScreen.main.bounds.width - 2 * 16)
|
|
// Set up the moderator icon image view
|
|
moderatorIconImageView.set(.width, to: 20)
|
|
moderatorIconImageView.set(.height, to: 20)
|
|
contentView.addSubview(moderatorIconImageView)
|
|
moderatorIconImageView.pin(.trailing, to: .trailing, of: profilePictureImageView)
|
|
moderatorIconImageView.pin(.bottom, to: .bottom, of: profilePictureImageView, withInset: 3.5)
|
|
}
|
|
|
|
// MARK: Updating
|
|
private func update() {
|
|
displayNameLabel.text = mentionCandidate.displayName
|
|
let profilePicture = OWSContactAvatarBuilder(signalId: mentionCandidate.hexEncodedPublicKey, colorName: .blue, diameter: 36).build()
|
|
profilePictureImageView.image = profilePicture
|
|
if let publicChatServer = publicChatServer, let publicChatServerID = publicChatServerID {
|
|
let isUserModerator = LokiPublicChatAPI.isUserModerator(mentionCandidate.hexEncodedPublicKey, for: publicChatServerID, on: publicChatServer)
|
|
moderatorIconImageView.isHidden = !isUserModerator
|
|
} else {
|
|
moderatorIconImageView.isHidden = true
|
|
}
|
|
}
|
|
}
|
|
}
|