Skip to content

Commit

Permalink
use flows in place of livedata
Browse files Browse the repository at this point in the history
  • Loading branch information
joelkanyi committed Jun 17, 2024
1 parent bb58c5b commit 6c1c9ec
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 17 deletions.
3 changes: 0 additions & 3 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,6 @@ dependencies {
// Swipe to refresh
implementation(libs.accompanist.swiperefresh)

// Livedata
implementation(libs.androidx.runtime.livedata)

// DataStore
implementation(libs.datastore.preferences)
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package com.kanyideveloper.muviz.favorites.data.data.local

import androidx.lifecycle.LiveData
import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
Expand All @@ -32,10 +31,10 @@ interface FavoritesDao {
fun getAllFavorites(): Flow<List<Favorite>>

@Query("SELECT * FROM favorites_table WHERE mediaId == :mediaId")
fun getAFavorites(mediaId: Int): LiveData<Favorite?>
fun getAFavorites(mediaId: Int): Flow<Favorite?>

@Query("SELECT favorite FROM favorites_table WHERE mediaId = :mediaId")
fun isFavorite(mediaId: Int): LiveData<Boolean>
fun isFavorite(mediaId: Int): Flow<Boolean>

@Delete
suspend fun deleteAFavorite(favorite: Favorite)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package com.kanyideveloper.muviz.favorites.data.data.repository

import androidx.lifecycle.LiveData
import com.kanyideveloper.muviz.favorites.data.data.local.Favorite
import com.kanyideveloper.muviz.favorites.data.data.local.FavoritesDatabase
import com.kanyideveloper.muviz.favorites.domain.repository.FavoritesRepository
Expand All @@ -32,11 +31,11 @@ class FavoritesRepositoryImpl @Inject constructor(private val database: Favorite
return database.dao.getAllFavorites()
}

override fun isFavorite(mediaId: Int): LiveData<Boolean>{
override fun isFavorite(mediaId: Int): Flow<Boolean>{
return database.dao.isFavorite(mediaId)
}

override fun getAFavorites(mediaId: Int): LiveData<Favorite?> {
override fun getAFavorites(mediaId: Int): Flow<Favorite?> {
return database.dao.getAFavorites(mediaId)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@
*/
package com.kanyideveloper.muviz.favorites.domain.repository

import androidx.lifecycle.LiveData
import com.kanyideveloper.muviz.favorites.data.data.local.Favorite
import kotlinx.coroutines.flow.Flow

interface FavoritesRepository {
suspend fun insertFavorite(favorite: Favorite)
fun getFavorites(): Flow<List<Favorite>>
fun isFavorite(mediaId: Int): LiveData<Boolean>
fun getAFavorites(mediaId: Int): LiveData<Favorite?>
fun isFavorite(mediaId: Int): Flow<Boolean>
fun getAFavorites(mediaId: Int): Flow<Favorite?>
suspend fun deleteOneFavorite(favorite: Favorite)
suspend fun deleteAllFavorites()
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
Expand Down Expand Up @@ -68,7 +67,7 @@ fun SharedTransitionScope.FilmDetailsScreen(
filmType = film.type
)
}
val isFilmFavorite = viewModel.isAFavorite(film.id).observeAsState().value != null
val isFilmFavorite = viewModel.isAFavorite(film.id).collectAsState(initial = false).value
val filmDetailsUiState by viewModel.filmDetailsUiState.collectAsState()

FilmDetailsScreenContent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import com.kanyideveloper.muviz.common.util.Resource
import com.kanyideveloper.muviz.favorites.data.data.local.Favorite
import com.kanyideveloper.muviz.favorites.domain.repository.FavoritesRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
Expand Down Expand Up @@ -179,7 +180,7 @@ class FilmDetailsViewModel @Inject constructor(
}
}

fun isAFavorite(mediaId: Int): LiveData<Boolean> {
fun isAFavorite(mediaId: Int): Flow<Boolean> {
return favoritesRepository.isFavorite(mediaId)
}

Expand Down
2 changes: 0 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ okhttp = "5.0.0-alpha.14"
pagingCompose = "3.3.0"
retrofit = "2.11.0"
roomRuntime = "2.6.1"
runtimeLivedata = "1.6.7"
spotless = "6.25.0"
google-services = "4.4.2"
accomapnistInsets = "0.30.1"
Expand Down Expand Up @@ -54,7 +53,6 @@ androidx-paging-compose = { module = "androidx.paging:paging-compose", version.r
androidx-room-compiler = { module = "androidx.room:room-compiler", version.ref = "roomRuntime" }
androidx-room-ktx = { module = "androidx.room:room-ktx", version.ref = "roomRuntime" }
androidx-room-runtime = { module = "androidx.room:room-runtime", version.ref = "roomRuntime" }
androidx-runtime-livedata = { module = "androidx.compose.runtime:runtime-livedata", version.ref = "runtimeLivedata" }
androidx-ui = { module = "androidx.compose.ui:ui", version.ref = "compose" }
androidx-ui-test-junit4 = { module = "androidx.compose.ui:ui-test-junit4", version.ref = "compose" }
androidx-ui-tooling = { module = "androidx.compose.ui:ui-tooling", version.ref = "compose" }
Expand Down

0 comments on commit 6c1c9ec

Please sign in to comment.