diff --git a/kDrive/UI/Controller/Files/Preview/PreviewViewController.swift b/kDrive/UI/Controller/Files/Preview/PreviewViewController.swift index 713451f64..492f07fb7 100644 --- a/kDrive/UI/Controller/Files/Preview/PreviewViewController.swift +++ b/kDrive/UI/Controller/Files/Preview/PreviewViewController.swift @@ -505,7 +505,12 @@ final class PreviewViewController: UIViewController, PreviewContentCellDelegate, } } previewErrors[fileId] = previewError - collectionView.reloadItems(at: [IndexPath(item: index, section: 0)]) + + // We have to delay reload because errorWhilePreviewing can be called when the collectionView requests a new cell in + // cellForItemAt and iOS 18 seems unhappy about this. + Task { @MainActor [weak self] in + self?.collectionView.reloadItems(at: [IndexPath(item: index, section: 0)]) + } } func openWith(from: UIView) { diff --git a/kDrive/UI/View/Files/Preview/CodePreviewCollectionViewCell.swift b/kDrive/UI/View/Files/Preview/CodePreviewCollectionViewCell.swift index 21edb04b7..1adaf97f4 100644 --- a/kDrive/UI/View/Files/Preview/CodePreviewCollectionViewCell.swift +++ b/kDrive/UI/View/Files/Preview/CodePreviewCollectionViewCell.swift @@ -34,6 +34,7 @@ class CodePreviewCollectionViewCell: PreviewCollectionViewCell { override func awakeFromNib() { super.awakeFromNib() + textView.text = "" textView.textContainerInset = UIEdgeInsets(top: 8, left: 3, bottom: 8, right: 3) markdownParser.code.font = UIFont.monospacedSystemFont( ofSize: UIFontMetrics.default.scaledValue(for: MarkdownParser.defaultFont.pointSize), @@ -66,7 +67,16 @@ class CodePreviewCollectionViewCell: PreviewCollectionViewCell { func configure(with file: File) { do { // Read file - let content = try String(contentsOf: file.localUrl) + let data = try Data(contentsOf: file.localUrl, options: .alwaysMapped) + var maybeString: NSString? + + NSString.stringEncoding(for: data, convertedString: &maybeString, usedLossyConversion: nil) + guard let maybeString else { + throw DriveError.unknownError + } + + let content = maybeString as String + // Display content if file.extension == "md" || file.extension == "markdown" { displayMarkdown(for: content)