diff --git a/app/src/main/java/com/flyinpancake/dndspells/ProfileActivity.kt b/app/src/main/java/com/flyinpancake/dndspells/ProfileActivity.kt index 1b03d94e6f4b755652ce793f3a5e33351e913e37..a1520c2248c788a8cbf2bdb2748d80fba9bcda44 100644 --- a/app/src/main/java/com/flyinpancake/dndspells/ProfileActivity.kt +++ b/app/src/main/java/com/flyinpancake/dndspells/ProfileActivity.kt @@ -1,17 +1,14 @@ package com.flyinpancake.dndspells -import android.content.Intent -import androidx.appcompat.app.AppCompatActivity import android.os.Bundle +import androidx.appcompat.app.AppCompatActivity import androidx.lifecycle.ViewModelProvider import androidx.recyclerview.widget.LinearLayoutManager import com.flyinpancake.dndspells.adapter.ProfileSpellRecyclerViewAdapter -import com.flyinpancake.dndspells.model.Spell import com.flyinpancake.dndspells.databinding.ActivityProfileBinding import com.flyinpancake.dndspells.databinding.RecyclerviewSpellRowBinding +import com.flyinpancake.dndspells.model.Spell import com.flyinpancake.dndspells.viewmodel.SpellViewModel -import kotlinx.serialization.encodeToString -import kotlinx.serialization.json.Json class ProfileActivity : AppCompatActivity(), ProfileSpellRecyclerViewAdapter.SpellListItemClickListener { companion object { diff --git a/app/src/main/java/com/flyinpancake/dndspells/SelectSpellsActivity.kt b/app/src/main/java/com/flyinpancake/dndspells/SelectSpellsActivity.kt index 8baf96c5ff4d2d2f95708bd7bd983679819b70bb..f79df12dbc50ee8f9dceb0c66071a7a85d8c3e0d 100644 --- a/app/src/main/java/com/flyinpancake/dndspells/SelectSpellsActivity.kt +++ b/app/src/main/java/com/flyinpancake/dndspells/SelectSpellsActivity.kt @@ -7,9 +7,10 @@ import androidx.compose.foundation.layout.* import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.items import androidx.compose.material.* -import androidx.compose.runtime.Composable +import androidx.compose.runtime.* import androidx.compose.runtime.livedata.observeAsState import androidx.compose.ui.Modifier +import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import androidx.lifecycle.ViewModelProvider @@ -27,27 +28,51 @@ class SelectSpellsActivity : ComponentActivity() { super.onCreate(savedInstanceState) setContent { val vmp = ViewModelProvider(this) - val spells = vmp[SpellViewModel::class.java].allSpells.observeAsState().value?: listOf() - val characterName = intent.getStringExtra(KEY_NAME)?: "" - val character = vmp[CharacterViewModel::class.java].get(characterName).observeAsState().value?: DndCharacter() + val spells = + vmp[SpellViewModel::class.java].allSpells.observeAsState().value ?: listOf() + val characterName = intent.getStringExtra(KEY_NAME) ?: "" + val character = + vmp[CharacterViewModel::class.java].get(characterName).observeAsState().value + ?: DndCharacter() MyApp { - SelectSpellsContent(spells = spells, character = character) + SelectSpellsContent( + spells = spells, + character = character, + updateCharacter = { vmp[CharacterViewModel::class.java].update(it) }) } } } } @Composable -fun SelectSpellsContent(spells: List<Spell>, character: DndCharacter) { +fun SelectSpellsContent( + spells: List<Spell>, + character: DndCharacter, + updateCharacter: (DndCharacter) -> Unit = {} +) { Scaffold( topBar = { DndTopBar("${character.name} - select spells") } ) { - LazyColumn{ + LazyColumn { items( items = spells.toList(), - itemContent = { - SpellCardWithCheckBox(spell = it, checked = false, onCheck = {}) - }) + itemContent = { spell -> + var selectedState by remember { mutableStateOf( character.spellList?.contains(spell) ?: false)} + SpellCardWithCheckBox( + spell = spell, + checked = selectedState, + onCheck = { newContains -> + val newSpellList = character.spellList?.toMutableList()?: mutableListOf() + if (newContains) { + newSpellList.add(spell) + } else { + newSpellList.remove(spell) + } + selectedState = !selectedState + character.spellList = newSpellList + updateCharacter(character) + }) + }) } @@ -58,14 +83,24 @@ fun SelectSpellsContent(spells: List<Spell>, character: DndCharacter) { private fun SpellCardWithCheckBox( spell: Spell, checked: Boolean, - onCheck: (Boolean) -> Unit) { - Card ( + onCheck: (Boolean) -> Unit +) { + Card( modifier = Modifier .fillMaxWidth() .padding(horizontal = 25.dp, vertical = 5.dp) + ) { + Row( + Modifier + .fillMaxSize() + .padding(10.dp), + ) { - Row(horizontalArrangement = Arrangement.Start) { - Checkbox(checked = checked, onCheckedChange = {onCheck(it)}) + Checkbox( + checked = checked, + onCheckedChange = { onCheck(it) }, + Modifier.padding(end = 2.dp) + ) SpellContent(spell = spell) } } @@ -73,15 +108,42 @@ private fun SpellCardWithCheckBox( @Composable private fun SpellContent(spell: Spell) { - Column ( - Modifier.fillMaxWidth() - ){ - Row (horizontalArrangement = Arrangement.SpaceBetween) { - Text(text = spell.name, style = MaterialTheme.typography.h6) - Text(text = "${spell.level}", style = MaterialTheme.typography.body1) + val textPadding = Modifier.padding(horizontal = 5.dp) + Column( + Modifier.fillMaxSize(), + verticalArrangement = Arrangement.Center + ) { + Row( + Modifier.fillMaxSize(), + horizontalArrangement = Arrangement.SpaceBetween + ) { + Text( + text = spell.name, + modifier = textPadding, + maxLines = 1, + overflow = TextOverflow.Ellipsis, + style = MaterialTheme.typography.h6 + ) + Text( + text = "Level ${spell.level}", + modifier = textPadding, + maxLines = 1, + style = MaterialTheme.typography.h6 + ) } - Row { - + Row( + Modifier.fillMaxSize(), + horizontalArrangement = Arrangement.SpaceBetween + ) { + Text(text = spell.range, modifier = textPadding, style = MaterialTheme.typography.body1) + Text(text = spell.time, modifier = textPadding, style = MaterialTheme.typography.body1) + Text( + text = spell.components, + style = MaterialTheme.typography.body1, + modifier = textPadding, + maxLines = 1, + overflow = TextOverflow.Ellipsis + ) } } } @@ -94,7 +156,29 @@ private fun SelectableSpellList(spells: List<Spell>, character: DndCharacter) { @Preview(showBackground = true) @Composable fun DefaultPreview() { + + val samplespells = listOf( + Spell( + name = "Power Word Kill", + level = 9, + classes = "Wizard", + components = "V", + desc = "Kil", + duration = "1 action", + range = "240 ft", + ritual = false, + school = "E", + time = "instantanius" + ) + ) + + val sampleCharacter = DndCharacter( + name = "Ba'luk", + level = 4, + dndClass = DndCharacter.DndClass.Druid + ) + DndSpellsTheme { - SelectSpellsContent(spells = listOf(), character = DndCharacter()) + SelectSpellsContent(spells = samplespells, character = sampleCharacter) } } diff --git a/app/src/main/java/com/flyinpancake/dndspells/database/CharacterDao.kt b/app/src/main/java/com/flyinpancake/dndspells/database/CharacterDao.kt index 105da69cd7e4e216449dd3ffe4425897099a957a..5c93c39514e53f0e64bafebdd57449e9bffa50ae 100644 --- a/app/src/main/java/com/flyinpancake/dndspells/database/CharacterDao.kt +++ b/app/src/main/java/com/flyinpancake/dndspells/database/CharacterDao.kt @@ -18,7 +18,7 @@ interface CharacterDao { fun getCharacterLiveDataByName(name: String): LiveData<RoomCharacter> @Update - fun updateCharacter(roomCharacter: RoomCharacter) + fun updateCharacter(roomCharacter: RoomCharacter): Int @Delete fun deleteCharacter( diff --git a/app/src/main/java/com/flyinpancake/dndspells/repository/CharacterRepository.kt b/app/src/main/java/com/flyinpancake/dndspells/repository/CharacterRepository.kt index fa5ab43170036abb98cfc839949e8067750a1ad2..48c7049d9841326eb09cc3670fa9b6a49073238b 100644 --- a/app/src/main/java/com/flyinpancake/dndspells/repository/CharacterRepository.kt +++ b/app/src/main/java/com/flyinpancake/dndspells/repository/CharacterRepository.kt @@ -5,9 +5,7 @@ import androidx.lifecycle.map import com.flyinpancake.dndspells.DndApplication import com.flyinpancake.dndspells.database.CharacterDao import com.flyinpancake.dndspells.database.RoomCharacter -import com.flyinpancake.dndspells.database.SpellDao import com.flyinpancake.dndspells.model.DndCharacter -import com.flyinpancake.dndspells.model.Spell import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext @@ -33,6 +31,11 @@ class CharacterRepository( fun get(name: String): LiveData<DndCharacter> { return characterDao.getCharacterLiveDataByName(name).map { it.toDomainModel() } } + + suspend fun update(character: DndCharacter) = withContext(Dispatchers.IO) { + val roomCharacter = character.toRoomDomain() + characterDao.updateCharacter(roomCharacter) + } } private fun DndCharacter.toRoomDomain(): RoomCharacter { diff --git a/app/src/main/java/com/flyinpancake/dndspells/viewmodel/CharacterViewModel.kt b/app/src/main/java/com/flyinpancake/dndspells/viewmodel/CharacterViewModel.kt index c2244d53b084186e4cf02460c4c105a08290bf89..009e4c40145b425a19ab28abf57c78109a3278e8 100644 --- a/app/src/main/java/com/flyinpancake/dndspells/viewmodel/CharacterViewModel.kt +++ b/app/src/main/java/com/flyinpancake/dndspells/viewmodel/CharacterViewModel.kt @@ -30,4 +30,8 @@ class CharacterViewModel: ViewModel() { fun get(name: String): LiveData<DndCharacter> { return repo.get(name) } + + fun update(character: DndCharacter) = viewModelScope.launch { + repo.update(character) + } } \ No newline at end of file