Move from onSet using state to computed property
This commit is contained in:
parent
ab831c727d
commit
1fc0647d90
@ -69,33 +69,25 @@ struct BindingReactorDemo: View {
|
||||
struct WindowContent: View {
|
||||
|
||||
@State private var password = ""
|
||||
@State var checkStatus = PasswordChecker.allCases.enumerated().reduce([String: Bool]()) { dict, checker in
|
||||
var dict = dict
|
||||
dict[checker.element.rawValue] = false
|
||||
return dict
|
||||
var checkStatus: [String: Bool] {
|
||||
PasswordChecker.allCases.enumerated().reduce([String: Bool]()) { dict, checker in
|
||||
var dict = dict
|
||||
dict[checker.element.rawValue] = check(password: password, checker: checker.element).1
|
||||
return dict
|
||||
}
|
||||
}
|
||||
|
||||
var view: Body {
|
||||
VStack {
|
||||
FormSection("Password Checker") {
|
||||
Form {
|
||||
EntryRow("Password", text: $password.onSet { _ in
|
||||
Task {
|
||||
let results = await checkPassword(content: password)
|
||||
|
||||
Idle {
|
||||
for result in results {
|
||||
checkStatus[result.0] = result.1
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
EntryRow("Password", text: $password)
|
||||
}
|
||||
}
|
||||
.padding()
|
||||
ForEach(PasswordChecker.allCases) { checker in
|
||||
CheckerButton(
|
||||
isValid: binding(for: checker.rawValue),
|
||||
isValid: .constant(checkStatus[checker.rawValue] ?? false),
|
||||
checkerName: checker.label,
|
||||
checkerInfo: checker.description
|
||||
)
|
||||
@ -109,14 +101,6 @@ struct BindingReactorDemo: View {
|
||||
}
|
||||
}
|
||||
|
||||
private func binding(for key: String) -> Binding<Bool> {
|
||||
.init {
|
||||
checkStatus[key] ?? false
|
||||
} set: { newValue in
|
||||
checkStatus[key] = newValue
|
||||
}
|
||||
}
|
||||
|
||||
private func checkPassword(content password: String) async -> [(String, Bool)] {
|
||||
var results: [(String, Bool)] = []
|
||||
|
||||
@ -135,7 +119,7 @@ struct BindingReactorDemo: View {
|
||||
return results
|
||||
}
|
||||
|
||||
private func check(password: String, checker: PasswordChecker) async -> (String, Bool) {
|
||||
private func check(password: String, checker: PasswordChecker) -> (String, Bool) {
|
||||
switch checker {
|
||||
case .length:
|
||||
return (PasswordChecker.length.rawValue, password.count > 8)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user