From 9b80faa222292c0708bb6aecd623261a33af2486 Mon Sep 17 00:00:00 2001 From: sh1mj1 <90040304+sh1mj1@users.noreply.github.com> Date: Mon, 9 Sep 2024 17:53:45 +0900 Subject: [PATCH] =?UTF-8?q?[1.1.0.alpha/AN=5FFEAT,=20AN=5FUI]=20=EB=8F=84?= =?UTF-8?q?=EA=B0=90=20=EC=83=81=EC=84=B8=20UI=20=EC=88=98=EC=A0=95,=20?= =?UTF-8?q?=EB=B0=94=EC=9D=B4=EC=98=B4=EC=97=90=EC=84=9C=EC=9D=98=20?= =?UTF-8?q?=ED=8F=AC=EC=BC=93=EB=AA=AC=20=ED=83=80=EC=9E=85=20=EA=B0=80?= =?UTF-8?q?=EC=A0=B8=EC=98=A4=EA=B8=B0=20(#308)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(PokemonBiomeUiModel): 포켓몬 바이옴 리스트와 모든 바이옴 정보를 통해 ui 모델로 * refactor(PokemonBiomeUiModel): 포켓몬 바이옴 리스트와 모든 바이옴 정보를 통해 ui 모델로 * feat(PokemonDetailUiState2): PokemonUiModel 을 가진다 * refactor(PokemonDetailViewModel): 바이옴 레포지토리를 추가로 가진다 * feat(PokemonDetailSkillFragment): PokemonDetailUiState2 에 대한 함수 추가 * feat(PokemonEvolutionFragment): PokemonDetailUiState2 에 대한 함수 추가 * feat(PokemonInformationFragment): PokemonDetailUiState2 에 대한 함수 추가 * feat(PokemonStatFragment): PokemonDetailUiState2 에 대한 함수 추가 * feat(PokemonDetailBiomeAdapter): PokemonDetailUiState2 에 대한 함수 추가 * refactor(PokemonDetailActivity): PokemonDetailUiState 에 대한 참조 제거 * refactor(PokemonEvolutionFragment): PokemonDetailUiState 에 대한 참조 제거 * refactor(PokemonInformationFragment): PokemonDetailUiState 에 대한 참조 제거 * refactor(PokemonDetailSkillFragment): PokemonDetailUiState 에 대한 참조 제거 * refactor(PokemonStatFragment): PokemonDetailUiState 에 대한 참조 제거 * refactor(PokemonDetailUiState.kt): PokemonDetailUiState2 -> PokemonDetailUiState * test(PokemonDetailViewModelTest): 포켓모 상세에서 바이옴 정보 * ui(PokemonDetailBiomeTypesAdapter): 바이옴의 타입 정보 추가 * ui(PokemonDetailBiomeViewHolder): 바이옴의 타입 정보 추가 * feat(PokemonInformationFragment.kt): 체중, 키를 서버로부터 로드한 데이터 보여준다 * feat(PokemonBiome): 바이옴에서 출연하는 포켓몬 타입 프로퍼티 추가 * feat(DefaultDexRepository): 바이옴 레포지토리 생성자 추가 * feat(PokemonBiomeUiModel): PokemonBiome 에서 ui 모델로 매핑 * refactor(PokemonBiomeUiModel): 포켓몬 뷰모델은 이전처럼 dex repository 하나만 갖는다 * refactor(PokemonBiomeUiModel): ui 모델로 매핑할 때 다른 파라미터를 갖지 않는다 * test(PokemonDetailViewModelTest): 포켓몬 상세 로드 * chore: ktlint * refactor(DefaultDexRepository): 포켓몬 상세 로직 가독성 개선 --- .../dex/detail/PokemonDetailUiState.kt | 19 +++++++-- .../dex/detail/PokemonDetailViewModel.kt | 5 +-- .../information/PokemonDetailBiomeAdapter.kt | 6 +-- .../PokemonDetailBiomeTypesAdapter.kt | 39 +++++++++++++++++++ .../PokemonDetailBiomeViewHolder.kt | 22 ++++++++++- .../information/PokemonInformationFragment.kt | 10 ++++- .../skill/PokemonDetailSkillFragment.kt | 1 - .../dex/detail/stat/PokemonStatFragment.kt | 4 +- .../dex/model/PokemonBiomeUiModel.kt | 22 +++++++++++ .../res/layout/activity_pokemon_detail.xml | 2 +- .../layout/fragment_pokemon_information.xml | 26 +++++++++---- .../item_pokemon_detail_information_biome.xml | 35 ++++++++++------- android/app/src/main/res/values/strings.xml | 8 +++- .../dex/detail/PokemonDetailViewModelTest.kt | 34 ++++++---------- .../rogue/helper/data/model/PokemonBiome.kt | 22 ++++++----- .../data/repository/DefaultDexRepository.kt | 38 ++++++++++++++++-- 16 files changed, 219 insertions(+), 74 deletions(-) create mode 100644 android/app/src/main/java/poke/rogue/helper/presentation/dex/detail/information/PokemonDetailBiomeTypesAdapter.kt create mode 100644 android/app/src/main/java/poke/rogue/helper/presentation/dex/model/PokemonBiomeUiModel.kt diff --git a/android/app/src/main/java/poke/rogue/helper/presentation/dex/detail/PokemonDetailUiState.kt b/android/app/src/main/java/poke/rogue/helper/presentation/dex/detail/PokemonDetailUiState.kt index 72b28857d..1d3551dab 100644 --- a/android/app/src/main/java/poke/rogue/helper/presentation/dex/detail/PokemonDetailUiState.kt +++ b/android/app/src/main/java/poke/rogue/helper/presentation/dex/detail/PokemonDetailUiState.kt @@ -1,10 +1,11 @@ package poke.rogue.helper.presentation.dex.detail -import poke.rogue.helper.data.model.PokemonBiome +import poke.rogue.helper.data.model.Biome import poke.rogue.helper.data.model.PokemonDetail import poke.rogue.helper.data.model.PokemonDetailSkills import poke.rogue.helper.data.model.Stat import poke.rogue.helper.presentation.dex.model.EvolutionsUiModel +import poke.rogue.helper.presentation.dex.model.PokemonBiomeUiModel import poke.rogue.helper.presentation.dex.model.PokemonDetailAbilityUiModel import poke.rogue.helper.presentation.dex.model.PokemonUiModel import poke.rogue.helper.presentation.dex.model.StatUiModel @@ -20,12 +21,24 @@ sealed interface PokemonDetailUiState { val skills: PokemonDetailSkills, val height: Float, val weight: Float, - val biomes: List, + val biomes: List, ) : PokemonDetailUiState data object IsLoading : PokemonDetailUiState } +fun PokemonDetail.toUi(allBiomes: List): PokemonDetailUiState.Success = + PokemonDetailUiState.Success( + pokemon = pokemon.toUi(), + stats = stats.map(Stat::toUi), + abilities = abilities.toPokemonDetailUi(), + evolutions = evolutions.toUi(), + skills = skills, + height = height.toFloat(), + weight = weight.toFloat(), + biomes = biomes.toUi(), + ) + fun PokemonDetail.toUi(): PokemonDetailUiState.Success = PokemonDetailUiState.Success( pokemon = pokemon.toUi(), @@ -35,5 +48,5 @@ fun PokemonDetail.toUi(): PokemonDetailUiState.Success = skills = skills, height = height.toFloat(), weight = weight.toFloat(), - biomes = biomes, + biomes = biomes.toUi(), ) diff --git a/android/app/src/main/java/poke/rogue/helper/presentation/dex/detail/PokemonDetailViewModel.kt b/android/app/src/main/java/poke/rogue/helper/presentation/dex/detail/PokemonDetailViewModel.kt index 8bbe22563..343b147cb 100644 --- a/android/app/src/main/java/poke/rogue/helper/presentation/dex/detail/PokemonDetailViewModel.kt +++ b/android/app/src/main/java/poke/rogue/helper/presentation/dex/detail/PokemonDetailViewModel.kt @@ -24,8 +24,7 @@ class PokemonDetailViewModel( ) : ErrorHandleViewModel(logger), PokemonDetailNavigateHandler { - private val _uiState: MutableStateFlow = - MutableStateFlow(PokemonDetailUiState.IsLoading) + private val _uiState: MutableStateFlow = MutableStateFlow(PokemonDetailUiState.IsLoading) val uiState = _uiState.asStateFlow() val isEmpty: StateFlow = @@ -44,7 +43,7 @@ class PokemonDetailViewModel( private val _navigateToPokemonDetailEvent = MutableSharedFlow() val navigateToPokemonDetailEvent = _navigateToPokemonDetailEvent.asSharedFlow() - fun updatePokemonDetail(pokemonId: String) { + fun updatePokemonDetail(pokemonId: String?) { requireNotNull(pokemonId) { "Pokemon ID must not be null" } viewModelScope.launch { _uiState.value = dexRepository.pokemonDetail(pokemonId).toUi() diff --git a/android/app/src/main/java/poke/rogue/helper/presentation/dex/detail/information/PokemonDetailBiomeAdapter.kt b/android/app/src/main/java/poke/rogue/helper/presentation/dex/detail/information/PokemonDetailBiomeAdapter.kt index 866107c5e..2f7b59e35 100644 --- a/android/app/src/main/java/poke/rogue/helper/presentation/dex/detail/information/PokemonDetailBiomeAdapter.kt +++ b/android/app/src/main/java/poke/rogue/helper/presentation/dex/detail/information/PokemonDetailBiomeAdapter.kt @@ -3,14 +3,14 @@ package poke.rogue.helper.presentation.dex.detail.information import android.view.LayoutInflater import android.view.ViewGroup import androidx.recyclerview.widget.ListAdapter -import poke.rogue.helper.data.model.PokemonBiome import poke.rogue.helper.databinding.ItemPokemonDetailInformationBiomeBinding import poke.rogue.helper.presentation.dex.detail.PokemonDetailNavigateHandler +import poke.rogue.helper.presentation.dex.model.PokemonBiomeUiModel import poke.rogue.helper.presentation.util.view.ItemDiffCallback class PokemonDetailBiomeAdapter( private val onClickBiomeItem: PokemonDetailNavigateHandler, -) : ListAdapter(biomeComparator) { +) : ListAdapter(biomeComparator) { override fun onCreateViewHolder( parent: ViewGroup, viewType: Int, @@ -34,7 +34,7 @@ class PokemonDetailBiomeAdapter( companion object { val biomeComparator = - ItemDiffCallback( + ItemDiffCallback( onItemsTheSame = { oldItem, newItem -> oldItem.id == newItem.id }, onContentsTheSame = { oldItem, newItem -> oldItem == newItem }, ) diff --git a/android/app/src/main/java/poke/rogue/helper/presentation/dex/detail/information/PokemonDetailBiomeTypesAdapter.kt b/android/app/src/main/java/poke/rogue/helper/presentation/dex/detail/information/PokemonDetailBiomeTypesAdapter.kt new file mode 100644 index 000000000..a7f46c109 --- /dev/null +++ b/android/app/src/main/java/poke/rogue/helper/presentation/dex/detail/information/PokemonDetailBiomeTypesAdapter.kt @@ -0,0 +1,39 @@ +package poke.rogue.helper.presentation.dex.detail.information + +import android.content.Context +import android.widget.ImageView +import com.google.android.flexbox.FlexboxLayout +import poke.rogue.helper.presentation.type.model.TypeUiModel +import poke.rogue.helper.presentation.util.view.dp + +class PokemonDetailBiomeTypesAdapter(private val context: Context, private val viewGroup: FlexboxLayout) { + fun addTypes( + types: List, + spacingBetweenTypes: Int = 0.dp, + iconSize: Int = 18.dp, + ) { + viewGroup.removeAllViews() + + types.forEach { type -> + val imageView = + ImageView(context).apply { + setImageResource(type.typeIconResId) + + layoutParams = + FlexboxLayout.LayoutParams( + iconSize, + iconSize, + ).apply { + setMargins( + spacingBetweenTypes, + 0, + 0, + 0, + ) + } + } + + viewGroup.addView(imageView) + } + } +} diff --git a/android/app/src/main/java/poke/rogue/helper/presentation/dex/detail/information/PokemonDetailBiomeViewHolder.kt b/android/app/src/main/java/poke/rogue/helper/presentation/dex/detail/information/PokemonDetailBiomeViewHolder.kt index 2d27427c2..767e13ec6 100644 --- a/android/app/src/main/java/poke/rogue/helper/presentation/dex/detail/information/PokemonDetailBiomeViewHolder.kt +++ b/android/app/src/main/java/poke/rogue/helper/presentation/dex/detail/information/PokemonDetailBiomeViewHolder.kt @@ -1,18 +1,36 @@ package poke.rogue.helper.presentation.dex.detail.information import androidx.recyclerview.widget.RecyclerView -import poke.rogue.helper.data.model.PokemonBiome import poke.rogue.helper.databinding.ItemPokemonDetailInformationBiomeBinding import poke.rogue.helper.presentation.dex.detail.PokemonDetailNavigateHandler +import poke.rogue.helper.presentation.dex.model.PokemonBiomeUiModel +import poke.rogue.helper.presentation.util.view.dp class PokemonDetailBiomeViewHolder( private val binding: ItemPokemonDetailInformationBiomeBinding, private val onClickBiomeItem: PokemonDetailNavigateHandler, ) : RecyclerView.ViewHolder(binding.root) { - fun bind(biome: PokemonBiome) { + fun bind(biome: PokemonBiomeUiModel) { binding.apply { this.biome = biome uiEventHandler = onClickBiomeItem } + + val typesLayout = binding.flBiomeTypeIcons + val biomeTypesAdapter = + PokemonDetailBiomeTypesAdapter( + context = binding.root.context, + viewGroup = typesLayout, + ) + biomeTypesAdapter.addTypes( + types = biome.types, + spacingBetweenTypes = TYPES_SPACING, + iconSize = TYPE_ICON_SIZE, + ) + } + + companion object { + private val TYPES_SPACING = 5.dp + private val TYPE_ICON_SIZE = 18.dp } } diff --git a/android/app/src/main/java/poke/rogue/helper/presentation/dex/detail/information/PokemonInformationFragment.kt b/android/app/src/main/java/poke/rogue/helper/presentation/dex/detail/information/PokemonInformationFragment.kt index a7f57c9ef..a573ef31f 100644 --- a/android/app/src/main/java/poke/rogue/helper/presentation/dex/detail/information/PokemonInformationFragment.kt +++ b/android/app/src/main/java/poke/rogue/helper/presentation/dex/detail/information/PokemonInformationFragment.kt @@ -39,12 +39,20 @@ class PokemonInformationFragment : activityViewModel.uiState.collect { pokemonDetailUiState -> when (pokemonDetailUiState) { is PokemonDetailUiState.IsLoading -> {} - is PokemonDetailUiState.Success -> biomesAdapter.submitList(pokemonDetailUiState.biomes) + is PokemonDetailUiState.Success -> bindPokemonInformation(pokemonDetailUiState) } } } } + private fun bindPokemonInformation(pokemonDetail: PokemonDetailUiState.Success) { + binding.apply { + height = pokemonDetail.height + weight = pokemonDetail.weight + } + biomesAdapter.submitList(pokemonDetail.biomes) + } + override fun onResume() { super.onResume() binding.root.requestLayout() diff --git a/android/app/src/main/java/poke/rogue/helper/presentation/dex/detail/skill/PokemonDetailSkillFragment.kt b/android/app/src/main/java/poke/rogue/helper/presentation/dex/detail/skill/PokemonDetailSkillFragment.kt index 05ad9d2c3..f2095d3e9 100644 --- a/android/app/src/main/java/poke/rogue/helper/presentation/dex/detail/skill/PokemonDetailSkillFragment.kt +++ b/android/app/src/main/java/poke/rogue/helper/presentation/dex/detail/skill/PokemonDetailSkillFragment.kt @@ -77,7 +77,6 @@ class PokemonDetailSkillFragment : BindingFragment activityViewModel.uiState.collect { state -> when (state) { is PokemonDetailUiState.IsLoading -> {} - // TODO: skill 을 현재는 한 종류의 스킬 목록만 사용하고 있음..... 이후에는 여러개의 스킬을 받아야함 is PokemonDetailUiState.Success -> { eggSkillsAdapter.submitList(state.skills.eggLearn.toUi()) skillsAdapter.submitList(state.skills.selfLearn.toUi()) diff --git a/android/app/src/main/java/poke/rogue/helper/presentation/dex/detail/stat/PokemonStatFragment.kt b/android/app/src/main/java/poke/rogue/helper/presentation/dex/detail/stat/PokemonStatFragment.kt index 6f3f7d3e7..9595ce919 100644 --- a/android/app/src/main/java/poke/rogue/helper/presentation/dex/detail/stat/PokemonStatFragment.kt +++ b/android/app/src/main/java/poke/rogue/helper/presentation/dex/detail/stat/PokemonStatFragment.kt @@ -38,7 +38,7 @@ class PokemonStatFragment : BindingFragment(R.layout activityViewModel.uiState.collect { uiState -> when (uiState) { is PokemonDetailUiState.IsLoading -> return@collect - is PokemonDetailUiState.Success -> bindDatas(uiState) + is PokemonDetailUiState.Success -> bindData(uiState) } } } @@ -64,7 +64,7 @@ class PokemonStatFragment : BindingFragment(R.layout } } - private fun bindDatas(uiState: PokemonDetailUiState.Success) { + private fun bindData(uiState: PokemonDetailUiState.Success) { binding.apply { pokemonStatAdapter.submitList(uiState.stats) abilityAdapter.submitList(uiState.abilities) diff --git a/android/app/src/main/java/poke/rogue/helper/presentation/dex/model/PokemonBiomeUiModel.kt b/android/app/src/main/java/poke/rogue/helper/presentation/dex/model/PokemonBiomeUiModel.kt new file mode 100644 index 000000000..dd663c2c7 --- /dev/null +++ b/android/app/src/main/java/poke/rogue/helper/presentation/dex/model/PokemonBiomeUiModel.kt @@ -0,0 +1,22 @@ +package poke.rogue.helper.presentation.dex.model + +import poke.rogue.helper.data.model.PokemonBiome +import poke.rogue.helper.presentation.type.model.TypeUiModel +import poke.rogue.helper.presentation.type.model.toUi + +data class PokemonBiomeUiModel( + val id: String, + val name: String, + val imageUrl: String, + val types: List, +) + +fun PokemonBiome.toUi(): PokemonBiomeUiModel = + PokemonBiomeUiModel( + id = id, + name = name, + imageUrl = imageUrl, + types = pokemonType.toUi(), + ) + +fun List.toUi(): List = this.map(PokemonBiome::toUi) diff --git a/android/app/src/main/res/layout/activity_pokemon_detail.xml b/android/app/src/main/res/layout/activity_pokemon_detail.xml index 6b8b47993..7f38db1a1 100644 --- a/android/app/src/main/res/layout/activity_pokemon_detail.xml +++ b/android/app/src/main/res/layout/activity_pokemon_detail.xml @@ -86,7 +86,7 @@ android:id="@+id/layout_pokemon_detail_pokemon_types" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:gravity="center" + android:gravity="start" android:orientation="vertical" app:layout_constraintStart_toStartOf="@id/tv_pokemon_detail_pokemon_name" app:layout_constraintTop_toBottomOf="@id/tv_pokemon_detail_pokemon_name" diff --git a/android/app/src/main/res/layout/fragment_pokemon_information.xml b/android/app/src/main/res/layout/fragment_pokemon_information.xml index 29dd2176a..fd704249c 100644 --- a/android/app/src/main/res/layout/fragment_pokemon_information.xml +++ b/android/app/src/main/res/layout/fragment_pokemon_information.xml @@ -3,6 +3,17 @@ xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"> + + + + + + + @@ -25,7 +36,7 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" - android:text="@string/pokemon_detail_information_weight" + android:text="@string/pokemon_detail_information_weight_title" android:textSize="16sp" app:layout_constraintEnd_toStartOf="@id/divider_pokemon_detail_information_weight_height" app:layout_constraintStart_toStartOf="parent" @@ -36,11 +47,12 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" - android:text="6.9kg" + android:text="@{String.format(@string/pokemon_detail_information_weight, weight)}" android:textSize="16sp" app:layout_constraintEnd_toEndOf="@id/tv_pokemon_detail_information_weight_title" app:layout_constraintStart_toStartOf="@id/tv_pokemon_detail_information_weight_title" - app:layout_constraintTop_toBottomOf="@id/tv_pokemon_detail_information_weight_title" /> + app:layout_constraintTop_toBottomOf="@id/tv_pokemon_detail_information_weight_title" + tools:text="6.9 kg" /> - - /> + type="poke.rogue.helper.presentation.dex.model.PokemonBiomeUiModel" /> + android:background="@drawable/shape_pokemon_corner_radius" + android:padding="10dp"> + + diff --git a/android/app/src/main/res/values/strings.xml b/android/app/src/main/res/values/strings.xml index 0a7592c8d..a7a907854 100644 --- a/android/app/src/main/res/values/strings.xml +++ b/android/app/src/main/res/values/strings.xml @@ -71,8 +71,12 @@ 자력으로 배우는 기술 - 체중 - + 체중 + + 바이옴 + %.1f kg + %.1f m + 진화 체인이 없는 포켓몬입니다. diff --git a/android/app/src/test/java/poke/rogue/helper/presentation/dex/detail/PokemonDetailViewModelTest.kt b/android/app/src/test/java/poke/rogue/helper/presentation/dex/detail/PokemonDetailViewModelTest.kt index 73345bffb..1e78783c6 100644 --- a/android/app/src/test/java/poke/rogue/helper/presentation/dex/detail/PokemonDetailViewModelTest.kt +++ b/android/app/src/test/java/poke/rogue/helper/presentation/dex/detail/PokemonDetailViewModelTest.kt @@ -16,6 +16,7 @@ import poke.rogue.helper.presentation.dex.model.EvolutionsUiModel import poke.rogue.helper.presentation.dex.model.PokemonDetailAbilityUiModel import poke.rogue.helper.presentation.dex.model.PokemonUiModel import poke.rogue.helper.presentation.dex.model.StatUiModel +import poke.rogue.helper.presentation.dex.model.toUi import poke.rogue.helper.presentation.type.model.TypeUiModel import poke.rogue.helper.testing.CoroutinesTestExtension import poke.rogue.helper.testing.data.repository.FakeDexRepository @@ -23,19 +24,22 @@ import poke.rogue.helper.testing.data.repository.FakeDexRepository @OptIn(ExperimentalCoroutinesApi::class) @ExtendWith(CoroutinesTestExtension::class) class PokemonDetailViewModelTest { - private lateinit var repository: DexRepository + private lateinit var dexRepository: DexRepository private lateinit var viewModel: PokemonDetailViewModel @BeforeEach fun setUp() { - repository = FakeDexRepository() + dexRepository = FakeDexRepository() } @Test fun `포켓몬 상세 데이터를 불러올 때 처음은 로딩 상태이다`() = runTest { // given - viewModel = PokemonDetailViewModel(repository) + viewModel = + PokemonDetailViewModel( + dexRepository, + ) // when val expectedPokemonDetailUiState = viewModel.uiState @@ -48,7 +52,10 @@ class PokemonDetailViewModelTest { fun `포켓몬 상세 데이터를 불러온다`() = runTest { // given - viewModel = PokemonDetailViewModel(repository) + viewModel = + PokemonDetailViewModel( + dexRepository, + ) // when viewModel.updatePokemonDetail(pokemonId = "1") @@ -101,24 +108,7 @@ class PokemonDetailViewModelTest { ), height = 0.7f, weight = 6.9f, - biomes = - listOf( - PokemonBiome( - "1", - "평야", - "https://pokeroguedex.com/biomes/plains.png", - ), - PokemonBiome( - "2", - "높은 풀숲", - "https://pokeroguedex.com/biomes/tall-grass.png", - ), - PokemonBiome( - "3", - "동굴", - "https://pokeroguedex.com/biomes/cave.png", - ), - ), + biomes = PokemonBiome.DUMMYS.toUi(), ) } } diff --git a/android/data/src/main/java/poke/rogue/helper/data/model/PokemonBiome.kt b/android/data/src/main/java/poke/rogue/helper/data/model/PokemonBiome.kt index 7dd0aa7a2..073918de1 100644 --- a/android/data/src/main/java/poke/rogue/helper/data/model/PokemonBiome.kt +++ b/android/data/src/main/java/poke/rogue/helper/data/model/PokemonBiome.kt @@ -6,24 +6,28 @@ data class PokemonBiome( val id: String, val name: String, val imageUrl: String, + val pokemonType: List = emptyList(), ) { companion object { val DUMMYS: List = listOf( PokemonBiome( - "1", - "평야", - "https://pokeroguedex.com/biomes/plains.png", + id = "1", + name = "평야", + imageUrl = "https://pokeroguedex.com/biomes/plains.png", + pokemonType = listOf(Type.GRASS, Type.BUG), ), PokemonBiome( - "2", - "높은 풀숲", - "https://pokeroguedex.com/biomes/tall-grass.png", + id = "2", + name = "높은 풀숲", + imageUrl = "https://pokeroguedex.com/biomes/tall-grass.png", + pokemonType = listOf(Type.GRASS, Type.BUG, Type.FLYING), ), PokemonBiome( - "3", - "동굴", - "https://pokeroguedex.com/biomes/cave.png", + id = "3", + name = "동굴", + imageUrl = "https://pokeroguedex.com/biomes/cave.png", + pokemonType = listOf(Type.GROUND, Type.ROCK), ), ) } diff --git a/android/data/src/main/java/poke/rogue/helper/data/repository/DefaultDexRepository.kt b/android/data/src/main/java/poke/rogue/helper/data/repository/DefaultDexRepository.kt index 34485d4d2..047463c3a 100644 --- a/android/data/src/main/java/poke/rogue/helper/data/repository/DefaultDexRepository.kt +++ b/android/data/src/main/java/poke/rogue/helper/data/repository/DefaultDexRepository.kt @@ -6,7 +6,9 @@ import poke.rogue.helper.analytics.AnalyticsLogger import poke.rogue.helper.analytics.analyticsLogger import poke.rogue.helper.data.datasource.LocalDexDataSource import poke.rogue.helper.data.datasource.RemoteDexDataSource +import poke.rogue.helper.data.model.Biome import poke.rogue.helper.data.model.Pokemon +import poke.rogue.helper.data.model.PokemonBiome import poke.rogue.helper.data.model.PokemonDetail import poke.rogue.helper.data.model.PokemonFilter import poke.rogue.helper.data.model.PokemonSort @@ -16,6 +18,7 @@ import poke.rogue.helper.stringmatcher.has class DefaultDexRepository( private val remotePokemonDataSource: RemoteDexDataSource, private val localPokemonDataSource: LocalDexDataSource, + private val biomeRepository: BiomeRepository, private val analyticsLogger: AnalyticsLogger, ) : DexRepository { private var cachedPokemons: List = emptyList() @@ -46,13 +49,31 @@ class DefaultDexRepository( }.toFilteredPokemons(sort, filters) } - override suspend fun pokemonDetail(id: String): PokemonDetail = - coroutineScope { - return@coroutineScope remotePokemonDataSource.pokemon(id).also { + override suspend fun pokemonDetail(id: String): PokemonDetail { + val allBiomes = biomeRepository.biomes() + return coroutineScope { + return@coroutineScope pokemonDetail(id, allBiomes).also { val pokemonName = it.pokemon.name + " " + it.pokemon.formName analyticsLogger.logPokemonDetail(id, pokemonName) } } + } + + private suspend fun pokemonDetail( + id: String, + allBiomes: List, + ): PokemonDetail { + val pokemonDetail = remotePokemonDataSource.pokemon(id) + val pokemonDetailIds = pokemonDetail.biomes.map(PokemonBiome::id) + val pokemonBiomes = + allBiomes + .filter { biome -> biome.id in pokemonDetailIds } + .toPokemonBiome() + + return pokemonDetail.copy( + biomes = pokemonBiomes, + ) + } private fun List.toFilteredPokemons( sort: PokemonSort, @@ -78,6 +99,7 @@ class DefaultDexRepository( DefaultDexRepository( RemoteDexDataSource.instance(), LocalDexDataSource.instance(context), + DefaultBiomeRepository.instance(), analyticsLogger(), ) } @@ -89,3 +111,13 @@ class DefaultDexRepository( } } } + +private fun Biome.toPokemonBiome(): PokemonBiome = + PokemonBiome( + id = id, + name = name, + imageUrl = image, + pokemonType = pokemonType, + ) + +private fun List.toPokemonBiome(): List = map(Biome::toPokemonBiome)