diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index bc1626ae..1d2efe6e 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -15,7 +15,7 @@ androidSupport = "1.5.0" materialDesign = "1.6.1" constraintLayout = "2.1.4" multidex = "2.0.1" -room = "2.4.3" +room = "2.5.0" kotlin = "1.7.22" kotlinxCoroutines = "1.6.4-native-mt" kotlinxGuava = "1.6.4" diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/data/AlbumDao.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/data/AlbumDao.kt index 0b8e2b7b..7c01e812 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/data/AlbumDao.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/data/AlbumDao.kt @@ -2,7 +2,6 @@ package org.moire.ultrasonic.data import androidx.room.Dao import androidx.room.Query -import androidx.room.Transaction import org.moire.ultrasonic.domain.Album @Dao @@ -66,39 +65,4 @@ interface AlbumDao : GenericDao { */ @Query("DELETE FROM albums WHERE id LIKE :id") fun delete(id: String) - - /** - * TODO: Make generic - * Upserts (insert or update) an object to the database - * - * @param obj the object to upsert - */ - @Transaction - @JvmSuppressWildcards - fun upsert(obj: Album) { - val id = insertIgnoring(obj) - if (id == -1L) { - update(obj) - } - } - - /** - * Upserts (insert or update) a list of objects - * - * @param objList the object to be upserted - */ - @Transaction - @JvmSuppressWildcards - fun upsert(objList: List) { - val insertResult = insertIgnoring(objList) - val updateList: MutableList = ArrayList() - for (i in insertResult.indices) { - if (insertResult[i] == -1L) { - updateList.add(objList[i]) - } - } - if (updateList.isNotEmpty()) { - update(updateList) - } - } } diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/data/BasicDaos.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/data/BasicDaos.kt index 9078f911..3355efe0 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/data/BasicDaos.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/data/BasicDaos.kt @@ -5,8 +5,8 @@ import androidx.room.Delete import androidx.room.Insert import androidx.room.OnConflictStrategy import androidx.room.Query -import androidx.room.Transaction import androidx.room.Update +import androidx.room.Upsert import org.moire.ultrasonic.domain.Index import org.moire.ultrasonic.domain.MusicFolder @@ -51,41 +51,6 @@ interface IndexDao : GenericDao { */ @Query("SELECT * FROM indexes where musicFolderId LIKE :musicFolderId") fun get(musicFolderId: String): List - - /** - * TODO: Make generic - * Upserts (insert or update) an object to the database - * - * @param obj the object to upsert - */ - @Transaction - @JvmSuppressWildcards - fun upsert(obj: Index) { - val id = insertIgnoring(obj) - if (id == -1L) { - update(obj) - } - } - - /** - * Upserts (insert or update) a list of objects - * - * @param objList the object to be upserted - */ - @Transaction - @JvmSuppressWildcards - fun upsert(objList: List) { - val insertResult = insertIgnoring(objList) - val updateList: MutableList = ArrayList() - for (i in insertResult.indices) { - if (insertResult[i] == -1L) { - updateList.add(objList[i]) - } - } - if (updateList.isNotEmpty()) { - update(updateList) - } - } } interface GenericDao { @@ -154,4 +119,20 @@ interface GenericDao { @Delete @JvmSuppressWildcards fun delete(obj: T) + + /** + * Upserts (insert or update) an object to the database + * + * @param obj the object to upsert + */ + @Upsert + fun upsert(obj: T) + + /** + * Upserts (insert or update) a list of objects + * + * @param objList the object to be upserted + */ + @Upsert + fun upsert(objList: List) } diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/AlbumListFragment.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/AlbumListFragment.kt index 1cae1c5a..8d33929d 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/AlbumListFragment.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/fragment/AlbumListFragment.kt @@ -76,14 +76,13 @@ class AlbumListFragment( } private fun fetchAlbums(refresh: Boolean = navArgs.refresh, append: Boolean = navArgs.append) { - val refresh = navArgs.refresh || refresh listModel.viewModelScope.launch(handler) { refreshListView?.isRefreshing = true if (navArgs.byArtist) { listModel.getAlbumsOfArtist( - refresh = navArgs.refresh, + refresh = refresh, id = navArgs.id!!, name = navArgs.title ) @@ -185,6 +184,12 @@ class AlbumListFragment( override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) + // Setup refresh handler + refreshListView = view.findViewById(refreshListId) + refreshListView?.setOnRefreshListener { + fetchAlbums(refresh = true) + } + // In most cases this fragment will be hosted by a ViewPager2 in the MainFragment, // which provides its own FilterBar. // But when we are looking at the Albums of a specific Artist this Fragment is standalone,