Skip to content

Commit

Permalink
fix: Fallback artwork to thumbnail
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 16002f0 commit 5353cdd
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions kDrive/UI/View/Files/Preview/AudioCollectionViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import Combine
import InfomaniakCore
import kDriveCore
import kDriveResources
import Kingfisher
import MediaPlayer
import UIKit

Expand All @@ -39,6 +40,7 @@ final class AudioCollectionViewCell: PreviewCollectionViewCell {
public lazy var singleTrackPlayer = SingleTrackPlayer(driveFileManager: driveFileManager)

private var cancellables = Set<AnyCancellable>()
private var thumbnailDownloadTask: Kingfisher.DownloadTask?

override func awakeFromNib() {
super.awakeFromNib()
Expand Down Expand Up @@ -77,6 +79,7 @@ final class AudioCollectionViewCell: PreviewCollectionViewCell {

override func prepareForReuse() {
super.prepareForReuse()
thumbnailDownloadTask?.cancel()
setControls(enabled: false)
singleTrackPlayer.reset()
songTitleLabel.text = ""
Expand All @@ -91,12 +94,12 @@ final class AudioCollectionViewCell: PreviewCollectionViewCell {
Task { @MainActor in
await singleTrackPlayer.setup(with: frozenFile)
setControls(enabled: true)
setupObservationFor(fileId: frozenFile.id)
setupObservationFor(frozenFile: frozenFile)
}
}

/// Setup data flow
@MainActor func setupObservationFor(fileId: Int) {
@MainActor func setupObservationFor(frozenFile: File) {
cancellables.forEach { $0.cancel() }
cancellables.removeAll()

Expand Down Expand Up @@ -153,17 +156,15 @@ final class AudioCollectionViewCell: PreviewCollectionViewCell {
.onCurrentTrackMetadata
.receive(on: DispatchQueue.main)
.sink { metadata in
self.artworkImageView.image = metadata.artwork ?? KDriveResourcesAsset.music.image
self.artistNameLabel.text = metadata.artist
self.songTitleLabel.text = metadata.title
self.handleMetadata(metadata, file: frozenFile)
}
.store(in: &cancellables)

singleTrackPlayer
.onItemError
.receive(on: DispatchQueue.main)
.sink { error in
self.previewDelegate?.errorWhilePreviewing(fileId: fileId, error: error)
self.previewDelegate?.errorWhilePreviewing(fileId: frozenFile.id, error: error)
}
.store(in: &cancellables)
}
Expand All @@ -175,6 +176,20 @@ final class AudioCollectionViewCell: PreviewCollectionViewCell {
iconHeightConstraint.constant = isPortrait ? 254 : 120
}

func handleMetadata(_ metadata: MediaMetadata, file: File) {
if let artwork = metadata.artwork {
artworkImageView.image = artwork
} else {
artworkImageView.image = KDriveResourcesAsset.music.image
thumbnailDownloadTask = file.getThumbnail { thumbnail, isThumbnailAvailable in
guard isThumbnailAvailable else { return }
self.artworkImageView.image = thumbnail
}
}
artistNameLabel.text = metadata.artist
songTitleLabel.text = metadata.title
}

@objc func rotated() {
setUpPlayButtons()
}
Expand Down

0 comments on commit 5353cdd

Please sign in to comment.