Change names of database methods
This commit is contained in:
parent
3af9a606ae
commit
93b4d04587
@ -14,9 +14,8 @@ interface FavouriteDao {
|
|||||||
|
|
||||||
@Delete
|
@Delete
|
||||||
suspend fun delete(favourite: Favourite)
|
suspend fun delete(favourite: Favourite)
|
||||||
|
@Query("SELECT * from favourites ORDER BY id ASC")
|
||||||
@Query("SELECT id FROM favourites")
|
fun getAllItems(): Flow<List<Favourite>>
|
||||||
fun getAllFavoriteIds(): Flow<List<Int>>
|
|
||||||
|
|
||||||
@Query("SELECT EXISTS(SELECT 1 FROM favourites WHERE id = :id LIMIT 1)")
|
@Query("SELECT EXISTS(SELECT 1 FROM favourites WHERE id = :id LIMIT 1)")
|
||||||
suspend fun isFavourite(id: Int): Boolean
|
suspend fun isFavourite(id: Int): Boolean
|
||||||
|
@ -9,7 +9,7 @@ interface FavouritesRepository {
|
|||||||
/**
|
/**
|
||||||
* Retrieve all the items from the the given data source.
|
* Retrieve all the items from the the given data source.
|
||||||
*/
|
*/
|
||||||
fun getAllFavoriteIds(): Flow<List<Int>>
|
fun getAllItems(): Flow<List<Favourite>>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Insert item in the data source
|
* Insert item in the data source
|
||||||
|
@ -3,8 +3,8 @@ package com.ti.mobpo.data
|
|||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
|
||||||
class OfflineFavouritesRepository(private val favouriteDao: FavouriteDao) : FavouritesRepository {
|
class OfflineFavouritesRepository(private val favouriteDao: FavouriteDao) : FavouritesRepository {
|
||||||
override fun getAllFavoriteIds(): Flow<List<Int>> {
|
override fun getAllItems(): Flow<List<Favourite>> {
|
||||||
return favouriteDao.getAllFavoriteIds()
|
return favouriteDao.getAllItems()
|
||||||
}
|
}
|
||||||
override suspend fun insertItem(item: Favourite) {
|
override suspend fun insertItem(item: Favourite) {
|
||||||
favouriteDao.insert(item)
|
favouriteDao.insert(item)
|
||||||
|
@ -1,10 +1,60 @@
|
|||||||
package com.ti.mobpo.ui.screens
|
package com.ti.mobpo.ui.screens
|
||||||
|
|
||||||
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
|
import androidx.compose.foundation.layout.Column
|
||||||
|
import androidx.compose.foundation.layout.Spacer
|
||||||
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
|
import androidx.compose.foundation.lazy.grid.GridCells
|
||||||
|
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
|
||||||
import androidx.compose.material3.Text
|
import androidx.compose.material3.Text
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.runtime.collectAsState
|
||||||
|
import androidx.compose.runtime.getValue
|
||||||
|
import androidx.compose.ui.Alignment
|
||||||
|
import androidx.compose.ui.unit.dp
|
||||||
|
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||||
|
import com.ti.mobpo.AppViewModelProvider
|
||||||
|
import com.ti.mobpo.ui.viewmodels.FavouritesViewModel
|
||||||
|
import androidx.compose.foundation.layout.height
|
||||||
|
import androidx.compose.foundation.layout.padding
|
||||||
|
import androidx.compose.foundation.lazy.grid.items
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun Favourites() {
|
fun FavoritesScreen(viewModel: FavouritesViewModel = viewModel(factory = AppViewModelProvider.Factory)) {
|
||||||
println("Favourites")
|
Favorites(viewModel)
|
||||||
Text("Favourites Page")
|
}
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun Favorites(favoritesViewModel: FavouritesViewModel) {
|
||||||
|
val favorites by favoritesViewModel.pokemonDetails.collectAsState()
|
||||||
|
favoritesViewModel.loadFavourites()
|
||||||
|
|
||||||
|
Column(
|
||||||
|
verticalArrangement = Arrangement.Top,
|
||||||
|
horizontalAlignment = Alignment.CenterHorizontally,
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxSize()
|
||||||
|
.padding(20.dp)
|
||||||
|
) {
|
||||||
|
Spacer(modifier = Modifier.height(16.dp))
|
||||||
|
|
||||||
|
favorites?.let { favoritesList ->
|
||||||
|
if (favoritesList.isNotEmpty()) {
|
||||||
|
LazyVerticalGrid(
|
||||||
|
columns = GridCells.Adaptive(150.dp),
|
||||||
|
verticalArrangement = Arrangement.spacedBy(8.dp),
|
||||||
|
horizontalArrangement = Arrangement.spacedBy(8.dp)
|
||||||
|
) {
|
||||||
|
items(items = favoritesList, key = { pokemon -> pokemon.id }) { pokemon ->
|
||||||
|
PokemonCard(pokemon, toggleFavorite = { pokemonId ->
|
||||||
|
favoritesViewModel.toggleFavorite(pokemonId)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Text("No favorites found")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user