Update room

pull/300/head
Koitharu 3 years ago
parent 1a70ccff55
commit f57d23026b
No known key found for this signature in database
GPG Key ID: 8E861F8CE6E7CE27

@ -108,9 +108,9 @@ dependencies {
//noinspection LifecycleAnnotationProcessorWithJava8 //noinspection LifecycleAnnotationProcessorWithJava8
kapt 'androidx.lifecycle:lifecycle-compiler:2.5.1' kapt 'androidx.lifecycle:lifecycle-compiler:2.5.1'
implementation 'androidx.room:room-runtime:2.4.3' implementation 'androidx.room:room-runtime:2.5.0'
implementation 'androidx.room:room-ktx:2.4.3' implementation 'androidx.room:room-ktx:2.5.0'
kapt 'androidx.room:room-compiler:2.4.3' kapt 'androidx.room:room-compiler:2.5.0'
implementation 'com.squareup.okhttp3:okhttp:4.10.0' implementation 'com.squareup.okhttp3:okhttp:4.10.0'
implementation 'com.squareup.okhttp3:okhttp-dnsoverhttps:4.9.3' implementation 'com.squareup.okhttp3:okhttp-dnsoverhttps:4.9.3'
@ -145,7 +145,7 @@ dependencies {
androidTestImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.4' androidTestImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.4'
androidTestImplementation 'androidx.room:room-testing:2.4.3' androidTestImplementation 'androidx.room:room-testing:2.5.0'
androidTestImplementation 'com.squareup.moshi:moshi-kotlin:1.14.0' androidTestImplementation 'com.squareup.moshi:moshi-kotlin:1.14.0'
androidTestImplementation 'com.google.dagger:hilt-android-testing:2.44.2' androidTestImplementation 'com.google.dagger:hilt-android-testing:2.44.2'

@ -13,16 +13,6 @@ abstract class PreferencesDao {
@Query("SELECT * FROM preferences WHERE manga_id = :mangaId") @Query("SELECT * FROM preferences WHERE manga_id = :mangaId")
abstract fun observe(mangaId: Long): Flow<MangaPrefsEntity?> abstract fun observe(mangaId: Long): Flow<MangaPrefsEntity?>
@Insert(onConflict = OnConflictStrategy.IGNORE) @Upsert
abstract suspend fun insert(pref: MangaPrefsEntity): Long abstract suspend fun upsert(pref: MangaPrefsEntity)
@Update
abstract suspend fun update(pref: MangaPrefsEntity): Int
@Transaction
open suspend fun upsert(pref: MangaPrefsEntity) {
if (update(pref) == 0) {
insert(pref)
}
}
} }

@ -14,7 +14,7 @@ abstract class TagsDao {
LEFT JOIN manga_tags ON tags.tag_id = manga_tags.tag_id LEFT JOIN manga_tags ON tags.tag_id = manga_tags.tag_id
GROUP BY tags.title GROUP BY tags.title
ORDER BY COUNT(manga_id) DESC ORDER BY COUNT(manga_id) DESC
LIMIT :limit""" LIMIT :limit""",
) )
abstract suspend fun findPopularTags(limit: Int): List<TagEntity> abstract suspend fun findPopularTags(limit: Int): List<TagEntity>
@ -24,7 +24,7 @@ abstract class TagsDao {
WHERE tags.source = :source WHERE tags.source = :source
GROUP BY tags.title GROUP BY tags.title
ORDER BY COUNT(manga_id) DESC ORDER BY COUNT(manga_id) DESC
LIMIT :limit""" LIMIT :limit""",
) )
abstract suspend fun findPopularTags(source: String, limit: Int): List<TagEntity> abstract suspend fun findPopularTags(source: String, limit: Int): List<TagEntity>
@ -34,7 +34,7 @@ abstract class TagsDao {
WHERE tags.source = :source AND title LIKE :query WHERE tags.source = :source AND title LIKE :query
GROUP BY tags.title GROUP BY tags.title
ORDER BY COUNT(manga_id) DESC ORDER BY COUNT(manga_id) DESC
LIMIT :limit""" LIMIT :limit""",
) )
abstract suspend fun findTags(source: String, query: String, limit: Int): List<TagEntity> abstract suspend fun findTags(source: String, query: String, limit: Int): List<TagEntity>
@ -44,22 +44,10 @@ abstract class TagsDao {
WHERE title LIKE :query WHERE title LIKE :query
GROUP BY tags.title GROUP BY tags.title
ORDER BY COUNT(manga_id) DESC ORDER BY COUNT(manga_id) DESC
LIMIT :limit""" LIMIT :limit""",
) )
abstract suspend fun findTags(query: String, limit: Int): List<TagEntity> abstract suspend fun findTags(query: String, limit: Int): List<TagEntity>
@Insert(onConflict = OnConflictStrategy.IGNORE) @Upsert
abstract suspend fun insert(tag: TagEntity): Long abstract suspend fun upsert(tags: Iterable<TagEntity>)
}
@Update(onConflict = OnConflictStrategy.IGNORE)
abstract suspend fun update(tag: TagEntity): Int
@Transaction
open suspend fun upsert(tags: Iterable<TagEntity>) {
tags.forEach { tag ->
if (update(tag) <= 0) {
insert(tag)
}
}
}
}

@ -21,9 +21,6 @@ abstract class FavouriteCategoriesDao {
@Insert(onConflict = OnConflictStrategy.ABORT) @Insert(onConflict = OnConflictStrategy.ABORT)
abstract suspend fun insert(category: FavouriteCategoryEntity): Long abstract suspend fun insert(category: FavouriteCategoryEntity): Long
@Update
abstract suspend fun update(category: FavouriteCategoryEntity): Int
suspend fun delete(id: Long) = setDeletedAt(id, System.currentTimeMillis()) suspend fun delete(id: Long) = setDeletedAt(id, System.currentTimeMillis())
@Query("UPDATE favourite_categories SET title = :title, `order` = :order, `track` = :tracker WHERE category_id = :id") @Query("UPDATE favourite_categories SET title = :title, `order` = :order, `track` = :tracker WHERE category_id = :id")
@ -51,12 +48,8 @@ abstract class FavouriteCategoriesDao {
return (getMaxSortKey() ?: 0) + 1 return (getMaxSortKey() ?: 0) + 1
} }
@Transaction @Upsert
open suspend fun upsert(entity: FavouriteCategoryEntity) { abstract suspend fun upsert(entity: FavouriteCategoryEntity)
if (update(entity) == 0) {
insert(entity)
}
}
@Query("UPDATE favourite_categories SET deleted_at = :deletedAt WHERE category_id = :id") @Query("UPDATE favourite_categories SET deleted_at = :deletedAt WHERE category_id = :id")
protected abstract suspend fun setDeletedAt(id: Long, deletedAt: Long) protected abstract suspend fun setDeletedAt(id: Long, deletedAt: Long)

@ -99,11 +99,6 @@ abstract class FavouritesDao {
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
abstract suspend fun insert(favourite: FavouriteEntity) abstract suspend fun insert(favourite: FavouriteEntity)
/** UPDATE **/
@Update
abstract suspend fun update(favourite: FavouriteEntity): Int
/** DELETE **/ /** DELETE **/
suspend fun delete(mangaId: Long) = setDeletedAt( suspend fun delete(mangaId: Long) = setDeletedAt(
@ -138,12 +133,8 @@ abstract class FavouritesDao {
/** TOOLS **/ /** TOOLS **/
@Transaction @Upsert
open suspend fun upsert(entity: FavouriteEntity) { abstract suspend fun upsert(entity: FavouriteEntity)
if (update(entity) == 0) {
insert(entity)
}
}
@Transaction @Transaction
@RawQuery(observedEntities = [FavouriteEntity::class]) @RawQuery(observedEntities = [FavouriteEntity::class])
@ -166,6 +157,7 @@ abstract class FavouritesDao {
SortOrder.NEWEST, SortOrder.NEWEST,
SortOrder.UPDATED, SortOrder.UPDATED,
-> "created_at DESC" -> "created_at DESC"
SortOrder.ALPHABETICAL -> "title ASC" SortOrder.ALPHABETICAL -> "title ASC"
else -> throw IllegalArgumentException("Sort order $sortOrder is not supported") else -> throw IllegalArgumentException("Sort order $sortOrder is not supported")
} }

@ -4,11 +4,11 @@ import android.app.backup.BackupManager
import android.content.Context import android.content.Context
import androidx.room.InvalidationTracker import androidx.room.InvalidationTracker
import dagger.hilt.android.qualifiers.ApplicationContext import dagger.hilt.android.qualifiers.ApplicationContext
import javax.inject.Inject
import javax.inject.Singleton
import org.koitharu.kotatsu.core.db.TABLE_FAVOURITES import org.koitharu.kotatsu.core.db.TABLE_FAVOURITES
import org.koitharu.kotatsu.core.db.TABLE_FAVOURITE_CATEGORIES import org.koitharu.kotatsu.core.db.TABLE_FAVOURITE_CATEGORIES
import org.koitharu.kotatsu.core.db.TABLE_HISTORY import org.koitharu.kotatsu.core.db.TABLE_HISTORY
import javax.inject.Inject
import javax.inject.Singleton
@Singleton @Singleton
class BackupObserver @Inject constructor( class BackupObserver @Inject constructor(
@ -17,7 +17,7 @@ class BackupObserver @Inject constructor(
private val backupManager = BackupManager(context) private val backupManager = BackupManager(context)
override fun onInvalidated(tables: MutableSet<String>) { override fun onInvalidated(tables: Set<String>) {
backupManager.dataChanged() backupManager.dataChanged()
} }
} }

@ -3,7 +3,6 @@ package org.koitharu.kotatsu.settings.tracker
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import androidx.room.InvalidationTracker import androidx.room.InvalidationTracker
import dagger.hilt.android.lifecycle.HiltViewModel import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import okio.Closeable import okio.Closeable
import org.koitharu.kotatsu.base.ui.BaseViewModel import org.koitharu.kotatsu.base.ui.BaseViewModel
@ -11,6 +10,7 @@ import org.koitharu.kotatsu.core.db.MangaDatabase
import org.koitharu.kotatsu.core.db.TABLE_FAVOURITE_CATEGORIES import org.koitharu.kotatsu.core.db.TABLE_FAVOURITE_CATEGORIES
import org.koitharu.kotatsu.core.db.removeObserverAsync import org.koitharu.kotatsu.core.db.removeObserverAsync
import org.koitharu.kotatsu.tracker.domain.TrackingRepository import org.koitharu.kotatsu.tracker.domain.TrackingRepository
import javax.inject.Inject
@HiltViewModel @HiltViewModel
class TrackerSettingsViewModel @Inject constructor( class TrackerSettingsViewModel @Inject constructor(
@ -39,7 +39,7 @@ class TrackerSettingsViewModel @Inject constructor(
InvalidationTracker.Observer(arrayOf(TABLE_FAVOURITE_CATEGORIES)), InvalidationTracker.Observer(arrayOf(TABLE_FAVOURITE_CATEGORIES)),
Closeable { Closeable {
override fun onInvalidated(tables: MutableSet<String>) { override fun onInvalidated(tables: Set<String>) {
vm?.updateCategoriesCount() vm?.updateCategoriesCount()
} }

@ -44,7 +44,7 @@ class SyncController @Inject constructor(
private val defaultGcPeriod: Long // gc period if sync disabled private val defaultGcPeriod: Long // gc period if sync disabled
get() = TimeUnit.HOURS.toMillis(2) get() = TimeUnit.HOURS.toMillis(2)
override fun onInvalidated(tables: MutableSet<String>) { override fun onInvalidated(tables: Set<String>) {
requestSync( requestSync(
favourites = TABLE_FAVOURITES in tables || TABLE_FAVOURITE_CATEGORIES in tables, favourites = TABLE_FAVOURITES in tables || TABLE_FAVOURITE_CATEGORIES in tables,
history = TABLE_HISTORY in tables, history = TABLE_HISTORY in tables,

@ -11,11 +11,10 @@ import androidx.sqlite.db.SupportSQLiteDatabase
import androidx.sqlite.db.SupportSQLiteQueryBuilder import androidx.sqlite.db.SupportSQLiteQueryBuilder
import dagger.hilt.EntryPoint import dagger.hilt.EntryPoint
import dagger.hilt.InstallIn import dagger.hilt.InstallIn
import dagger.hilt.android.AndroidEntryPoint
import dagger.hilt.android.EntryPointAccessors import dagger.hilt.android.EntryPointAccessors
import dagger.hilt.components.SingletonComponent import dagger.hilt.components.SingletonComponent
import java.util.concurrent.Callable
import org.koitharu.kotatsu.core.db.* import org.koitharu.kotatsu.core.db.*
import java.util.concurrent.Callable
abstract class SyncProvider : ContentProvider() { abstract class SyncProvider : ContentProvider() {
@ -44,15 +43,14 @@ abstract class SyncProvider : ContentProvider() {
selection: String?, selection: String?,
selectionArgs: Array<out String>?, selectionArgs: Array<out String>?,
sortOrder: String?, sortOrder: String?,
): Cursor? = if (getTableName(uri) != null) { ): Cursor? {
val sqlQuery = SupportSQLiteQueryBuilder.builder(uri.lastPathSegment) val tableName = getTableName(uri) ?: return null
val sqlQuery = SupportSQLiteQueryBuilder.builder(tableName)
.columns(projection) .columns(projection)
.selection(selection, selectionArgs) .selection(selection, selectionArgs)
.orderBy(sortOrder) .orderBy(sortOrder)
.create() .create()
database.openHelper.readableDatabase.query(sqlQuery) return database.openHelper.readableDatabase.query(sqlQuery)
} else {
null
} }
override fun getType(uri: Uri): String? { override fun getType(uri: Uri): String? {

@ -1,12 +1,10 @@
package org.koitharu.kotatsu.tracker.data package org.koitharu.kotatsu.tracker.data
import androidx.room.Dao import androidx.room.Dao
import androidx.room.Insert
import androidx.room.MapInfo import androidx.room.MapInfo
import androidx.room.OnConflictStrategy
import androidx.room.Query import androidx.room.Query
import androidx.room.Transaction import androidx.room.Transaction
import androidx.room.Update import androidx.room.Upsert
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import org.koitharu.kotatsu.core.db.entity.MangaWithTags import org.koitharu.kotatsu.core.db.entity.MangaWithTags
@ -46,22 +44,12 @@ abstract class TracksDao {
@Query("UPDATE tracks SET chapters_new = 0") @Query("UPDATE tracks SET chapters_new = 0")
abstract suspend fun clearCounters() abstract suspend fun clearCounters()
@Insert(onConflict = OnConflictStrategy.IGNORE)
abstract suspend fun insert(entity: TrackEntity): Long
@Update
abstract suspend fun update(entity: TrackEntity): Int
@Query("DELETE FROM tracks WHERE manga_id = :mangaId") @Query("DELETE FROM tracks WHERE manga_id = :mangaId")
abstract suspend fun delete(mangaId: Long) abstract suspend fun delete(mangaId: Long)
@Query("DELETE FROM tracks WHERE manga_id NOT IN (SELECT manga_id FROM history UNION SELECT manga_id FROM favourites)") @Query("DELETE FROM tracks WHERE manga_id NOT IN (SELECT manga_id FROM history UNION SELECT manga_id FROM favourites)")
abstract suspend fun gc() abstract suspend fun gc()
@Transaction @Upsert
open suspend fun upsert(entity: TrackEntity) { abstract suspend fun upsert(entity: TrackEntity)
if (update(entity) == 0) {
insert(entity)
}
}
} }

@ -6,19 +6,19 @@ import android.content.Context
import android.content.Intent import android.content.Intent
import androidx.room.InvalidationTracker import androidx.room.InvalidationTracker
import dagger.hilt.android.qualifiers.ApplicationContext import dagger.hilt.android.qualifiers.ApplicationContext
import javax.inject.Inject
import javax.inject.Singleton
import org.koitharu.kotatsu.core.db.TABLE_FAVOURITES import org.koitharu.kotatsu.core.db.TABLE_FAVOURITES
import org.koitharu.kotatsu.core.db.TABLE_HISTORY import org.koitharu.kotatsu.core.db.TABLE_HISTORY
import org.koitharu.kotatsu.widget.recent.RecentWidgetProvider import org.koitharu.kotatsu.widget.recent.RecentWidgetProvider
import org.koitharu.kotatsu.widget.shelf.ShelfWidgetProvider import org.koitharu.kotatsu.widget.shelf.ShelfWidgetProvider
import javax.inject.Inject
import javax.inject.Singleton
@Singleton @Singleton
class WidgetUpdater @Inject constructor( class WidgetUpdater @Inject constructor(
@ApplicationContext private val context: Context, @ApplicationContext private val context: Context,
) : InvalidationTracker.Observer(TABLE_HISTORY, TABLE_FAVOURITES) { ) : InvalidationTracker.Observer(TABLE_HISTORY, TABLE_FAVOURITES) {
override fun onInvalidated(tables: MutableSet<String>) { override fun onInvalidated(tables: Set<String>) {
if (TABLE_HISTORY in tables) { if (TABLE_HISTORY in tables) {
updateWidgets(RecentWidgetProvider::class.java) updateWidgets(RecentWidgetProvider::class.java)
} }

Loading…
Cancel
Save