Skip to content

Commit 2d7ce64

Browse files
Merge pull request #23 from NicosNicolaou16/bug_fixes
Bug fixes
2 parents dd8cbaa + 7da4d14 commit 2d7ce64

12 files changed

Lines changed: 77 additions & 44 deletions

File tree

app/src/main/java/com/nicos/pokedex_compose/data/mappers/PokemonDetailsMapper.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package com.nicos.pokedex_compose.data.mappers
33
import com.nicos.pokedex_compose.data.room_database.entities.PokemonDetailsEntity
44
import com.nicos.pokedex_compose.data.room_database.entities.PokemonDetailsWithStatsEntity
55
import com.nicos.pokedex_compose.data.room_database.entities.StatsEntity
6-
import com.nicos.pokedex_compose.presentation.pokemon_details_screen.models.PokemonDetailsUI
7-
import com.nicos.pokedex_compose.presentation.pokemon_details_screen.models.StatsUi
86

97
fun PokemonDetailsWithStatsEntity.toPokemonDetailsUi(): PokemonDetailsUI {
108
return PokemonDetailsUI(

app/src/main/java/com/nicos/pokedex_compose/presentation/pokemon_details_screen/models/PokemonDetailsUI.kt renamed to app/src/main/java/com/nicos/pokedex_compose/data/mappers/PokemonDetailsUI.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.nicos.pokedex_compose.presentation.pokemon_details_screen.models
1+
package com.nicos.pokedex_compose.data.mappers
22

33
data class PokemonDetailsUI(
44
val name: String,
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.nicos.pokedex_compose.data.mappers
2+
3+
data class PokemonUi(
4+
val name: String,
5+
val url: String?,
6+
var imageUrl: String?,
7+
var order: Int?,
8+
)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.nicos.pokedex_compose.data.mappers
2+
3+
import com.nicos.pokedex_compose.data.room_database.entities.PokemonEntity
4+
5+
fun PokemonEntity.toPokemonUi(): PokemonUi {
6+
return PokemonUi(
7+
name = this.name,
8+
url = this.url,
9+
order = this.order,
10+
imageUrl = this.imageUrl
11+
)
12+
}
13+
14+
fun PokemonUi.toPokemonEntity(): PokemonEntity {
15+
return PokemonEntity(
16+
name = this.name,
17+
url = null,
18+
order = null,
19+
imageUrl = null
20+
)
21+
}

app/src/main/java/com/nicos/pokedex_compose/data/repository_impl/PokemonDetailsRepositoryImpl.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import com.nicos.pokedex_compose.data.room_database.init_database.MyRoomDatabase
99
import com.nicos.pokedex_compose.data.network.dto.PokemonDetailsDto
1010
import com.nicos.pokedex_compose.data.network.PokemonService
1111
import com.nicos.pokedex_compose.domain.repositories.PokemonDetailsRepository
12-
import com.nicos.pokedex_compose.presentation.pokemon_details_screen.models.PokemonDetailsUI
12+
import com.nicos.pokedex_compose.data.mappers.PokemonDetailsUI
1313
import com.nicos.pokedex_compose.utils.generic_classes.HandlingError
1414
import com.nicos.pokedex_compose.utils.generic_classes.Resource
1515
import kotlinx.coroutines.Dispatchers
@@ -45,7 +45,7 @@ class PokemonDetailsRepositoryImpl @Inject constructor(
4545
}.flowOn(Dispatchers.IO)
4646
}
4747

48-
override suspend fun savePokemonDetails(pokemonDetailsDto: PokemonDetailsDto) {
48+
private suspend fun savePokemonDetails(pokemonDetailsDto: PokemonDetailsDto) {
4949
myRoomDatabase.statsDao().deleteByPokemonName(name = pokemonDetailsDto.name)
5050
myRoomDatabase.pokemonDetailDao()
5151
.insertOrReplaceObject(data = pokemonDetailsDto.toPokemonDetailsEntity())

app/src/main/java/com/nicos/pokedex_compose/data/repository_impl/PokemonListRepositoryImpl.kt

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.nicos.pokedex_compose.data.repository_impl
22

33
import androidx.core.text.isDigitsOnly
4+
import com.nicos.pokedex_compose.data.mappers.toPokemonUi
45
import com.nicos.pokedex_compose.data.room_database.entities.PokemonEntity
56
import com.nicos.pokedex_compose.data.room_database.entities.toPokemonEntity
67
import com.nicos.pokedex_compose.data.room_database.init_database.MyRoomDatabase
78
import com.nicos.pokedex_compose.data.network.PokemonService
89
import com.nicos.pokedex_compose.domain.repositories.PokemonListRepository
10+
import com.nicos.pokedex_compose.data.mappers.PokemonUi
911
import com.nicos.pokedex_compose.utils.generic_classes.HandlingError
1012
import com.nicos.pokedex_compose.utils.generic_classes.Resource
1113
import kotlinx.coroutines.Dispatchers
@@ -26,7 +28,7 @@ class PokemonListRepositoryImpl @Inject constructor(
2628
private const val PNG_FORMAT = ".png"
2729
}
2830

29-
override suspend fun fetchPokemonList(url: String?): Flow<Resource<MutableList<PokemonEntity>>> {
31+
override suspend fun fetchPokemonList(url: String?): Flow<Resource<MutableList<PokemonUi>>> {
3032
return flow {
3133
try {
3234
val pokemonService =
@@ -38,7 +40,8 @@ class PokemonListRepositoryImpl @Inject constructor(
3840

3941
emit(
4042
Resource.Success(
41-
data = myRoomDatabase.pokemonDao().getAllPokemon(),
43+
data = myRoomDatabase.pokemonDao().getAllPokemon().map { it.toPokemonUi() }
44+
.toMutableList(),
4245
nextUrl = nextUrl
4346
)
4447
)
@@ -48,7 +51,7 @@ class PokemonListRepositoryImpl @Inject constructor(
4851
}.flowOn(Dispatchers.IO)
4952
}
5053

51-
override suspend fun savePokemon(pokemonEntityList: MutableList<PokemonEntity>) {
54+
private suspend fun savePokemon(pokemonEntityList: MutableList<PokemonEntity>) {
5255
pokemonEntityList.forEach {
5356
buildPokemonImageUrl(it)
5457
if (it.imageUrl != null) {
@@ -66,10 +69,15 @@ class PokemonListRepositoryImpl @Inject constructor(
6669
}
6770
}
6871

69-
override suspend fun offline(): Flow<Resource<MutableList<PokemonEntity>>> {
72+
override suspend fun offline(): Flow<Resource<MutableList<PokemonUi>>> {
7073
return flow {
7174
try {
72-
emit(Resource.Success(data = myRoomDatabase.pokemonDao().getAllPokemon()))
75+
emit(
76+
Resource.Success(
77+
data = myRoomDatabase.pokemonDao().getAllPokemon().map { it.toPokemonUi() }
78+
.toMutableList()
79+
)
80+
)
7381
} catch (e: Exception) {
7482
emit(Resource.Error(message = handlingError.handleErrorMessage(e)))
7583
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package com.nicos.pokedex_compose.domain.repositories
22

3-
import com.nicos.pokedex_compose.data.network.dto.PokemonDetailsDto
4-
import com.nicos.pokedex_compose.presentation.pokemon_details_screen.models.PokemonDetailsUI
3+
import com.nicos.pokedex_compose.data.mappers.PokemonDetailsUI
54
import com.nicos.pokedex_compose.utils.generic_classes.Resource
65
import kotlinx.coroutines.flow.Flow
76

87
interface PokemonDetailsRepository {
98
suspend fun fetchPokemonDetails(url: String, name: String): Flow<Resource<PokemonDetailsUI>>
10-
suspend fun savePokemonDetails(pokemonDetailsDto: PokemonDetailsDto)
119
suspend fun offline(name: String): Flow<Resource<PokemonDetailsUI>>
1210
}
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package com.nicos.pokedex_compose.domain.repositories
22

3-
import com.nicos.pokedex_compose.data.room_database.entities.PokemonEntity
3+
import com.nicos.pokedex_compose.data.mappers.PokemonUi
44
import com.nicos.pokedex_compose.utils.generic_classes.Resource
55
import kotlinx.coroutines.flow.Flow
66

77
interface PokemonListRepository {
8-
suspend fun fetchPokemonList(url: String?): Flow<Resource<MutableList<PokemonEntity>>>
9-
suspend fun savePokemon(pokemonEntityList: MutableList<PokemonEntity>)
10-
suspend fun offline(): Flow<Resource<MutableList<PokemonEntity>>>
8+
suspend fun fetchPokemonList(url: String?): Flow<Resource<MutableList<PokemonUi>>>
9+
suspend fun offline(): Flow<Resource<MutableList<PokemonUi>>>
1110
}

app/src/main/java/com/nicos/pokedex_compose/presentation/pokemon_details_screen/models/PokemonDetailsDataModel.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.nicos.pokedex_compose.presentation.pokemon_details_screen.models
22

3+
import com.nicos.pokedex_compose.data.mappers.PokemonDetailsUI
4+
import com.nicos.pokedex_compose.data.mappers.StatsUi
35
import kotlinx.coroutines.Dispatchers
46
import kotlinx.coroutines.flow.flow
57
import kotlinx.coroutines.flow.flowOn

app/src/main/java/com/nicos/pokedex_compose/presentation/pokemon_list_screen/PokemonListScreen.kt

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import androidx.compose.animation.AnimatedVisibilityScope
55
import androidx.compose.animation.SharedTransitionScope
66
import androidx.compose.foundation.clickable
77
import androidx.compose.foundation.layout.Box
8-
import androidx.compose.foundation.layout.BoxWithConstraints
98
import androidx.compose.foundation.layout.PaddingValues
109
import androidx.compose.foundation.layout.fillMaxSize
1110
import androidx.compose.foundation.layout.padding
@@ -22,7 +21,6 @@ import androidx.compose.runtime.LaunchedEffect
2221
import androidx.compose.runtime.collectAsState
2322
import androidx.compose.runtime.getValue
2423
import androidx.compose.runtime.mutableIntStateOf
25-
import androidx.compose.runtime.mutableStateOf
2624
import androidx.compose.runtime.remember
2725
import androidx.compose.runtime.setValue
2826
import androidx.compose.ui.Modifier
@@ -37,11 +35,11 @@ import androidx.hilt.lifecycle.viewmodel.compose.hiltViewModel
3735
import coil.compose.AsyncImage
3836
import coil.request.CachePolicy
3937
import coil.request.ImageRequest
40-
import com.nicos.pokedex_compose.data.room_database.entities.PokemonEntity
4138
import com.nicos.pokedex_compose.presentation.generic_compose_views.CustomToolbar
4239
import com.nicos.pokedex_compose.presentation.generic_compose_views.ShowDialog
4340
import com.nicos.pokedex_compose.presentation.generic_compose_views.StartDefaultLoader
4441
import com.nicos.pokedex_compose.presentation.navigation.navigation_3.Navigator
42+
import com.nicos.pokedex_compose.data.mappers.PokemonUi
4543
import com.nicos.pokedex_compose.utils.extensions.encodeStringUrl
4644
import com.nicos.pokedex_compose.utils.extensions.getProgressDrawable
4745
import com.nicos.pokedex_compose.utils.screen_routes.PokemonDetails
@@ -75,7 +73,7 @@ fun SharedTransitionScope.PokemonListScreen(
7573

7674
@Composable
7775
fun SharedTransitionScope.GridViewPokemonList(
78-
listener: (PokemonEntity) -> Unit,
76+
listener: (PokemonUi) -> Unit,
7977
paddingValues: PaddingValues,
8078
animatedVisibilityScope: AnimatedVisibilityScope,
8179
pokemonListViewModel: PokemonListViewModel = hiltViewModel()
@@ -89,20 +87,21 @@ fun SharedTransitionScope.GridViewPokemonList(
8987
var columns by remember { mutableIntStateOf(2) }
9088
// Get the screen density to convert px to dp
9189
val density = LocalDensity.current
92-
Box( modifier = Modifier
93-
.padding(paddingValues)
94-
.fillMaxSize()
95-
.onPlaced { coordinates ->
96-
// This block is called after the Box is measured and placed.
97-
// We get the measured width in pixels and convert it to Dp.
98-
val widthInDp = with(density) { coordinates.size.width.toDp() }
90+
Box(
91+
modifier = Modifier
92+
.padding(paddingValues)
93+
.fillMaxSize()
94+
.onPlaced { coordinates ->
95+
// This block is called after the Box is measured and placed.
96+
// We get the measured width in pixels and convert it to Dp.
97+
val widthInDp = with(density) { coordinates.size.width.toDp() }
9998

100-
// Calculate columns based on the actual width.
101-
// Check for > 0.dp to avoid issues on the very first composition.
102-
if (widthInDp > 0.dp) {
103-
columns = (widthInDp / 180.dp).toInt().coerceAtLeast(2)
104-
}
105-
}) {
99+
// Calculate columns based on the actual width.
100+
// Check for > 0.dp to avoid issues on the very first composition.
101+
if (widthInDp > 0.dp) {
102+
columns = (widthInDp / 180.dp).toInt().coerceAtLeast(2)
103+
}
104+
}) {
106105
LazyVerticalGrid(
107106
modifier = Modifier.fillMaxSize(),
108107
columns = GridCells.Fixed(columns)
@@ -113,7 +112,7 @@ fun SharedTransitionScope.GridViewPokemonList(
113112
LoadPokemonImage(
114113
listener = listener,
115114
animatedVisibilityScope = animatedVisibilityScope,
116-
pokemonEntity = pokemon
115+
pokemonUi = pokemon
117116
)
118117
}
119118
item {
@@ -132,16 +131,16 @@ fun SharedTransitionScope.GridViewPokemonList(
132131

133132
@Composable
134133
fun SharedTransitionScope.LoadPokemonImage(
135-
listener: (PokemonEntity) -> Unit,
134+
listener: (PokemonUi) -> Unit,
136135
animatedVisibilityScope: AnimatedVisibilityScope,
137-
pokemonEntity: PokemonEntity
136+
pokemonUi: PokemonUi
138137
) {
139138
val context = LocalContext.current
140139
Card(
141140
modifier = Modifier
142141
.padding(5.dp)
143142
.clickable {
144-
listener(pokemonEntity)
143+
listener(pokemonUi)
145144
},
146145
elevation = CardDefaults.cardElevation(
147146
defaultElevation = 0.dp
@@ -156,7 +155,7 @@ fun SharedTransitionScope.LoadPokemonImage(
156155
) {
157156
AsyncImage(
158157
model = ImageRequest.Builder(context = context).apply {
159-
data(pokemonEntity.imageUrl)
158+
data(pokemonUi.imageUrl)
160159
placeholder(getProgressDrawable(context))
161160
error(android.R.drawable.stat_notify_error)
162161
fallback(android.R.drawable.stat_notify_error)
@@ -165,7 +164,7 @@ fun SharedTransitionScope.LoadPokemonImage(
165164
modifier = Modifier
166165
.sharedElement(
167166
sharedContentState = rememberSharedContentState(
168-
key = pokemonEntity.imageUrl ?: ""
167+
key = pokemonUi.imageUrl ?: ""
169168
),
170169
animatedVisibilityScope = animatedVisibilityScope,
171170
)

0 commit comments

Comments
 (0)