Skip to content

Commit

Permalink
fix: [#114] Display artwork in view by resolving cache issue
Browse files Browse the repository at this point in the history
- 플레이어 뷰에서 아트워크 이미지가 보이지 않는 이슈 해결
- queue에 음악을 넣으면서 관련 track을 배열에 따로 저장한 뒤, 해당 값의 아트워크에 접근하여 뷰에 보여줌
  • Loading branch information
zaehorang committed Sep 6, 2024
1 parent 633049d commit 8cd7f78
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
6 changes: 3 additions & 3 deletions MinGenie/MinGenie/Playback/Cells/NowQueueItemCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import SwiftUI
/// A view that displays information about a music item.
struct NowQueueItemCell: View {
// MARK: - Properties
private var artworkSize: CGFloat = 51
private var artworkCornerRadius: CGFloat = 11
private var subtitleVerticalOffset: CGFloat = -8
private let artworkSize: CGFloat = 51
private let artworkCornerRadius: CGFloat = 11
private let subtitleVerticalOffset: CGFloat = -8

let artwork: Artwork?
let title: String
Expand Down
26 changes: 23 additions & 3 deletions MinGenie/MinGenie/Playback/MusicPlayerModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ final class MusicPlayerModel: ObservableObject {
@Published var isPlaying = false
@Published var playbackQueue = ApplicationMusicPlayer.shared.queue
@Published var currentMusicIndex: Int = 0
// Track 캐싱을 위한 배열
@Published var queueTracks: [Track] = []

var playbackStateObserver: AnyCancellable?

Expand Down Expand Up @@ -151,15 +153,18 @@ final class MusicPlayerModel: ObservableObject {
/// - Returns: 필터링된 트랙 배열
private func filterInstrumentalTracks(from tracks: MusicItemCollection<Track>) -> [Track] {
return tracks.filter { track in
// 대, 소문자 구분 없이 제외
return track.title.range(of: "(instrumental)", options: .caseInsensitive) == nil
// 대, 소문자 구분 없이 'instrumental' 및 'inst.'를 제외
let lowercasedTitle = track.title.lowercased()
return !lowercasedTitle.contains("instrumental") && !lowercasedTitle.contains("inst.")
}
}

/// 🐯 특정 노래를 재생하고 그 뒤에 추천 플레이리스트 붙여주기
/// - Parameter song: 관련된 노래를 찾을 때 사용할 노래
func playMusicWithRecommendedList(_ song: Song) {
let track = fromSongToTrack(song)
queueTracks.removeAll()
saveTracks(for: [track])

// 개별 곡 재생
play(track, in: nil, with: nil)
Expand All @@ -168,25 +173,29 @@ final class MusicPlayerModel: ObservableObject {
Task {
let recommendedList = try await getRelatedSongs(song)
if let recommendedList {
saveTracks(for: recommendedList)
try await ApplicationMusicPlayer.shared.queue.insert(recommendedList, position: .tail)
}
}
}


/// 🐯 앨범 전체 재생하고 그 뒤에 추천 플레이리스트 붙여주기
/// - Parameter tracks: 사용자가 선택한 전체 재생할 앨범에 담긴 트랙
/// - Parameter album: 관련된 노래를 찾을 때 사용할 앨범
func playAlbumWithRecommendedList(_ tracks: MusicItemCollection<Track>, album: Album) {
// ⁉️호랑: 이후에 DetailedAlbumModel에서 진행중인 로직을 여기다가 합칠 지 고민해보기 -> 현재는 앨범을 통해 트랙 배열을 받고 해당 메서드에 파라미터로 사용하는 로직

queueTracks.removeAll()
saveTracks(for: tracks)

// 앨범 재생
play(tracks[0], in: tracks, with: nil)

// 추천 트랙 추가
Task {
let recommendedList = try await getRelatedSongs(album)
if let recommendedList {
saveTracks(for: recommendedList)
try await ApplicationMusicPlayer.shared.queue.insert(recommendedList, position: .tail)
}
}
Expand Down Expand Up @@ -254,3 +263,14 @@ final class MusicPlayerModel: ObservableObject {
}

}


extension MusicPlayerModel {
/// queue에 음악을 넣기 전에 따로 배열에 저장을 해주는 메서드
private func saveTracks(for tracks: MusicItemCollection<Track>) {
DispatchQueue.main.async {
self.queueTracks += tracks
}
}
}

4 changes: 2 additions & 2 deletions MinGenie/MinGenie/Playback/NowPlayingView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ struct NowPlayingView: View {
List {
ForEach(playbackQueue.entries.indices, id: \.self) { index in
NowQueueItemCell(
artwork: playbackQueue.entries[index].artwork,
artwork: musicPlayer.queueTracks[index].artwork,
title: playbackQueue.entries[index].title,
subtitle: playbackQueue.entries[index].subtitle
)
Expand Down Expand Up @@ -133,7 +133,7 @@ struct NowPlayingView: View {
if startIndex <= endIndex {
ForEach(startIndex...endIndex, id: \.self) { index in

imageContainer(for: playbackQueue.entries[index].artwork)
imageContainer(for: musicPlayer.queueTracks[index].artwork)
.scaleEffect(1.0 - CGFloat(abs(index - musicPlayer.currentMusicIndex)) * 0.1)
.zIndex(1.0 - Double(abs(index - musicPlayer.currentMusicIndex)))
.offset(x: CGFloat(index - musicPlayer.currentMusicIndex) * 50 * (1 - CGFloat(abs(index - musicPlayer.currentMusicIndex)) * 0.1) + dragOffset, y: 0)
Expand Down

0 comments on commit 8cd7f78

Please sign in to comment.