Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve "Edit Name" dialog #2288

Merged
merged 2 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 48 additions & 20 deletions deltachat-ios/Controller/EditContactController.swift
Original file line number Diff line number Diff line change
@@ -1,37 +1,65 @@
import UIKit
import DcCore

class EditContactController: NewContactController {
class EditContactController: UITableViewController {
let dcContext: DcContext
let dcContact: DcContact
let authNameOrAddr: String
let nameCell = TextFieldCell.makeNameCell()
let cells: [UITableViewCell]

init(dcContext: DcContext, contactIdForUpdate: Int) {
super.init(dcContext: dcContext)
title = String.localized("edit_contact")
self.dcContext = dcContext
dcContact = dcContext.getContact(id: contactIdForUpdate)
authNameOrAddr = dcContact.authName.isEmpty ? dcContact.email : dcContact.authName
cells = [nameCell]
super.init(style: .grouped)

let contact = dcContext.getContact(id: contactIdForUpdate)
nameCell.textFieldDelegate = self
nameCell.textField.text = dcContact.editedName
nameCell.textField.enablesReturnKeyAutomatically = false
nameCell.textField.returnKeyType = .done
nameCell.useFullWidth()
nameCell.placeholder = String.localizedStringWithFormat(String.localized("edit_name_placeholder"), authNameOrAddr)

nameCell.textField.text = contact.editedName
if !contact.authName.isEmpty { // else show string "Name" as set by super.init()
nameCell.placeholder = contact.authName
}
emailCell.textField.text = contact.email
emailCell.textField.isEnabled = false // only contact name can be edited
emailCell.contentView.alpha = 0.3

model.name = contact.editedName
model.email = contact.email

doneButton = UIBarButtonItem(barButtonSystemItem: .save, target: self, action: #selector(saveContactButtonPressed))
doneButton?.isEnabled = contactIsValid()
navigationItem.rightBarButtonItem = doneButton
title = String.localized("menu_edit_name")
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .save, target: self, action: #selector(saveButtonPressed))
navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(cancelButtonPressed))
}

required init?(coder _: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

@objc override func saveContactButtonPressed() {
_ = dcContext.createContact(name: model.name, email: model.email)
override func viewDidAppear(_: Bool) {
nameCell.textField.becomeFirstResponder()
}

@objc func saveButtonPressed() {
_ = dcContext.createContact(name: nameCell.textField.text ?? "", email: dcContact.email)
navigationController?.popViewController(animated: true)
}

@objc func cancelButtonPressed() {
navigationController?.popViewController(animated: true)
}

override func tableView(_: UITableView, numberOfRowsInSection _: Int) -> Int {
return cells.count
}

override func tableView(_: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
return cells[indexPath.row]
}

override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
return String.localizedStringWithFormat(String.localized("edit_name_explain"), authNameOrAddr)
}
}

extension EditContactController: UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
saveButtonPressed()
return true
}
}
5 changes: 5 additions & 0 deletions deltachat-ios/View/TextFieldCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ class TextFieldCell: UITableViewCell {
textField.text = text
}

func useFullWidth() {
title.isHidden = true
textField.textAlignment = .left
}

override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
if previousTraitCollection?.preferredContentSizeCategory !=
traitCollection.preferredContentSizeCategory {
Expand Down
Loading