Code improvement in StatsViewModel

This commit is contained in:
Lucas Lima 2020-12-30 17:14:23 -03:00 committed by Lucas Nunes
parent e6bf0b50e8
commit b4753c0cb0

View file

@ -3,6 +3,7 @@ package dev.lucasnlm.antimine.stats.viewmodel
import dev.lucasnlm.antimine.R import dev.lucasnlm.antimine.R
import dev.lucasnlm.antimine.common.level.database.models.Stats import dev.lucasnlm.antimine.common.level.database.models.Stats
import dev.lucasnlm.antimine.common.level.models.Difficulty import dev.lucasnlm.antimine.common.level.models.Difficulty
import dev.lucasnlm.antimine.common.level.models.Minefield
import dev.lucasnlm.antimine.common.level.repository.IDimensionRepository import dev.lucasnlm.antimine.common.level.repository.IDimensionRepository
import dev.lucasnlm.antimine.common.level.repository.IMinefieldRepository import dev.lucasnlm.antimine.common.level.repository.IMinefieldRepository
import dev.lucasnlm.antimine.common.level.repository.IStatsRepository import dev.lucasnlm.antimine.common.level.repository.IStatsRepository
@ -27,42 +28,33 @@ class StatsViewModel(
preferenceRepository, preferenceRepository,
) )
return listOf( return with(stats) {
// General listOf(
stats.fold().copy(title = R.string.general), // General
fold().copy(title = R.string.general),
// Standard // Standard
stats.filter { filterStandard(standardSize).fold().copy(title = R.string.standard),
it.width == standardSize.width && it.height == standardSize.height
}.fold().copy(title = R.string.standard),
// Expert // Expert
stats.filter { filter(::isExpert).fold().copy(title = R.string.expert),
it.mines == 99 && it.width == 24 && it.height == 24
}.fold().copy(title = R.string.expert),
// Intermediate // Intermediate
stats.filter { filter(::isIntermediate).fold().copy(title = R.string.intermediate),
it.mines == 40 && it.width == 16 && it.height == 16
}.fold().copy(title = R.string.intermediate),
// Beginner // Beginner
stats.filter { filter(::isBeginner).fold().copy(title = R.string.beginner),
it.mines == 10 && it.width == 9 && it.height == 9
}.fold().copy(title = R.string.beginner),
// Custom // Custom
stats.filterNot { filterNot(::isExpert)
it.mines == 99 && it.width == 24 && it.height == 24 .filterNot(::isIntermediate)
}.filterNot { .filterNot(::isBeginner)
it.mines == 40 && it.width == 16 && it.height == 16 .filterNotStandard(standardSize)
}.filterNot { .fold()
it.mines == 10 && it.width == 9 && it.height == 9 .copy(title = R.string.custom),
}.filterNot { ).filter {
it.width == standardSize.width && it.height == standardSize.height it.totalGames > 0
}.fold().copy(title = R.string.custom), }
).filter {
it.totalGames > 0
} }
} }
@ -144,4 +136,26 @@ class StatsViewModel(
} }
} }
} }
companion object {
private fun isExpert(stats: Stats): Boolean {
return stats.mines == 99 && stats.width == 24 && stats.height == 24
}
private fun isIntermediate(stats: Stats): Boolean {
return stats.mines == 40 && stats.width == 16 && stats.height == 16
}
private fun isBeginner(stats: Stats): Boolean {
return stats.mines == 10 && stats.width == 9 && stats.height == 9
}
private fun List<Stats>.filterStandard(standardSize: Minefield) = filter {
it.width == standardSize.width && it.height == standardSize.height
}
private fun List<Stats>.filterNotStandard(standardSize: Minefield) = filterNot {
it.width == standardSize.width && it.height == standardSize.height
}
}
} }