Building Spotify App in Swift 5 & UIKit - Part 15 - Categories (Xcode 12, 2021, Swift 5)
public func getCategoryPlaylists(category: Category,completionHandler: @escaping((Result<[Playlist], Error>)->())) {
createRequest(with: URL(string: Constants.baseAPIURL + "/browse/categories/\(category.id)/playlists?limit=50"), type: .GET) { request in
URLSession.shared.dataTask(with: request) { data, response, error in
guard
let data = data,
error == nil else {
completionHandler(.failure(APIError.failedToGetData))
return
}
do {
let result = try JSONDecoder().decode(CategoryPlaylistsResponse.self, from: data)
let playlists = result.playlists.items
completionHandler(.success(playlists))
} catch {
completionHandler(.failure(error))
}
}
.resume()
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: FeaturedPlaylistCollectionViewCell.identifier, for: indexPath) as? FeaturedPlaylistCollectionViewCell else {
return UICollectionViewCell()
}
let playlist = playlists[indexPath.row]
let viewModel = FeaturedPlaylistCellViewModel(name: playlist.name, artworkURL: URL(string: playlist.images.first?.url ?? ""), creatorName: playlist.owner.display_name)
cell.configure(with: viewModel)
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
collectionView.deselectItem(at: indexPath, animated: true)
let vc = PlaylistViewController(playlist: playlists[indexPath.row])
vc.navigationItem.largeTitleDisplayMode = .never
navigationController?.pushViewController(vc, animated: true)
}
PlaylisyViewController
재활용)뷰 컨트롤러 간의 재활용, 데이터 바인딩은 이전과 매우 동일한 로직이다. 어떻게 컬렉션 뷰의 배치를 짜고 엮을지가 가장 큰 차이점이랄까.
track
JSON 모델의 특정 변수 하나가 어제까지의 타입 정보와 달라지는 바람에 JSON 디코딩이 계속 안 되는 일이 일어났다. available_markets
변수가 [String]
타입이 아니라 [String]?
이어야 함을 알 수 있었다.