Skip to content

Commit

Permalink
Allow edition on all monsters (#293)
Browse files Browse the repository at this point in the history
* Allow edition on all monsters

* Remove change measurements options
  • Loading branch information
alexandregpereira authored Jul 12, 2024
1 parent 9883d8c commit 0c58b20
Show file tree
Hide file tree
Showing 33 changed files with 279 additions and 276 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ internal fun AppBottomNavigationTransition(
contentPadding = contentPadding,
)
BottomBarItemIcon.SETTINGS -> SettingsFeature(
versionName = "VersionName",
versionName = "",
contentPadding = contentPadding,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ internal class MonsterDaoImpl(
.map { it.toLocalEntity() }
}

override suspend fun getMonstersPreviewsEdited(): List<MonsterEntity> {
return withContext(dispatcher) {
monsterQueries.getMonstersEdited()
.executeAsList()
.map { it.toLocalEntity() }
}
}

override suspend fun getMonsters(): List<MonsterCompleteEntity> = withContext(dispatcher) {
monsterQueries.getMonsters().executeAsList().queryMonsterCompleteEntities()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ internal fun MonsterEntity.toDatabaseEntity(): MonsterDatabaseEntity {
senses = this.senses,
languages = this.languages,
sourceName = this.sourceName,
isClone = if (this.isClone) 1L else 0L
isClone = when (this.status) {
MonsterEntityStatus.Original -> 0L
MonsterEntityStatus.Clone -> 1L
MonsterEntityStatus.Edited -> 2L
}
)
}

Expand Down Expand Up @@ -318,7 +322,7 @@ internal fun MonsterDatabaseEntity.toLocalEntity(): MonsterEntity {
senses = this.senses,
languages = this.languages,
sourceName = this.sourceName,
isClone = this.isClone == 1L,
status = MonsterEntityStatus.entries[this.isClone.toInt()],
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package br.alexandregpereira.hunter.data.database.dao
import br.alexandregpereira.hunter.data.monster.folder.local.dao.MonsterFolderDao
import br.alexandregpereira.hunter.data.monster.folder.local.entity.MonsterFolderEntity
import br.alexandregpereira.hunter.data.monster.local.entity.MonsterEntity
import br.alexandregpereira.hunter.data.monster.local.entity.MonsterEntityStatus
import br.alexandregpereira.hunter.database.MonsterFolderCompleteEntityView
import br.alexandregpereira.hunter.database.MonsterFolderQueries
import kotlinx.coroutines.CoroutineDispatcher
Expand Down Expand Up @@ -95,7 +96,7 @@ internal class MonsterFolderDaoImpl(
senses = it.senses,
languages = it.languages,
sourceName = it.sourceName,
isClone = it.isClone == 1L,
status = MonsterEntityStatus.entries[it.isClone.toInt()],
)
}.groupBy(keySelector = { it.first }, valueTransform = { it.second })
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ SELECT * FROM MonsterEntity WHERE `index` == ?;

getMonsters:
SELECT * FROM MonsterEntity;

getMonstersEdited:
SELECT * FROM MonsterEntity WHERE isClone == 2;
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ val monsterDomainModule = module {
factory { SaveCompendiumScrollItemPositionUseCase(get()) }
factory { SaveMeasurementUnitUseCase(get(), get()) }
factory { SaveMonstersUseCase(get(), get(), get()) }
factory { SyncMonstersUseCase(get(), get(), get(), get(), get(), get()) }
factory { SyncMonstersUseCase(get(), get(), get(), get(), get(), get(), get()) }
factory { GetRemoteMonstersBySourceUseCase(get(), get()) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,8 @@
package br.alexandregpereira.hunter.domain.model

import br.alexandregpereira.hunter.domain.locale.formatToNumber
import br.alexandregpereira.hunter.domain.monster.spell.model.SchoolOfMagic
import br.alexandregpereira.hunter.domain.monster.spell.model.SpellPreview
import br.alexandregpereira.hunter.domain.monster.spell.model.SpellUsage
import br.alexandregpereira.hunter.domain.monster.spell.model.Spellcasting
import br.alexandregpereira.hunter.domain.monster.spell.model.SpellcastingType
import kotlin.native.ObjCName

@ObjCName(name = "Monster", exact = true)
data class Monster(
val index: String,
val name: String = "",
Expand Down Expand Up @@ -54,14 +48,18 @@ data class Monster(
val reactions: List<AbilityDescription> = emptyList(),
val spellcastings: List<Spellcasting> = emptyList(),
val lore: String? = null,
val isClone: Boolean = false,
val status: MonsterStatus = MonsterStatus.Original,
) {

val xp: Int = challengeRatingToXp()

val challengeRatingFormatted: String = challengeRating.getChallengeRatingFormatted()
}

enum class MonsterStatus {
Original, Edited, Clone
}

private fun Float.getChallengeRatingFormatted(): String {
return if (this < 1) {
val value = 1 / this
Expand Down Expand Up @@ -136,160 +134,3 @@ fun Monster.xpFormatted(): String {

return "$xpString XP"
}

fun getFakeMonster(): Monster {
return Monster(
index = "1",
name = "Monster Name",
group = "Group",
stats = Stats(
armorClass = 10,
hitPoints = 10,
hitDice = "1d10",
),
speed = Speed(
hover = false,
values = listOf(
SpeedValue(
type = SpeedType.WALK,
valueFormatted = "10 ft.",
),
SpeedValue(
type = SpeedType.SWIM,
valueFormatted = "10 ft.",
),
),
),
abilityScores = AbilityScoreType.entries.map {
AbilityScore(
type = it,
value = 10,
modifier = 0,
)
},
specialAbilities = listOf(
AbilityDescription(
name = "Special Ability Name",
description = "Special Ability Description",
)
),
actions = listOf(
Action(
id = "id",
damageDices = listOf(
DamageDice(
dice = "1d6",
damage = Damage(index = "accumsan", type = DamageType.ACID, name = "Fran Case"),
)
),
attackBonus = 2,
abilityDescription = AbilityDescription(
name = "Ignacio Allen",
description = "percipit"
)
)
),
legendaryActions = listOf(
Action(
id = "id2",
damageDices = listOf(
DamageDice(
dice = "1d6",
damage = Damage(index = "accumsan", type = DamageType.ACID, name = "Fran Case"),
)
),
attackBonus = 2,
abilityDescription = AbilityDescription(
name = "Ignacio Allen",
description = "percipit"
)
)
),
reactions = listOf(
AbilityDescription(
name = "Reaction Name",
description = "Reaction Description",
)
),
spellcastings = listOf(
Spellcasting(
description = "latine",
type = SpellcastingType.SPELLCASTER,
usages = listOf(
SpellUsage(
group = "group",
spells = listOf(
SpellPreview(
index = "index",
name = "name",
level = 1,
school = SchoolOfMagic.ABJURATION,
)
)
)
)
)
),
type = MonsterType.ABERRATION,
challengeRating = 10f,
imageData = MonsterImageData(
url = "http://www.bing.com/search?q=neglegentur",
backgroundColor = Color(
light = "adipisci",
dark = "libero"
),
isHorizontal = false
),
subtype = "subtype",
subtitle = "curae",
size = "mazim",
alignment = "conubia",
senses = listOf("senses"),
languages = "epicuri",
sourceName = "Domingo Woods",
savingThrows = listOf(
SavingThrow(
index = "index",
type = AbilityScoreType.CHARISMA,
modifier = 5
)
),
skills = listOf(
Skill(
index = "index",
name = "name",
modifier = 5
)
),
damageVulnerabilities = listOf(
Damage(
index = "index",
type = DamageType.ACID,
name = "name"
)
),
damageResistances = listOf(
Damage(
index = "index",
type = DamageType.ACID,
name = "name"
)
),
damageImmunities = listOf(
Damage(
index = "index",
type = DamageType.ACID,
name = "name"
)
),
conditionImmunities = listOf(
Condition(
index = "index",
name = "name",
type = ConditionType.BLINDED
)
),
lore = "lore",
isClone = true,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ interface MonsterLocalRepository {

fun saveMonsters(monsters: List<Monster>, isSync: Boolean = false): Flow<Unit>
fun getMonsterPreviews(): Flow<List<Monster>>
fun getMonsterPreviewsEdited(): Flow<List<Monster>>
fun getMonsters(): Flow<List<Monster>>
fun getMonsters(indexes: List<String>): Flow<List<Monster>>
fun getMonster(index: String): Flow<Monster>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import br.alexandregpereira.hunter.domain.model.Monster
import br.alexandregpereira.hunter.domain.model.MonsterImage
import br.alexandregpereira.hunter.domain.model.MonsterSource
import br.alexandregpereira.hunter.domain.repository.MonsterAlternativeSourceRepository
import br.alexandregpereira.hunter.domain.repository.MonsterRepository
import br.alexandregpereira.hunter.domain.repository.MonsterLocalRepository
import br.alexandregpereira.hunter.domain.repository.MonsterRemoteRepository
import br.alexandregpereira.hunter.domain.repository.MonsterSettingsRepository
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
Expand All @@ -30,10 +31,12 @@ import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flatMapMerge
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.reduce
import kotlinx.coroutines.flow.single
import kotlinx.coroutines.flow.zip

class SyncMonstersUseCase internal constructor(
private val repository: MonsterRepository,
private val remoteRepository: MonsterRemoteRepository,
private val localRepository: MonsterLocalRepository,
private val alternativeSourceRepository: MonsterAlternativeSourceRepository,
private val monsterSettingsRepository: MonsterSettingsRepository,
private val getMonsterImages: GetMonsterImagesUseCase,
Expand All @@ -56,8 +59,9 @@ class SyncMonstersUseCase internal constructor(
}
.reduce { accumulator, value -> accumulator + value }
}
.flatMapLatest {
saveMonstersUseCase(monsters = it, isSync = true)
.filterMonstersNotEdited()
.flatMapLatest { monsters ->
saveMonstersUseCase(monsters = monsters, isSync = true)
}.flatMapLatest {
saveCompendiumScrollItemPositionUseCase(position = 0)
}
Expand All @@ -67,11 +71,11 @@ class SyncMonstersUseCase internal constructor(
private fun MonsterSource.getRemoteMonsters(monsterImages: List<MonsterImage>): Flow<List<Monster>> {
return if (this == srdSource) {
monsterSettingsRepository.getLanguage().flatMapLatest {
repository.getRemoteMonsters(lang = it)
remoteRepository.getMonsters(lang = it)
}
} else {
monsterSettingsRepository.getLanguage().flatMapLatest {
repository.getRemoteMonsters(
remoteRepository.getMonsters(
sourceAcronym = this.acronym,
lang = it
).catch { error ->
Expand All @@ -87,4 +91,11 @@ class SyncMonstersUseCase internal constructor(
): Flow<List<Monster>> = map {
it.appendMonsterImages(monsterImages)
}

private fun Flow<List<Monster>>.filterMonstersNotEdited(): Flow<List<Monster>> = map { monsters ->
val monstersEditedIndexes = localRepository.getMonsterPreviewsEdited().single()
.map { it.index }
.toSet()
monsters.filterNot { monstersEditedIndexes.contains(it.index) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ internal class DefaultMonsterLocalRepository(
return localDataSource.getMonsterPreviews().map { it.toDomainMonsterEntity() }
}

override fun getMonsterPreviewsEdited(): Flow<List<Monster>> {
return localDataSource.getMonsterPreviewsEdited().map { it.toDomainMonsterEntity() }
}

override fun getMonsters(): Flow<List<Monster>> {
return localDataSource.getMonsters().map { it.toDomain() }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ internal class DefaultMonsterLocalDataSource(
}
}

override fun getMonsterPreviewsEdited(): Flow<List<MonsterEntity>> = flow {
emit(mutex.withLock { monsterDao.getMonstersPreviewsEdited() })
}

override fun getMonsters(): Flow<List<MonsterCompleteEntity>> = flow {
mutex.withLock {
monsterDao.getMonsters()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import kotlinx.coroutines.flow.Flow
internal interface MonsterLocalDataSource {

fun getMonsterPreviews(): Flow<List<MonsterEntity>>
fun getMonsterPreviewsEdited(): Flow<List<MonsterEntity>>
fun getMonsters(): Flow<List<MonsterCompleteEntity>>
fun getMonsters(indexes: List<String>): Flow<List<MonsterCompleteEntity>>
fun getMonstersByQuery(query: String): Flow<List<MonsterEntity>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ interface MonsterDao {

suspend fun getMonsterPreviews(indexes: List<String>): List<MonsterEntity>

suspend fun getMonstersPreviewsEdited(): List<MonsterEntity>

suspend fun getMonsters(): List<MonsterCompleteEntity>

suspend fun getMonsters(indexes: List<String>): List<MonsterCompleteEntity>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,9 @@ data class MonsterEntity(
val senses: String,
val languages: String,
val sourceName: String,
val isClone: Boolean
val status: MonsterEntityStatus,
)

enum class MonsterEntityStatus {
Original, Clone, Edited
}
Loading

0 comments on commit 0c58b20

Please sign in to comment.