Skip to content

Commit

Permalink
[1.1.0/AN-FEAT] 배틀 선택 / 결과 UI 개선 (#316)
Browse files Browse the repository at this point in the history
* feat: 기술 위력이 마이너스 값일 경우, 화면 표시 변경

* feat: 배수 값에 따라 색상 다르게 설정

* feat: 선택했던 값 위치로 스크롤 되도록 설정

* chore: 변수명 수정

* feat: 명중률이 마이너스 값일 경우 -로 표시

* feat: 위력이 마이너스일 경우, 배수 표기 방식 변경
  • Loading branch information
JoYehyun99 authored Sep 14, 2024
1 parent 7ad0891 commit 854f204
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import poke.rogue.helper.presentation.base.toolbar.ToolbarActivity
import poke.rogue.helper.presentation.battle.model.SelectionData
import poke.rogue.helper.presentation.battle.model.WeatherUiModel
import poke.rogue.helper.presentation.battle.selection.BattleSelectionActivity
import poke.rogue.helper.presentation.util.context.colorOf
import poke.rogue.helper.presentation.util.parcelable
import poke.rogue.helper.presentation.util.repeatOnStarted
import poke.rogue.helper.presentation.util.view.setImage
Expand Down Expand Up @@ -123,6 +124,7 @@ class BattleActivity : ToolbarActivity<ActivityBattleBinding>(R.layout.activity_
val result = it.result
binding.tvPowerContent.text = result.power
binding.tvMultiplierContent.text = result.multiplier
binding.tvMultiplierContent.setTextColor(colorOf(result.colorRes))
binding.tvCalculatedPowerContent.text = result.calculatedResult
binding.tvAccuracyContent.text =
getString(R.string.battle_accuracy_title, result.accuracy)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,46 @@
package poke.rogue.helper.presentation.battle.model

import androidx.annotation.ColorRes
import poke.rogue.helper.R
import poke.rogue.helper.data.model.BattlePrediction
import poke.rogue.helper.presentation.battle.model.BattlePredictionUiModel.Companion.DEFAULT_NUMBER_FORMAT
import poke.rogue.helper.presentation.battle.model.BattlePredictionUiModel.Companion.NO_EFFECT_VALUE

data class BattlePredictionUiModel(val power: String, val accuracy: String, val multiplier: String, val calculatedResult: String)
data class BattlePredictionUiModel(
val power: String,
val accuracy: String,
val multiplier: String,
val calculatedResult: String,
@ColorRes val colorRes: Int,
) {
companion object {
const val NO_EFFECT_VALUE = "-"
const val DEFAULT_NUMBER_FORMAT = "%.1f"
}
}

fun BattlePrediction.toUi(format: String = "%.1f"): BattlePredictionUiModel =
BattlePredictionUiModel(
power = power.toString(),
accuracy = String.format(format, accuracy),
multiplier = String.format(format, multiplier),
calculatedResult = String.format(format, calculatedResult),
fun BattlePrediction.toUi(format: String = DEFAULT_NUMBER_FORMAT): BattlePredictionUiModel {
val formattedPower = if (power < 0) NO_EFFECT_VALUE else power.toString()
val formattedAccuracy = if (accuracy < 0) NO_EFFECT_VALUE else String.format(format, accuracy)
val formattedMultiplier = if (power < 0) NO_EFFECT_VALUE else String.format(format, multiplier)
val formattedResult =
if (calculatedResult < 0) NO_EFFECT_VALUE else String.format(format, calculatedResult)

val color = selectedColorResource(multiplier)

return BattlePredictionUiModel(
power = formattedPower,
accuracy = formattedAccuracy,
multiplier = formattedMultiplier,
calculatedResult = formattedResult,
colorRes = color,
)
}

private fun selectedColorResource(value: Double): Int =
when {
value < 1.0 -> R.color.poke_grey_60
value in 1.0..2.9 -> R.color.poke_red_20
value >= 3 -> R.color.poke_green_20
else -> R.color.poke_white
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ import poke.rogue.helper.presentation.util.view.LinearSpacingItemDecoration
import poke.rogue.helper.presentation.util.view.dp
import poke.rogue.helper.presentation.util.view.setOnSearchAction

class PokemonSelectionFragment :
ErrorHandleFragment<FragmentPokemonSelectionBinding>(R.layout.fragment_pokemon_selection) {
class PokemonSelectionFragment : ErrorHandleFragment<FragmentPokemonSelectionBinding>(R.layout.fragment_pokemon_selection) {
private val sharedViewModel: BattleSelectionViewModel by activityViewModels()
private val viewModel: PokemonSelectionViewModel by viewModels<PokemonSelectionViewModel> {
PokemonSelectionViewModel.factory(
Expand Down Expand Up @@ -72,7 +71,12 @@ class PokemonSelectionFragment :
private fun initObserver() {
repeatOnStarted {
viewModel.filteredPokemon.collect {
pokemonAdapter.submitList(it)
pokemonAdapter.submitList(it) {
if (sharedViewModel.previousSelection.selectedPokemon() != null) {
val position = it.indexOfFirst { it.isSelected }
binding.rvPokemons.scrollToPosition(position)
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,12 @@ class SkillSelectionFragment :

repeatOnStarted {
viewModel.filteredSkills.collect {
skillAdapter.submitList(it)
skillAdapter.submitList(it) {
if (viewModel.previousSkillId != null) {
val position = it.indexOfFirst { it.isSelected }
binding.rvSkills.scrollToPosition(position)
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class SkillSelectionViewModel(
var previousPokemonDexNumber: Long? = previousSelection?.selectedPokemon?.dexNumber
private set

var previousSkillsId: String? = previousSelection?.selectedSkill?.id
var previousSkillId: String? = previousSelection?.selectedSkill?.id
private set

private val _skillSelectedEvent = MutableSharedFlow<SkillSelectionUiModel>()
Expand Down Expand Up @@ -72,7 +72,7 @@ class SkillSelectionViewModel(
viewModelScope.launch {
_skillSelectedEvent.emit(selected)
}
previousSkillsId = selected.id
previousSkillId = selected.id
}

fun updateSkills(pokemonDexNumber: Long) {
Expand All @@ -89,7 +89,7 @@ class SkillSelectionViewModel(
viewModelScope.launch(errorHandler) {
val availableSkills =
battleRepository.availableSkills(pokemonDexNumber).map { it.toUi() }
_skills.value = availableSkills.toSelectableModelsBy { it.id == previousSkillsId }
_skills.value = availableSkills.toSelectableModelsBy { it.id == previousSkillId }
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class RemoteBattleDataSource(
}
.getOrThrow()
.map { it.toData() }
.filter { it.power > 0 }

suspend fun calculatedBattlePrediction(
weatherId: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import poke.rogue.helper.data.model.Weather
class DefaultBattleRepository(private val remoteBattleDataSource: RemoteBattleDataSource) : BattleRepository {
override suspend fun weathers(): List<Weather> = remoteBattleDataSource.weathers()

override suspend fun availableSkills(dexNumber: Long): List<BattleSkill> = remoteBattleDataSource.availableSkills(dexNumber)
override suspend fun availableSkills(dexNumber: Long): List<BattleSkill> = remoteBattleDataSource.availableSkills(dexNumber).distinct()

override suspend fun calculatedBattlePrediction(
weatherId: String,
Expand Down

0 comments on commit 854f204

Please sign in to comment.