Skip to content

Commit

Permalink
Merge pull request #1300 from Infomaniak/fix-preview
Browse files Browse the repository at this point in the history
fix: Improve decoding of text files for preview
  • Loading branch information
adrien-coye authored Sep 24, 2024
2 parents 94294fc + 5fe4081 commit 7d9d7f0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
12 changes: 11 additions & 1 deletion kDrive/UI/View/Files/Preview/CodePreviewCollectionViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 7d9d7f0

Please sign in to comment.