KMBaseTextFieldPresenter.swift 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. //
  2. // KMBaseTextFieldPresenter.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2022/12/23.
  6. //
  7. import Cocoa
  8. class KMBaseTextFieldPresenter: NSObject {
  9. var model: KMBaseTextFieldModel = KMBaseTextFieldModel()
  10. var delegate: KMBaseTextFieldPresenterDelegate?
  11. func initPresenter(delegate: KMBaseTextFieldPresenterDelegate, model: KMBaseTextFieldModel) {
  12. self.delegate = delegate
  13. self.model = model
  14. if self.delegate != nil {
  15. self.delegate?.valueDidChange(presenter: self, model: self.model)
  16. }
  17. }
  18. }
  19. extension KMBaseTextFieldPresenter: KMBaseTextFieldPresenterProtocol {
  20. func changeValue(value: String) {
  21. let string = value.replacingOccurrences(of: model.specialChart, with: "")
  22. if model.onlyNumber {
  23. if model.isCanNull {
  24. if string == "" {
  25. self.model.value = ""
  26. } else {
  27. self.model.value = String(min(max(Int(string) ?? 0, model.minValue),model.maxValue))
  28. }
  29. } else {
  30. self.model.value = String(min(max(Int(string) ?? 0, model.minValue),model.maxValue))
  31. }
  32. } else {
  33. self.model.value = string
  34. }
  35. if self.delegate != nil {
  36. self.delegate?.valueDidChange(presenter: self, model: self.model)
  37. }
  38. }
  39. }
  40. protocol KMBaseTextFieldPresenterDelegate: AnyObject {
  41. func valueDidChange(presenter: KMBaseTextFieldPresenter, model: KMBaseTextFieldModel)
  42. }
  43. protocol KMBaseTextFieldPresenterProtocol: AnyObject {
  44. func changeValue(value: String)
  45. }
  46. protocol KMBaseTextFieldPresenterPrivate {}