mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-04 01:20:12 +00:00
Reduce boilerplate in Developer Options Screen (#1378)
* Reduce developer options boilerplate * Add protocol DeveloperOptions * Delete timelineDiffableAnimationsEnabled * Refactor DeveloperOptions -> DeveloperOptionsProtocol
This commit is contained in:
@@ -228,8 +228,8 @@
|
|||||||
"kind" : "remoteSourceControl",
|
"kind" : "remoteSourceControl",
|
||||||
"location" : "https://github.com/pointfreeco/xctest-dynamic-overlay",
|
"location" : "https://github.com/pointfreeco/xctest-dynamic-overlay",
|
||||||
"state" : {
|
"state" : {
|
||||||
"revision" : "4af50b38daf0037cfbab15514a241224c3f62f98",
|
"revision" : "245d527002f29c5a616c5b7131f6a50ac7f41cbf",
|
||||||
"version" : "0.8.5"
|
"version" : "0.8.6"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
|||||||
+1
-1
@@ -26,7 +26,7 @@ final class DeveloperOptionsScreenCoordinator: CoordinatorProtocol {
|
|||||||
var callback: ((DeveloperOptionsScreenCoordinatorAction) -> Void)?
|
var callback: ((DeveloperOptionsScreenCoordinatorAction) -> Void)?
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
viewModel = DeveloperOptionsScreenViewModel(appSettings: ServiceLocator.shared.settings)
|
viewModel = DeveloperOptionsScreenViewModel(developerOptions: ServiceLocator.shared.settings)
|
||||||
viewModel.callback = { [weak self] action in
|
viewModel.callback = { [weak self] action in
|
||||||
switch action {
|
switch action {
|
||||||
case .clearCache:
|
case .clearCache:
|
||||||
|
|||||||
+21
-10
@@ -24,19 +24,30 @@ struct DeveloperOptionsScreenViewState: BindableState {
|
|||||||
var bindings: DeveloperOptionsScreenViewStateBindings
|
var bindings: DeveloperOptionsScreenViewStateBindings
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@dynamicMemberLookup
|
||||||
struct DeveloperOptionsScreenViewStateBindings {
|
struct DeveloperOptionsScreenViewStateBindings {
|
||||||
var shouldCollapseRoomStateEvents: Bool
|
private let developerOptions: DeveloperOptionsProtocol
|
||||||
var userSuggestionsEnabled: Bool
|
|
||||||
var readReceiptsEnabled: Bool
|
init(developerOptions: DeveloperOptionsProtocol) {
|
||||||
var isEncryptionSyncEnabled: Bool
|
self.developerOptions = developerOptions
|
||||||
var notificationSettingsEnabled: Bool
|
}
|
||||||
|
|
||||||
|
subscript<Setting>(dynamicMember keyPath: ReferenceWritableKeyPath<DeveloperOptionsProtocol, Setting>) -> Setting {
|
||||||
|
get { developerOptions[keyPath: keyPath] }
|
||||||
|
set { developerOptions[keyPath: keyPath] = newValue }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
enum DeveloperOptionsScreenViewAction {
|
enum DeveloperOptionsScreenViewAction {
|
||||||
case changedShouldCollapseRoomStateEvents
|
|
||||||
case changedUserSuggestionsEnabled
|
|
||||||
case changedReadReceiptsEnabled
|
|
||||||
case changedIsEncryptionSyncEnabled
|
|
||||||
case changedNotificationSettingsEnabled
|
|
||||||
case clearCache
|
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 { }
|
||||||
|
|||||||
+2
-24
@@ -19,39 +19,17 @@ import SwiftUI
|
|||||||
typealias DeveloperOptionsScreenViewModelType = StateStoreViewModel<DeveloperOptionsScreenViewState, DeveloperOptionsScreenViewAction>
|
typealias DeveloperOptionsScreenViewModelType = StateStoreViewModel<DeveloperOptionsScreenViewState, DeveloperOptionsScreenViewAction>
|
||||||
|
|
||||||
class DeveloperOptionsScreenViewModel: DeveloperOptionsScreenViewModelType, DeveloperOptionsScreenViewModelProtocol {
|
class DeveloperOptionsScreenViewModel: DeveloperOptionsScreenViewModelType, DeveloperOptionsScreenViewModelProtocol {
|
||||||
private let appSettings: AppSettings
|
|
||||||
|
|
||||||
var callback: ((DeveloperOptionsScreenViewModelAction) -> Void)?
|
var callback: ((DeveloperOptionsScreenViewModelAction) -> Void)?
|
||||||
|
|
||||||
init(appSettings: AppSettings) {
|
init(developerOptions: DeveloperOptionsProtocol) {
|
||||||
self.appSettings = appSettings
|
let bindings = DeveloperOptionsScreenViewStateBindings(developerOptions: developerOptions)
|
||||||
|
|
||||||
let bindings = DeveloperOptionsScreenViewStateBindings(shouldCollapseRoomStateEvents: appSettings.shouldCollapseRoomStateEvents,
|
|
||||||
userSuggestionsEnabled: appSettings.userSuggestionsEnabled,
|
|
||||||
readReceiptsEnabled: appSettings.readReceiptsEnabled,
|
|
||||||
isEncryptionSyncEnabled: appSettings.isEncryptionSyncEnabled,
|
|
||||||
notificationSettingsEnabled: appSettings.notificationSettingsEnabled)
|
|
||||||
let state = DeveloperOptionsScreenViewState(bindings: bindings)
|
let state = DeveloperOptionsScreenViewState(bindings: bindings)
|
||||||
|
|
||||||
super.init(initialViewState: state)
|
super.init(initialViewState: state)
|
||||||
|
|
||||||
appSettings.$shouldCollapseRoomStateEvents
|
|
||||||
.weakAssign(to: \.state.bindings.shouldCollapseRoomStateEvents, on: self)
|
|
||||||
.store(in: &cancellables)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override func process(viewAction: DeveloperOptionsScreenViewAction) {
|
override func process(viewAction: DeveloperOptionsScreenViewAction) {
|
||||||
switch viewAction {
|
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:
|
case .clearCache:
|
||||||
callback?(.clearCache)
|
callback?(.clearCache)
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-16
@@ -26,17 +26,11 @@ struct DeveloperOptionsScreen: View {
|
|||||||
Toggle(isOn: $context.shouldCollapseRoomStateEvents) {
|
Toggle(isOn: $context.shouldCollapseRoomStateEvents) {
|
||||||
Text("Collapse room state events")
|
Text("Collapse room state events")
|
||||||
}
|
}
|
||||||
.onChange(of: context.shouldCollapseRoomStateEvents) { _ in
|
|
||||||
context.send(viewAction: .changedShouldCollapseRoomStateEvents)
|
|
||||||
}
|
|
||||||
|
|
||||||
Toggle(isOn: $context.readReceiptsEnabled) {
|
Toggle(isOn: $context.readReceiptsEnabled) {
|
||||||
Text("Show read receipts")
|
Text("Show read receipts")
|
||||||
Text("Requires app reboot")
|
Text("Requires app reboot")
|
||||||
}
|
}
|
||||||
.onChange(of: context.readReceiptsEnabled) { _ in
|
|
||||||
context.send(viewAction: .changedReadReceiptsEnabled)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Section("Notifications") {
|
Section("Notifications") {
|
||||||
@@ -44,25 +38,16 @@ struct DeveloperOptionsScreen: View {
|
|||||||
Text("Use notification encryption sync")
|
Text("Use notification encryption sync")
|
||||||
Text("Requires app reboot")
|
Text("Requires app reboot")
|
||||||
}
|
}
|
||||||
.onChange(of: context.isEncryptionSyncEnabled) { _ in
|
|
||||||
context.send(viewAction: .changedIsEncryptionSyncEnabled)
|
|
||||||
}
|
|
||||||
|
|
||||||
Toggle(isOn: $context.notificationSettingsEnabled) {
|
Toggle(isOn: $context.notificationSettingsEnabled) {
|
||||||
Text("Show notification settings")
|
Text("Show notification settings")
|
||||||
}
|
}
|
||||||
.onChange(of: context.notificationSettingsEnabled) { _ in
|
|
||||||
context.send(viewAction: .changedNotificationSettingsEnabled)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Section("Room creation") {
|
Section("Room creation") {
|
||||||
Toggle(isOn: $context.userSuggestionsEnabled) {
|
Toggle(isOn: $context.userSuggestionsEnabled) {
|
||||||
Text("User suggestions")
|
Text("User suggestions")
|
||||||
}
|
}
|
||||||
.onChange(of: context.userSuggestionsEnabled) { _ in
|
|
||||||
context.send(viewAction: .changedUserSuggestionsEnabled)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Section {
|
Section {
|
||||||
@@ -116,7 +101,7 @@ struct DeveloperOptionsScreen: View {
|
|||||||
// MARK: - Previews
|
// MARK: - Previews
|
||||||
|
|
||||||
struct DeveloperOptionsScreen_Previews: PreviewProvider {
|
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 {
|
static var previews: some View {
|
||||||
NavigationStack {
|
NavigationStack {
|
||||||
DeveloperOptionsScreen(context: viewModel.context)
|
DeveloperOptionsScreen(context: viewModel.context)
|
||||||
|
|||||||
Reference in New Issue
Block a user