-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from joelkanyi/enhancements
Enhancements
- Loading branch information
Showing
22 changed files
with
1,184 additions
and
807 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
app/src/main/java/com/kanyideveloper/muviz/cast/domain/usecase/GetCastDetailsUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
20 changes: 20 additions & 0 deletions
20
...src/main/java/com/kanyideveloper/muviz/cast/presentation/castdetails/CastDetailsEvents.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
126 changes: 126 additions & 0 deletions
126
...src/main/java/com/kanyideveloper/muviz/cast/presentation/castdetails/CastDetailsScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = {} | ||
) | ||
} |
22 changes: 22 additions & 0 deletions
22
...rc/main/java/com/kanyideveloper/muviz/cast/presentation/castdetails/CastDetailsUiState.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
65 changes: 65 additions & 0 deletions
65
.../main/java/com/kanyideveloper/muviz/cast/presentation/castdetails/CastDetailsViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.