Small Settings QOL improvements
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user