Signal-iOS/Signal/src/ViewControllers/ThreadSettings/GroupNameViewController.swift
2021-05-17 09:58:35 -03:00

145 lines
3.9 KiB
Swift

//
// Copyright (c) 2021 Open Whisper Systems. All rights reserved.
//
import Foundation
protocol GroupNameViewControllerDelegate: class {
func groupNameViewControllerDidComplete(groupName: String?)
}
@objc
class GroupNameViewController: OWSTableViewController2 {
private let helper: GroupAttributesEditorHelper
weak var nameDelegate: GroupNameViewControllerDelegate?
required init(groupModel: TSGroupModel, groupNameCurrent: String? = nil) {
self.helper = GroupAttributesEditorHelper(groupModel: groupModel)
if let groupNameCurrent = groupNameCurrent {
self.helper.groupNameOriginal = groupNameCurrent
}
super.init()
shouldAvoidKeyboard = true
}
// MARK: -
override func viewDidLoad() {
super.viewDidLoad()
helper.delegate = self
helper.buildContents()
updateNavigation()
updateTableContents()
}
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
coordinator.animate(alongsideTransition: { (_) in
self.helper.descriptionTextView.scrollToFocus(in: self.tableView, animated: true)
}, completion: nil)
}
// Don't allow interactive dismiss when there are unsaved changes.
override var isModalInPresentation: Bool {
get { helper.hasUnsavedChanges }
set {}
}
private func updateNavigation() {
title = NSLocalizedString(
"GROUP_NAME_VIEW_TITLE",
comment: "Title for the group name view."
)
navigationItem.leftBarButtonItem = UIBarButtonItem(
barButtonSystemItem: .cancel,
target: self,
action: #selector(didTapCancel),
accessibilityIdentifier: "cancel_button"
)
if helper.hasUnsavedChanges {
navigationItem.rightBarButtonItem = UIBarButtonItem(
barButtonSystemItem: .done,
target: self,
action: #selector(didTapDone),
accessibilityIdentifier: "done_button"
)
} else {
navigationItem.rightBarButtonItem = nil
}
}
public override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
updateNavigation()
helper.nameTextField.becomeFirstResponder()
}
func updateTableContents() {
let contents = OWSTableContents()
let nameTextField = helper.nameTextField
let section = OWSTableSection()
section.add(.init(
customCellBlock: {
let cell = OWSTableItem.newCell()
cell.selectionStyle = .none
nameTextField.font = .ows_dynamicTypeBodyClamped
nameTextField.textColor = Theme.primaryTextColor
cell.contentView.addSubview(nameTextField)
nameTextField.autoPinEdgesToSuperviewMargins()
return cell
},
actionBlock: {
nameTextField.becomeFirstResponder()
}
))
contents.addSection(section)
self.contents = contents
}
@objc
func didTapCancel() {
guard helper.hasUnsavedChanges else {
dismiss(animated: true)
return
}
OWSActionSheets.showPendingChangesActionSheet(discardAction: { [weak self] in
self?.dismiss(animated: true)
})
}
@objc
func didTapDone() {
helper.nameTextField.acceptAutocorrectSuggestion()
nameDelegate?.groupNameViewControllerDidComplete(groupName: helper.groupNameCurrent)
dismiss(animated: true)
}
}
extension GroupNameViewController: GroupAttributesEditorHelperDelegate {
func groupAttributesEditorContentsDidChange() {
updateNavigation()
}
func groupAttributesEditorSelectionDidChange() {}
}