mirror of
https://github.com/danbee/persephone
synced 2025-03-04 08:39:11 +00:00
The way I was doing it before was resulting in reused views getting updated by the code, which caused albums to appear in the wrong place.
38 lines
1.1 KiB
Swift
38 lines
1.1 KiB
Swift
//
|
|
// AlbumDataSource.swift
|
|
// Persephone
|
|
//
|
|
// Created by Daniel Barber on 2019/2/20.
|
|
// Copyright © 2019 Dan Barber. All rights reserved.
|
|
//
|
|
|
|
import Cocoa
|
|
|
|
class AlbumDataSource: NSObject, NSCollectionViewDataSource {
|
|
var albums: [AlbumItem] = []
|
|
|
|
func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
|
|
return albums.count
|
|
}
|
|
|
|
func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
|
|
let item = collectionView.makeItem(withIdentifier: .albumViewItem, for: indexPath)
|
|
guard let albumViewItem = item as? AlbumViewItem else { return item }
|
|
|
|
albumViewItem.view.wantsLayer = true
|
|
albumViewItem.setAlbum(albums[indexPath.item])
|
|
|
|
if albums[indexPath.item].coverArt == nil {
|
|
AlbumArtService.shared.fetchAlbumArt(for: albums[indexPath.item]) { image in
|
|
self.albums[indexPath.item].coverArt = image
|
|
|
|
DispatchQueue.main.async {
|
|
collectionView.reloadItems(at: [indexPath])
|
|
}
|
|
}
|
|
}
|
|
|
|
return albumViewItem
|
|
}
|
|
}
|