Stuff…
This commit is contained in:
77
TagTunes/AlbumItem.swift
Executable file
77
TagTunes/AlbumItem.swift
Executable file
@@ -0,0 +1,77 @@
|
||||
// AlbumItem.swift
|
||||
// TagTunes
|
||||
//
|
||||
// Created by Kim Wittenburg on 29.05.15.
|
||||
// Copyright (c) 2015 Kim Wittenburg. All rights reserved.
|
||||
//
|
||||
|
||||
import SearchAPI
|
||||
|
||||
/// Represents an `Album` from the Search API.
|
||||
public class AlbumItem: TagTunesGroupItem {
|
||||
|
||||
/// Returns the `entity` as an `Album`.
|
||||
public var album: Album {
|
||||
return entity as! Album
|
||||
}
|
||||
|
||||
public override var children: [TagTunesEntityItem] {
|
||||
didSet {
|
||||
super.children.sortInPlace { (item1: TagTunesEntityItem, item2: TagTunesEntityItem) in
|
||||
let song1 = (item1 as! SongItem).song
|
||||
let song2 = (item2 as! SongItem).song
|
||||
return song1.discNumber < song2.discNumber || (song1.discNumber == song2.discNumber && song1.trackNumber < song2.trackNumber)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override func addAssociatedTracks<S : SequenceType where S.Generator.Element == TagTunesTrack>(tracks: S) -> Set<TagTunesTrack> {
|
||||
var unmatchedTracks = Set<TagTunesTrack>()
|
||||
for track in tracks {
|
||||
var inserted = false
|
||||
for child in children where child is SongItem {
|
||||
let songItem = child as! SongItem
|
||||
if (track.discNumber == songItem.song.discNumber || track.discNumber == 0) && track.trackNumber == songItem.song.trackNumber {
|
||||
songItem.addAssociatedTracks([track])
|
||||
inserted = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !inserted {
|
||||
unmatchedTracks.insert(track)
|
||||
}
|
||||
}
|
||||
return unmatchedTracks
|
||||
}
|
||||
|
||||
public override func addAssociatedTrack(track: TagTunesTrack, forChildEntity childEntity: SearchAPIEntity) {
|
||||
for child in children where child.entity.id == childEntity.id {
|
||||
child.addAssociatedTracks([track])
|
||||
return
|
||||
}
|
||||
let child = SongItem(entity: childEntity, parentItem: self)
|
||||
child.addAssociatedTracks([track])
|
||||
children.append(child)
|
||||
}
|
||||
|
||||
public override var hasCommonArtist: Bool {
|
||||
for child in children where child is SongItem {
|
||||
if album.artist != (child as! SongItem).song.artist {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
override class var childEntityType: SearchAPIRequest.EntityType {
|
||||
return .Song
|
||||
}
|
||||
|
||||
override func processLookupResult(result: SearchAPIResult) {
|
||||
for song in result.contentsOfCollectionWithID(album.id) where !children.contains({ $0.entity == song }) {
|
||||
let item = SongItem(entity: song, parentItem: self)
|
||||
children.append(item)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user