Fixes #367 - Refactor AppSettings usage patterns

This commit is contained in:
Stefan Ceriu
2022-12-22 10:59:12 +02:00
committed by Stefan Ceriu
parent e5522e7753
commit 98cfc2c9fb
9 changed files with 177 additions and 19 deletions
@@ -35,7 +35,7 @@ struct SettingsScreenViewState: BindableState {
}
struct SettingsScreenViewStateBindings {
var enableAnalytics = ServiceLocator.shared.settings.enableAnalytics
var timelineStyle: TimelineStyle
}
enum SettingsScreenViewAction {
@@ -44,4 +44,5 @@ enum SettingsScreenViewAction {
case reportBug
case sessionVerification
case logout
case changedTimelineStyle
}
@@ -14,6 +14,7 @@
// limitations under the License.
//
import Combine
import SwiftUI
typealias SettingsScreenViewModelType = StateStoreViewModel<SettingsScreenViewState, SettingsScreenViewAction>
@@ -25,13 +26,15 @@ class SettingsScreenViewModel: SettingsScreenViewModelType, SettingsScreenViewMo
init(withUserSession userSession: UserSessionProtocol) {
self.userSession = userSession
let bindings = SettingsScreenViewStateBindings()
let bindings = SettingsScreenViewStateBindings(timelineStyle: ServiceLocator.shared.settings.timelineStyle)
super.init(initialViewState: .init(bindings: bindings,
deviceID: userSession.deviceId,
userID: userSession.userID,
showSessionVerificationSection: !(userSession.sessionVerificationController?.isVerified ?? false)),
imageProvider: userSession.mediaProvider)
listenToSettingsChange(publisher: ServiceLocator.shared.settings.$timelineStyle, keyPath: \.timelineStyle)
Task {
if case let .success(userAvatarURL) = await userSession.clientProxy.loadUserAvatarURL() {
state.userAvatarURL = userAvatarURL
@@ -71,6 +74,20 @@ class SettingsScreenViewModel: SettingsScreenViewModelType, SettingsScreenViewMo
callback?(.logout)
case .sessionVerification:
callback?(.sessionVerification)
case .changedTimelineStyle:
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)
}
}
@@ -19,7 +19,6 @@ import SwiftUI
struct SettingsScreen: View {
@State private var showingLogoutConfirmation = false
@Environment(\.colorScheme) private var colorScheme
@ObservedObject private var settings = ServiceLocator.shared.settings
@ScaledMetric private var avatarSize = AvatarSize.user(on: .settings).value
@ScaledMetric private var menuIconSize = 30.0
@@ -104,13 +103,16 @@ struct SettingsScreen: View {
Section {
SettingsPickerRow(title: ElementL10n.settingsTimelineStyle,
image: Image(systemName: "rectangle.grid.1x2"),
selection: $settings.timelineStyle) {
selection: $context.timelineStyle) {
ForEach(TimelineStyle.allCases, id: \.self) { style in
Text(style.description)
Text(style.name)
.tag(style)
}
}
.accessibilityIdentifier("timelineStylePicker")
.onChange(of: context.timelineStyle) { _ in
context.send(viewAction: .changedTimelineStyle)
}
SettingsDefaultRow(title: ElementL10n.sendBugReport,
image: Image(systemName: "questionmark.circle")) {
@@ -166,8 +168,8 @@ struct SettingsScreen: View {
}
}
extension TimelineStyle: CustomStringConvertible {
var description: String {
private extension TimelineStyle {
var name: String {
switch self {
case .plain:
return ElementL10n.roomTimelineStylePlainLongDescription