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.
		
		
		
		
		
			
		
			
	
	
		
			78 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			Swift
		
	
		
		
			
		
	
	
			78 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			Swift
		
	
| 
											5 years ago
										 | 
 | ||
| 
											5 years ago
										 | final class UserSelectionVC : BaseVC, UITableViewDataSource, UITableViewDelegate { | ||
| 
											5 years ago
										 |     private let navBarTitle: String | ||
|  |     private let usersToExclude: Set<String> | ||
| 
											5 years ago
										 |     private let completion: (Set<String>) -> Void | ||
|  |     private var selectedUsers: Set<String> = [] | ||
| 
											5 years ago
										 | 
 | ||
|  |     private lazy var users: [String] = { | ||
|  |         var result = ContactUtilities.getAllContacts() | ||
|  |         result.removeAll { usersToExclude.contains($0) } | ||
|  |         return result | ||
|  |     }() | ||
|  | 
 | ||
|  |     // MARK: Components | ||
|  |     @objc private lazy var tableView: UITableView = { | ||
|  |         let result = UITableView() | ||
|  |         result.dataSource = self | ||
| 
											5 years ago
										 |         result.delegate = self | ||
| 
											5 years ago
										 |         result.register(UserCell.self, forCellReuseIdentifier: "UserCell") | ||
|  |         result.separatorStyle = .none | ||
|  |         result.backgroundColor = .clear | ||
|  |         result.showsVerticalScrollIndicator = false | ||
|  |         result.alwaysBounceVertical = false | ||
|  |         return result | ||
|  |     }() | ||
|  | 
 | ||
|  |     // MARK: Lifecycle | ||
| 
											5 years ago
										 |     @objc init(with title: String, excluding usersToExclude: Set<String>, completion: @escaping (Set<String>) -> Void) { | ||
| 
											5 years ago
										 |         self.navBarTitle = title | ||
|  |         self.usersToExclude = usersToExclude | ||
| 
											5 years ago
										 |         self.completion = completion | ||
| 
											5 years ago
										 |         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) | ||
| 
											5 years ago
										 |         navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(handleDoneButtonTapped)) | ||
| 
											5 years ago
										 |         view.addSubview(tableView) | ||
|  |         tableView.pin(to: view) | ||
|  |     } | ||
|  | 
 | ||
|  |     // MARK: Data | ||
|  |     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | ||
|  |         return users.count | ||
|  |     } | ||
|  | 
 | ||
|  |     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | ||
|  |         let cell = tableView.dequeueReusableCell(withIdentifier: "UserCell") as! UserCell | ||
|  |         let publicKey = users[indexPath.row] | ||
|  |         cell.publicKey = publicKey | ||
| 
											5 years ago
										 |         let isSelected = selectedUsers.contains(publicKey) | ||
|  |         cell.accessory = .tick(isSelected: isSelected) | ||
| 
											5 years ago
										 |         cell.update() | ||
|  |         return cell | ||
|  |     } | ||
| 
											5 years ago
										 | 
 | ||
|  |     // MARK: Interaction | ||
|  |     func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { | ||
|  |         let publicKey = users[indexPath.row] | ||
|  |         if !selectedUsers.contains(publicKey) { selectedUsers.insert(publicKey) } else { selectedUsers.remove(publicKey) } | ||
|  |         guard let cell = tableView.cellForRow(at: indexPath) as? UserCell else { return } | ||
|  |         let isSelected = selectedUsers.contains(publicKey) | ||
|  |         cell.accessory = .tick(isSelected: isSelected) | ||
|  |         cell.update() | ||
|  |     } | ||
|  | 
 | ||
|  |     @objc private func handleDoneButtonTapped() { | ||
|  |         completion(selectedUsers) | ||
|  |         navigationController!.popViewController(animated: true) | ||
|  |     } | ||
| 
											5 years ago
										 | } |