Signal-iOS/Signal/src/ViewControllers/ThreadSettings/ReplaceAdminViewController.swift
2021-06-07 11:11:28 -04:00

93 lines
3.2 KiB
Swift

//
// Copyright (c) 2021 Open Whisper Systems. All rights reserved.
//
import Foundation
import UIKit
protocol ReplaceAdminViewControllerDelegate: AnyObject {
func replaceAdmin(uuid: UUID)
}
// MARK: -
class ReplaceAdminViewController: OWSTableViewController2 {
weak var replaceAdminViewControllerDelegate: ReplaceAdminViewControllerDelegate?
private let candidates: Set<SignalServiceAddress>
required init(candidates: Set<SignalServiceAddress>,
replaceAdminViewControllerDelegate: ReplaceAdminViewControllerDelegate) {
assert(!candidates.isEmpty)
self.candidates = candidates
self.replaceAdminViewControllerDelegate = replaceAdminViewControllerDelegate
super.init()
}
// MARK: - View Lifecycle
@objc
public override func viewDidLoad() {
super.viewDidLoad()
title = NSLocalizedString("REPLACE_ADMIN_VIEW_TITLE",
comment: "The title for the 'replace group admin' view.")
tableView.register(ContactTableViewCell.self, forCellReuseIdentifier: ContactTableViewCell.reuseIdentifier)
updateTableContents()
}
private func updateTableContents() {
let contents = OWSTableContents()
let section = OWSTableSection()
let sortedCandidates = databaseStorage.uiRead { transaction in
self.contactsManagerImpl.sortSignalServiceAddresses(Array(self.candidates), transaction: transaction)
}
for address in sortedCandidates {
section.add(OWSTableItem(dequeueCellBlock: { tableView in
guard let cell = tableView.dequeueReusableCell(withIdentifier: ContactTableViewCell.reuseIdentifier) as? ContactTableViewCell else {
owsFailDebug("Missing cell.")
return UITableViewCell()
}
Self.databaseStorage.read { transaction in
let configuration = ContactCellConfiguration.build(address: address,
localUserDisplayMode: .asUser,
transaction: transaction)
let imageView = CVImageView()
imageView.setTemplateImageName("empty-circle-outline-24", tintColor: .ows_gray25)
configuration.accessoryView = ContactCellAccessoryView(accessoryView: imageView, size: .square(24))
cell.configure(configuration: configuration, transaction: transaction)
}
return cell
},
actionBlock: { [weak self] in
self?.candidateWasSelected(candidate: address)
}
))
}
contents.addSection(section)
self.contents = contents
}
private func candidateWasSelected(candidate: SignalServiceAddress) {
guard let replacementAdminUuid = candidate.uuid else {
owsFailDebug("Invalid replacement Admin.")
return
}
replaceAdminViewControllerDelegate?.replaceAdmin(uuid: replacementAdminUuid)
}
}