session-ios/Session/Shared/UserSelectionVC.swift
Morgan Pretty 06eef99766 Cleared out some legacy code, fixed a few bugs, got typing indicators and mentions working
Got mentions working again
Got typing indicators working again
Got the notification sound and preview preferences working
Fixed a few issues with attachment image loading
Fixed an issue where enum settings weren't getting stored correctly
2022-05-10 17:42:15 +10:00

93 lines
3.1 KiB
Swift

// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
import UIKit
import SessionMessagingKit
@objc(SNUserSelectionVC)
final class UserSelectionVC: BaseVC, UITableViewDataSource, UITableViewDelegate {
private let navBarTitle: String
private let usersToExclude: Set<String>
private let completion: (Set<String>) -> Void
private var selectedUsers: Set<String> = []
private lazy var users: [Profile] = {
return Profile
.fetchAllContactProfiles(excluding: usersToExclude)
}()
// MARK: - Components
@objc private lazy var tableView: UITableView = {
let result: UITableView = UITableView()
result.dataSource = self
result.delegate = self
result.separatorStyle = .none
result.backgroundColor = .clear
result.showsVerticalScrollIndicator = false
result.alwaysBounceVertical = false
result.register(view: UserCell.self)
return result
}()
// MARK: - Lifecycle
@objc(initWithTitle:excluding:completion:)
init(with title: String, excluding usersToExclude: Set<String>, completion: @escaping (Set<String>) -> Void) {
self.navBarTitle = title
self.usersToExclude = usersToExclude
self.completion = completion
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) { preconditionFailure("Use UserSelectionVC.init(excluding:) instead.") }
override init(nibName: String?, bundle: Bundle?) { preconditionFailure("Use UserSelectionVC.init(excluding:) instead.") }
override func viewDidLoad() {
super.viewDidLoad()
setUpGradientBackground()
setUpNavBarStyle()
setNavBarTitle(navBarTitle)
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(handleDoneButtonTapped))
view.addSubview(tableView)
tableView.pin(to: view)
}
// MARK: - UITableViewDataSource
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return users.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: UserCell = tableView.dequeue(type: UserCell.self, for: indexPath)
cell.update(
with: users[indexPath.row].id,
profile: users[indexPath.row],
isZombie: false,
accessory: .tick(isSelected: selectedUsers.contains(users[indexPath.row].id))
)
return cell
}
// MARK: - Interaction
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if !selectedUsers.contains(users[indexPath.row].id) {
selectedUsers.insert(users[indexPath.row].id)
}
else {
selectedUsers.remove(users[indexPath.row].id)
}
tableView.deselectRow(at: indexPath, animated: true)
tableView.reloadRows(at: [indexPath], with: .none)
}
@objc private func handleDoneButtonTapped() {
completion(selectedUsers)
navigationController!.popViewController(animated: true)
}
}