Skip to content

Commit

Permalink
(improve): few improvements in readability (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorHVS authored Aug 4, 2023
1 parent a8b4078 commit 56b170e
Show file tree
Hide file tree
Showing 13 changed files with 23 additions and 41 deletions.
8 changes: 8 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ android {
kotlinOptions {
jvmTarget = "1.8"

freeCompilerArgs = freeCompilerArgs + listOf(
"-opt-in=kotlin.RequiresOptIn",
"-opt-in=androidx.compose.ui.ExperimentalComposeUiApi",
"-opt-in=androidx.compose.material3.ExperimentalMaterial3Api",
"-opt-in=androidx.compose.animation.ExperimentalAnimationApi",
"-opt-in=kotlinx.coroutines.ExperimentalCoroutinesApi",
)

if (project.findProperty("enableComposeCompilerReports") == "true") {
val outputDir = project.buildDir.path + "/compose-reports"
freeCompilerArgs = freeCompilerArgs + listOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ package com.victorhvs.rnm.presentation
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import com.victorhvs.rnm.presentation.navigation.SetupNavGraph
import com.victorhvs.rnm.presentation.theme.RicknmortyTheme
import dagger.hilt.android.AndroidEntryPoint

@OptIn(ExperimentalAnimationApi::class)
@AndroidEntryPoint
class MainActivity : ComponentActivity() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.semantics.semantics
Expand All @@ -18,13 +17,11 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.victorhvs.rnm.presentation.theme.RicknmortyTheme

@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun CharRounded(
text: String,
modifier: Modifier = Modifier
) {

Box(
modifier = modifier
.testTag("CharRounded")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.widthIn
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.stringResource
Expand All @@ -21,7 +19,6 @@ import androidx.compose.ui.unit.dp
import com.victorhvs.rnm.R
import com.victorhvs.rnm.presentation.theme.RicknmortyTheme

@OptIn(ExperimentalMaterial3Api::class, ExperimentalComposeUiApi::class)
@Composable
fun CharVerticalCard(
modifier: Modifier = Modifier,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ import androidx.compose.material3.ListItemDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.semantics.testTagsAsResourceId
import androidx.compose.ui.tooling.preview.Preview
import com.victorhvs.rnm.presentation.theme.RicknmortyTheme

@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun EpisodeHorizontalCard(
modifier: Modifier = Modifier,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.testTag
Expand All @@ -20,7 +19,6 @@ import com.skydoves.landscapist.glide.GlideImage
import com.victorhvs.rnm.R
import com.victorhvs.rnm.presentation.theme.RicknmortyTheme

@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun RNMImage(
modifier: Modifier = Modifier,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
Expand All @@ -28,7 +27,6 @@ import androidx.compose.ui.unit.dp
import com.victorhvs.rnm.R
import com.victorhvs.rnm.presentation.theme.RicknmortyTheme

@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun SearchWidget(
modifier: Modifier = Modifier,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.stringResource
Expand All @@ -23,7 +22,6 @@ import androidx.compose.ui.tooling.preview.Preview
import com.victorhvs.rnm.R
import com.victorhvs.rnm.presentation.theme.RicknmortyTheme

@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun SectionTitle(
modifier: Modifier = Modifier,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.victorhvs.rnm.presentation.navigation

import android.content.Intent
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.runtime.Composable
import androidx.navigation.NavHostController
import androidx.navigation.NavType
Expand All @@ -12,7 +11,6 @@ import androidx.navigation.navDeepLink
import com.victorhvs.rnm.presentation.screens.detail.DetailScreen
import com.victorhvs.rnm.presentation.screens.list.ListScreen

@ExperimentalAnimationApi
@Composable
fun SetupNavGraph(navController: NavHostController) {
NavHost(
Expand All @@ -36,7 +34,10 @@ fun SetupNavGraph(navController: NavHostController) {
type = NavType.IntType
})
) {
DetailScreen(onBackPressed = { navController.popBackStack() })
DetailScreen(
charId = it.arguments?.getInt(Screen.DETAILS_ARGUMENT_KEY) ?: 1,
onBackPressed = { navController.popBackStack() }
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@ import com.victorhvs.rnm.presentation.theme.spacing
@Composable
fun DetailScreen(
onBackPressed: () -> Unit,
charId: Int,
viewModel: DetailViewModel = hiltViewModel()
) {
val uiState = viewModel.state.collectAsState()

LaunchedEffect(Unit) {
viewModel.fetchCharacterAndEpisodes()
viewModel.fetchCharacterAndEpisodes(charId)
}

Scaffold { paddingValues ->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.victorhvs.rnm.presentation.screens.detail

import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.victorhvs.rnm.core.DispatcherProvider
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 com.victorhvs.rnm.presentation.navigation.Screen
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
Expand All @@ -22,18 +20,16 @@ import javax.inject.Inject
class DetailViewModel @Inject constructor(
private val charRepository: CharacterRepository,
private val epiRepository: EpisodesRepository,
private val dispatcherProvider: DispatcherProvider,
private val savedStateHandle: SavedStateHandle
private val dispatcherProvider: DispatcherProvider
) : ViewModel() {

private val _state = MutableStateFlow<UiState>(UiState.Loading)
val state: StateFlow<UiState> =
_state.asStateFlow()

fun fetchCharacterAndEpisodes() {
fun fetchCharacterAndEpisodes(charId: Int) {
viewModelScope.launch {
try {
val charId = savedStateHandle.get<Int>(Screen.DETAILS_ARGUMENT_KEY)!!
val character = fetchCharacter(charId)
val episodes = fetchEpisodes(character.episode)
updateUiState(character, episodes)
Expand All @@ -54,7 +50,6 @@ class DetailViewModel @Inject constructor(
}

private fun updateUiState(character: Character, episodes: List<Episode>) {
println("ENTROU NO UPDATE")
_state.update { uiState ->
if (uiState is UiState.Success) {
uiState.copy(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ fun ListScreen(
viewModel: ListViewModel = hiltViewModel()
) {

val pagingCharacters = viewModel.state.collectAsLazyPagingItems()
val pagingCharacters = viewModel.searchedCharacter.collectAsLazyPagingItems()
val coroutineScope = rememberCoroutineScope()
val gridState = rememberLazyGridState()

LaunchedEffect(viewModel.searchQuery.value) {
LaunchedEffect(Unit) {
viewModel.searchCharacter(viewModel.searchQuery.value)
}

Expand Down Expand Up @@ -72,7 +72,7 @@ fun ListScreen(
fun ListContent(
modifier: Modifier = Modifier,
characters: LazyPagingItems<Character>,
gridState: LazyGridState = rememberLazyGridState(),
gridState: LazyGridState,
onCardClicked: (Int) -> Unit
) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,9 @@ class ListViewModel @Inject constructor(
private val _searchQuery = mutableStateOf("")
val searchQuery = _searchQuery

// private val _searchedCharacter = MutableStateFlow<PagingData<Character>>(PagingData.empty())
// val searchedCharacter get() = _searchedCharacter.asStateFlow()

private val _state = MutableStateFlow<PagingData<Character>>(PagingData.empty())
val state: StateFlow<PagingData<Character>> =
_state.asStateFlow()

init {
searchCharacter(_searchQuery.value)
}
private val _searchedCharacter = MutableStateFlow<PagingData<Character>>(PagingData.empty())
val searchedCharacter: StateFlow<PagingData<Character>> =
_searchedCharacter.asStateFlow()

fun updateSearchQuery(query: String) {
_searchQuery.value = query
Expand All @@ -41,7 +34,7 @@ class ListViewModel @Inject constructor(
fun searchCharacter(query: String) {
viewModelScope.launch {
repository.searchCharacter(query = query).cachedIn(viewModelScope).collect { result ->
_state.update {
_searchedCharacter.update {
result
}
}
Expand Down

0 comments on commit 56b170e

Please sign in to comment.