-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
301 additions
and
15 deletions.
There are no files selected for viewing
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
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
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
8 changes: 8 additions & 0 deletions
8
app/src/main/java/com/victorhvs/rnm/data/repositories/EpisodesRepository.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,8 @@ | ||
package com.victorhvs.rnm.data.repositories | ||
|
||
import com.victorhvs.rnm.data.models.Episode | ||
|
||
interface EpisodesRepository { | ||
|
||
suspend fun getEpisodes(ids: List<Int>): List<Episode> | ||
} |
18 changes: 18 additions & 0 deletions
18
app/src/main/java/com/victorhvs/rnm/data/repositories/EpisodesRepositoryImpl.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,18 @@ | ||
package com.victorhvs.rnm.data.repositories | ||
|
||
import com.victorhvs.rnm.core.DispatcherProvider | ||
import com.victorhvs.rnm.data.datasources.remote.RNMService | ||
import com.victorhvs.rnm.data.models.Episode | ||
import kotlinx.coroutines.withContext | ||
import javax.inject.Inject | ||
|
||
class EpisodesRepositoryImpl @Inject constructor( | ||
private val rnmService: RNMService, | ||
private val dispatcher: DispatcherProvider | ||
) : EpisodesRepository { | ||
override suspend fun getEpisodes(ids: List<Int>): List<Episode> { | ||
return withContext(dispatcher.io()) { | ||
rnmService.getEpisodes(ids = ids) | ||
} | ||
} | ||
} |
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
148 changes: 148 additions & 0 deletions
148
app/src/main/java/com/victorhvs/rnm/presentation/screens/detail/DetailScreen.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,148 @@ | ||
package com.victorhvs.rnm.presentation.screens.detail | ||
|
||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.lazy.LazyColumn | ||
import androidx.compose.foundation.lazy.items | ||
import androidx.compose.material.icons.Icons | ||
import androidx.compose.material.icons.outlined.Badge | ||
import androidx.compose.material.icons.outlined.FilterFrames | ||
import androidx.compose.material.icons.outlined.TheaterComedy | ||
import androidx.compose.material3.Icon | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.SuggestionChip | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.collectAsState | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.hilt.navigation.compose.hiltViewModel | ||
import com.victorhvs.rnm.R | ||
import com.victorhvs.rnm.data.models.Character | ||
import com.victorhvs.rnm.data.models.Episode | ||
import com.victorhvs.rnm.presentation.components.EpisodeHorizontalCard | ||
import com.victorhvs.rnm.presentation.components.ProgressBar | ||
import com.victorhvs.rnm.presentation.components.RNMImage | ||
import com.victorhvs.rnm.presentation.components.SectionTitle | ||
import com.victorhvs.rnm.presentation.theme.spacing | ||
|
||
@Composable | ||
fun DetailScreen( | ||
viewModel: DetailViewModel = hiltViewModel() | ||
) { | ||
val uiState = viewModel.state.collectAsState() | ||
|
||
when (val state = uiState.value) { | ||
is DetailViewModel.UiState.Error -> state.e.printStackTrace() | ||
DetailViewModel.UiState.Loading -> ProgressBar() | ||
is DetailViewModel.UiState.Success -> DetailContent( | ||
character = state.charInfo, | ||
episodes = state.episodes | ||
) | ||
} | ||
} | ||
|
||
@Composable | ||
fun DetailContent( | ||
character: Character, | ||
episodes: List<Episode> | ||
) { | ||
LazyColumn( | ||
modifier = Modifier, | ||
verticalArrangement = Arrangement.spacedBy(MaterialTheme.spacing.small), | ||
) { | ||
item { | ||
CharInfoHeader( | ||
modifier = Modifier.padding(top = MaterialTheme.spacing.small), | ||
character = character | ||
) | ||
} | ||
|
||
if (episodes.isNotEmpty()) { | ||
item { | ||
SectionTitle( | ||
modifier = Modifier | ||
.padding(horizontal = MaterialTheme.spacing.medium) | ||
.fillMaxWidth(), | ||
title = R.string.episodes | ||
) | ||
} | ||
|
||
items(episodes) { episode -> | ||
EpisodeHorizontalCard( | ||
modifier = Modifier.padding(horizontal = MaterialTheme.spacing.medium), | ||
position = episode.id, | ||
name = episode.name, | ||
episode = episode.episode, | ||
airDate = episode.airDate | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
fun CharInfoHeader( | ||
modifier: Modifier = Modifier, | ||
character: Character | ||
) { | ||
Column( | ||
modifier = modifier.fillMaxWidth(), | ||
horizontalAlignment = Alignment.CenterHorizontally | ||
) { | ||
RNMImage( | ||
imageUrl = character.image, | ||
contentDescription = character.name | ||
) | ||
Text( | ||
text = character.name, | ||
style = MaterialTheme.typography.headlineMedium, | ||
color = MaterialTheme.colorScheme.onSurface | ||
) | ||
Row( | ||
horizontalArrangement = Arrangement.spacedBy(MaterialTheme.spacing.small), | ||
) { | ||
SuggestionChip( | ||
onClick = { }, | ||
label = { Text(text = character.status) }, | ||
icon = { | ||
Icon( | ||
imageVector = Icons.Outlined.Badge, | ||
contentDescription = character.status, | ||
) | ||
} | ||
) | ||
SuggestionChip( | ||
onClick = { }, | ||
label = { Text(text = character.species) }, | ||
icon = { | ||
Icon( | ||
imageVector = Icons.Outlined.FilterFrames, | ||
contentDescription = character.status, | ||
) | ||
} | ||
) | ||
SuggestionChip( | ||
onClick = { }, | ||
label = { | ||
Text( | ||
text = stringResource( | ||
id = R.string.n_episodes, | ||
character.episode.size | ||
) | ||
) | ||
}, | ||
icon = { | ||
Icon( | ||
imageVector = Icons.Outlined.TheaterComedy, | ||
contentDescription = character.status, | ||
) | ||
} | ||
) | ||
} | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
app/src/main/java/com/victorhvs/rnm/presentation/screens/detail/DetailViewModel.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,71 @@ | ||
package com.victorhvs.rnm.presentation.screens.detail | ||
|
||
import androidx.lifecycle.SavedStateHandle | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.victorhvs.rnm.data.models.Character | ||
import com.victorhvs.rnm.data.models.Episode | ||
import com.victorhvs.rnm.data.repositories.CharacterRepository | ||
import com.victorhvs.rnm.data.repositories.EpisodesRepository | ||
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.update | ||
import kotlinx.coroutines.launch | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class DetailViewModel @Inject constructor( | ||
private val charRepository: CharacterRepository, | ||
private val epiRepository: EpisodesRepository, | ||
savedStateHandle: SavedStateHandle | ||
) : ViewModel() { | ||
|
||
private val _state = MutableStateFlow<UiState>(UiState.Loading) | ||
val state: StateFlow<UiState> = | ||
_state.asStateFlow() | ||
|
||
init { | ||
viewModelScope.launch { | ||
runCatching { | ||
// val charId = savedStateHandle.get<Int>("1") | ||
val charId = 1 | ||
val charResult = charRepository.getCharacter(id = charId) | ||
.also { char -> | ||
_state.update { | ||
UiState.Success( | ||
charInfo = char | ||
) | ||
} | ||
} | ||
epiRepository.getEpisodes(ids = charResult.episode.map { | ||
it.substringAfterLast("/").toInt() | ||
}).also { episodes -> | ||
_state.update { | ||
UiState.Success( | ||
charInfo = charResult, | ||
episodes = episodes | ||
) | ||
} | ||
} | ||
}.onFailure { throwable -> | ||
_state.update { | ||
UiState.Error( | ||
e = throwable | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
sealed class UiState { | ||
data class Success( | ||
val charInfo: Character, | ||
val episodes: List<Episode> = emptyList() | ||
) : UiState() | ||
|
||
data object Loading : UiState() | ||
data class Error(val e: Throwable) : UiState() | ||
} | ||
} |
Oops, something went wrong.