mirror of https://github.com/oxen-io/session-ios
				
				
				
			Create more re-usable components
							parent
							
								
									4d9b7bffa9
								
							
						
					
					
						commit
						23599b1418
					
				@ -0,0 +1,17 @@
 | 
			
		||||
 | 
			
		||||
enum ContactUtilities {
 | 
			
		||||
 | 
			
		||||
    static func getAllContacts() -> [String] {
 | 
			
		||||
        var result: [String] = []
 | 
			
		||||
        Storage.read { transaction in
 | 
			
		||||
            TSContactThread.enumerateCollectionObjects(with: transaction) { object, _ in
 | 
			
		||||
                guard let thread = object as? TSContactThread else { return }
 | 
			
		||||
                result.append(thread.contactIdentifier())
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        func getDisplayName(for publicKey: String) -> String {
 | 
			
		||||
            return UserDisplayNameUtilities.getPrivateChatDisplayName(for: publicKey) ?? publicKey
 | 
			
		||||
        }
 | 
			
		||||
        return result.sorted { getDisplayName(for: $0) < getDisplayName(for: $1) }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -0,0 +1,56 @@
 | 
			
		||||
 | 
			
		||||
final class UserSelectionVC : BaseVC, UITableViewDataSource {
 | 
			
		||||
    private let navBarTitle: String
 | 
			
		||||
    private let usersToExclude: Set<String>
 | 
			
		||||
 | 
			
		||||
    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
 | 
			
		||||
        result.register(UserCell.self, forCellReuseIdentifier: "UserCell")
 | 
			
		||||
        result.separatorStyle = .none
 | 
			
		||||
        result.backgroundColor = .clear
 | 
			
		||||
        result.showsVerticalScrollIndicator = false
 | 
			
		||||
        result.alwaysBounceVertical = false
 | 
			
		||||
        return result
 | 
			
		||||
    }()
 | 
			
		||||
 | 
			
		||||
    // MARK: Lifecycle
 | 
			
		||||
    @objc init(with title: String, excluding usersToExclude: Set<String>) {
 | 
			
		||||
        self.navBarTitle = title
 | 
			
		||||
        self.usersToExclude = usersToExclude
 | 
			
		||||
        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)
 | 
			
		||||
        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
 | 
			
		||||
        cell.accessory = .tick(isSelected: false)
 | 
			
		||||
        cell.update()
 | 
			
		||||
        return cell
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
					Loading…
					
					
				
		Reference in New Issue