Skip to content

Commit

Permalink
LibraryUpdater: fix duration update
Browse files Browse the repository at this point in the history
  • Loading branch information
BLeeEZ committed Jan 24, 2024
1 parent 40ca005 commit b1578c5
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 38 deletions.
3 changes: 1 addition & 2 deletions AmperfyKit/Api/Ampache/AlbumParserDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ class AlbumParserDelegate: AmpacheXmlLibParser {
albumBuffer?.rating = rating
rating = 0
if let parsedAlbum = albumBuffer {
parsedAlbum.updateDuration()
parsedAlbum.artist?.updateDuration()
parsedAlbum.updateDuration(updateArtistToo: true)
albumsParsed.insert(parsedAlbum)
}
parseNotifier?.notifyParsedObject(ofType: .album)
Expand Down
2 changes: 1 addition & 1 deletion AmperfyKit/Api/Subsonic/SsAlbumParserDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class SsAlbumParserDelegate: SsXmlLibWithArtworkParser {
case "album":
parsedCount += 1
if let album = albumBuffer {
album.updateDuration()
album.updateDuration(updateArtistToo: true)
parsedAlbums.append(album)
}
albumBuffer = nil
Expand Down
9 changes: 6 additions & 3 deletions AmperfyKit/Storage/EntityWrappers/AbstractPlayable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,14 @@ public class AbstractPlayable: AbstractLibraryEntity, Downloadable {
public var duration: Int {
get { return Int(playableManagedObject.combinedDuration) }
}
public func updateDuration() {
public func updateDuration() -> Bool {
var isUpdated = false
let combinedDuration = playableManagedObject.playDuration > 0 ? playableManagedObject.playDuration : playableManagedObject.remoteDuration
if playableManagedObject.combinedDuration != combinedDuration {
playableManagedObject.combinedDuration = combinedDuration
isUpdated = true
}
return isUpdated
}

/// duration based on the data from the xml parser
Expand All @@ -120,10 +123,10 @@ public class AbstractPlayable: AbstractLibraryEntity, Downloadable {
guard Int16.isValid(value: newValue), playableManagedObject.playDuration != Int16(newValue) else { return }
playableManagedObject.playDuration = Int16(newValue)
playableManagedObject.combinedDuration = Int16(newValue)
updateDuration()
_ = updateDuration()
// songs need to update more members
if let song = asSong {
song.updateDuration()
_ = song.updateDuration()
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions AmperfyKit/Storage/EntityWrappers/Album.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ public class Album: AbstractLibraryEntity {
if Int16.isValid(value: newValue), managedObject.remoteDuration != Int16(newValue) {
managedObject.remoteDuration = Int16(newValue)
}
updateDuration()
updateDuration(updateArtistToo: true)
}
}
public func updateDuration() {
public func updateDuration(updateArtistToo: Bool) {
if isSongsMetaDataSynced {
let playablesDuration = playables.reduce(0){ $0 + $1.duration }
if Int16.isValid(value: playablesDuration), managedObject.duration != Int16(playablesDuration) {
Expand All @@ -89,7 +89,9 @@ public class Album: AbstractLibraryEntity {
managedObject.duration = managedObject.remoteDuration
}
}
artist?.updateDuration()
if updateArtistToo {
artist?.updateDuration()
}
}
public var artist: Artist? {
get {
Expand Down
13 changes: 9 additions & 4 deletions AmperfyKit/Storage/EntityWrappers/Song.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,15 @@ public class Song: AbstractPlayable, Identifyable {
guard let album = album else { return true }
return album.isOrphaned
}
public override func updateDuration() {
super.updateDuration()
album?.updateDuration()
artist?.updateDuration()
public override func updateDuration() -> Bool {
return updateDuration(updateArtistAndAlbumToo: true)
}
public func updateDuration(updateArtistAndAlbumToo: Bool) -> Bool {
let isUpdateNeeded = super.updateDuration()
if updateArtistAndAlbumToo, isUpdateNeeded {
album?.updateDuration(updateArtistToo: true)
}
return isUpdateNeeded
}

override public var creatorName: String {
Expand Down
42 changes: 17 additions & 25 deletions AmperfyKit/Storage/LibraryUpdater.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,16 @@ public class LibraryUpdater {
os_log("Perform blocking library update (DONE): alphabeticSectionInitial", log: log, type: .info)
}
if storage.librarySyncVersion < .v13 {
storage.librarySyncVersion = .v13 // if App crashes don't do this step again -> This step is only for convenience
os_log("Perform blocking library update (START): AbstractPlayable.duration", log: log, type: .info)
updateAbstractPlayableDuration()
os_log("Perform blocking library update (DONE): AbstractPlayable.duration", log: log, type: .info)
}
if storage.librarySyncVersion < .v14 {
os_log("Perform blocking library update (START): Artist,Album,Playlist duration", log: log, type: .info)
updateArtistAlbumPlaylistDuration()
os_log("Perform blocking library update (DONE): Artist,Album,Playlist duration", log: log, type: .info)
}
if storage.librarySyncVersion < .v15 {
os_log("Perform blocking library update (START): Playlist remoteSongCount", log: log, type: .info)
updatePlaylistRemoteSongCount()
os_log("Perform blocking library update (DONE): Playlist remoteSongCount", log: log, type: .info)
storage.librarySyncVersion = .v15 // if App crashes don't do this step again -> This step is only for convenience
os_log("Perform blocking library update (START): Artist,Album,Playlist duration,remoteSongCount", log: log, type: .info)
updateArtistAlbumPlaylistDurationAndSongCount()
os_log("Perform blocking library update (DONE): Artist,Album,Playlist duration,remoteSongCount", log: log, type: .info)
}
storage.librarySyncVersion = .newestVersion
}
Expand Down Expand Up @@ -120,31 +117,26 @@ public class LibraryUpdater {
private func updateAbstractPlayableDuration() {
os_log("Library update: Songs", log: log, type: .info)
let songs = storage.main.library.getSongs()
songs.forEach{ $0.updateDuration() }
songs.forEach{ _ = $0.updateDuration(updateArtistAndAlbumToo: false) }
os_log("Library update: PodcastEpisodes", log: log, type: .info)
let podcastEpisodes = storage.main.library.getPodcastEpisodes()
podcastEpisodes.forEach{ $0.updateDuration() }
podcastEpisodes.forEach{ _ = $0.updateDuration() }
storage.main.saveContext()
}

private func updateArtistAlbumPlaylistDuration() {
os_log("Library update: Artists", log: log, type: .info)
private func updateArtistAlbumPlaylistDurationAndSongCount() {
os_log("Library update: Albums Duration", log: log, type: .info)
let albums = storage.main.library.getAlbums()
albums.forEach{ $0.updateDuration(updateArtistToo: false) }
os_log("Library update: Artists Duration", log: log, type: .info)
let artists = storage.main.library.getArtists()
artists.forEach{ $0.updateDuration() }
os_log("Library update: Albums", log: log, type: .info)
let albums = storage.main.library.getAlbums()
albums.forEach{ $0.updateDuration() }
os_log("Library update: Playlists", log: log, type: .info)
os_log("Library update: Playlists Duration and SongCount", log: log, type: .info)
let playlists = storage.main.library.getPlaylists()
playlists.forEach{ $0.updateDuration() }
storage.main.saveContext()
}

private func updatePlaylistRemoteSongCount() {
os_log("Library update: Playlists", log: log, type: .info)
let playlists = storage.main.library.getPlaylists()
playlists.forEach{ $0.remoteSongCount = $0.songCount }
storage.main.saveContext()
playlists.forEach{
$0.updateDuration()
$0.remoteSongCount = $0.songCount
}
}

}

0 comments on commit b1578c5

Please sign in to comment.