session-ios/Session/Shared/UserSelectionVC.swift

104 lines
3.5 KiB
Swift
Raw Permalink Normal View History

// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
import UIKit
import SessionUIKit
import SessionMessagingKit
2020-09-28 03:43:15 +02:00
final class UserSelectionVC: BaseVC, UITableViewDataSource, UITableViewDelegate {
2020-09-28 03:43:15 +02:00
private let navBarTitle: String
private let usersToExclude: Set<String>
2020-09-28 03:55:56 +02:00
private let completion: (Set<String>) -> Void
private var selectedUsers: Set<String> = []
2020-09-28 03:43:15 +02:00
private lazy var users: [Profile] = {
return Profile
.fetchAllContactProfiles(excluding: usersToExclude)
2020-09-28 03:43:15 +02:00
}()
// MARK: - Components
2020-09-28 03:43:15 +02:00
@objc private lazy var tableView: UITableView = {
let result: UITableView = UITableView()
2020-09-28 03:43:15 +02:00
result.dataSource = self
2020-09-28 03:55:56 +02:00
result.delegate = self
2020-09-28 03:43:15 +02:00
result.separatorStyle = .none
result.themeBackgroundColor = .clear
2020-09-28 03:43:15 +02:00
result.showsVerticalScrollIndicator = false
result.alwaysBounceVertical = false
result.register(view: SessionCell.self)
2020-09-28 03:43:15 +02:00
return result
}()
// MARK: - Lifecycle
2021-05-07 05:50:32 +02:00
init(with title: String, excluding usersToExclude: Set<String>, completion: @escaping (Set<String>) -> Void) {
2020-09-28 03:43:15 +02:00
self.navBarTitle = title
self.usersToExclude = usersToExclude
2020-09-28 03:55:56 +02:00
self.completion = completion
2020-09-28 03:43:15 +02:00
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) { preconditionFailure("Use init(excluding:) instead.") }
override init(nibName: String?, bundle: Bundle?) { preconditionFailure("Use init(excluding:) instead.") }
2020-09-28 03:43:15 +02:00
override func viewDidLoad() {
super.viewDidLoad()
2020-09-28 03:43:15 +02:00
setNavBarTitle(navBarTitle)
let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(handleDoneButtonTapped))
doneButton.accessibilityLabel = "Done"
navigationItem.rightBarButtonItem = doneButton
2020-09-28 03:43:15 +02:00
view.addSubview(tableView)
tableView.pin(to: view)
}
// MARK: - UITableViewDataSource
2020-09-28 03:43:15 +02:00
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return users.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: SessionCell = tableView.dequeue(type: SessionCell.self, for: indexPath)
let profile: Profile = users[indexPath.row]
cell.update(
with: SessionCell.Info(
id: profile,
position: Position.with(indexPath.row, count: users.count),
leftAccessory: .profile(id: profile.id, profile: profile),
title: profile.displayName(),
rightAccessory: .radio(isSelected: { [weak self] in
self?.selectedUsers.contains(profile.id) == true
}),
styling: SessionCell.StyleInfo(backgroundStyle: .edgeToEdge),
2023-04-26 07:13:39 +02:00
accessibility: Accessibility(identifier: "Contact")
)
)
2020-09-28 03:43:15 +02:00
return cell
}
2020-09-28 03:55:56 +02:00
// MARK: - Interaction
2020-09-28 03:55:56 +02:00
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)
2020-09-28 03:55:56 +02:00
}
@objc private func handleDoneButtonTapped() {
completion(selectedUsers)
navigationController!.popViewController(animated: true)
}
2020-09-28 03:43:15 +02:00
}