Switch to weakAssign for all AppSettings listeners

This commit is contained in:
Stefan Ceriu
2023-02-09 09:22:05 +02:00
committed by Stefan Ceriu
parent 98cfc2c9fb
commit a02b461c37
4 changed files with 35 additions and 17 deletions
@@ -33,7 +33,9 @@ class SettingsScreenViewModel: SettingsScreenViewModelType, SettingsScreenViewMo
showSessionVerificationSection: !(userSession.sessionVerificationController?.isVerified ?? false)),
imageProvider: userSession.mediaProvider)
listenToSettingsChange(publisher: ServiceLocator.shared.settings.$timelineStyle, keyPath: \.timelineStyle)
ServiceLocator.shared.settings.$timelineStyle
.weakAssign(to: \.state.bindings.timelineStyle, on: self)
.store(in: &cancellables)
Task {
if case let .success(userAvatarURL) = await userSession.clientProxy.loadUserAvatarURL() {
@@ -78,16 +80,4 @@ class SettingsScreenViewModel: SettingsScreenViewModelType, SettingsScreenViewMo
ServiceLocator.shared.settings.timelineStyle = state.bindings.timelineStyle
}
}
private func listenToSettingsChange<Value>(publisher: AnyPublisher<Value, Never>,
keyPath: WritableKeyPath<SettingsScreenViewStateBindings, Value>) where Value: Equatable {
publisher.sink { [weak self] newValue in
guard newValue != self?.state.bindings[keyPath: keyPath] else {
return
}
self?.state.bindings[keyPath: keyPath] = newValue
}
.store(in: &cancellables)
}
}