Small Settings QOL improvements

This commit is contained in:
James Magahern
2023-04-19 14:30:06 -07:00
parent 6a187b0df3
commit 98dfd85781
2 changed files with 60 additions and 5 deletions

View File

@@ -32,18 +32,45 @@ struct TextFieldContentConfiguration : UIContentConfiguration
{
var text: String = ""
var placeholderText: String? = nil
var textChanged: ((String) -> Void)
var textChanged: ((String) -> Void)? = nil
var pressedReturn: ((UITextField) -> Void)? = nil
var keyboardType: UIKeyboardType = .default
var returnKeyType: UIReturnKeyType = .default
func makeContentView() -> UIView & UIContentView {
let textField = UITextField(frame: .zero)
textField.borderStyle = .roundedRect
textField.autocorrectionType = .no
textField.autocapitalizationType = .none
textField.keyboardType = keyboardType
textField.returnKeyType = returnKeyType
return GenericContentView<UITextField, TextFieldContentConfiguration>(configuration: self, view: textField) { config, textField in
textField.text = config.text
textField.placeholder = config.placeholderText
textField.addAction(UIAction { _ in config.textChanged(textField.text ?? "") }, for: .editingChanged)
if let textChanged = config.textChanged {
textField.addAction(UIAction { _ in textChanged(textField.text ?? "") }, for: .editingChanged)
}
if let pressedReturn = config.pressedReturn {
class ReturnDelegate : NSObject, UITextFieldDelegate {
var pressedReturn: ((UITextField) -> Void)
public init(pressedReturn: @escaping ((UITextField) -> Void)) {
self.pressedReturn = pressedReturn
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
pressedReturn(textField)
return false
}
}
let delegate = ReturnDelegate(pressedReturn: pressedReturn)
objc_setAssociatedObject(textField, "returnDelegate", delegate, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
textField.delegate = delegate
}
}
}
@@ -173,7 +200,10 @@ class GeneralSettingsViewController: UIViewController
placeholderText: "https://sync.server.com",
textChanged: { newString in
Settings.shared.syncServer = newString
}
},
pressedReturn: { $0.resignFirstResponder() },
keyboardType: .URL,
returnKeyType: .done
)
}
@@ -203,6 +233,7 @@ class GeneralSettingsViewController: UIViewController
super.init(nibName: nil, bundle: nil)
collectionView.delegate = self
tabBarItem.title = "General"
tabBarItem.image = UIImage(systemName: "gear")
}
@@ -228,3 +259,9 @@ class GeneralSettingsViewController: UIViewController
}
}
extension GeneralSettingsViewController : UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
false
}
}

View File

@@ -229,13 +229,19 @@ class CreateRedirectRuleViewController: UICollectionViewController
sectionHeaderImageView.contentMode = .scaleAspectFit
fromHostField.placeholder = "https://fromhostname.com"
fromHostField.placeholder = "fromhostname.com"
fromHostField.autocorrectionType = .no
fromHostField.autocapitalizationType = .none
fromHostField.keyboardType = .URL
fromHostField.returnKeyType = .next
fromHostField.delegate = self
toHostField.placeholder = "https://tohostname.com"
toHostField.placeholder = "tohostname.com"
toHostField.autocorrectionType = .no
toHostField.autocapitalizationType = .none
toHostField.keyboardType = .URL
toHostField.returnKeyType = .done
toHostField.delegate = self
}
required init?(coder: NSCoder) {
@@ -256,3 +262,15 @@ class CreateRedirectRuleViewController: UICollectionViewController
}
}
extension CreateRedirectRuleViewController : UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField == fromHostField {
toHostField.becomeFirstResponder()
} else if textField == toHostField {
toHostField.resignFirstResponder()
}
return false
}
}