Skip to content

Commit

Permalink
fix: optimize service validation blink issue (#593)
Browse files Browse the repository at this point in the history
Co-authored-by: Tisfeng <[email protected]>
  • Loading branch information
phlpsong and tisfeng committed Jun 21, 2024
1 parent b0985fe commit d2a6e1e
Showing 1 changed file with 22 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ struct ServiceConfigurationSecretSectionView<Content: View>: View {
) {
self.service = service
self.content = content()
self.viewModel = ServiceValidationViewModel(service: service, observing: observeKeys)
self._viewModel = StateObject(wrappedValue: ServiceValidationViewModel(
service: service,
observing: observeKeys
))
}

// MARK: Internal
Expand Down Expand Up @@ -104,7 +107,7 @@ struct ServiceConfigurationSecretSectionView<Content: View>: View {

// MARK: Private

@ObservedObject private var viewModel: ServiceValidationViewModel
@StateObject private var viewModel: ServiceValidationViewModel
}

// MARK: - ServiceValidationViewModel
Expand All @@ -116,22 +119,22 @@ private class ServiceValidationViewModel: ObservableObject {
init(service: QueryService, observing keys: [Defaults.Key<String?>]) {
self.service = service
self.name = service.name()
cancellables.append(
// check secret key empty input
Defaults.publisher(keys: keys)
.sink { [weak self] _ in
let hasEmptyInput = keys.contains(where: { (Defaults[$0] ?? "").isEmpty })
DispatchQueue.main.async {
self?.isValidateBtnDisabled = hasEmptyInput
}
}
)
cancellables.append(
serviceUpdatePublisher
.sink { [weak self] notification in
self?.didReceive(notification)
}
)
// check secret key empty input
Defaults.publisher(keys: keys)
.throttle(for: 0.5, scheduler: DispatchQueue.main, latest: true)
.receive(on: DispatchQueue.main)
.sink { [weak self] _ in
guard let self else { return }
let hasEmptyInput = keys.contains(where: { (Defaults[$0] ?? "").isEmpty })
guard isValidateBtnDisabled != hasEmptyInput else { return }
self.isValidateBtnDisabled = hasEmptyInput
}
.store(in: &cancellables)
serviceUpdatePublisher
.sink { [weak self] notification in
self?.didReceive(notification)
}
.store(in: &cancellables)
}

// MARK: Internal
Expand All @@ -142,7 +145,7 @@ private class ServiceValidationViewModel: ObservableObject {
@Published var errorMessage = ""
@Published var isValidateBtnDisabled = false

var cancellables: [AnyCancellable] = []
var cancellables: Set<AnyCancellable> = []

let service: QueryService

Expand Down

0 comments on commit d2a6e1e

Please sign in to comment.