Skip to content

知了 Cicada · 任何会议和直播的实时字幕、同声传译、AI 会议纪要,Mac、Windows、Linux、Android 免费开源。Live subtitles, interpreting and meeting notes for any call or stream, on Mac, Windows, Linux and Android.

fix(ios): opening a meeting from search goes to the line found and highlights it

wquguru20bc2d9ca850
diff --git a/Sources/Cicada/TranscriptSearch.swift b/Sources/Cicada/TranscriptSearch.swift
index a0e9f13..d21d3f2 100644
--- a/Sources/Cicada/TranscriptSearch.swift
+++ b/Sources/Cicada/TranscriptSearch.swift
@@ -1,7 +1,10 @@
+import Foundation
+#if !CICADA_IOS
import SwiftUI
+#endif
-/// Searching a meeting's lines, shared by the notes window and the main window's records, so a
-/// search finds the same lines (misheard names included) and marks them the same way in both.
+/// Searching a meeting's lines, shared by the notes window, the main window's records and the iOS
+/// app's search, so a search finds the same lines (misheard names included) and marks them the same way.
enum TranscriptSearch {
/// The query, plus every way the notes say that name was heard (江充 also finds 江松).
static func terms(_ query: String, notes: [NoteTerm]) -> [String] {
@@ -27,7 +30,9 @@ enum TranscriptSearch {
return lines.filter { matches($0, terms) }.map(\.id)
}
- /// Every term marked in place; the match being looked at stands out from the rest.
+ #if !CICADA_IOS
+ /// Every term marked in place; the match being looked at stands out from the rest. iOS marks
+ /// them in its own colours (ios/Sources/Cicada/RecordView.swift).
static func highlighted(_ base: AttributedString, _ terms: [String], current: Bool) -> AttributedString {
var a = base
for term in terms where !term.isEmpty {
@@ -40,4 +45,5 @@ enum TranscriptSearch {
}
return a
}
+ #endif
}
diff --git a/ios/Sources/Cicada/CicadaApp.swift b/ios/Sources/Cicada/CicadaApp.swift
index 3b2fcba..3000a95 100644
--- a/ios/Sources/Cicada/CicadaApp.swift
+++ b/ios/Sources/Cicada/CicadaApp.swift
@@ -198,6 +198,8 @@ final class AppModel: ObservableObject {
/// The record page's 文字记录 / 纪要 switch, kept here so a debug command can flip it.
@Published var recordPage: RecordPage = .transcript
@Published var query = ""
+ /// Which of the open meeting's matches the search is on (#33: a search opens the line, not just the meeting).
+ @Published var searchMatch = 0
/// iPad: settings is a sheet over the records.
@Published var showSettings = false
/// What the 听会议室 card and the import button would start; they come with M2 and M3.
diff --git a/ios/Sources/Cicada/Debug.swift b/ios/Sources/Cicada/Debug.swift
index e1a8626..d1ff260 100644
--- a/ios/Sources/Cicada/Debug.swift
+++ b/ios/Sources/Cicada/Debug.swift
@@ -111,6 +111,17 @@ extension AppModel {
tab = .search
searchPath = []
query = arg
+ logSearch()
+ case "searchOpen":
+ // searchOpen:<id>, as tapping a meeting in 搜索's results (no id: the first found)
+ tab = .search
+ let found = SearchView.matches(store.sessions, query)
+ searchPath = [arg.isEmpty ? found.first?.id : arg].compactMap { $0 }
+ logSearch()
+ case "searchStep":
+ // searchStep:<1|-1>, 下一个 / 上一个 in the open meeting
+ if let s = store.session(searchPath.last ?? selection ?? store.sessions.first?.id) { stepSearch(Int(arg) ?? 1, in: s) }
+ logSearch()
case "mainShow":
// mainShow:<id>[,transcript|notes]; an empty id is the newest meeting
let parts = arg.split(separator: ",", omittingEmptySubsequences: false).map(String.init)
@@ -385,6 +396,19 @@ extension AppModel {
debugLog("debug command unknown: \(cmd)")
}
}
+
+ /// After the open meeting has settled: how many meetings the search finds, and where it is in the open one.
+ private func logSearch() {
+ DispatchQueue.main.asyncAfter(deadline: .now() + 0.6) { [self] in
+ let found = SearchView.matches(store.sessions, query)
+ let open = store.session(searchPath.last ?? (tab == .search ? nil : selection))
+ let ms = open.map(searchMatches) ?? []
+ let at = ms.isEmpty ? nil : ms[min(searchMatch, ms.count - 1)]
+ let text = at.flatMap { id in open?.lines.first { $0.id == id } }.map { String($0.shown.prefix(40)) } ?? "-"
+ debugLog("search: \"\(query)\" \(found.count) meetings, open \(open?.id ?? "-") page=\(recordPage) "
+ + "matches=\(ms.count) at=\(ms.isEmpty ? 0 : min(searchMatch, ms.count - 1) + 1)/\(ms.count) line=\(at ?? "-") \"\(text)\"")
+ }
+ }
}
/// Dev only: the iPad windows the simulator can't be put into from a script. There is no
diff --git a/ios/Sources/Cicada/RecordView.swift b/ios/Sources/Cicada/RecordView.swift
index 2f161c5..737dfe5 100644
--- a/ios/Sources/Cicada/RecordView.swift
+++ b/ios/Sources/Cicada/RecordView.swift
@@ -10,6 +10,9 @@ struct RecordView: View {
@EnvironmentObject var ling: LingClient
@EnvironmentObject var notes: NotesEngine
let session: Session
+ /// Opened from 搜索 (or the iPad's sidebar search): the transcript opens at the first line found,
+ /// every match marked, as the Mac's records (#33).
+ var searching = false
@StateObject private var player = RecordPlayer()
@State private var needsKey = false
/// What's typed into the title; nil until it is touched, so opening it changes nothing.
@@ -66,7 +69,7 @@ struct RecordView: View {
.padding(.top, 14)
}
TranscriptList(session: session, playing: player.lineID(in: session), focus: picked ?? playingNumber(voicesOn ? TranscriptExport.speakerNumbers(session) : [:]), dimOthers: picked != nil,
- playable: player.available) { player.play(from: $0) }
+ playable: player.available, terms: searchTerms, found: currentMatch) { player.play(from: $0) }
case .notes:
if notes.generatingID == session.id {
HStack(spacing: 10) {
@@ -91,6 +94,15 @@ struct RecordView: View {
.onReceive(NotificationCenter.default.publisher(for: .debugScroll)) { n in
if let id = n.object as? String { proxy.scrollTo(id, anchor: .center) }
}
+ .onAppear {
+ showMatches()
+ // The lazy list needs a moment to lay out (and the page to switch) before it can go to the line found.
+ if let id = currentMatch { DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { proxy.scrollTo(id, anchor: .center) } }
+ }
+ .onChange(of: model.query) { showMatches() }
+ .onChange(of: currentMatch) { _, id in
+ if let id, model.recordPage == .transcript { withAnimation { proxy.scrollTo(id, anchor: .center) } }
+ }
// The transcript follows the recording while it plays, until scrolled by hand (as on the Mac).
.onScrollPhaseChange { _, phase in if phase == .interacting { player.userScrolled() } }
.onChange(of: player.lineID(in: session)) { _, id in
@@ -138,6 +150,7 @@ struct RecordView: View {
SpeakerUndoBar(text: u.text) { model.undoSpeakerEdit() }.padding(.horizontal, 16).padding(.bottom, 8)
}
if model.copiedNotes == session.id { CopiedToast().padding(.horizontal, 16).padding(.bottom, player.available ? 0 : 8) }
+ if !player.available, let bar = matchBar { bar.padding(.bottom, 8) }
if !player.available { FixToast(fixes: model.fixes).padding(.horizontal, 16).padding(.bottom, 8) }
if !player.available, player.missing {
Text(player.own ? L("找不到录音,没法播放") : L("找不到原文件,没法播放"))
@@ -152,6 +165,7 @@ struct RecordView: View {
SpeakerUndoBar(text: u.text) { model.undoSpeakerEdit() }
}
FixToast(fixes: model.fixes)
+ if let bar = matchBar { bar }
FollowPill(player: player)
PlayerBar(player: player)
}
@@ -180,6 +194,27 @@ struct RecordView: View {
}
}
+ /// The search in this meeting: its terms (misheard names too), and the line being looked at.
+ private var searchTerms: [String] { searching ? model.searchTerms(session) : [] }
+ private var matches: [String] { TranscriptSearch.matches(session.lines, searchTerms) }
+ private var currentMatch: String? {
+ let ms = matches
+ return ms.isEmpty ? nil : ms[min(model.searchMatch, ms.count - 1)]
+ }
+
+ /// A search that finds lines here opens the transcript at the first one.
+ private func showMatches() {
+ guard searching else { return }
+ model.searchMatch = 0
+ if !matches.isEmpty { model.recordPage = .transcript }
+ }
+
+ private var matchBar: SearchMatchBar? {
+ let n = matches.count
+ guard n > 0, model.recordPage == .transcript else { return nil }
+ return SearchMatchBar(index: min(model.searchMatch, n - 1), count: n) { model.stepSearch($0, in: session) }
+ }
+
/// The speaker the recording is at, while it plays.
private func playingNumber(_ numbers: [String: Int]) -> Int? {
guard player.playing, let id = player.lineID(in: session) else { return nil }
@@ -331,6 +366,9 @@ struct TranscriptList: View {
var dimOthers = false
/// With a recording, a tap on a line plays from it (and opens what was fixed in it, if anything).
var playable = false
+ /// A search's terms, marked in place; the line it is on stands out.
+ var terms: [String] = []
+ var found: String? = nil
let seek: (Double) -> Void
@Environment(\.dynamicTypeSize) private var typeSize
/// The design's 14 pt grey original over a 16 pt translation, scaling with the text size.
@@ -358,9 +396,9 @@ struct TranscriptList: View {
.foregroundStyle(l.speaker == "me" ? Ink.accentText : (focus == nil || mine ? Ink.ink : Ink.tertiary))
}
}
- Text(FixedText.attributed(l)).font(.system(size: srcSize)).foregroundStyle(Ink.secondary).lineSpacing(2)
+ Text(TranscriptSearch.highlighted(FixedText.attributed(l), terms, current: l.id == found)).font(.system(size: srcSize)).foregroundStyle(Ink.secondary).lineSpacing(2)
if let d = l.dst, d != l.src, d != l.shown {
- Text(d).font(.system(size: dstSize)).foregroundStyle(Ink.ink).lineSpacing(4)
+ Text(TranscriptSearch.highlighted(AttributedString(d), terms, current: l.id == found)).font(.system(size: dstSize)).foregroundStyle(Ink.ink).lineSpacing(4)
}
// Tapped open: what its fixed words were heard as, 改回, and 改这句….
if model.openLine == l.id {
@@ -719,6 +757,22 @@ struct FollowPill: View {
}
}
+extension TranscriptSearch {
+ /// The Mac's marks in iOS's colours: every term on the pale accent, the line being looked at solid.
+ static func highlighted(_ base: AttributedString, _ terms: [String], current: Bool) -> AttributedString {
+ var a = base
+ for term in terms where !term.isEmpty {
+ var from = a.startIndex
+ while from < a.endIndex, let r = a[from...].range(of: term, options: .caseInsensitive) {
+ a[r].backgroundColor = current ? Ink.accent : Ink.accentWash
+ a[r].foregroundColor = current ? .white : Ink.accentText
+ from = r.upperBound
+ }
+ }
+ return a
+ }
+}
+
extension Notification.Name {
/// Debug `play:<seconds>`: the open record plays from there.
static let debugPlay = Notification.Name("cicada.debugPlay")
diff --git a/ios/Sources/Cicada/RecordsView.swift b/ios/Sources/Cicada/RecordsView.swift
index dd33d88..034323d 100644
--- a/ios/Sources/Cicada/RecordsView.swift
+++ b/ios/Sources/Cicada/RecordsView.swift
@@ -85,7 +85,7 @@ struct PadRoot: View {
if room.isRunning {
LivePad()
} else if let s = shown {
- NavigationStack { RecordView(session: s) }.id(s.id)
+ NavigationStack { RecordView(session: s, searching: true) }.id(s.id)
} else {
Color.white
}
@@ -282,7 +282,8 @@ struct RecordRow: View {
}
}
-/// Search on its own tab: titles and every line, original and translation.
+/// Search on its own tab: titles and every line, original and translation. A meeting opened from
+/// here opens at the first line found (RecordView's `searching`).
struct SearchView: View {
@EnvironmentObject var model: AppModel
@ObservedObject var store: SessionStore
@@ -291,8 +292,9 @@ struct SearchView: View {
let q = query.trimmingCharacters(in: .whitespaces)
guard !q.isEmpty else { return sessions }
return sessions.filter { s in
- s.displayTitle.localizedCaseInsensitiveContains(q)
- || s.lines.contains { $0.shown.localizedCaseInsensitiveContains(q) || ($0.dst?.localizedCaseInsensitiveContains(q) ?? false) }
+ // As the Mac's records: a name the notes say was misheard finds those lines too.
+ let terms = TranscriptSearch.terms(q, notes: s.notes?.terms ?? [])
+ return s.displayTitle.localizedCaseInsensitiveContains(q) || s.lines.contains { TranscriptSearch.matches($0, terms) }
}
}
@@ -313,10 +315,51 @@ struct SearchView: View {
.background(Ink.ground)
.navigationTitle("搜索")
.navigationDestination(for: String.self) { id in
- if let s = store.sessions.first(where: { $0.id == id }) { RecordView(session: s) }
+ if let s = store.sessions.first(where: { $0.id == id }) { RecordView(session: s, searching: true) }
}
// At the top, under the title: the bottom is the tab bar's.
.searchable(text: $model.query, placement: .navigationBarDrawer(displayMode: .always), prompt: Text("搜索文字记录"))
}
}
}
+
+extension AppModel {
+ /// The search in a meeting: the query, plus how its notes say those names were heard.
+ func searchTerms(_ s: Session) -> [String] { TranscriptSearch.terms(query, notes: s.notes?.terms ?? []) }
+ /// The ids of the lines the search finds in a meeting, in order.
+ func searchMatches(_ s: Session) -> [String] { TranscriptSearch.matches(s.lines, searchTerms(s)) }
+ /// 上一个 / 下一个, round from the last back to the first.
+ func stepSearch(_ d: Int, in s: Session) {
+ let n = searchMatches(s).count
+ guard n > 0 else { return }
+ searchMatch = (min(searchMatch, n - 1) + d + n) % n
+ }
+}
+
+/// Where the search is in the open meeting (本场 2 / 7), and the way to the previous and next line.
+struct SearchMatchBar: View {
+ let index: Int
+ let count: Int
+ let step: (Int) -> Void
+
+ var body: some View {
+ HStack(spacing: 0) {
+ Text("本场 \(index + 1) / \(count)")
+ .font(.subheadline.monospacedDigit()).foregroundStyle(Ink.ink)
+ .padding(.leading, 16).padding(.trailing, 6)
+ button("chevron.up", "上一个", -1)
+ button("chevron.down", "下一个", 1)
+ }
+ .padding(.trailing, 4)
+ .glassEffect(.regular, in: Capsule())
+ }
+
+ private func button(_ icon: String, _ label: LocalizedStringKey, _ d: Int) -> some View {
+ Button { step(d) } label: {
+ Image(systemName: icon).font(.system(size: 15, weight: .semibold)).foregroundStyle(Ink.ink)
+ .frame(width: 44, height: 44).contentShape(Rectangle())
+ }
+ .buttonStyle(.plain)
+ .accessibilityLabel(Text(label))
+ }
+}
diff --git a/ios/shared-sources.txt b/ios/shared-sources.txt
index 26aaa6a..617df4b 100644
--- a/ios/shared-sources.txt
+++ b/ios/shared-sources.txt
@@ -32,3 +32,4 @@ Sources/Cicada/RichCopy.swift
Sources/Cicada/NotesEditing.swift
Sources/Cicada/SpeakerEnrollment.swift
Sources/Cicada/HandsFree.swift
+Sources/Cicada/TranscriptSearch.swift