mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-28 13:02:21 +01:00
- HexInbox recognised only Documents/Inbox as iOS's copy of an opened file. On iOS 27 the copy lands in tmp/<bundle id>-Inbox, so it stayed behind after the sheet closed (seen in the simulator session). Both folders are now treated as Hex's Inbox, for deleting after the sheet and for the launch sweep. - The deletionNotSupported sentence now matches Android's approved correction: "1 replacement writes nothing, or only spaces or line breaks, which this iPhone can't do yet: %@" (plural "%lld replacements write ..."), zh "%lld 项替换不写入任何内容,或只写入空格或换行,这台 iPhone 暂时做不到:%@". The old "remove words instead of writing something" was false for "new paragraph". Checked in the simbox (iPhone 18 Pro, iOS 27.2): read 59/59, merge 12/12 phone, write 2/2 phone, testWriterWritesTheBundlesVersion, testReviewSheetWords and the existing suites pass; desktop.hexwords opened, Add, Undo; both file copies gone after Add and after Cancel.
136 lines
6 KiB
Swift
136 lines
6 KiB
Swift
import OSLog
|
|
import SwiftUI
|
|
import UIKit
|
|
|
|
/// Files other apps hand to Hex: AirDrop, Files and Open in Hex arrive here as a copy iOS makes
|
|
/// in Hex's own container (`LSSupportsOpeningDocumentsInPlace` is NO): `Documents/Inbox`, or
|
|
/// `tmp/<bundle id>-Inbox` on iOS 27. Today that is only Hex words
|
|
/// files; another kind (a typing config, #2529) would add its own `Item` case and sheet.
|
|
///
|
|
/// Each file is copied to a private temporary folder under a name Hex picks, read there, shown
|
|
/// in a sheet over whatever is on screen, and both copies are deleted when the sheet closes:
|
|
/// after Add, Cancel or a refusal. Nothing applies without the sheet's Add, and only counts and
|
|
/// reasons are logged, never what a file holds.
|
|
@MainActor
|
|
enum HexInbox {
|
|
enum Item {
|
|
/// nil: the file couldn't be opened at all.
|
|
case words(HexWordsReadResult?)
|
|
}
|
|
|
|
private static let log = Logger(subsystem: "anomaly.hex.mobile", category: "inbox")
|
|
private static let folderName = "HexInbox"
|
|
|
|
private static var privateFolder: URL {
|
|
FileManager.default.temporaryDirectory.appendingPathComponent(folderName, isDirectory: true)
|
|
}
|
|
|
|
/// Where iOS puts the copies it hands Hex.
|
|
private static var inboxFolders: [URL] {
|
|
var folders: [URL] = []
|
|
if let documents = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
|
|
folders.append(documents.appendingPathComponent("Inbox", isDirectory: true))
|
|
}
|
|
if let bundle = Bundle.main.bundleIdentifier {
|
|
folders.append(FileManager.default.temporaryDirectory.appendingPathComponent(bundle + "-Inbox", isDirectory: true))
|
|
}
|
|
return folders
|
|
}
|
|
|
|
/// Handles a file URL Hex was asked to open; false for any other URL.
|
|
@discardableResult
|
|
static func open(_ url: URL) -> Bool {
|
|
guard url.isFileURL else { return false }
|
|
let scoped = url.startAccessingSecurityScopedResource()
|
|
defer { if scoped { url.stopAccessingSecurityScopedResource() } }
|
|
|
|
var leftovers: [URL] = []
|
|
if isInInbox(url) { leftovers.append(url) }
|
|
let result: HexWordsReadResult?
|
|
if let size = try? url.resourceValues(forKeys: [.fileSizeKey]).fileSize, size > HexWordsReader.maxBytes {
|
|
result = .refused(.tooLarge)
|
|
} else if let copy = privateCopy(of: url) {
|
|
leftovers.append(copy)
|
|
result = HexWordsReader.read(contentsOf: copy)
|
|
} else {
|
|
result = HexWordsReader.read(contentsOf: url)
|
|
}
|
|
logResult(result)
|
|
// On a cold launch the window may not be up yet when the URL arrives.
|
|
DispatchQueue.main.async {
|
|
present(.words(result)) {
|
|
for file in leftovers { try? FileManager.default.removeItem(at: file) }
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
/// Clears copies a sheet never got to delete (Hex ended while it showed). Inbox copies
|
|
/// are cleared only once they are an hour old, so a file that is arriving now stays.
|
|
static func sweep(now: Date = Date()) {
|
|
try? FileManager.default.removeItem(at: privateFolder)
|
|
for inbox in inboxFolders {
|
|
guard let files = try? FileManager.default.contentsOfDirectory(
|
|
at: inbox, includingPropertiesForKeys: [.contentModificationDateKey]
|
|
) else { continue }
|
|
for file in files {
|
|
let modified = (try? file.resourceValues(forKeys: [.contentModificationDateKey]))?.contentModificationDate
|
|
if let modified, now.timeIntervalSince(modified) < 3600 { continue }
|
|
try? FileManager.default.removeItem(at: file)
|
|
}
|
|
}
|
|
}
|
|
|
|
private static func isInInbox(_ url: URL) -> Bool {
|
|
let path = url.standardizedFileURL.resolvingSymlinksInPath().path
|
|
return inboxFolders.contains { path.hasPrefix($0.standardizedFileURL.resolvingSymlinksInPath().path + "/") }
|
|
}
|
|
|
|
/// A copy under a name Hex picks: a file's own name is never used as a path.
|
|
private static func privateCopy(of url: URL) -> URL? {
|
|
let folder = privateFolder
|
|
try? FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)
|
|
let copy = folder.appendingPathComponent(UUID().uuidString)
|
|
do {
|
|
try FileManager.default.copyItem(at: url, to: copy)
|
|
return copy
|
|
} catch {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
private static func logResult(_ result: HexWordsReadResult?) {
|
|
switch result {
|
|
case nil:
|
|
log.notice("words file couldn't be opened")
|
|
case .refused(let reason):
|
|
log.notice("words file refused: \(reason.rawValue, privacy: .public)")
|
|
case .read(let file):
|
|
let reasons = Dictionary(grouping: file.skipped, by: \.reason.rawValue)
|
|
.map { "\($0.key) \($0.value.count)" }.sorted().joined(separator: ", ")
|
|
log.notice("words file read: version \(file.version), \(file.vocabulary.count) terms, \(file.replacements.count) replacements, skipped: \(reasons, privacy: .public)")
|
|
}
|
|
}
|
|
|
|
private static func present(_ item: Item, onClose: @escaping () -> Void) {
|
|
let scene = UIApplication.shared.connectedScenes
|
|
.compactMap { $0 as? UIWindowScene }
|
|
.first { $0.activationState == .foregroundActive }
|
|
?? UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }.first
|
|
guard var top = scene?.keyWindow?.rootViewController ?? scene?.windows.first?.rootViewController else {
|
|
onClose()
|
|
return
|
|
}
|
|
while let presented = top.presentedViewController, !presented.isBeingDismissed {
|
|
top = presented
|
|
}
|
|
let view: AnyView
|
|
switch item {
|
|
case .words(let result):
|
|
view = AnyView(HexWordsReviewSheet(result: result, onClose: onClose))
|
|
}
|
|
let controller = UIHostingController(rootView: view)
|
|
controller.overrideUserInterfaceStyle = .dark
|
|
top.present(controller, animated: true)
|
|
}
|
|
}
|