Skip to content

Commit

Permalink
Use bulk db updates for (un)marking multiple episodes as played or fa…
Browse files Browse the repository at this point in the history
…vourite.
  • Loading branch information
GetToSet committed Oct 2, 2022
1 parent f870245 commit 5010e39
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 32 deletions.
54 changes: 54 additions & 0 deletions Doughnut/Library/Library.swift
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,60 @@ class Library: NSObject {
}
}

func batchUpdateEpisodes(favourite: Bool, episodes: [Episode], completion: ((Result<[Episode], LibraryError>) -> Void)? = nil) {
dbQueue?.asyncWrite({ db in
let keys = episodes.compactMap { $0.id }
try Episode.filter(keys: keys)
.updateAll(db, Column("favourite").set(to: favourite))
}, completion: { _, result in
episodes.forEach {
$0.favourite = favourite
}

switch result {
case .success:
DispatchQueue.main.async {
self.delegate?.libraryUpdatedEpisodes(episodes: episodes)
}
completion?(.success(episodes))
case let .failure(error):
if let error = error as? DatabaseError {
Library.handleDatabaseError(error)
completion?(.failure(.databaseError(error)))
} else {
completion?(.failure(.unknown(error)))
}
}
})
}

func batchUpdateEpisodes(played: Bool, episodes: [Episode], completion: ((Result<[Episode], LibraryError>) -> Void)? = nil) {
dbQueue?.asyncWrite({ db in
let keys = episodes.compactMap { $0.id }
try Episode.filter(keys: keys)
.updateAll(db, Column("played").set(to: played))
}, completion: { _, result in
episodes.forEach {
$0.played = played
}

switch result {
case .success:
DispatchQueue.main.async {
self.delegate?.libraryUpdatedEpisodes(episodes: episodes)
}
completion?(.success(episodes))
case let .failure(error):
if let error = error as? DatabaseError {
Library.handleDatabaseError(error)
completion?(.failure(.databaseError(error)))
} else {
completion?(.failure(.unknown(error)))
}
}
})
}

// Async episode save and event emission
func save(episode: Episode, completion: ((Result<Episode, LibraryError>) -> Void)? = nil) {
dbQueue?.asyncWrite({ db in
Expand Down
10 changes: 2 additions & 8 deletions Doughnut/View Controllers/EpisodeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -449,10 +449,7 @@ final class EpisodeViewController: NSViewController, NSTableViewDelegate, NSTabl

let shouldMarkPlayed = !allPlayed

for episode in episodes {
episode.played = shouldMarkPlayed
Library.global.save(episode: episode)
}
Library.global.batchUpdateEpisodes(played: shouldMarkPlayed, episodes: episodes)
}

@IBAction func toggleFavourite(_ sender: Any) {
Expand All @@ -462,10 +459,7 @@ final class EpisodeViewController: NSViewController, NSTableViewDelegate, NSTabl

let shouldMarkAsFavourite = !allMarkedAsFavourite

for episode in activeEpisodesForAction() {
episode.favourite = shouldMarkAsFavourite
Library.global.save(episode: episode)
}
Library.global.batchUpdateEpisodes(favourite: shouldMarkAsFavourite, episodes: episodes)
}

@IBAction func downloadEpisode(_ sender: Any) {
Expand Down
28 changes: 4 additions & 24 deletions Doughnut/View Controllers/PodcastViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -333,34 +333,14 @@ final class PodcastViewController: NSViewController, NSTableViewDelegate, NSTabl

@IBAction func markAllAsPlayed(_ sender: Any) {
let podcasts = activePodcastsForAction()

for podcast in podcasts {
for episode in podcast.episodes {
episode.played = true
}

// Manually trigger a view reload to make update seem instant
viewController.libraryUpdatedPodcasts(podcasts: [podcast])

// Commit changes to library
Library.global.update(podcast: podcast)
}
let episodes = podcasts.flatMap { $0.episodes }
Library.global.batchUpdateEpisodes(played: true, episodes: episodes)
}

@IBAction func markAllAsUnplayed(_ sender: Any) {
let podcasts = activePodcastsForAction()

for podcast in podcasts {
for episode in podcast.episodes {
episode.played = false
}

// Manually trigger a view reload to make update seem instant
viewController.libraryUpdatedPodcasts(podcasts: [podcast])

// Commit changes to library
Library.global.update(podcast: podcast)
}
let episodes = podcasts.flatMap { $0.episodes }
Library.global.batchUpdateEpisodes(played: false, episodes: episodes)
}

@IBAction func copyPodcastURL(_ sender: Any) {
Expand Down

0 comments on commit 5010e39

Please sign in to comment.