Skip to content

Commit

Permalink
fix: `Publishing changes from within view updates is not allowed, thi…
Browse files Browse the repository at this point in the history
…s will cause undefined behavior.` in `ServiceConfigurationSecretSectionView`
  • Loading branch information
CanglongCl committed Feb 5, 2024
1 parent dbf9177 commit 5333ac2
Showing 1 changed file with 18 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct ServiceConfigurationSecretSectionView<Content: View>: View {
var service: QueryService
let content: Content

@ObservedObject private var viewModel = ServiceValidationViewModel()
@StateObject private var viewModel: ServiceValidationViewModel

init(
service: QueryService,
Expand All @@ -24,8 +24,7 @@ struct ServiceConfigurationSecretSectionView<Content: View>: View {
) {
self.service = service
self.content = content()

viewModel.observeInput(for: observeKeys)
_viewModel = .init(wrappedValue: ServiceValidationViewModel(observing: observeKeys))
}

var header: some View {
Expand Down Expand Up @@ -60,18 +59,22 @@ struct ServiceConfigurationSecretSectionView<Content: View>: View {
} footer: {
footer
}
.alert(isPresented: $viewModel.isAlertPresented) {
Alert(title: Text(viewModel.alertMessage))
}
.alert(viewModel.alertMessage, isPresented: $viewModel.isAlertPresented, actions: {
Button("ok") {
viewModel.isAlertPresented = false
}
})
}

func validate() {
viewModel.isValidating.toggle()
service.validate { _, error in
viewModel.alertMessage = error == nil ? "service.configuration.validation_success" : "service.configuration.validation_fail"
print("\(service.serviceType()) validate \(error == nil ? "success" : "fail")!")
viewModel.isValidating.toggle()
viewModel.isAlertPresented.toggle()
DispatchQueue.main.async {
viewModel.alertMessage = error == nil ? "service.configuration.validation_success" : "service.configuration.validation_fail"
print("\(service.serviceType()) validate \(error == nil ? "success" : "fail")!")
viewModel.isValidating.toggle()
viewModel.isAlertPresented.toggle()
}
}
}
}
Expand All @@ -88,13 +91,15 @@ private class ServiceValidationViewModel: ObservableObject {

var cancellables: [AnyCancellable] = []

// check secret key empty input
func observeInput(for keys: [Defaults.Key<String?>]) {
init(observing keys: [Defaults.Key<String?>]) {
cancellables.append(
// check secret key empty input
Defaults.publisher(keys: keys)
.sink { [weak self] _ in
let hasEmptyInput = keys.contains(where: { (Defaults[$0] ?? "").isEmpty })
self?.isValidateBtnDisabled = hasEmptyInput
DispatchQueue.main.async {
self?.isValidateBtnDisabled = hasEmptyInput
}
}
)
}
Expand Down

0 comments on commit 5333ac2

Please sign in to comment.