|
| 1 | +// The MIT License (MIT) |
| 2 | +// |
| 3 | +// Copyright (c) 2020–2023 Alexander Grebenyuk (github.com/kean). |
| 4 | + |
| 5 | +import SwiftUI |
| 6 | +import Pulse |
| 7 | + |
| 8 | +struct DebugAnalyticsView: View { |
| 9 | + @FetchRequest( |
| 10 | + sortDescriptors: [NSSortDescriptor(keyPath: \LoggerMessageEntity.createdAt, ascending: true)], |
| 11 | + predicate: makePredicate(searchText: "") |
| 12 | + ) var messages: FetchedResults<LoggerMessageEntity> |
| 13 | + |
| 14 | + @State private var searchText = "" |
| 15 | + |
| 16 | + var body: some View { |
| 17 | + List(messages, id: \.objectID) { message in |
| 18 | + VStack(alignment: .leading) { |
| 19 | + HStack { |
| 20 | + Text(timeFormatter.string(from: message.createdAt)) |
| 21 | + .font(.footnote) |
| 22 | + .foregroundColor(.secondary) |
| 23 | + Spacer() |
| 24 | + ListDisclosureIndicator() |
| 25 | + } |
| 26 | + Text(message.text) |
| 27 | + .lineLimit(2) |
| 28 | + } |
| 29 | + .background(NavigationLink("", destination: DebugAnalyticsDetailsView(message: message)).opacity(0)) |
| 30 | + } |
| 31 | + .searchable(text: $searchText) |
| 32 | + .onChange(of: searchText) { |
| 33 | + messages.nsPredicate = makePredicate(searchText: $0) |
| 34 | + } |
| 35 | + .navigationTitle("Analyics") |
| 36 | + .listStyle(.plain) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +private func makePredicate(searchText: String) -> NSPredicate { |
| 41 | + let basePredicate = NSPredicate(format: "label == %@ && session == %@", "analytics", LoggerStore.shared.session.id as NSUUID) |
| 42 | + let searchTerms = searchText |
| 43 | + .trimmingCharacters(in: .whitespaces) |
| 44 | + .components(separatedBy: .whitespaces) |
| 45 | + .filter { !$0.isEmpty } |
| 46 | + guard !searchTerms.isEmpty else { return basePredicate } |
| 47 | + let searchPredicates = NSCompoundPredicate(andPredicateWithSubpredicates: searchTerms.map { |
| 48 | + NSPredicate(format: "text CONTAINS[cd] %@", $0) |
| 49 | + }) |
| 50 | + return NSCompoundPredicate(andPredicateWithSubpredicates: [basePredicate, searchPredicates]) |
| 51 | +} |
| 52 | + |
| 53 | +private struct DebugAnalyticsDetailsView: View { |
| 54 | + let message: LoggerMessageEntity |
| 55 | + |
| 56 | + @State private var searchText = "" |
| 57 | + |
| 58 | + var body: some View { |
| 59 | + List { |
| 60 | + Section { |
| 61 | + makeRow(title: "Event", value: message.text) |
| 62 | + makeRow(title: "Date", value: message.createdAt.description) |
| 63 | + } |
| 64 | + let metadata = self.metadata |
| 65 | + if !metadata.isEmpty { |
| 66 | + Section("Metadata") { |
| 67 | + ForEach(metadata, id: \.0, content: makeRow) |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + .searchable(text: $searchText) |
| 72 | + .navigationTitle("Event") |
| 73 | + .navigationBarTitleDisplayMode(.inline) |
| 74 | + } |
| 75 | + |
| 76 | + private var metadata: [(String, String)] { |
| 77 | + Array(message.metadata).sorted { |
| 78 | + $0.key.localizedCaseInsensitiveCompare($1.key) == .orderedAscending |
| 79 | + }.filter { |
| 80 | + guard !searchText.isEmpty else { return true } |
| 81 | + return $0.key.localizedCaseInsensitiveContains(searchText) || |
| 82 | + $0.value.localizedCaseInsensitiveContains(searchText) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private func makeRow(title: String, value: String) -> some View { |
| 87 | + VStack(alignment: .leading) { |
| 88 | + Text(higlighted(title, searchText: searchText)) |
| 89 | + Text(higlighted(value, searchText: searchText)) |
| 90 | + .foregroundStyle(.secondary) |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +private struct ListDisclosureIndicator: View { |
| 96 | + var body: some View { |
| 97 | + Image(systemName: "chevron.right") |
| 98 | + .foregroundColor(Color(UIColor.separator)) |
| 99 | + .lineLimit(1) |
| 100 | + .font(.caption) |
| 101 | + .foregroundColor(.secondary) |
| 102 | + .padding(.trailing, -12) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +private func higlighted(_ string: String, searchText: String) -> AttributedString { |
| 107 | + var output = AttributedString(string) |
| 108 | + if !searchText.isEmpty, let range = output.range(of: searchText, options: [.caseInsensitive, .diacriticInsensitive]) { |
| 109 | + output[range].backgroundColor = .yellow.opacity(0.33) |
| 110 | + } |
| 111 | + return output |
| 112 | +} |
| 113 | + |
| 114 | +private let timeFormatter: DateFormatter = { |
| 115 | + let formatter = DateFormatter() |
| 116 | + formatter.dateFormat = "HH:mm:ss.SSS" |
| 117 | + return formatter |
| 118 | +}() |
| 119 | + |
| 120 | +#if DEBUG |
| 121 | +struct Previews_ShareView_Previews: PreviewProvider { |
| 122 | + static var previews: some View { |
| 123 | + Group { |
| 124 | + NavigationView { |
| 125 | + DebugAnalyticsView() |
| 126 | + .environment(\.managedObjectContext, LoggerStore.mock.viewContext) |
| 127 | + } |
| 128 | + NavigationView { |
| 129 | + DebugAnalyticsDetailsView(message: try! LoggerStore.mock.allMessages()[0]) |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | +} |
| 134 | +#endif |
0 commit comments