2020-07-08 06:49:18 +02:00
|
|
|
import PromiseKit
|
2020-01-28 06:27:42 +01:00
|
|
|
|
2020-09-30 05:52:49 +02:00
|
|
|
private protocol TableViewTouchDelegate {
|
2020-01-28 06:27:42 +01:00
|
|
|
|
2020-09-30 05:52:49 +02:00
|
|
|
func tableViewWasTouched(_ tableView: TableView)
|
|
|
|
}
|
|
|
|
|
|
|
|
private final class TableView : UITableView {
|
|
|
|
var touchDelegate: TableViewTouchDelegate?
|
|
|
|
|
|
|
|
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
|
|
|
|
touchDelegate?.tableViewWasTouched(self)
|
|
|
|
return super.hitTest(point, with: event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
final class NewClosedGroupVC : BaseVC, UITableViewDataSource, UITableViewDelegate, TableViewTouchDelegate, UITextFieldDelegate, UIScrollViewDelegate {
|
|
|
|
private let contacts = ContactUtilities.getAllContacts()
|
|
|
|
private var selectedContacts: Set<String> = []
|
2020-01-28 06:27:42 +01:00
|
|
|
|
|
|
|
// MARK: Components
|
2020-07-27 03:32:47 +02:00
|
|
|
private lazy var nameTextField = TextField(placeholder: NSLocalizedString("vc_create_closed_group_text_field_hint", comment: ""))
|
2020-09-30 05:52:49 +02:00
|
|
|
|
|
|
|
private lazy var tableView: TableView = {
|
|
|
|
let result = TableView()
|
2020-01-28 06:27:42 +01:00
|
|
|
result.dataSource = self
|
|
|
|
result.delegate = self
|
2020-09-30 05:52:49 +02:00
|
|
|
result.touchDelegate = self
|
|
|
|
result.register(UserCell.self, forCellReuseIdentifier: "UserCell")
|
2020-01-28 06:27:42 +01:00
|
|
|
result.separatorStyle = .none
|
|
|
|
result.backgroundColor = .clear
|
2020-06-25 09:36:32 +02:00
|
|
|
result.isScrollEnabled = false
|
2020-01-28 06:27:42 +01:00
|
|
|
return result
|
|
|
|
}()
|
|
|
|
|
|
|
|
// MARK: Lifecycle
|
|
|
|
override func viewDidLoad() {
|
2020-02-20 04:37:17 +01:00
|
|
|
super.viewDidLoad()
|
2020-05-30 00:20:30 +02:00
|
|
|
setUpGradientBackground()
|
|
|
|
setUpNavBarStyle()
|
2020-06-25 09:36:32 +02:00
|
|
|
let customTitleFontSize = Values.largeFontSize
|
2020-07-27 03:32:47 +02:00
|
|
|
setNavBarTitle(NSLocalizedString("vc_create_closed_group_title", comment: ""), customFontSize: customTitleFontSize)
|
2020-01-29 00:18:45 +01:00
|
|
|
// Set up navigation bar buttons
|
|
|
|
let closeButton = UIBarButtonItem(image: #imageLiteral(resourceName: "X"), style: .plain, target: self, action: #selector(close))
|
|
|
|
closeButton.tintColor = Colors.text
|
|
|
|
navigationItem.leftBarButtonItem = closeButton
|
|
|
|
let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(createClosedGroup))
|
|
|
|
doneButton.tintColor = Colors.text
|
|
|
|
navigationItem.rightBarButtonItem = doneButton
|
2020-01-29 03:32:08 +01:00
|
|
|
// Set up content
|
2020-09-30 05:52:49 +02:00
|
|
|
setUpViewHierarchy()
|
|
|
|
}
|
|
|
|
|
|
|
|
private func setUpViewHierarchy() {
|
2020-01-29 03:32:08 +01:00
|
|
|
if !contacts.isEmpty {
|
2020-06-25 09:36:32 +02:00
|
|
|
let mainStackView = UIStackView()
|
|
|
|
mainStackView.axis = .vertical
|
|
|
|
nameTextField.delegate = self
|
|
|
|
let nameTextFieldContainer = UIView()
|
|
|
|
nameTextFieldContainer.addSubview(nameTextField)
|
|
|
|
nameTextField.pin(.leading, to: .leading, of: nameTextFieldContainer, withInset: Values.largeSpacing)
|
|
|
|
nameTextField.pin(.top, to: .top, of: nameTextFieldContainer, withInset: Values.mediumSpacing)
|
|
|
|
nameTextFieldContainer.pin(.trailing, to: .trailing, of: nameTextField, withInset: Values.largeSpacing)
|
|
|
|
nameTextFieldContainer.pin(.bottom, to: .bottom, of: nameTextField, withInset: Values.largeSpacing)
|
|
|
|
mainStackView.addArrangedSubview(nameTextFieldContainer)
|
2020-01-30 01:59:12 +01:00
|
|
|
let separator = UIView()
|
2020-09-30 05:52:49 +02:00
|
|
|
separator.backgroundColor = Colors.separator
|
2020-01-30 01:59:12 +01:00
|
|
|
separator.set(.height, to: Values.separatorThickness)
|
2020-06-25 09:36:32 +02:00
|
|
|
mainStackView.addArrangedSubview(separator)
|
|
|
|
tableView.set(.height, to: CGFloat(contacts.count * 67)) // A cell is exactly 67 points high
|
|
|
|
tableView.set(.width, to: UIScreen.main.bounds.width)
|
|
|
|
mainStackView.addArrangedSubview(tableView)
|
|
|
|
let scrollView = UIScrollView(wrapping: mainStackView, withInsets: UIEdgeInsets.zero)
|
|
|
|
scrollView.showsVerticalScrollIndicator = false
|
|
|
|
scrollView.delegate = self
|
|
|
|
view.addSubview(scrollView)
|
|
|
|
scrollView.set(.width, to: UIScreen.main.bounds.width)
|
|
|
|
scrollView.pin(to: view)
|
2020-01-29 03:32:08 +01:00
|
|
|
} else {
|
|
|
|
let explanationLabel = UILabel()
|
|
|
|
explanationLabel.textColor = Colors.text
|
|
|
|
explanationLabel.font = .systemFont(ofSize: Values.smallFontSize)
|
|
|
|
explanationLabel.numberOfLines = 0
|
|
|
|
explanationLabel.lineBreakMode = .byWordWrapping
|
|
|
|
explanationLabel.textAlignment = .center
|
2020-07-27 03:32:47 +02:00
|
|
|
explanationLabel.text = NSLocalizedString("vc_create_closed_group_empty_state_message", comment: "")
|
2020-02-20 04:37:17 +01:00
|
|
|
let createNewPrivateChatButton = Button(style: .prominentOutline, size: .large)
|
2020-07-27 03:32:47 +02:00
|
|
|
createNewPrivateChatButton.setTitle(NSLocalizedString("vc_create_closed_group_empty_state_button_title", comment: ""), for: UIControl.State.normal)
|
2020-03-02 06:27:33 +01:00
|
|
|
createNewPrivateChatButton.addTarget(self, action: #selector(createNewPrivateChat), for: UIControl.Event.touchUpInside)
|
2020-07-27 05:35:00 +02:00
|
|
|
createNewPrivateChatButton.set(.width, to: 196)
|
2020-01-29 03:32:08 +01:00
|
|
|
let stackView = UIStackView(arrangedSubviews: [ explanationLabel, createNewPrivateChatButton ])
|
|
|
|
stackView.axis = .vertical
|
|
|
|
stackView.spacing = Values.mediumSpacing
|
|
|
|
stackView.alignment = .center
|
|
|
|
view.addSubview(stackView)
|
|
|
|
stackView.center(.horizontal, in: view)
|
|
|
|
let verticalCenteringConstraint = stackView.center(.vertical, in: view)
|
|
|
|
verticalCenteringConstraint.constant = -16 // Makes things appear centered visually
|
|
|
|
}
|
2020-01-28 06:27:42 +01:00
|
|
|
}
|
|
|
|
|
2020-09-30 05:52:49 +02:00
|
|
|
// MARK: Table View Data Source
|
2020-01-28 06:27:42 +01:00
|
|
|
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
|
|
|
return contacts.count
|
|
|
|
}
|
|
|
|
|
|
|
|
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
2020-09-30 05:52:49 +02:00
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: "UserCell") as! UserCell
|
|
|
|
let publicKey = contacts[indexPath.row]
|
|
|
|
cell.publicKey = publicKey
|
|
|
|
let isSelected = selectedContacts.contains(publicKey)
|
|
|
|
cell.accessory = .tick(isSelected: isSelected)
|
|
|
|
cell.update()
|
2020-01-28 06:27:42 +01:00
|
|
|
return cell
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Interaction
|
2020-06-25 09:36:32 +02:00
|
|
|
func textFieldDidEndEditing(_ textField: UITextField) {
|
2020-07-27 03:32:47 +02:00
|
|
|
crossfadeLabel.text = textField.text!.isEmpty ? NSLocalizedString("vc_create_closed_group_title", comment: "") : textField.text!
|
2020-06-25 09:36:32 +02:00
|
|
|
}
|
|
|
|
|
2020-09-30 05:52:49 +02:00
|
|
|
fileprivate func tableViewWasTouched(_ tableView: TableView) {
|
2020-06-25 09:36:32 +02:00
|
|
|
if nameTextField.isFirstResponder {
|
|
|
|
nameTextField.resignFirstResponder()
|
|
|
|
}
|
2020-09-30 05:52:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func scrollViewDidScroll(_ scrollView: UIScrollView) {
|
2020-06-25 09:36:32 +02:00
|
|
|
let nameTextFieldCenterY = nameTextField.convert(nameTextField.bounds.center, to: scrollView).y
|
|
|
|
let tableViewOriginY = tableView.convert(tableView.bounds.origin, to: scrollView).y
|
|
|
|
let titleLabelAlpha = 1 - (scrollView.contentOffset.y - nameTextFieldCenterY) / (tableViewOriginY - nameTextFieldCenterY)
|
|
|
|
let crossfadeLabelAlpha = 1 - titleLabelAlpha
|
|
|
|
navBarTitleLabel.alpha = titleLabelAlpha
|
|
|
|
crossfadeLabel.alpha = crossfadeLabelAlpha
|
|
|
|
}
|
|
|
|
|
2020-01-28 06:27:42 +01:00
|
|
|
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
2020-09-30 05:52:49 +02:00
|
|
|
let publicKey = contacts[indexPath.row]
|
|
|
|
if !selectedContacts.contains(publicKey) { selectedContacts.insert(publicKey) } else { selectedContacts.remove(publicKey) }
|
|
|
|
guard let cell = tableView.cellForRow(at: indexPath) as? UserCell else { return }
|
|
|
|
let isSelected = selectedContacts.contains(publicKey)
|
|
|
|
cell.accessory = .tick(isSelected: isSelected)
|
|
|
|
cell.update()
|
2020-01-29 00:18:45 +01:00
|
|
|
tableView.deselectRow(at: indexPath, animated: true)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc private func close() {
|
|
|
|
dismiss(animated: true, completion: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc private func createClosedGroup() {
|
2020-01-30 01:59:12 +01:00
|
|
|
func showError(title: String, message: String = "") {
|
|
|
|
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
|
|
|
|
alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: ""), style: .default, handler: nil))
|
|
|
|
presentAlert(alert)
|
|
|
|
}
|
|
|
|
guard let name = nameTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines), name.count > 0 else {
|
2020-07-27 03:32:47 +02:00
|
|
|
return showError(title: NSLocalizedString("vc_create_closed_group_group_name_missing_error", comment: ""))
|
2020-01-30 01:59:12 +01:00
|
|
|
}
|
2020-11-23 06:35:49 +01:00
|
|
|
guard name.count < 64 else {
|
2020-07-27 03:32:47 +02:00
|
|
|
return showError(title: NSLocalizedString("vc_create_closed_group_group_name_too_long_error", comment: ""))
|
2020-01-30 01:59:12 +01:00
|
|
|
}
|
2020-08-26 04:54:28 +02:00
|
|
|
guard selectedContacts.count >= 1 else {
|
2020-08-27 01:17:57 +02:00
|
|
|
return showError(title: "Please pick at least 1 group member")
|
2020-01-30 01:59:12 +01:00
|
|
|
}
|
2021-01-19 03:51:26 +01:00
|
|
|
guard selectedContacts.count < 100 else { // Minus one because we're going to include self later
|
2020-07-27 03:32:47 +02:00
|
|
|
return showError(title: NSLocalizedString("vc_create_closed_group_too_many_group_members_error", comment: ""))
|
2020-02-17 00:58:42 +01:00
|
|
|
}
|
2020-06-25 09:36:32 +02:00
|
|
|
let selectedContacts = self.selectedContacts
|
2021-01-19 04:12:32 +01:00
|
|
|
let message: String? = (selectedContacts.count > 20) ? "Please wait while the group is created..." : nil
|
|
|
|
ModalActivityIndicatorViewController.present(fromViewController: navigationController!, message: message) { [weak self] _ in
|
2020-11-16 00:34:47 +01:00
|
|
|
var promise: Promise<TSGroupThread>!
|
|
|
|
Storage.writeSync { transaction in
|
2021-01-13 05:25:38 +01:00
|
|
|
promise = MessageSender.createClosedGroup(name: name, members: selectedContacts, transaction: transaction)
|
2020-11-16 00:34:47 +01:00
|
|
|
}
|
|
|
|
let _ = promise.done(on: DispatchQueue.main) { thread in
|
2021-01-13 05:25:38 +01:00
|
|
|
let appDelegate = UIApplication.shared.delegate as! AppDelegate
|
|
|
|
appDelegate.forceSyncConfigurationNowIfNeeded().retainUntilComplete() // FIXME: It's probably cleaner to do this inside createClosedGroup(...)
|
2020-07-08 06:49:18 +02:00
|
|
|
self?.presentingViewController?.dismiss(animated: true, completion: nil)
|
|
|
|
SignalApp.shared().presentConversation(for: thread, action: .compose, animated: false)
|
2020-11-16 00:34:47 +01:00
|
|
|
}
|
|
|
|
promise.catch(on: DispatchQueue.main) { _ in
|
2020-07-27 01:17:49 +02:00
|
|
|
self?.dismiss(animated: true, completion: nil) // Dismiss the loader
|
2020-07-27 03:32:47 +02:00
|
|
|
let title = "Couldn't Create Group"
|
|
|
|
let message = "Please check your internet connection and try again."
|
2020-07-08 06:49:18 +02:00
|
|
|
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
|
|
|
|
alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: ""), style: .default, handler: nil))
|
|
|
|
self?.presentAlert(alert)
|
2020-01-29 00:18:45 +01:00
|
|
|
}
|
|
|
|
}
|
2020-01-28 06:27:42 +01:00
|
|
|
}
|
2020-01-29 03:32:08 +01:00
|
|
|
|
2020-03-02 06:27:33 +01:00
|
|
|
@objc private func createNewPrivateChat() {
|
2020-01-29 03:32:08 +01:00
|
|
|
presentingViewController?.dismiss(animated: true, completion: nil)
|
2020-03-02 06:27:33 +01:00
|
|
|
SignalApp.shared().homeViewController!.createNewPrivateChat()
|
2020-01-29 03:32:08 +01:00
|
|
|
}
|
2020-01-28 06:27:42 +01:00
|
|
|
}
|