Skip to content

Commit

Permalink
MusicFolder: add songs to musicFolder
Browse files Browse the repository at this point in the history
  • Loading branch information
BLeeEZ committed Jun 2, 2021
1 parent 1f169ed commit 23b65bd
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 3 deletions.
8 changes: 7 additions & 1 deletion Amperfy/Api/Subsonic/SsDirectoryParserDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class SsDirectoryParserDelegate: SsSongParserDelegate {
self.directory = nil
self.musicFolder = musicFolder
directoriesBeforeFetch = Set(musicFolder.directories)
songsBeforeFetch = Set()
songsBeforeFetch = Set(musicFolder.songs)
super.init(libraryStorage: libraryStorage, syncWave: syncWave, subsonicUrlCreator: subsonicUrlCreator)
}

Expand Down Expand Up @@ -56,6 +56,9 @@ class SsDirectoryParserDelegate: SsSongParserDelegate {
if let directory = directory {
directory.managedObject.addToSongs(song.managedObject)
songsParsed.insert(song)
} else if let musicFolder = musicFolder {
musicFolder.managedObject.addToSongs(song.managedObject)
songsParsed.insert(song)
}
}
}
Expand Down Expand Up @@ -88,6 +91,9 @@ class SsDirectoryParserDelegate: SsSongParserDelegate {
if let directory = self.directory {
let removedSongs = songsBeforeFetch.subtracting(songsParsed)
removedSongs.forEach{ directory.managedObject.removeFromSongs($0.managedObject) }
} else if let musicFolder = musicFolder {
let removedSongs = songsBeforeFetch.subtracting(songsParsed)
removedSongs.forEach{ musicFolder.managedObject.removeFromSongs($0.managedObject) }
}
}

Expand Down
3 changes: 3 additions & 0 deletions Amperfy/Storage/EntityWrappers/MusicFolder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public class MusicFolder {
var directories: [Directory] {
return managedObject.directories?.compactMap{ Directory(managedObject: $0 as! DirectoryMO) } ?? [Directory]()
}
var songs: [Song] {
return managedObject.songs?.compactMap{ Song(managedObject: $0 as! SongMO) } ?? [Song]()
}

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<attribute name="id" attributeType="String" defaultValueString=""/>
<attribute name="name" attributeType="String" defaultValueString=""/>
<relationship name="directories" optional="YES" toMany="YES" deletionRule="Nullify" ordered="YES" destinationEntity="Directory" inverseName="musicFolder" inverseEntity="Directory"/>
<relationship name="songs" optional="YES" toMany="YES" deletionRule="Nullify" ordered="YES" destinationEntity="Song" inverseName="musicFolder" inverseEntity="Song"/>
</entity>
<entity name="Player" representedClassName="PlayerMO" syncable="YES">
<attribute name="autoCachePlayedSongSetting" optional="YES" attributeType="Integer 16" defaultValueString="1" usesScalarValueType="YES"/>
Expand Down Expand Up @@ -89,6 +90,7 @@
<relationship name="directory" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="Directory" inverseName="songs" inverseEntity="Directory"/>
<relationship name="file" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="SongFile" inverseName="info" inverseEntity="SongFile" elementID="dataMO"/>
<relationship name="genre" optional="YES" maxCount="1" deletionRule="Nullify" ordered="YES" destinationEntity="Genre" inverseName="songs" inverseEntity="Genre"/>
<relationship name="musicFolder" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="MusicFolder" inverseName="songs" inverseEntity="MusicFolder"/>
<relationship name="playlistItems" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="PlaylistItem" inverseName="song" inverseEntity="PlaylistItem" elementID="playlistElements"/>
<relationship name="syncInfo" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="SyncWave" inverseName="songs" inverseEntity="SyncWave"/>
</entity>
Expand Down Expand Up @@ -162,11 +164,11 @@
<element name="Directory" positionX="-30465" positionY="-8235" width="128" height="104"/>
<element name="Genre" positionX="-30465" positionY="-8235" width="128" height="104"/>
<element name="LogEntry" positionX="-30474" positionY="-8244" width="128" height="104"/>
<element name="MusicFolder" positionX="-30456" positionY="-8226" width="128" height="74"/>
<element name="MusicFolder" positionX="-30456" positionY="-8226" width="128" height="89"/>
<element name="Player" positionX="-29890.359375" positionY="-8230.625" width="128" height="119"/>
<element name="Playlist" positionX="-30080.7734375" positionY="-8216.7734375" width="128" height="119"/>
<element name="PlaylistItem" positionX="-30254.0078125" positionY="-8216.28125" width="128" height="74"/>
<element name="Song" positionX="-30463.86328125" positionY="-8276.4765625" width="128" height="269"/>
<element name="Song" positionX="-30463.86328125" positionY="-8276.4765625" width="128" height="284"/>
<element name="SongFile" positionX="-30352.42578125" positionY="-8090.62109375" width="128" height="59"/>
<element name="SyncWave" positionX="-30465" positionY="-8244" width="128" height="209"/>
<element name="UserStatistics" positionX="-30474" positionY="-8244" width="128" height="659"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ extension MusicFolderMO {
@NSManaged public var id: String
@NSManaged public var name: String
@NSManaged public var directories: NSOrderedSet?
@NSManaged public var songs: NSOrderedSet?

}

Expand Down Expand Up @@ -48,3 +49,38 @@ extension MusicFolderMO {
@NSManaged public func removeFromDirectories(_ values: NSOrderedSet)

}

// MARK: Generated accessors for songs
extension MusicFolderMO {

@objc(insertObject:inSongsAtIndex:)
@NSManaged public func insertIntoSongs(_ value: SongMO, at idx: Int)

@objc(removeObjectFromSongsAtIndex:)
@NSManaged public func removeFromSongs(at idx: Int)

@objc(insertSongs:atIndexes:)
@NSManaged public func insertIntoSongs(_ values: [SongMO], at indexes: NSIndexSet)

@objc(removeSongsAtIndexes:)
@NSManaged public func removeFromSongs(at indexes: NSIndexSet)

@objc(replaceObjectInSongsAtIndex:withObject:)
@NSManaged public func replaceSongs(at idx: Int, with value: SongMO)

@objc(replaceSongsAtIndexes:withSongs:)
@NSManaged public func replaceSongs(at indexes: NSIndexSet, with values: [SongMO])

@objc(addSongsObject:)
@NSManaged public func addToSongs(_ value: SongMO)

@objc(removeSongsObject:)
@NSManaged public func removeFromSongs(_ value: SongMO)

@objc(addSongs:)
@NSManaged public func addToSongs(_ values: NSOrderedSet)

@objc(removeSongs:)
@NSManaged public func removeFromSongs(_ values: NSOrderedSet)

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ extension SongMO {
@NSManaged public var artist: ArtistMO?
@NSManaged public var file: SongFileMO?
@NSManaged public var genre: GenreMO?
@NSManaged public var musicFolder: MusicFolderMO?
@NSManaged public var playlistItems: NSSet?
@NSManaged public var syncInfo: SyncWaveMO?

Expand Down
37 changes: 37 additions & 0 deletions AmperfyTests/Cases/API/Subsonic/SsIndexesParserTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,43 @@ class SsIndexesParserTest: AbstractSsParserTest {
XCTAssertEqual(directories[2].name, "Alphaville")
XCTAssertEqual(directories[3].id, "4")
XCTAssertEqual(directories[3].name, "Bob Dylan")

let songs = musicFolder.songs.sorted(by: {Int($0.id)! < Int($1.id)!} )
XCTAssertEqual(songs.count, 2)

var song = songs[0]
XCTAssertEqual(song.id, "111")
XCTAssertEqual(song.title, "Dancing Queen")
XCTAssertNil(song.artist)
XCTAssertNil(song.album)
XCTAssertNil(song.disk)
XCTAssertEqual(song.track, 7)
XCTAssertEqual(song.genre?.id, "")
XCTAssertEqual(song.genre?.name, "Pop")
XCTAssertEqual(song.duration, 146)
XCTAssertEqual(song.year, 1978)
XCTAssertEqual(song.bitrate, 128000)
XCTAssertEqual(song.contentType, "audio/mpeg")
XCTAssertNil(song.url)
XCTAssertEqual(song.size, 8421341)
XCTAssertEqual(song.artwork?.url, "24")

song = songs[1]
XCTAssertEqual(song.id, "112")
XCTAssertEqual(song.title, "Money, Money, Money")
XCTAssertNil(song.artist)
XCTAssertNil(song.album)
XCTAssertNil(song.disk)
XCTAssertEqual(song.track, 7)
XCTAssertEqual(song.genre?.id, "")
XCTAssertEqual(song.genre?.name, "Pop")
XCTAssertEqual(song.duration, 208)
XCTAssertEqual(song.year, 1978)
XCTAssertEqual(song.bitrate, 128000)
XCTAssertEqual(song.contentType, "audio/flac")
XCTAssertNil(song.url)
XCTAssertEqual(song.size, 4910028)
XCTAssertEqual(song.artwork?.url, "25")
}

}

0 comments on commit 23b65bd

Please sign in to comment.