Skip to content

Commit

Permalink
- Code optimization and simplify view model
Browse files Browse the repository at this point in the history
  • Loading branch information
piashcse committed Jul 28, 2024
1 parent 473d8ec commit bd198f7
Show file tree
Hide file tree
Showing 10 changed files with 264 additions and 212 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ interface ApiService {
@GET("movie/{movieId}/recommendations")
suspend fun recommendedMovie(
@Path("movieId") movieId: Int,
@Query("page") one: Int,
@Query("api_key") api_key: String = ApiURL.API_KEY
): BaseModel

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ class MovieRepository @Inject constructor(
}
}

override suspend fun recommendedMovie(movieId: Int, page: Int): Flow<DataState<BaseModel>> =
override suspend fun recommendedMovie(movieId: Int): Flow<DataState<List<MovieItem>>> =
flow {
emit(DataState.Loading)
try {
val searchResult = apiService.recommendedMovie(movieId, page)
emit(DataState.Success(searchResult))
val searchResult = apiService.recommendedMovie(movieId)
emit(DataState.Success(searchResult.results))

} catch (e: Exception) {
emit(DataState.Error(e))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import kotlinx.coroutines.flow.Flow

interface MovieRepositoryInterface {
suspend fun movieDetail(movieId: Int): Flow<DataState<MovieDetail>>
suspend fun recommendedMovie(movieId: Int, page: Int): Flow<DataState<BaseModel>>
suspend fun recommendedMovie(movieId: Int): Flow<DataState<List<MovieItem>>>
suspend fun search(searchKey: String): Flow<DataState<BaseModel>>
suspend fun genreList(): Flow<DataState<Genres>>
suspend fun movieCredit(movieId: Int): Flow<DataState<Artist>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
Expand All @@ -19,13 +19,10 @@ import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import com.piashcse.hilt_mvvm_compose_movie.R
import com.piashcse.hilt_mvvm_compose_movie.data.datasource.remote.ApiURL
import com.piashcse.hilt_mvvm_compose_movie.data.model.artist.ArtistDetail
import com.piashcse.hilt_mvvm_compose_movie.ui.component.CircularIndeterminateProgressBar
import com.piashcse.hilt_mvvm_compose_movie.ui.component.text.BioGraphyText
import com.piashcse.hilt_mvvm_compose_movie.ui.theme.*
import com.piashcse.hilt_mvvm_compose_movie.utils.genderInString
import com.piashcse.hilt_mvvm_compose_movie.utils.network.DataState
import com.piashcse.hilt_mvvm_compose_movie.utils.pagingLoadingState
import com.skydoves.landscapist.ImageOptions
import com.skydoves.landscapist.animation.circular.CircularRevealPlugin
import com.skydoves.landscapist.coil.CoilImage
Expand All @@ -35,11 +32,12 @@ import com.skydoves.landscapist.placeholder.shimmer.ShimmerPlugin

@Composable
fun ArtistDetail(personId: Int) {
val artistDetailViewModel = hiltViewModel<ArtistDetailViewModel>()
val artistDetail = artistDetailViewModel.artistDetail
val progressBar = remember { mutableStateOf(false) }
LaunchedEffect(true) {
artistDetailViewModel.artistDetail(personId)
val viewModel = hiltViewModel<ArtistDetailViewModel>()
val artistDetail by viewModel.artistDetail.collectAsState()
val isLoading by viewModel.isLoading.collectAsState()

LaunchedEffect(Unit) {
viewModel.artistDetail(personId)
}
Column(
modifier = Modifier
Expand All @@ -50,77 +48,72 @@ fun ArtistDetail(personId: Int) {
)
.padding(start = 8.dp, top = 8.dp, end = 8.dp)
) {
CircularIndeterminateProgressBar(isDisplayed = progressBar.value, 0.4f)

artistDetail.value.let {
if (it is DataState.Success<ArtistDetail>) {
Row {
CoilImage(
modifier = Modifier
.padding(bottom = 8.dp)
.height(250.dp)
.width(190.dp)
.cornerRadius(10),
imageModel = { ApiURL.IMAGE_URL.plus(it.data.profilePath) },
imageOptions = ImageOptions(
contentScale = ContentScale.Crop,
alignment = Alignment.Center,
contentDescription = "artist image",
colorFilter = null,
),
component = rememberImageComponent {
+CircularRevealPlugin(
duration = 800
)
+ShimmerPlugin(shimmer = Shimmer.Flash(
CircularIndeterminateProgressBar(isDisplayed = isLoading, 0.4f)
artistDetail?.let {
Row {
CoilImage(
modifier = Modifier
.padding(bottom = 8.dp)
.height(250.dp)
.width(190.dp)
.cornerRadius(10),
imageModel = { ApiURL.IMAGE_URL.plus(it) },
imageOptions = ImageOptions(
contentScale = ContentScale.Crop,
alignment = Alignment.Center,
contentDescription = "artist image",
colorFilter = null,
),
component = rememberImageComponent {
+CircularRevealPlugin(
duration = 800
)
+ShimmerPlugin(
shimmer = Shimmer.Flash(
baseColor = SecondaryFontColor,
highlightColor = DefaultBackgroundColor
))
},
)
)
},
)
Column {
Text(
modifier = Modifier.padding(start = 8.dp),
text = it.name,
color = FontColor,
fontSize = 26.sp,
fontWeight = FontWeight.Medium
)
Column {
Text(
modifier = Modifier.padding(start = 8.dp),
text = it.data.name,
color = FontColor,
fontSize = 26.sp,
fontWeight = FontWeight.Medium
PersonalInfo(stringResource(R.string.know_for), it.knownForDepartment)
PersonalInfo(
stringResource(R.string.gender), it.gender.genderInString()
)
it.birthday?.let { birthday ->
PersonalInfo(
stringResource(R.string.birth_day),
birthday
)
PersonalInfo(stringResource(R.string.know_for), it.data.knownForDepartment)
}
it.placeOfBirth?.let { birthPlace ->
PersonalInfo(
stringResource(R.string.gender), it.data.gender.genderInString()
stringResource(R.string.place_of_birth),
birthPlace
)
it.data.birthday?.let { birthday ->
PersonalInfo(
stringResource(R.string.birth_day),
birthday
)
}
it.data.placeOfBirth?.let { birthPlace ->
PersonalInfo(
stringResource(R.string.place_of_birth),
birthPlace
)
}
}
}
Text(
modifier = Modifier.padding(bottom = 8.dp),
text = stringResource(R.string.biography),
color = SecondaryFontColor,
fontSize = 22.sp,
fontWeight = FontWeight.Medium
)
BioGraphyText(
text = it.data.biography
)
}
Text(
modifier = Modifier.padding(bottom = 8.dp),
text = stringResource(R.string.biography),
color = SecondaryFontColor,
fontSize = 22.sp,
fontWeight = FontWeight.Medium
)
BioGraphyText(
text = it.biography
)
}
}

artistDetail.pagingLoadingState {
progressBar.value = it
}
}

@Composable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,40 @@ import com.piashcse.hilt_mvvm_compose_movie.data.model.artist.ArtistDetail
import com.piashcse.hilt_mvvm_compose_movie.data.repository.MovieRepository
import com.piashcse.hilt_mvvm_compose_movie.utils.network.DataState
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class ArtistDetailViewModel @Inject constructor(private val repo: MovieRepository) : ViewModel() {
val artistDetail: MutableState<DataState<ArtistDetail>?> = mutableStateOf(null)
private val _artistDetail = MutableStateFlow<ArtistDetail?>(null)
val artistDetail: StateFlow<ArtistDetail?> get ()= _artistDetail.asStateFlow()

private val _isLoading = MutableStateFlow<Boolean>(false)
val isLoading get() = _isLoading.asStateFlow()


fun artistDetail(personId: Int) {
viewModelScope.launch {
repo.artistDetail(personId).onEach {
artistDetail.value = it
when (it) {
is DataState.Loading -> {
_isLoading.value = true
}

is DataState.Success -> {
_artistDetail.value = it.data
_isLoading.value = false
}

is DataState.Error -> {
_isLoading.value = false
}
}
}.launchIn(viewModelScope)
}
}
Expand Down
Loading

0 comments on commit bd198f7

Please sign in to comment.