Quellcode durchsuchen

feat: 补充价格的输入限制以及提示

陈文艺 vor 2 Monaten
Ursprung
Commit
8a3a21dd16

+ 23 - 0
Lanu/Localizable.xcstrings

@@ -8947,6 +8947,29 @@
           }
         }
       }
+    },
+    "B00102" : {
+      "extractionState" : "manual",
+      "localizations" : {
+        "en" : {
+          "stringUnit" : {
+            "state" : "translated",
+            "value" : "Must be between %1$@-%2$@ gold coins"
+          }
+        },
+        "id" : {
+          "stringUnit" : {
+            "state" : "translated",
+            "value" : "Harus di antara %1$@-%2$@ koin emas"
+          }
+        },
+        "zh-Hans" : {
+          "stringUnit" : {
+            "state" : "translated",
+            "value" : "必须在 %1$@-%2$@ 金币之间"
+          }
+        }
+      }
     }
   },
   "version" : "1.1"

+ 44 - 1
Lanu/Views/Game/Skill/Edit/LNSkillFieldPriceEditView.swift

@@ -44,6 +44,7 @@ extension LNSkillFieldPriceEditView {
             let panel = LNSkillPriceEditPanel()
             panel.titleLabel.text = field.fieldName
             panel.inputField.text = (field.value as? Double)?.toDisplay
+            panel.limit = field.validate.numLimit
             panel.handler = { [weak self] price in
                 guard let self else { return }
                 curValueLabel.text = price.toDisplay
@@ -83,9 +84,10 @@ extension LNSkillFieldPriceEditView {
     }
 }
 
-private class LNSkillPriceEditPanel: LNPopupView {
+private class LNSkillPriceEditPanel: LNPopupView, UITextFieldDelegate {
     let titleLabel = UILabel()
     let inputField = UITextField()
+    var limit: LNSkillFieldValidateNumLimit?
     private let confirmButton = UIButton()
     
     var handler: ((Double) -> Void)?
@@ -131,6 +133,7 @@ private class LNSkillPriceEditPanel: LNPopupView {
         inputField.textColor = .text_5
         inputField.placeholder = .init(key: "B00097")
         inputField.keyboardType = .decimalPad
+        inputField.delegate = self
         inputField.addAction(UIAction(handler: { [weak self] _ in
             guard let self else { return }
             let inputAvailable = if let text = inputField.text, Double(text) != nil {
@@ -169,6 +172,12 @@ private class LNSkillPriceEditPanel: LNPopupView {
         confirmButton.backgroundColor = .fill_4
         confirmButton.addAction(UIAction(handler: { [weak self] _ in
             guard let self else { return }
+            let text = inputField.text ?? "0"
+            let value = Double(text) ?? 0
+            if let limit, value < limit.min || value > limit.max {
+                showToast(.init(key: "B00102", limit.min.toDisplay, limit.max.toDisplay))
+                return
+            }
             dismiss()
             handler?(Double(inputField.text ?? "0") ?? 0)
         }), for: .touchUpInside)
@@ -184,4 +193,38 @@ private class LNSkillPriceEditPanel: LNPopupView {
     required init?(coder: NSCoder) {
         fatalError("init(coder:) has not been implemented")
     }
+    
+    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
+        guard let limit else { return true }
+        let currentText = textField.text ?? ""
+                
+        guard let range = Range(range, in: currentText) else { return false }
+        let newText = currentText.replacingCharacters(in: range, with: string)
+        if newText.isEmpty {
+            return true
+        }
+        if newText.starts(with: "00") {
+            return false
+        }
+        
+        if newText.filter({ $0 == "." }).count > 2 {
+            return false
+        }
+        
+        guard let value = Double(newText) else {
+            return false
+        }
+        
+        if value > limit.max {
+            showToast(.init(key: "B00102", limit.min.toDisplay, limit.max.toDisplay))
+            return false
+        }
+        
+        return true
+    }
+    
+    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
+        textField.resignFirstResponder()
+        return true
+    }
 }