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.

feat(ios): save each meeting as Markdown into a folder picked in Files, such as iCloud Drive or an Obsidian vault

wquguru4e2880691b93
diff --git a/Sources/Cicada/FolderExport.swift b/Sources/Cicada/FolderExport.swift
index 5d830e4..74abd9f 100644
--- a/Sources/Cicada/FolderExport.swift
+++ b/Sources/Cicada/FolderExport.swift
@@ -1,7 +1,22 @@
+#if os(macOS)
import AppKit
+#endif
import Foundation
import UserNotifications
+/// Where 存到文件夹 is set: `AppSettings` on the Mac, the iOS app's own (its folder comes back from a
+/// security-scoped bookmark, since a sandboxed app can't reopen a path it was once given).
+@MainActor protocol FolderExportPrefs: AnyObject {
+ var exportOn: Bool { get set }
+ var exportFolder: String { get set }
+ /// The folder to write to; the Mac's is the path itself.
+ var exportFolderURL: URL? { get }
+}
+
+extension FolderExportPrefs {
+ var exportFolderURL: URL? { exportFolder.isEmpty ? nil : URL(fileURLWithPath: exportFolder, isDirectory: true) }
+}
+
/// 存到文件夹: the file a settled meeting becomes (docs/folder-export.md). The name and frontmatter
/// are a contract with the ports (Tests/fixtures/folder-export); the body is 导出 Markdown's.
enum FolderExportFormat {
@@ -118,11 +133,13 @@ enum FolderExportFormat {
.map { URL(fileURLWithPath: $0.0, isDirectory: true) }
}
+ #if os(macOS)
static var obsidianVaults: [URL] {
let file = FileManager.default.homeDirectoryForCurrentUser
.appendingPathComponent("Library/Application Support/obsidian/obsidian.json")
return (try? Data(contentsOf: file)).map { obsidianVaults($0) } ?? []
}
+ #endif
}
/// Writes settled meetings into the chosen folder and keeps the index of what it wrote. UI state on
@@ -153,7 +170,7 @@ enum FolderExportFormat {
@Published private(set) var checking = false
let store: SessionStore
- let settings: AppSettings
+ let settings: any FolderExportPrefs
/// The meeting's final notes pass is running or waiting.
var settling: (String) -> Bool = { _ in false }
private let indexURL: URL
@@ -162,7 +179,7 @@ enum FolderExportFormat {
/// Told about this problem already (cleared once a write works).
private var notified = false
- init(store: SessionStore, settings: AppSettings, indexURL: URL? = nil) {
+ init(store: SessionStore, settings: any FolderExportPrefs, indexURL: URL? = nil) {
self.store = store
self.settings = settings
self.indexURL = indexURL ?? store.root.deletingLastPathComponent().appendingPathComponent("FolderExport.json")
@@ -172,7 +189,7 @@ enum FolderExportFormat {
}
var on: Bool { settings.exportOn && !settings.exportFolder.isEmpty }
- var folder: URL? { settings.exportFolder.isEmpty ? nil : URL(fileURLWithPath: settings.exportFolder, isDirectory: true) }
+ var folder: URL? { settings.exportFolder.isEmpty ? nil : settings.exportFolderURL }
func file(for id: String) -> URL? {
guard on, let folder, index.folder == settings.exportFolder, let name = index.files[id] else { return nil }
@@ -195,9 +212,11 @@ enum FolderExportFormat {
return [url.deletingLastPathComponent().path, url.path].map { fm.displayName(atPath: $0) }.joined(separator: " › ")
}
+ #if os(macOS)
static func vault(of url: URL) -> URL? {
FolderExportFormat.obsidianVaults.first { url.path == $0.path || url.path.hasPrefix($0.path + "/") }
}
+ #endif
// MARK: Triggers
@@ -336,6 +355,9 @@ enum FolderExportFormat {
/// Off the main thread. Stops at the first problem: the rest would fail the same way.
nonisolated static func write(_ jobs: [Job], to folder: URL, ours: [String: String]) -> [Outcome] {
+ // iOS: a folder picked in Files is reachable only inside this (the Mac's path: no-op).
+ let scoped = folder.startAccessingSecurityScopedResource()
+ defer { if scoped { folder.stopAccessingSecurityScopedResource() } }
if let p = ensure(folder) { return [.stopped(p)] }
let fm = FileManager.default
var ours = ours
@@ -379,6 +401,8 @@ enum FolderExportFormat {
/// Writes and removes a hidden file: the check after choosing a folder.
nonisolated static func probe(_ folder: URL) -> Problem? {
+ let scoped = folder.startAccessingSecurityScopedResource()
+ defer { if scoped { folder.stopAccessingSecurityScopedResource() } }
if let p = ensure(folder) { return p }
let file = folder.appendingPathComponent(".cicada-check")
do {
@@ -423,8 +447,10 @@ enum FolderExportFormat {
}
}
+ #if os(macOS)
/// System Settings › 隐私与安全性 › 文件和文件夹.
static func openPrivacySettings() {
NSWorkspace.shared.open(URL(string: "x-apple.systempreferences:com.apple.preference.security?Privacy_FilesAndFolders")!)
}
+ #endif
}
diff --git a/Sources/Cicada/Settings.swift b/Sources/Cicada/Settings.swift
index 188fbdf..d5d400c 100644
--- a/Sources/Cicada/Settings.swift
+++ b/Sources/Cicada/Settings.swift
@@ -451,3 +451,5 @@ func contentLog(_ message: @autoclosure () -> String) {
extension AppSettings: FixSwitches {}
extension AppSettings: ImportSettings {}
+
+extension AppSettings: FolderExportPrefs {}
diff --git a/ios/Sources/Cicada/FolderPage.swift b/ios/Sources/Cicada/FolderPage.swift
new file mode 100644
index 0000000..c20801f
--- /dev/null
+++ b/ios/Sources/Cicada/FolderPage.swift
@@ -0,0 +1,152 @@
+import SwiftUI
+import UniformTypeIdentifiers
+
+/// 存到文件夹's settings on iOS: the Mac's names (`exportOn`, `exportFolder`), plus the folder as a
+/// security-scoped bookmark, since an app can reopen a folder picked in Files only through one.
+@MainActor
+final class FolderPrefs: FolderExportPrefs {
+ private let d = UserDefaults.standard
+ private static let bookmarkKey = "iosExportBookmark"
+
+ var exportOn: Bool {
+ get { d.bool(forKey: "exportOn") }
+ set { d.set(newValue, forKey: "exportOn") }
+ }
+ var exportFolder: String {
+ get { d.string(forKey: "exportFolder") ?? "" }
+ set { d.set(newValue, forKey: "exportFolder") }
+ }
+
+ var exportFolderURL: URL? {
+ guard let data = d.data(forKey: Self.bookmarkKey) else { return nil }
+ var stale = false
+ guard let url = try? URL(resolvingBookmarkData: data, options: [], relativeTo: nil, bookmarkDataIsStale: &stale) else { return nil }
+ if stale { remember(url) }
+ return url
+ }
+
+ /// Keeps the picked folder for later launches. Must run while the picker's access is open.
+ @discardableResult
+ func remember(_ url: URL) -> Bool {
+ let scoped = url.startAccessingSecurityScopedResource()
+ defer { if scoped { url.stopAccessingSecurityScopedResource() } }
+ guard let data = try? url.bookmarkData(options: [], includingResourceValuesForKeys: nil, relativeTo: nil) else {
+ debugLog("folder export: no bookmark for \(url.lastPathComponent)")
+ return false
+ }
+ d.set(data, forKey: Self.bookmarkKey)
+ return true
+ }
+}
+
+/// 设置 › 存到文件夹: on/off, the folder (picked in Files: iCloud Drive, an Obsidian vault, any
+/// app's folder), what was saved last or why it wasn't, and the meetings from before.
+struct FolderPage: View {
+ @EnvironmentObject var model: AppModel
+ @ObservedObject var exporter: FolderExporter
+ @State private var choosing = false
+
+ var body: some View {
+ ScrollView {
+ VStack(alignment: .leading, spacing: 0) {
+ Card {
+ VStack(alignment: .leading, spacing: 4) {
+ Toggle(isOn: Binding(get: { exporter.on }, set: { on in
+ if on {
+ if let f = exporter.folder { exporter.choose(f) } else { choosing = true }
+ } else {
+ exporter.turnOff()
+ }
+ model.objectWillChange.send()
+ })) { Text("存到文件夹") }
+ .tint(Ink.ok)
+ Text("纪要和文字记录存成 Markdown;文件夹同步到哪,内容就到哪")
+ .font(.footnote).foregroundStyle(Ink.secondary).fixedSize(horizontal: false, vertical: true)
+ }
+ .padding(.horizontal, 16).padding(.vertical, 12)
+ if let folder = exporter.folder {
+ Hairline()
+ HStack(spacing: 12) {
+ Image(systemName: "folder.fill").font(.system(size: 18)).foregroundStyle(Color(hex: 0x2B4A8B))
+ .frame(width: 40, height: 40).background(Color(hex: 0xE8EEF9), in: RoundedRectangle(cornerRadius: 10, style: .continuous))
+ VStack(alignment: .leading, spacing: 2) {
+ Text(verbatim: FolderExporter.shortPath(folder)).foregroundStyle(Ink.ink).lineLimit(2)
+ if exporter.on, let name = exporter.index.lastFile, let at = exporter.index.lastAt {
+ Text(L("上次存于 \(dateText(at, "MMMdHHmm")) · \(name)")).font(.caption).foregroundStyle(Ink.secondary).lineLimit(1)
+ }
+ }
+ Spacer(minLength: 8)
+ Button { choosing = true } label: {
+ Text("更改…").font(.subheadline).foregroundStyle(Ink.ink)
+ .padding(.horizontal, 12).frame(minHeight: 34).background(Ink.ground, in: Capsule())
+ }
+ .buttonStyle(.plain)
+ }
+ .padding(.horizontal, 16).padding(.vertical, 10)
+ }
+ }
+ .padding(.top, 12)
+
+ if exporter.on, let problem = exporter.problem, let folder = exporter.folder {
+ Card {
+ HStack(alignment: .top, spacing: 12) {
+ Image(systemName: "exclamationmark.circle").foregroundStyle(Ink.accentText).padding(.top, 2)
+ VStack(alignment: .leading, spacing: 3) {
+ switch problem {
+ case .denied:
+ Text(L("没有权限写入 \(FolderExporter.shortPath(folder))")).font(.subheadline.weight(.semibold))
+ Text("换个文件夹后自动补存").font(.footnote).foregroundStyle(Ink.secondary)
+ case .missing:
+ Text(L("找不到 \(FolderExporter.shortPath(folder))")).font(.subheadline.weight(.semibold))
+ Text("接上或换个文件夹后自动补存").font(.footnote).foregroundStyle(Ink.secondary)
+ case .failed(let message):
+ Text("没存上").font(.subheadline.weight(.semibold))
+ Text(verbatim: message).font(.footnote).foregroundStyle(Ink.secondary).lineLimit(2)
+ }
+ }
+ Spacer(minLength: 0)
+ }
+ .padding(16)
+ }
+ .padding(.top, 12)
+ } else if exporter.checking, let folder = exporter.folder {
+ Text(L("正在确认能写入 \(FolderExporter.shortPath(folder))…")).font(.footnote).foregroundStyle(Ink.secondary)
+ .padding(.horizontal, 4).padding(.top, 10)
+ }
+
+ let left = exporter.unwritten.count
+ if exporter.on, left > 0 {
+ Card {
+ HStack {
+ VStack(alignment: .leading, spacing: 2) {
+ Text("以前的会议").foregroundStyle(Ink.ink)
+ Text(L("还有 \(left) 场没存")).font(.footnote).foregroundStyle(Ink.secondary)
+ }
+ Spacer()
+ Button { exporter.writeAll() } label: {
+ Text("也存一份").font(.subheadline.weight(.semibold)).foregroundStyle(.white)
+ .padding(.horizontal, 14).frame(minHeight: 36).background(Ink.ink, in: Capsule())
+ }
+ .buttonStyle(.plain)
+ }
+ .padding(.horizontal, 16).padding(.vertical, 10)
+ }
+ .padding(.top, 12)
+ }
+ Text("在“文件”里选一次文件夹:iCloud 云盘、Obsidian 库,或其他支持“文件”的 app。")
+ .font(.footnote).foregroundStyle(Ink.secondary).padding(.horizontal, 4).padding(.top, 10)
+ .fixedSize(horizontal: false, vertical: true)
+ }
+ .padding(.horizontal, 16).padding(.bottom, 120)
+ .frame(maxWidth: 640).frame(maxWidth: .infinity)
+ }
+ .background(Ink.ground)
+ .navigationTitle(Text("存到文件夹"))
+ .navigationBarTitleDisplayMode(.inline)
+ .fileImporter(isPresented: $choosing, allowedContentTypes: [.folder]) { result in
+ guard case .success(let url) = result else { return }
+ if model.folderPrefs.remember(url) { exporter.choose(url) }
+ model.objectWillChange.send()
+ }
+ }
+}