Skip to content

Commit

Permalink
feat: Try to download audio file on playback error
Browse files Browse the repository at this point in the history
Signed-off-by: Philippe Weidmann <philippe.weidmann@infomaniak.com>
  • Loading branch information
PhilippeWeidmann committed Dec 6, 2024
1 parent 9c52b2b commit 16002f0
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions kDrive/UI/Controller/Files/Preview/PreviewViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import AVFoundation
import FloatingPanel
import InfomaniakCore
import InfomaniakDI
Expand Down Expand Up @@ -451,6 +452,8 @@ final class PreviewViewController: UIViewController, PreviewContentCellDelegate,
let file = previewFiles[index]
if ConvertedType.documentTypes.contains(file.convertedType) {
handleOfficePreviewError(error, previewIndex: index)
} else if file.convertedType == .audio {
handleAudioPreviewError(error, previewIndex: index)
}

// We have to delay reload because errorWhilePreviewing can be called when the collectionView requests a new cell in
Expand All @@ -460,6 +463,19 @@ final class PreviewViewController: UIViewController, PreviewContentCellDelegate,
}
}

func handleAudioPreviewError(_ error: Error, previewIndex: Int) {
let file = previewFiles[previewIndex]

guard let avError = error as? AVError,
avError.code == .fileFormatNotRecognized else {
return
}

previewErrors[file.id] = PreviewError(fileId: file.id, downloadError: nil)
guard file.isLocalVersionOlderThanRemote else { return }
downloadFile(at: IndexPath(item: previewIndex, section: 0))
}

func handleOfficePreviewError(_ error: Error, previewIndex: Int) {
let file = previewFiles[previewIndex]

Expand Down Expand Up @@ -526,6 +542,10 @@ final class PreviewViewController: UIViewController, PreviewContentCellDelegate,
return
}

downloadFile(at: indexPath)
}

private func downloadFile(at indexPath: IndexPath) {
DownloadQueue.instance.temporaryDownload(
file: currentFile,
userId: accountManager.currentUserId,
Expand Down Expand Up @@ -558,6 +578,7 @@ final class PreviewViewController: UIViewController, PreviewContentCellDelegate,
} else {
(collectionView.cellForItem(at: indexPath) as? DownloadingPreviewCollectionViewCell)?
.previewDownloadTask?.cancel()
previewErrors[currentFile.id] = nil
collectionView.endEditing(true)
collectionView.reloadItems(at: [indexPath])
updateNavigationBar()
Expand Down

0 comments on commit 16002f0

Please sign in to comment.