Fixes #322 - Developer options menu (#581)

This commit is contained in:
Stefan Ceriu
2023-02-15 13:07:06 +02:00
committed by GitHub
parent 654ed1196a
commit e3b557cf0c
21 changed files with 390 additions and 7 deletions

View File

@@ -14,6 +14,7 @@
"settings_appearance" = "Appearance";
"settings_timeline_style" = "Message layout";
"settings_session_verification" = "Complete verification";
"settings_developer_options" = "Developer options";
"room_timeline_style_plain_long_description" = "Modern";
"room_timeline_style_bubbled_long_description" = "Bubbles";

View File

@@ -49,6 +49,15 @@ final class AppSettings: ObservableObject {
// MARK: - Application
lazy var canShowDeveloperOptions: Bool = {
#if DEBUG
true
#else
let apps = ["io.element.elementx.nightly", "io.element.elementx.pr"]
return apps.contains(InfoPlistReader.main.baseBundleIdentifier)
#endif
}()
/// The last known version of the app that was launched on this device, which is
/// used to detect when migrations should be run. When `nil` the app may have been
/// deleted between runs so should clear data in the shared container and keychain.

View File

@@ -140,6 +140,8 @@ extension ElementL10n {
public static let sessionVerificationStart = ElementL10n.tr("Untranslated", "session_verification_start")
/// Appearance
public static let settingsAppearance = ElementL10n.tr("Untranslated", "settings_appearance")
/// Developer options
public static let settingsDeveloperOptions = ElementL10n.tr("Untranslated", "settings_developer_options")
/// Complete verification
public static let settingsSessionVerification = ElementL10n.tr("Untranslated", "settings_session_verification")
/// Message layout

Binary file not shown.

View File

@@ -0,0 +1,74 @@
//
// Copyright 2022 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import SceneKit
import SwiftUI
class EffectsScene: SCNScene {
private enum Constants {
static let confettiSceneName = "ConfettiScene.scn"
static let particlesNodeName = "particles"
}
static func confetti() -> EffectsScene? {
guard let scene = EffectsScene(named: Constants.confettiSceneName) else { return nil }
let colors: [[Float]] = Color.element.contentAndAvatars.compactMap(\.floatComponents)
if let particles = scene.rootNode.childNode(withName: Constants.particlesNodeName, recursively: false)?.particleSystems?.first {
// The particles need a non-zero color variation for the handler to affect the color
particles.particleColorVariation = SCNVector4(x: 0, y: 0, z: 0, w: 0.1)
// Add a handler to customize the color of the particles.
particles.handle(.birth, forProperties: [.color]) { data, dataStride, _, count in
for index in 0..<count {
// Pick a random color to apply to the particle.
guard let color = colors.randomElement() else { continue }
// Get the particle's color pointer.
let colorPointer = data[0] + dataStride[0] * index
let rgbaPointer = colorPointer.bindMemory(to: Float.self, capacity: dataStride[0])
// Update the color for the particle.
rgbaPointer[0] = color[0]
rgbaPointer[1] = color[1]
rgbaPointer[2] = color[2]
rgbaPointer[3] = 1
}
}
}
return scene
}
}
private extension Color {
/// The color's components as an array of floats in the extended linear sRGB colorspace.
///
/// SceneKit works in a colorspace with a linear gamma, which is why this conversion is necessary.
var floatComponents: [Float]? {
// Get the CGColor from a UIColor as it is nil on Color when loaded from an asset catalog.
let cgColor = UIColor(self).cgColor
guard
let colorSpace = CGColorSpace(name: CGColorSpace.extendedLinearSRGB),
let linearColor = cgColor.converted(to: colorSpace, intent: .defaultIntent, options: nil),
let components = linearColor.components
else { return nil }
return components.map { Float($0) }
}
}

View File

@@ -0,0 +1,58 @@
//
// Copyright 2022 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import SceneKit
import SwiftUI
/// A SwiftUI wrapper around `SCNView`, that unlike `SceneView` allows the
/// scene to have a transparent background and be rendered on top of other views.
struct EffectsView: UIViewRepresentable {
enum Effect {
/// A confetti drop effect from the top centre of the screen.
case confetti
/// No effect will be shown.
case none
}
/// The type of effects to be shown in the view.
var effect: Effect
func makeUIView(context: Context) -> SCNView {
SCNView(frame: .zero)
}
func updateUIView(_ sceneView: SCNView, context: Context) {
sceneView.scene = makeScene()
sceneView.backgroundColor = .clear
}
// MARK: - Private
private func makeScene() -> EffectsScene? {
switch effect {
case .confetti:
return EffectsScene.confetti()
case .none:
return nil
}
}
}
struct EffectsView_Previews: PreviewProvider {
static var previews: some View {
EffectsView(effect: .confetti)
}
}

View File

@@ -0,0 +1,29 @@
//
// Copyright 2022 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import SwiftUI
final class DeveloperOptionsScreenCoordinator: CoordinatorProtocol {
private let viewModel: DeveloperOptionsScreenViewModelProtocol
init() {
viewModel = DeveloperOptionsScreenViewModel()
}
func toPresentable() -> AnyView {
AnyView(DeveloperOptionsScreenScreen(context: viewModel.context))
}
}

View File

@@ -0,0 +1,27 @@
//
// Copyright 2022 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
enum DeveloperOptionsScreenViewModelAction { }
struct DeveloperOptionsScreenViewState: BindableState {
var bindings: DeveloperOptionsScreenViewStateBindings
}
struct DeveloperOptionsScreenViewStateBindings { }
enum DeveloperOptionsScreenViewAction { }

View File

@@ -0,0 +1,27 @@
//
// Copyright 2022 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import SwiftUI
typealias DeveloperOptionsScreenViewModelType = StateStoreViewModel<DeveloperOptionsScreenViewState, DeveloperOptionsScreenViewAction>
class DeveloperOptionsScreenViewModel: DeveloperOptionsScreenViewModelType, DeveloperOptionsScreenViewModelProtocol {
var callback: ((DeveloperOptionsScreenViewModelAction) -> Void)?
init() {
super.init(initialViewState: DeveloperOptionsScreenViewState(bindings: DeveloperOptionsScreenViewStateBindings()))
}
}

View File

@@ -0,0 +1,23 @@
//
// Copyright 2022 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
@MainActor
protocol DeveloperOptionsScreenViewModelProtocol {
var callback: ((DeveloperOptionsScreenViewModelAction) -> Void)? { get set }
var context: DeveloperOptionsScreenViewModelType.Context { get }
}

View File

@@ -0,0 +1,64 @@
//
// Copyright 2022 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import SwiftUI
struct DeveloperOptionsScreenScreen: View {
@ObservedObject var context: DeveloperOptionsScreenViewModel.Context
@State private var showConfetti = false
var body: some View {
Form {
Section {
Button {
showConfetti = true
} label: {
Text("🥳")
.frame(maxWidth: .infinity)
}
}
.listRowBackground(Color.element.formRowBackground)
}
.overlay(effectsView)
.scrollContentBackground(.hidden)
.background(Color.element.formBackground.ignoresSafeArea())
.navigationTitle(ElementL10n.settingsDeveloperOptions)
.navigationBarTitleDisplayMode(.inline)
}
@ViewBuilder
private var effectsView: some View {
if showConfetti {
EffectsView(effect: .confetti)
.task { await removeConfettiAfterDelay() }
}
}
private func removeConfettiAfterDelay() async {
try? await Task.sleep(for: .seconds(4))
showConfetti = false
}
}
// MARK: - Previews
struct DeveloperOptionsScreen_Previews: PreviewProvider {
static var previews: some View {
let viewModel = DeveloperOptionsScreenViewModel()
DeveloperOptionsScreenScreen(context: viewModel.context)
.tint(.element.accent)
}
}

View File

@@ -88,7 +88,7 @@ struct RoomScreenViewStateBindings {
/// Information describing the currently displayed alert.
var alertInfo: AlertInfo<RoomScreenErrorType>?
var debugInfo: DebugScreen.DebugInfo?
var debugInfo: TimelineItemDebugView.DebugInfo?
}
enum RoomScreenErrorType: Hashable {

View File

@@ -229,7 +229,7 @@ class RoomScreenViewModel: RoomScreenViewModelType, RoomScreenViewModelProtocol
actions.append(.redact)
}
var debugActions: [TimelineItemContextMenuAction] = [.viewSource]
var debugActions: [TimelineItemContextMenuAction] = ServiceLocator.shared.settings.canShowDeveloperOptions ? [.viewSource] : []
if let item = timelineItem as? EncryptedRoomTimelineItem,
case let .megolmV1AesSha2(sessionID) = item.encryptionType {

View File

@@ -30,7 +30,7 @@ struct RoomScreen: View {
.toolbarBackground(.visible, for: .navigationBar) // Fix the toolbar's background.
.overlay { loadingIndicator }
.alert(item: $context.alertInfo) { $0.alert }
.sheet(item: $context.debugInfo) { DebugScreen(info: $0) }
.sheet(item: $context.debugInfo) { TimelineItemDebugView(info: $0) }
.task(id: context.viewState.roomId) {
// Give a couple of seconds for items to load and to see them.
try? await Task.sleep(for: .seconds(2))

View File

@@ -16,7 +16,7 @@
import SwiftUI
struct DebugScreen: View {
struct TimelineItemDebugView: View {
struct DebugInfo: Identifiable {
let id = UUID()
let title: String

View File

@@ -52,6 +52,8 @@ final class SettingsScreenCoordinator: CoordinatorProtocol {
self.presentBugReportScreen()
case .sessionVerification:
self.verifySession()
case .developerOptions:
self.presentDeveloperOptions()
case .logout:
self.callback?(.logout)
}
@@ -110,6 +112,11 @@ final class SettingsScreenCoordinator: CoordinatorProtocol {
self?.parameters.navigationStackCoordinator?.setSheetCoordinator(nil)
}
}
private func presentDeveloperOptions() {
let coordinator = DeveloperOptionsScreenCoordinator()
parameters.navigationStackCoordinator?.push(coordinator)
}
private func showSuccess(label: String) {
parameters.userIndicatorController?.submitIndicator(UserIndicator(title: label))

View File

@@ -22,6 +22,7 @@ enum SettingsScreenViewModelAction {
case toggleAnalytics
case reportBug
case sessionVerification
case developerOptions
case logout
}
@@ -32,6 +33,7 @@ struct SettingsScreenViewState: BindableState {
var userAvatarURL: URL?
var userDisplayName: String?
var showSessionVerificationSection: Bool
var showDeveloperOptions: Bool
}
struct SettingsScreenViewStateBindings {
@@ -45,4 +47,5 @@ enum SettingsScreenViewAction {
case sessionVerification
case logout
case changedTimelineStyle
case developerOptions
}

View File

@@ -23,14 +23,15 @@ class SettingsScreenViewModel: SettingsScreenViewModelType, SettingsScreenViewMo
private let userSession: UserSessionProtocol
var callback: ((SettingsScreenViewModelAction) -> Void)?
init(withUserSession userSession: UserSessionProtocol) {
self.userSession = userSession
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)),
showSessionVerificationSection: !(userSession.sessionVerificationController?.isVerified ?? false),
showDeveloperOptions: ServiceLocator.shared.settings.canShowDeveloperOptions),
imageProvider: userSession.mediaProvider)
ServiceLocator.shared.settings.$timelineStyle
@@ -78,6 +79,8 @@ class SettingsScreenViewModel: SettingsScreenViewModelType, SettingsScreenViewMo
callback?(.sessionVerification)
case .changedTimelineStyle:
ServiceLocator.shared.settings.timelineStyle = state.bindings.timelineStyle
case .developerOptions:
callback?(.developerOptions)
}
}
}

View File

@@ -18,7 +18,6 @@ import SwiftUI
struct SettingsScreen: View {
@State private var showingLogoutConfirmation = false
@Environment(\.colorScheme) private var colorScheme
@ScaledMetric private var avatarSize = AvatarSize.user(on: .settings).value
@ScaledMetric private var menuIconSize = 30.0
@@ -40,6 +39,11 @@ struct SettingsScreen: View {
simplifiedSection
.listRowBackground(Color.element.formRowBackground)
if context.viewState.showDeveloperOptions {
developerOptionsSection
.listRowBackground(Color.element.formRowBackground)
}
signOutSection
.listRowBackground(Color.element.formRowBackground)
}
@@ -89,6 +93,16 @@ struct SettingsScreen: View {
}
}
private var developerOptionsSection: some View {
Section {
SettingsDefaultRow(title: ElementL10n.settingsDeveloperOptions,
image: Image(systemName: "hammer.circle")) {
context.send(viewAction: .developerOptions)
}
.accessibilityIdentifier("sessionVerificationButton")
}
}
private var simplifiedSection: some View {
Section {
SettingsPickerRow(title: ElementL10n.settingsTimelineStyle,

View File

@@ -0,0 +1,20 @@
//
// Copyright 2022 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import ElementX
import XCTest
class DeveloperOptionsScreenScreenUITests: XCTestCase { }

View File

@@ -0,0 +1,22 @@
//
// Copyright 2022 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import XCTest
@testable import ElementX
@MainActor
class DeveloperOptionsScreenViewModelTests: XCTestCase { }