142 lines
5.7 KiB
Swift
142 lines
5.7 KiB
Swift
|
|
||
|
final class NewClosedGroupVC : UIViewController, UITableViewDataSource, UITableViewDelegate {
|
||
|
|
||
|
private lazy var contacts: [String] = {
|
||
|
var result: [String] = []
|
||
|
TSContactThread.enumerateCollectionObjects { object, _ in
|
||
|
guard let thread = object as? TSContactThread, thread.isContactFriend else { return }
|
||
|
let hexEncodedPublicKey = thread.contactIdentifier()
|
||
|
result.append(hexEncodedPublicKey)
|
||
|
}
|
||
|
func getDisplayName(for hexEncodedPublicKey: String) -> String {
|
||
|
return DisplayNameUtilities.getPrivateChatDisplayName(for: hexEncodedPublicKey) ?? "Unknown Contact"
|
||
|
}
|
||
|
result = result.sorted {
|
||
|
getDisplayName(for: $0) < getDisplayName(for: $1)
|
||
|
}
|
||
|
return result
|
||
|
}()
|
||
|
|
||
|
// MARK: Components
|
||
|
@objc private lazy var tableView: UITableView = {
|
||
|
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: Lifecycle
|
||
|
override func viewDidLoad() {
|
||
|
// Set gradient background
|
||
|
view.backgroundColor = .clear
|
||
|
let gradient = Gradients.defaultLokiBackground
|
||
|
view.setGradient(gradient)
|
||
|
// Set navigation bar background color
|
||
|
let navigationBar = navigationController!.navigationBar
|
||
|
navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
|
||
|
navigationBar.shadowImage = UIImage()
|
||
|
navigationBar.isTranslucent = false
|
||
|
navigationBar.barTintColor = Colors.navigationBarBackground
|
||
|
// Set up table view
|
||
|
view.addSubview(tableView)
|
||
|
tableView.pin(to: view)
|
||
|
}
|
||
|
|
||
|
// MARK: Data
|
||
|
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
||
|
return contacts.count
|
||
|
}
|
||
|
|
||
|
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
||
|
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! Cell
|
||
|
let contact = contacts[indexPath.row]
|
||
|
cell.hexEncodedPublicKey = contact
|
||
|
return cell
|
||
|
}
|
||
|
|
||
|
// MARK: Interaction
|
||
|
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
||
|
// TODO: Implement
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// MARK: - Cell
|
||
|
|
||
|
private extension NewClosedGroupVC {
|
||
|
|
||
|
final class Cell : UITableViewCell {
|
||
|
var hexEncodedPublicKey: String = "" { didSet { update() } }
|
||
|
|
||
|
// MARK: Components
|
||
|
private lazy var profilePictureView = ProfilePictureView()
|
||
|
|
||
|
private lazy var displayNameLabel: UILabel = {
|
||
|
let result = UILabel()
|
||
|
result.textColor = Colors.text
|
||
|
result.font = .boldSystemFont(ofSize: Values.mediumFontSize)
|
||
|
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() {
|
||
|
// Set the cell background color
|
||
|
backgroundColor = Colors.cellBackground
|
||
|
// Set up the highlight color
|
||
|
let selectedBackgroundView = UIView()
|
||
|
selectedBackgroundView.backgroundColor = Colors.cellSelected
|
||
|
self.selectedBackgroundView = selectedBackgroundView
|
||
|
// Set up the profile picture image view
|
||
|
let profilePictureViewSize = Values.smallProfilePictureSize
|
||
|
profilePictureView.set(.width, to: profilePictureViewSize)
|
||
|
profilePictureView.set(.height, to: profilePictureViewSize)
|
||
|
profilePictureView.size = profilePictureViewSize
|
||
|
// Set up the main stack view
|
||
|
let stackView = UIStackView(arrangedSubviews: [ profilePictureView, displayNameLabel ])
|
||
|
stackView.axis = .horizontal
|
||
|
stackView.alignment = .center
|
||
|
stackView.spacing = Values.mediumSpacing
|
||
|
stackView.set(.height, to: profilePictureViewSize)
|
||
|
contentView.addSubview(stackView)
|
||
|
stackView.pin(.leading, to: .leading, of: contentView, withInset: Values.mediumSpacing)
|
||
|
stackView.pin(.top, to: .top, of: contentView, withInset: Values.mediumSpacing)
|
||
|
contentView.pin(.trailing, to: .trailing, of: stackView, withInset: Values.mediumSpacing)
|
||
|
contentView.pin(.bottom, to: .bottom, of: stackView, withInset: Values.mediumSpacing)
|
||
|
stackView.set(.width, to: UIScreen.main.bounds.width - 2 * Values.mediumSpacing)
|
||
|
// Set up the separator
|
||
|
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 = DisplayNameUtilities.getPrivateChatDisplayName(for: hexEncodedPublicKey) ?? "Unknown Contact"
|
||
|
profilePictureView.hexEncodedPublicKey = hexEncodedPublicKey
|
||
|
profilePictureView.update()
|
||
|
}
|
||
|
}
|
||
|
}
|