Skip to content

Commit

Permalink
Update flow only really updated
Browse files Browse the repository at this point in the history
  • Loading branch information
Wanjuuuuu committed Nov 21, 2021
1 parent 4571c22 commit 73f2178
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions app/src/main/java/com/wanjuuuuu/androiddictionary/data/TermDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,41 @@ package com.wanjuuuuu.androiddictionary.data
import androidx.lifecycle.LiveData
import androidx.room.*
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.distinctUntilChanged

@Dao
interface TermDao {
abstract class TermDao {

@get:Query("select id, name, category, bookmarked from terms order by id")
val allTerms: Flow<List<TermListItem>>
get() = allTerms().distinctUntilChanged()

@get:Query("select id, name, category, bookmarked from terms where bookmarked = 1 order by id")
val bookmarkedTerms: Flow<List<TermListItem>>
get() = bookmarkedTerms().distinctUntilChanged()

@Query("select id, name, category, bookmarked from terms order by id")
protected abstract fun allTerms(): Flow<List<TermListItem>>

@Query("select id, name, category, bookmarked from terms where bookmarked = 1 order by id")
protected abstract fun bookmarkedTerms(): Flow<List<TermListItem>>

@Query("select * from terms where id = :id")
fun getTerm(id: Long): LiveData<Term>
abstract fun getTerm(id: Long): LiveData<Term>

@Query("select * from terms where id = :id")
suspend fun getNaiveTerm(id: Long): Term
abstract suspend fun getNaiveTerm(id: Long): Term

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertTerms(terms: List<Term>)
abstract suspend fun insertTerms(terms: List<Term>)

@Query("update terms set description = :description, scrapedTime = :scrapedTime where id = :id")
suspend fun updateTerm(id: Long, description: String, scrapedTime: Long)
abstract suspend fun updateTerm(id: Long, description: String, scrapedTime: Long)

@Query("update terms set bookmarked = :bookmarked where id = :id")
suspend fun updateTermBookmarked(id: Long, bookmarked: Boolean)
abstract suspend fun updateTermBookmarked(id: Long, bookmarked: Boolean)

@Delete
fun deleteTerm(term: Term)
abstract fun deleteTerm(term: Term)

@Delete
fun deleteTerms(term: Term)
abstract fun deleteTerms(term: Term)
}

0 comments on commit 73f2178

Please sign in to comment.