The problem i'm facing is whenever i add product in fav list or recent list all fragments observe this change because i'm using single table and cannot put product on top in recent list i want to achieve this 1.recent list where every product that is opened from all Products list should go in recent list top 2. Main list doesn't observe change if product is added in recent or fav list because i'm only changing product isrecent = true and is fav = true
here is my Product Dao
@Dao
interface ProductDao{
@Upsert
suspend fun insert(mProduct: MyProduct)
@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun insertAll(list: List<MyProduct>)
@Delete
suspend fun delete(mProduct: MyProduct)
@Update(onConflict = OnConflictStrategy.REPLACE)
suspend fun update(mProduct: MyProduct)
//Main List
@Query("SELECT * from product_table ")
fun getAllProducts(): LiveData<List<MyProduct>>
//Recent List
@Query("SELECT * from product_table Where is_recent = :isRecent")
fun getRecentProducts(isRecent:Boolean): LiveData<List<MyProduct>>
//Favorite List
@Query("SELECT * from product_table Where is_fav = :isFav")
fun getFavoriteProducts(isFav:Boolean): LiveData<List<MyProduct>>
@Query("DELETE FROM product_table")
suspend fun deleteAllProducts()
}
i didn't tried but found a solution by splitting table but thats not solution because db table schema is same so creating 3 table of same data is not good solution
LiveDatawill refresh whenever database table data changes.You can create many functions but all return
LiveDatawill react to change in same table. You are also right it does not make sense to create classes with same scheme but it is only right way to do. However we can improve it with inheritance.