Skip to content

Commit

Permalink
Merge pull request #57 from joelkanyi/enhancements
Browse files Browse the repository at this point in the history
Enhancements
  • Loading branch information
joelkanyi authored Feb 10, 2024
2 parents 7a1973d + 6f8889c commit 0bf0303
Show file tree
Hide file tree
Showing 22 changed files with 1,184 additions and 807 deletions.
15 changes: 14 additions & 1 deletion .idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,15 @@ class CastRepositoryImpl @Inject constructor(
Timber.d("Movie Casts $response")
return Resource.Success(response.toDomain())
}

override suspend fun getCastDetails(id: Int): Resource<Unit> {
val response = try {
api.getCreditDetails(creditId = id)
} catch (e: Exception) {
Timber.d(e.message)
return Resource.Error("Unknown error occurred")
}
Timber.d("Cast Details $response")
return Resource.Success(Unit)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ import com.kanyideveloper.muviz.common.util.Resource
interface CastRepository {
suspend fun getTvSeriesCasts(id: Int): Resource<Credits>
suspend fun getMovieCasts(id: Int): Resource<Credits>
suspend fun getCastDetails(id: Int): Resource<Unit>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2024 Joel Kanyi.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.kanyideveloper.muviz.cast.domain.usecase

import com.kanyideveloper.muviz.cast.domain.repository.CastRepository
import javax.inject.Inject

class GetCastDetailsUseCase @Inject constructor(
private val repository: CastRepository
) {
suspend operator fun invoke(id: Int) = repository.getCastDetails(id)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright 2024 Joel Kanyi.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.kanyideveloper.muviz.cast.presentation.castdetails

sealed interface CastDetailsEvents {
data object NavigateBack : CastDetailsEvents
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* Copyright 2024 Joel Kanyi.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.kanyideveloper.muviz.cast.presentation.castdetails

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.CircularProgressIndicator
import androidx.compose.material.Scaffold
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.hilt.navigation.compose.hiltViewModel
import com.kanyideveloper.muviz.R
import com.kanyideveloper.muviz.cast.domain.model.Cast
import com.kanyideveloper.muviz.common.presentation.components.StandardToolbar
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.navigation.DestinationsNavigator

@Destination
@Composable
fun CastDetailsScreen(
cast: Cast,
navigator: DestinationsNavigator,
viewModel: CastDetailsViewModel = hiltViewModel(),
) {
val castDetailsUiState by viewModel.castDetailsUiState.collectAsState()

LaunchedEffect(key1 = viewModel) {
viewModel.getCastDetails(cast.id)
}

CastDetailsScreenContent(
castName = cast.name,
state = castDetailsUiState,
onEvent = { event ->
when (event) {
is CastDetailsEvents.NavigateBack -> {
navigator.popBackStack()
}
}
}
)
}

@Composable
fun CastDetailsScreenContent(
castName: String,
state: CastDetailsUiState,
onEvent: (CastDetailsEvents) -> Unit,
) {
Scaffold(
topBar = {
StandardToolbar(
onBackArrowClicked = {
onEvent(CastDetailsEvents.NavigateBack)
},
title = {
Text(
text = castName,
color = Color.White,
fontWeight = FontWeight.SemiBold
)
},
showBackArrow = true
)
}
) { innerPadding ->
Box(
modifier = Modifier
.fillMaxSize()
.padding(innerPadding),
) {
if (state.isLoading) {
CircularProgressIndicator(
modifier = Modifier
.align(Alignment.Center)
)
}

if (state.isLoading.not() && state.error != null) {
Text(
text = state.error,
color = Color.Red,
modifier = Modifier
.align(Alignment.Center)
)
}

if (state.isLoading.not() && state.castDetails != null) {
// Show cast details
}
}
}
}

@Preview
@Composable
fun CastDetailsScreenPreview() {
CastDetailsScreenContent(
castName = "Tom Cruise",
state = CastDetailsUiState(),
onEvent = {}
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2024 Joel Kanyi.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.kanyideveloper.muviz.cast.presentation.castdetails

data class CastDetailsUiState(
val isLoading: Boolean = false,
val castDetails: Any? = null,
val error: String? = null
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2024 Joel Kanyi.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.kanyideveloper.muviz.cast.presentation.castdetails

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.kanyideveloper.muviz.cast.domain.usecase.GetCastDetailsUseCase
import com.kanyideveloper.muviz.common.util.Resource
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class CastDetailsViewModel @Inject constructor(
private val getCastDetailsUseCase: GetCastDetailsUseCase,
) : ViewModel() {
private val _castDetailsUiState = MutableStateFlow(CastDetailsUiState())
val castDetailsUiState = _castDetailsUiState.asStateFlow()
fun getCastDetails(id: Int) {
viewModelScope.launch {
_castDetailsUiState.update {
it.copy(
isLoading = true
)
}
when (val result = getCastDetailsUseCase(id)) {
is Resource.Error -> {
_castDetailsUiState.update {
it.copy(
isLoading = false,
error = result.message
)
}
}
is Resource.Success -> {
_castDetailsUiState.update {
it.copy(
isLoading = false,
castDetails = result.data
)
}
}
else -> {
castDetailsUiState
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.kanyideveloper.muviz.cast.presentation
package com.kanyideveloper.muviz.cast.presentation.casts

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
Expand Down Expand Up @@ -45,6 +46,7 @@ import com.kanyideveloper.muviz.common.presentation.components.StandardToolbar
import com.kanyideveloper.muviz.common.presentation.theme.MuvizTheme
import com.kanyideveloper.muviz.common.presentation.theme.lightGray
import com.kanyideveloper.muviz.common.util.Constants
import com.kanyideveloper.muviz.destinations.CastDetailsScreenDestination
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.navigation.DestinationsNavigator

Expand All @@ -61,6 +63,12 @@ fun CastsScreen(
is CastsUiEvents.NavigateBack -> {
navigator.popBackStack()
}

is CastsUiEvents.NavigateToCastDetails -> {
navigator.navigate(
CastDetailsScreenDestination(event.cast)
)
}
}
},
)
Expand Down Expand Up @@ -104,7 +112,10 @@ fun CastsScreenContent(
CastItem(
imageSize = 170.dp,
castImageUrl = "${Constants.IMAGE_BASE_UR}/${cast.profilePath}",
castName = cast.name
castName = cast.name,
onClick = {
onEvent(CastsUiEvents.NavigateToCastDetails(cast))
}
)
}
}
Expand All @@ -116,10 +127,13 @@ fun CastItem(
modifier: Modifier = Modifier,
imageSize: Dp,
castName: String,
castImageUrl: String
castImageUrl: String,
onClick: () -> Unit,
) {
Column(
modifier = modifier,
modifier = modifier.clickable {
onClick()
},
verticalArrangement = Arrangement.spacedBy(4.dp),
horizontalAlignment = Alignment.CenterHorizontally
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.kanyideveloper.muviz.cast.presentation
package com.kanyideveloper.muviz.cast.presentation.casts

import com.kanyideveloper.muviz.cast.domain.model.Cast

sealed interface CastsUiEvents {
data class NavigateToCastDetails(val cast: Cast) :
CastsUiEvents

data object NavigateBack : CastsUiEvents
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ interface TMDBApi {
@Query("language") language: String = "en"
): CreditsResponse

@GET("credit/{credit_id}")
suspend fun getCreditDetails(
@Path("credit_id") creditId: Int,
@Query("api_key") apiKey: String = API_KEY,
)

@GET("genre/movie/list")
suspend fun getMovieGenres(
@Query("api_key") apiKey: String = API_KEY,
Expand Down
Loading

0 comments on commit 0bf0303

Please sign in to comment.