Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Improve decoding of text files for preview #1300

Merged
merged 3 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading