Skip to content

Commit

Permalink
Merge pull request #111 from gabrielbmoro/bugfix/some-impro
Browse files Browse the repository at this point in the history
Some impro
  • Loading branch information
gabrielbmoro committed Feb 27, 2024
2 parents 8901509 + 4c722df commit 2f2b7bb
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 60 deletions.
66 changes: 31 additions & 35 deletions src/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,6 @@ plugins {
alias(libs.plugins.ksp)
}

koverReport {
filters {
excludes {
packages(
"*.di",
"*.datasources",
"*.providers",
"dagger.hilt.internal.aggregatedroot.codegen",
"hilt_aggregated_deps",
"*.dto",
"*.core.ui.*",
"*.widgets",
"*.navigation"
)

classes(
"*.BuildConfig",
"*.ComposableSingletons",
"*.Hilt_MainActivity",
"*_Factory*",
"*Activity",
"*ScreenKt*",
"*_HiltModules*"
)
annotatedBy("Generated")
}
}
}

dependencies {
implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))

Expand Down Expand Up @@ -70,12 +41,7 @@ dependencies {
implementation(libs.navigation.compose)

// Kover
kover(projects.core)
kover(projects.data)
kover(projects.featureDetails)
kover(projects.featureMovies)
kover(projects.featureSearch)
kover(projects.featureWishlist)
rootProject.subprojects.forEach { kover(it) }

implementation(libs.core.ktx)
implementation(libs.appcompat)
Expand All @@ -97,8 +63,38 @@ dependencies {
androidTestImplementation(libs.bundles.test)
androidTestImplementation(libs.ui.compose.test)
}

android {
buildFeatures {
buildConfig = true
}
}

koverReport {
filters {
excludes {
packages(
"*.di",
"*.datasources",
"*.providers",
"dagger.hilt.internal.aggregatedroot.codegen",
"hilt_aggregated_deps",
"*.dto",
"*.core.ui.*",
"*.widgets",
"*.navigation"
)

classes(
"*.BuildConfig",
"*.ComposableSingletons",
"*.Hilt_MainActivity",
"*_Factory*",
"*Activity",
"*ScreenKt*",
"*_HiltModules*"
)
annotatedBy("Generated")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,17 @@ package com.gabrielbmoro.moviedb.core.ui.mvi

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch

abstract class ViewModelMVI<in UserIntent : Any, ScreenState : Any, Label : Any> : ViewModel() {
abstract class ViewModelMVI<in UserIntent : Any, ScreenState : Any> : ViewModel() {

private val _uiState = MutableStateFlow(this.defaultEmptyState())
val uiState = _uiState.stateIn(viewModelScope, SharingStarted.Eagerly, _uiState.value)

private val _label = MutableSharedFlow<Label?>(0)
val label: SharedFlow<Label?> = _label

protected abstract suspend fun execute(intent: UserIntent): ScreenState

protected abstract fun defaultEmptyState(): ScreenState
Expand All @@ -29,10 +24,6 @@ abstract class ViewModelMVI<in UserIntent : Any, ScreenState : Any, Label : Any>
}
}

suspend fun publish(label: Label) {
_label.emit(label)
}

protected fun updateState(state: ScreenState) {
_uiState.update { state }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class DetailsScreenViewModel @Inject constructor(
private val favoriteMovieUseCase: FavoriteMovieUseCase,
private val isFavoriteMovieUseCase: IsFavoriteMovieUseCase,
private val getMovieDetailsUseCase: GetMovieDetailsUseCase
) : ViewModelMVI<DetailsUserIntent, DetailsUIState, Any>() {
) : ViewModelMVI<DetailsUserIntent, DetailsUIState>() {

private lateinit var movie: Movie

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class MoviesViewModel @Inject constructor(
private val getPopularMoviesUseCase: GetPopularMoviesUseCase,
private val getTopRatedMoviesUseCase: GetTopRatedMoviesUseCase,
private val getNowPlayingMoviesUseCase: GetNowPlayingMoviesUseCase
) : ViewModelMVI<Any, MoviesUIState, Any>() {
) : ViewModelMVI<Any, MoviesUIState>() {

init {
loadMovies()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import javax.inject.Inject
@HiltViewModel
class SearchViewModel @Inject constructor(
private val searchMovieUseCase: SearchMovieUseCase
) : ViewModelMVI<SearchUserIntent, SearchUIState, Any>() {
) : ViewModelMVI<SearchUserIntent, SearchUIState>() {

override fun defaultEmptyState() = SearchUIState(TextFieldValue(""))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ sealed class WishlistUserIntent {
data class WishlistUIState(
val favoriteMovies: List<Movie>? = null,
val isLoading: Boolean = false,
val areBarsVisible: Boolean = true
val areBarsVisible: Boolean = true,
val resultMessage: String? = null,
)
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,11 @@ fun WishlistScreen(
}

LaunchedEffect(
key1 = Unit,
key1 = uiState.value,
block = {
viewModel.label.collect { userMessage ->
userMessage?.let {
snackbarHostState.showSnackbar(message = it)
}
uiState.value.resultMessage?.let { resultMessage ->
snackbarHostState.showSnackbar(resultMessage)
viewModel.onResultMessageReset()
}
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class WishlistViewModel @Inject constructor(
private val getFavoriteMoviesUseCase: GetFavoriteMoviesUseCase,
private val deleteMovieUseCase: DeleteMovieUseCase,
private val resourcesProvider: ResourcesProvider
) : ViewModelMVI<WishlistUserIntent, WishlistUIState, String>() {
) : ViewModelMVI<WishlistUserIntent, WishlistUIState>() {
fun load() = viewModelScope.launch {
getFavoriteMoviesUseCase().collect { movies ->
updateState(
Expand All @@ -32,17 +32,22 @@ class WishlistViewModel @Inject constructor(
is WishlistUserIntent.DeleteMovie -> {
val result = deleteMovieUseCase(intent.movie.title)
if (result == true) {
publish(
resourcesProvider.getString(R.string.delete_success_message)
uiState.value.copy(
favoriteMovies = getFavoriteMoviesUseCase().first(),
resultMessage = resourcesProvider.getString(
R.string.delete_success_message
)
)

uiState.value.copy(favoriteMovies = getFavoriteMoviesUseCase().first())
} else {
uiState.value
}
}
}
}

fun onResultMessageReset() {
updateState(uiState.value.copy(resultMessage = null))
}

override fun defaultEmptyState(): WishlistUIState = WishlistUIState()
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ fun MovieList(
contentPadding = PaddingValues(vertical = 8.dp)
) {
items(
count = moviesList.size
count = moviesList.size,
key = { index ->
moviesList[index].id
}
) { index ->

moviesList[index].let { movie ->
Expand Down

0 comments on commit 2f2b7bb

Please sign in to comment.