move both artist and album refreshing from fragment to activity

This commit is contained in:
tibbi 2022-03-14 21:33:23 +01:00
parent f7be9c3b57
commit c1f4b739f0
2 changed files with 50 additions and 56 deletions

View file

@ -71,13 +71,6 @@ fun Context.broadcastUpdateWidgetTrackState(isPlaying: Boolean) {
}
}
fun Context.getArtists(callback: (artists: ArrayList<Artist>) -> Unit) {
ensureBackgroundThread {
val artists = getArtistsSync()
callback(artists)
}
}
fun Context.getArtistsSync(): ArrayList<Artist> {
val artists = ArrayList<Artist>()
val uri = Audio.Artists.EXTERNAL_CONTENT_URI
@ -293,25 +286,58 @@ fun Context.getAllInitialTracks(): ArrayList<Track> {
// store new artists, albums and tracks into our local db, delete invalid items
fun Context.updateAllDatabases() {
ensureBackgroundThread {
val artists = getArtistsSync()
artists.forEach { artist ->
artistDAO.insert(artist)
updateCachedArtists { artists ->
updateCachedAlbums(artists)
}
}
}
// remove deleted artists from cache
val cachedArtists = artistDAO.getAll() as ArrayList<Artist>
val newIds = artists.map { it.id }
val idsToRemove = arrayListOf<Long>()
cachedArtists.forEach { artist ->
if (!newIds.contains(artist.id)) {
idsToRemove.add(artist.id)
}
}
fun Context.updateCachedArtists(callback: (artists: ArrayList<Artist>) -> Unit) {
val artists = getArtistsSync()
artists.forEach { artist ->
artistDAO.insert(artist)
}
idsToRemove.forEach {
artistDAO.deleteArtist(it)
// remove deleted artists from cache
val cachedArtists = artistDAO.getAll() as ArrayList<Artist>
val newIds = artists.map { it.id }
val idsToRemove = arrayListOf<Long>()
cachedArtists.forEach { artist ->
if (!newIds.contains(artist.id)) {
idsToRemove.add(artist.id)
}
}
idsToRemove.forEach {
artistDAO.deleteArtist(it)
}
callback(artists)
}
fun Context.updateCachedAlbums(artists: ArrayList<Artist>) {
val albums = ArrayList<Album>()
artists.forEach { artist ->
albums.addAll(getAlbumsSync(artist))
}
albums.forEach {
albumsDAO.insert(it)
}
// remove deleted artists from cache
val cachedAlbums = albumsDAO.getAll() as ArrayList<Album>
val newIds = albums.map { it.id }
val idsToRemove = arrayListOf<Long>()
cachedAlbums.forEach { album ->
if (!newIds.contains(album.id)) {
idsToRemove.add(album.id)
}
}
idsToRemove.forEach {
albumsDAO.deleteAlbum(it)
}
}
fun Context.getMediaStoreIdFromPath(path: String): Long {

View file

@ -27,28 +27,17 @@ class AlbumsFragment(context: Context, attributeSet: AttributeSet) : MyViewPager
ensureBackgroundThread {
val cachedAlbums = activity.albumsDAO.getAll() as ArrayList<Album>
activity.runOnUiThread {
gotAlbums(activity, cachedAlbums, true)
ensureBackgroundThread {
val albums = ArrayList<Album>()
val artists = context.getArtistsSync()
artists.forEach { artist ->
albums.addAll(context.getAlbumsSync(artist))
}
gotAlbums(activity, albums, false)
}
gotAlbums(activity, cachedAlbums)
}
}
}
private fun gotAlbums(activity: SimpleActivity, albums: ArrayList<Album>, isFromCache: Boolean) {
private fun gotAlbums(activity: SimpleActivity, albums: ArrayList<Album>) {
albums.sort()
activity.runOnUiThread {
albums_placeholder.text = context.getString(R.string.no_items_found)
albums_placeholder.beVisibleIf(albums.isEmpty() && !isFromCache)
albums_placeholder.beVisibleIf(albums.isEmpty())
val adapter = albums_list.adapter
if (adapter == null) {
@ -69,27 +58,6 @@ class AlbumsFragment(context: Context, attributeSet: AttributeSet) : MyViewPager
val oldItems = (adapter as AlbumsAdapter).albums
if (oldItems.sortedBy { it.id }.hashCode() != albums.sortedBy { it.id }.hashCode()) {
adapter.updateItems(albums)
ensureBackgroundThread {
albums.forEach {
context.albumsDAO.insert(it)
}
// remove deleted albums from cache
if (!isFromCache) {
val newIds = albums.map { it.id }
val idsToRemove = arrayListOf<Long>()
oldItems.forEach { album ->
if (!newIds.contains(album.id)) {
idsToRemove.add(album.id)
}
}
idsToRemove.forEach {
activity.albumsDAO.deleteAlbum(it)
}
}
}
}
}
}