Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.1.0/AN-FEAT] 배틀 선택 / 결과 UI 개선 #316

Merged
merged 7 commits into from
Sep 14, 2024
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)
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 섬세한데요~
submitList 에 람다로 바로 콜백을 지정할 수 있군요~

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

헐 대박.. 제 코드 수정하러 갑니다 ㅋㅋ

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 이게 되네 ㅋㅋㅋ

}
}
}

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
Loading