123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- import UIKit
- class EditSettingsController: UITableViewController, MediaPickerDelegate {
- private let dcContext: DcContext
- weak var coordinator: EditSettingsCoordinator?
- private var displayNameBackup: String?
- private var statusCellBackup: String?
- private let groupBadgeSize: CGFloat = 72
- private let section1 = 0
- private let section1Avatar = 0
- private let section1Name = 1
- private let section1Status = 2
- private let section1RowCount = 3
- private let section2 = 1
- private let section2AccountSettings = 0
- private let section2RowCount = 1
- private let sectionCount = 2
- private let tagAccountSettingsCell = 1
- private var childCoordinators: Coordinator?
- private lazy var statusCell: TextFieldCell = {
- let cell = TextFieldCell(description: String.localized("pref_default_status_label"), placeholder: String.localized("pref_default_status_label"))
- cell.setText(text: DcConfig.selfstatus ?? nil)
- return cell
- }()
- private lazy var accountSettingsCell: UITableViewCell = {
- let cell = UITableViewCell(style: .value1, reuseIdentifier: nil)
- cell.textLabel?.text = String.localized("pref_password_and_account_settings")
- cell.accessoryType = .disclosureIndicator
- cell.tag = tagAccountSettingsCell
- return cell
- }()
- private lazy var avatarSelectionCell: AvatarSelectionCell = {
- return createPictureAndNameCell()
- }()
- private lazy var nameCell: TextFieldCell = {
- let cell = TextFieldCell(description: String.localized("pref_your_name"), placeholder: String.localized("pref_your_name"))
- cell.setText(text: DcConfig.displayname)
- return cell
- }()
- init(dcContext: DcContext) {
- self.dcContext = dcContext
- super.init(style: .grouped)
- hidesBottomBarWhenPushed = true
- }
- required init?(coder aDecoder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- title = String.localized("pref_profile_info_headline")
- avatarSelectionCell.onAvatarTapped = onAvatarTapped
- }
- override func viewWillDisappear(_ animated: Bool) {
- DcConfig.selfstatus = statusCell.getText()
- DcConfig.displayname = nameCell.getText()
- dc_configure(mailboxPointer)
- }
- // MARK: - Table view data source
- override func numberOfSections(in tableView: UITableView) -> Int {
- return sectionCount
- }
- override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- if section == section1 {
- return section1RowCount
- } else {
- return section2RowCount
- }
- }
- override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- if indexPath.section == section1 {
- switch indexPath.row {
- case section1Avatar:
- return avatarSelectionCell
- case section1Name:
- return nameCell
- case section1Status:
- return statusCell
- default:
- return UITableViewCell()
- }
- } else {
- return accountSettingsCell
- }
- }
- override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
- if indexPath.section == section1 && indexPath.row == section1Avatar {
- return AvatarSelectionCell.cellSize
- } else {
- return Constants.defaultCellHeight
- }
- }
- override func tableView(_: UITableView, titleForFooterInSection section: Int) -> String? {
- if section == section1 {
- return String.localized("pref_who_can_see_profile_explain")
- } else {
- return nil
- }
- }
- override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
- guard let cell = tableView.cellForRow(at: indexPath) else { return }
- if cell.tag == tagAccountSettingsCell {
- tableView.deselectRow(at: indexPath, animated: true)
- guard let nc = navigationController else { return }
- let accountSetupVC = AccountSetupController(dcContext: dcContext, editView: true)
- let coordinator = AccountSetupCoordinator(dcContext: dcContext, navigationController: nc)
- self.childCoordinators = coordinator
- accountSetupVC.coordinator = coordinator
- nc.pushViewController(accountSetupVC, animated: true)
- }
- }
- private func galleryButtonPressed(_ action: UIAlertAction) {
- coordinator?.showPhotoPicker(delegate: self)
- }
- private func cameraButtonPressed(_ action: UIAlertAction) {
- coordinator?.showCamera(delegate: self)
- }
- private func onAvatarTapped() {
- let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
- let photoAction = PhotoPickerAlertAction(title: String.localized("gallery"), style: .default, handler: galleryButtonPressed(_:))
- let videoAction = PhotoPickerAlertAction(title: String.localized("camera"), style: .default, handler: cameraButtonPressed(_:))
- alert.addAction(photoAction)
- alert.addAction(videoAction)
- alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
- self.present(alert, animated: true, completion: nil)
- }
- func onImageSelected(image: UIImage) {
- AvatarHelper.saveSelfAvatarImage(image: image)
- self.avatarSelectionCell = createPictureAndNameCell()
- self.avatarSelectionCell.onAvatarTapped = onAvatarTapped
- self.tableView.beginUpdates()
- let indexPath = IndexPath(row: section1Avatar, section: section1)
- self.tableView.reloadRows(at: [indexPath], with: UITableView.RowAnimation.none)
- self.tableView.endUpdates()
- }
- private func createPictureAndNameCell() -> AvatarSelectionCell {
- let cell = AvatarSelectionCell(context: dcContext)
- return cell
- }
- }
|