Skip to content

Commit

Permalink
improve submitting with editingformviewcontroller
Browse files Browse the repository at this point in the history
  • Loading branch information
phillipthelen committed Oct 18, 2024
1 parent 809aeca commit 40aa0d1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ extension EditingFormViewController {
controller.autoDismiss = false
controller.formTitle = title
controller.saveButtonTitle = saveButtonTitle
controller.fields.append(EditingTextField(key: "username", title: subtitle, type: .name, placeholder: L10n.username))
controller.fields.append(EditingTextField(key: "username", title: subtitle, type: .name, submitOnEnter: true, placeholder: L10n.username))
controller.onSave = {values in
controller.isLoading(true)
if let username = values["username"] {
Expand Down
9 changes: 9 additions & 0 deletions HabitRPG/UI/Settings/EditingFormViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,12 @@ class EditingTextField: UIStackView, UITextFieldDelegate {

let key: String
private let type: EditingTextFieldType
let submitOnEnter: Bool

init(key: String, title: String, type: EditingTextFieldType, submitOnEnter: Bool = false, placeholder: String? = nil, value: String? = nil) {
self.key = key
self.type = type
self.submitOnEnter = submitOnEnter
super.init(frame: CGRect.zero)
textField.delegate = self
textField.text = value
Expand Down Expand Up @@ -242,6 +244,13 @@ class EditingFormViewController: UIViewController, Themeable {
stackView.addArrangedSubview(field)
}
stackView.addArrangedSubview(loadingIndicator)

if let field = fields.last, field.submitOnEnter {
field.textField.returnKeyType = .done
if !field.textField.allTargets.contains(self) {
field.textField.addTarget(self, action: #selector(saveForm), for: .editingDidEndOnExit)
}
}
}

override func viewDidAppear(_ animated: Bool) {
Expand Down

0 comments on commit 40aa0d1

Please sign in to comment.