Non-decryptable timeline items and debug menu (#300)

* Fixes #292 - Added a timeline item context menu option for printing and showing their debug description
* Fixes #291 - Add support for non-decryptable timeline items
This commit is contained in:
Stefan Ceriu
2022-11-08 13:17:38 +02:00
committed by GitHub
parent 7a7680b165
commit 89742ceb53
17 changed files with 312 additions and 21 deletions
@@ -0,0 +1,53 @@
//
// 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 DebugScreen: View {
struct DebugInfo: Identifiable {
let id = UUID()
let title: String
var content: String
}
@Environment(\.presentationMode) private var presentationMode
let info: DebugInfo
var body: some View {
NavigationView {
ScrollView {
Text(info.content)
.padding()
.font(.element.footnote)
}
.navigationBarTitleDisplayMode(.inline)
.navigationTitle(info.title)
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button(ElementL10n.actionCancel) {
presentationMode.wrappedValue.dismiss()
}
}
ToolbarItem(placement: .secondaryAction) {
Button(ElementL10n.actionCopy) {
UIPasteboard.general.string = info.content
}
}
}
}
}
}
@@ -41,6 +41,7 @@ struct RoomScreen: View {
}
}
.alert(item: $context.alertInfo) { $0.alert }
.sheet(item: $context.debugInfo) { DebugScreen(info: $0) }
}
private func sendMessage() {
@@ -0,0 +1,91 @@
//
// 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 EncryptedRoomTimelineView: View {
@State private var showEncryptionInfo = false
let timelineItem: EncryptedRoomTimelineItem
var body: some View {
TimelineStyler(timelineItem: timelineItem) {
Button {
showEncryptionInfo = !showEncryptionInfo
} label: {
HStack(alignment: .top) {
Image(systemName: "lock.shield")
.foregroundColor(.red)
.padding(.top, 1.0)
if showEncryptionInfo {
FormattedBodyText(text: encryptionDetails)
} else {
FormattedBodyText(text: timelineItem.text)
}
}
.animation(nil, value: showEncryptionInfo)
}
}
.id(timelineItem.id)
}
private var encryptionDetails: String {
switch timelineItem.encryptionType {
case .unknown:
return "Unknown"
case .megolmV1AesSha2(let sessionId):
return "Megolm session id: \(sessionId)"
case .olmV1Curve25519AesSha2(let senderKey):
return "Olm sender key: \(senderKey)"
}
}
}
struct EncryptedRoomTimelineView_Previews: PreviewProvider {
static var previews: some View {
body.preferredColorScheme(.light)
body.preferredColorScheme(.dark)
body.preferredColorScheme(.light)
.timelineStyle(.plain)
body.preferredColorScheme(.dark)
.timelineStyle(.plain)
}
@ViewBuilder
static var body: some View {
VStack(alignment: .leading, spacing: 20.0) {
EncryptedRoomTimelineView(timelineItem: itemWith(text: "Text",
timestamp: "Now",
isOutgoing: false,
senderId: "Bob"))
EncryptedRoomTimelineView(timelineItem: itemWith(text: "Some other text",
timestamp: "Later",
isOutgoing: true,
senderId: "Anne"))
}
}
private static func itemWith(text: String, timestamp: String, isOutgoing: Bool, senderId: String) -> EncryptedRoomTimelineItem {
EncryptedRoomTimelineItem(id: UUID().uuidString,
text: text,
encryptionType: .unknown,
timestamp: timestamp,
inGroupState: .single,
isOutgoing: isOutgoing,
senderId: senderId)
}
}
@@ -16,13 +16,38 @@
import SwiftUI
struct TimelineItemContextMenuActions {
let actions: [TimelineItemContextMenuAction]
let debugActions: [TimelineItemContextMenuAction] = [.viewSource]
}
enum TimelineItemContextMenuAction: Identifiable, Hashable {
case copy
case quote
case copyPermalink
case redact
case reply
case viewSource
var id: Self { self }
}
public struct TimelineItemContextMenu: View {
let contextMenuActions: [TimelineItemContextMenuAction]
let contextMenuActions: TimelineItemContextMenuActions
let callback: (TimelineItemContextMenuAction) -> Void
@ViewBuilder
public var body: some View {
ForEach(contextMenuActions, id: \.self) { item in
viewsForActions(contextMenuActions.actions)
Menu {
viewsForActions(contextMenuActions.debugActions)
} label: {
Label("Developer", systemImage: "hammer")
}
}
private func viewsForActions(_ actions: [TimelineItemContextMenuAction]) -> some View {
ForEach(actions, id: \.self) { item in
switch item {
case .copy:
Button { callback(item) } label: {
@@ -44,6 +69,10 @@ public struct TimelineItemContextMenu: View {
Button(role: .destructive) { callback(item) } label: {
Label(ElementL10n.messageActionItemRedact, systemImage: "trash")
}
case .viewSource:
Button { callback(item) } label: {
Label(ElementL10n.viewSource, systemImage: "doc.text.below.ecg")
}
}
}
}