forked from evgenmikhaylov/Deboogger
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
Example/DebooggerExample/Plugins/TextFieldTestPlugin.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// | ||
// Copyright © 2020 Rosberry. All rights reserved. | ||
// | ||
|
||
import Deboogger | ||
|
||
final class TextFieldTestPlugin: TextFieldPlugin { | ||
var placeholder: String = "test" | ||
var title: NSAttributedString = .init(string: "TextFieldPlugin") | ||
|
||
func textFieldValueChanged(_ textField: UITextField) { | ||
guard let text = textField.text else { | ||
return | ||
} | ||
print(#function, text) | ||
} | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
Sources/Cells/TextFieldTableViewCell/TextFieldTableViewCell.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// | ||
// Copyright © 2019 Rosberry. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
final class TextFieldTableViewCell: BaseTableViewCell { | ||
|
||
private var plugin: TextFieldPlugin? | ||
|
||
// MARK: - Subviews | ||
|
||
private lazy var textField: UITextField = { | ||
let view = UITextField() | ||
view.borderStyle = .roundedRect | ||
view.delegate = self | ||
view.addTarget(self, action: #selector(textFieldValueChanged), for: .editingChanged) | ||
return view | ||
}() | ||
|
||
// MARK: - Lifecycle | ||
|
||
override func setup() { | ||
super.setup() | ||
accessoryView = textField | ||
} | ||
|
||
override func configure(with plugin: Plugin) { | ||
super.configure(with: plugin) | ||
self.plugin = plugin as? TextFieldPlugin | ||
|
||
if let plugin = self.plugin { | ||
textField.placeholder = plugin.placeholder | ||
textField.text = plugin.initialValue | ||
textField.keyboardType = plugin.keyboardType | ||
} | ||
} | ||
|
||
override func layoutSubviews() { | ||
textField.bounds = CGRect(origin: .zero, size: .init(width: bounds.midX, height: 40)) | ||
super.layoutSubviews() | ||
} | ||
|
||
// MARK: - Actions | ||
|
||
@objc private func textFieldValueChanged(_ sender: UITextField) { | ||
plugin?.textFieldValueChanged(sender) | ||
} | ||
} | ||
|
||
// MARK: - UITextFieldDelegate | ||
|
||
extension TextFieldTableViewCell: UITextFieldDelegate { | ||
func textFieldShouldReturn(_ textField: UITextField) -> Bool { | ||
endEditing(true) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// | ||
// Copyright © 2020 Rosberry. All rights reserved. | ||
// | ||
|
||
public protocol TextFieldPlugin: Plugin { | ||
|
||
var placeholder: String { get } | ||
var keyboardType: UIKeyboardType { get } | ||
var initialValue: String? { get } | ||
|
||
func textFieldValueChanged(_ textField: UITextField) | ||
} | ||
|
||
public extension TextFieldPlugin { | ||
var cellClass: BaseTableViewCell.Type { | ||
return TextFieldTableViewCell.self | ||
} | ||
|
||
var placeholder: String { | ||
"Enter text" | ||
} | ||
|
||
var keyboardType: UIKeyboardType { | ||
.default | ||
} | ||
|
||
var initialValue: String? { | ||
nil | ||
} | ||
} |