Reduce boilerplate in Developer Options Screen (#1378)

* Reduce developer options boilerplate

* Add protocol DeveloperOptions

* Delete timelineDiffableAnimationsEnabled

* Refactor DeveloperOptions -> DeveloperOptionsProtocol
This commit is contained in:
Alfonso Grillo
2023-07-24 10:42:14 +02:00
committed by GitHub
parent 156e33b27c
commit 6fc8c31449
5 changed files with 27 additions and 53 deletions
@@ -26,7 +26,7 @@ final class DeveloperOptionsScreenCoordinator: CoordinatorProtocol {
var callback: ((DeveloperOptionsScreenCoordinatorAction) -> Void)?
init() {
viewModel = DeveloperOptionsScreenViewModel(appSettings: ServiceLocator.shared.settings)
viewModel = DeveloperOptionsScreenViewModel(developerOptions: ServiceLocator.shared.settings)
viewModel.callback = { [weak self] action in
switch action {
case .clearCache:
@@ -24,19 +24,30 @@ struct DeveloperOptionsScreenViewState: BindableState {
var bindings: DeveloperOptionsScreenViewStateBindings
}
@dynamicMemberLookup
struct DeveloperOptionsScreenViewStateBindings {
var shouldCollapseRoomStateEvents: Bool
var userSuggestionsEnabled: Bool
var readReceiptsEnabled: Bool
var isEncryptionSyncEnabled: Bool
var notificationSettingsEnabled: Bool
private let developerOptions: DeveloperOptionsProtocol
init(developerOptions: DeveloperOptionsProtocol) {
self.developerOptions = developerOptions
}
subscript<Setting>(dynamicMember keyPath: ReferenceWritableKeyPath<DeveloperOptionsProtocol, Setting>) -> Setting {
get { developerOptions[keyPath: keyPath] }
set { developerOptions[keyPath: keyPath] = newValue }
}
}
enum DeveloperOptionsScreenViewAction {
case changedShouldCollapseRoomStateEvents
case changedUserSuggestionsEnabled
case changedReadReceiptsEnabled
case changedIsEncryptionSyncEnabled
case changedNotificationSettingsEnabled
case clearCache
}
protocol DeveloperOptionsProtocol: AnyObject {
var shouldCollapseRoomStateEvents: Bool { get set }
var userSuggestionsEnabled: Bool { get set }
var readReceiptsEnabled: Bool { get set }
var isEncryptionSyncEnabled: Bool { get set }
var notificationSettingsEnabled: Bool { get set }
}
extension AppSettings: DeveloperOptionsProtocol { }
@@ -19,39 +19,17 @@ import SwiftUI
typealias DeveloperOptionsScreenViewModelType = StateStoreViewModel<DeveloperOptionsScreenViewState, DeveloperOptionsScreenViewAction>
class DeveloperOptionsScreenViewModel: DeveloperOptionsScreenViewModelType, DeveloperOptionsScreenViewModelProtocol {
private let appSettings: AppSettings
var callback: ((DeveloperOptionsScreenViewModelAction) -> Void)?
init(appSettings: AppSettings) {
self.appSettings = appSettings
let bindings = DeveloperOptionsScreenViewStateBindings(shouldCollapseRoomStateEvents: appSettings.shouldCollapseRoomStateEvents,
userSuggestionsEnabled: appSettings.userSuggestionsEnabled,
readReceiptsEnabled: appSettings.readReceiptsEnabled,
isEncryptionSyncEnabled: appSettings.isEncryptionSyncEnabled,
notificationSettingsEnabled: appSettings.notificationSettingsEnabled)
init(developerOptions: DeveloperOptionsProtocol) {
let bindings = DeveloperOptionsScreenViewStateBindings(developerOptions: developerOptions)
let state = DeveloperOptionsScreenViewState(bindings: bindings)
super.init(initialViewState: state)
appSettings.$shouldCollapseRoomStateEvents
.weakAssign(to: \.state.bindings.shouldCollapseRoomStateEvents, on: self)
.store(in: &cancellables)
}
override func process(viewAction: DeveloperOptionsScreenViewAction) {
switch viewAction {
case .changedShouldCollapseRoomStateEvents:
appSettings.shouldCollapseRoomStateEvents = state.bindings.shouldCollapseRoomStateEvents
case .changedUserSuggestionsEnabled:
appSettings.userSuggestionsEnabled = state.bindings.userSuggestionsEnabled
case .changedReadReceiptsEnabled:
appSettings.readReceiptsEnabled = state.bindings.readReceiptsEnabled
case .changedIsEncryptionSyncEnabled:
appSettings.isEncryptionSyncEnabled = state.bindings.isEncryptionSyncEnabled
case .changedNotificationSettingsEnabled:
appSettings.notificationSettingsEnabled = state.bindings.notificationSettingsEnabled
case .clearCache:
callback?(.clearCache)
}
@@ -26,17 +26,11 @@ struct DeveloperOptionsScreen: View {
Toggle(isOn: $context.shouldCollapseRoomStateEvents) {
Text("Collapse room state events")
}
.onChange(of: context.shouldCollapseRoomStateEvents) { _ in
context.send(viewAction: .changedShouldCollapseRoomStateEvents)
}
Toggle(isOn: $context.readReceiptsEnabled) {
Text("Show read receipts")
Text("Requires app reboot")
}
.onChange(of: context.readReceiptsEnabled) { _ in
context.send(viewAction: .changedReadReceiptsEnabled)
}
}
Section("Notifications") {
@@ -44,25 +38,16 @@ struct DeveloperOptionsScreen: View {
Text("Use notification encryption sync")
Text("Requires app reboot")
}
.onChange(of: context.isEncryptionSyncEnabled) { _ in
context.send(viewAction: .changedIsEncryptionSyncEnabled)
}
Toggle(isOn: $context.notificationSettingsEnabled) {
Text("Show notification settings")
}
.onChange(of: context.notificationSettingsEnabled) { _ in
context.send(viewAction: .changedNotificationSettingsEnabled)
}
}
Section("Room creation") {
Toggle(isOn: $context.userSuggestionsEnabled) {
Text("User suggestions")
}
.onChange(of: context.userSuggestionsEnabled) { _ in
context.send(viewAction: .changedUserSuggestionsEnabled)
}
}
Section {
@@ -116,7 +101,7 @@ struct DeveloperOptionsScreen: View {
// MARK: - Previews
struct DeveloperOptionsScreen_Previews: PreviewProvider {
static let viewModel = DeveloperOptionsScreenViewModel(appSettings: ServiceLocator.shared.settings)
static let viewModel = DeveloperOptionsScreenViewModel(developerOptions: ServiceLocator.shared.settings)
static var previews: some View {
NavigationStack {
DeveloperOptionsScreen(context: viewModel.context)