Skip to content
Snippets Groups Projects
Commit b2a4db34 authored by flyinpancake's avatar flyinpancake :8ball:
Browse files

Veri gud

parent 7bfcda13
No related branches found
No related tags found
No related merge requests found
package com.flyinpancake.dndspells package com.flyinpancake.dndspells
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import com.flyinpancake.dndspells.adapter.ProfileSpellRecyclerViewAdapter import com.flyinpancake.dndspells.adapter.ProfileSpellRecyclerViewAdapter
import com.flyinpancake.dndspells.model.Spell
import com.flyinpancake.dndspells.databinding.ActivityProfileBinding import com.flyinpancake.dndspells.databinding.ActivityProfileBinding
import com.flyinpancake.dndspells.databinding.RecyclerviewSpellRowBinding import com.flyinpancake.dndspells.databinding.RecyclerviewSpellRowBinding
import com.flyinpancake.dndspells.model.Spell
import com.flyinpancake.dndspells.viewmodel.SpellViewModel import com.flyinpancake.dndspells.viewmodel.SpellViewModel
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
class ProfileActivity : AppCompatActivity(), ProfileSpellRecyclerViewAdapter.SpellListItemClickListener { class ProfileActivity : AppCompatActivity(), ProfileSpellRecyclerViewAdapter.SpellListItemClickListener {
companion object { companion object {
......
...@@ -7,9 +7,10 @@ import androidx.compose.foundation.layout.* ...@@ -7,9 +7,10 @@ import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items import androidx.compose.foundation.lazy.items
import androidx.compose.material.* import androidx.compose.material.*
import androidx.compose.runtime.Composable import androidx.compose.runtime.*
import androidx.compose.runtime.livedata.observeAsState import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProvider
...@@ -27,26 +28,50 @@ class SelectSpellsActivity : ComponentActivity() { ...@@ -27,26 +28,50 @@ class SelectSpellsActivity : ComponentActivity() {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContent { setContent {
val vmp = ViewModelProvider(this) val vmp = ViewModelProvider(this)
val spells = vmp[SpellViewModel::class.java].allSpells.observeAsState().value?: listOf() val spells =
vmp[SpellViewModel::class.java].allSpells.observeAsState().value ?: listOf()
val characterName = intent.getStringExtra(KEY_NAME) ?: "" val characterName = intent.getStringExtra(KEY_NAME) ?: ""
val character = vmp[CharacterViewModel::class.java].get(characterName).observeAsState().value?: DndCharacter() val character =
vmp[CharacterViewModel::class.java].get(characterName).observeAsState().value
?: DndCharacter()
MyApp { MyApp {
SelectSpellsContent(spells = spells, character = character) SelectSpellsContent(
spells = spells,
character = character,
updateCharacter = { vmp[CharacterViewModel::class.java].update(it) })
} }
} }
} }
} }
@Composable @Composable
fun SelectSpellsContent(spells: List<Spell>, character: DndCharacter) { fun SelectSpellsContent(
spells: List<Spell>,
character: DndCharacter,
updateCharacter: (DndCharacter) -> Unit = {}
) {
Scaffold( Scaffold(
topBar = { DndTopBar("${character.name} - select spells") } topBar = { DndTopBar("${character.name} - select spells") }
) { ) {
LazyColumn { LazyColumn {
items( items(
items = spells.toList(), items = spells.toList(),
itemContent = { itemContent = { spell ->
SpellCardWithCheckBox(spell = it, checked = false, onCheck = {}) 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) { ...@@ -58,14 +83,24 @@ fun SelectSpellsContent(spells: List<Spell>, character: DndCharacter) {
private fun SpellCardWithCheckBox( private fun SpellCardWithCheckBox(
spell: Spell, spell: Spell,
checked: Boolean, checked: Boolean,
onCheck: (Boolean) -> Unit) { onCheck: (Boolean) -> Unit
) {
Card( Card(
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.padding(horizontal = 25.dp, vertical = 5.dp) .padding(horizontal = 25.dp, vertical = 5.dp)
) { ) {
Row(horizontalArrangement = Arrangement.Start) { Row(
Checkbox(checked = checked, onCheckedChange = {onCheck(it)}) Modifier
.fillMaxSize()
.padding(10.dp),
) {
Checkbox(
checked = checked,
onCheckedChange = { onCheck(it) },
Modifier.padding(end = 2.dp)
)
SpellContent(spell = spell) SpellContent(spell = spell)
} }
} }
...@@ -73,15 +108,42 @@ private fun SpellCardWithCheckBox( ...@@ -73,15 +108,42 @@ private fun SpellCardWithCheckBox(
@Composable @Composable
private fun SpellContent(spell: Spell) { private fun SpellContent(spell: Spell) {
val textPadding = Modifier.padding(horizontal = 5.dp)
Column( Column(
Modifier.fillMaxWidth() Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Center
) { ) {
Row (horizontalArrangement = Arrangement.SpaceBetween) { Row(
Text(text = spell.name, style = MaterialTheme.typography.h6) Modifier.fillMaxSize(),
Text(text = "${spell.level}", style = MaterialTheme.typography.body1) horizontalArrangement = Arrangement.SpaceBetween
} ) {
Row { 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(
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) { ...@@ -94,7 +156,29 @@ private fun SelectableSpellList(spells: List<Spell>, character: DndCharacter) {
@Preview(showBackground = true) @Preview(showBackground = true)
@Composable @Composable
fun DefaultPreview() { 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 { DndSpellsTheme {
SelectSpellsContent(spells = listOf(), character = DndCharacter()) SelectSpellsContent(spells = samplespells, character = sampleCharacter)
} }
} }
...@@ -18,7 +18,7 @@ interface CharacterDao { ...@@ -18,7 +18,7 @@ interface CharacterDao {
fun getCharacterLiveDataByName(name: String): LiveData<RoomCharacter> fun getCharacterLiveDataByName(name: String): LiveData<RoomCharacter>
@Update @Update
fun updateCharacter(roomCharacter: RoomCharacter) fun updateCharacter(roomCharacter: RoomCharacter): Int
@Delete @Delete
fun deleteCharacter( fun deleteCharacter(
......
...@@ -5,9 +5,7 @@ import androidx.lifecycle.map ...@@ -5,9 +5,7 @@ import androidx.lifecycle.map
import com.flyinpancake.dndspells.DndApplication import com.flyinpancake.dndspells.DndApplication
import com.flyinpancake.dndspells.database.CharacterDao import com.flyinpancake.dndspells.database.CharacterDao
import com.flyinpancake.dndspells.database.RoomCharacter import com.flyinpancake.dndspells.database.RoomCharacter
import com.flyinpancake.dndspells.database.SpellDao
import com.flyinpancake.dndspells.model.DndCharacter import com.flyinpancake.dndspells.model.DndCharacter
import com.flyinpancake.dndspells.model.Spell
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
...@@ -33,6 +31,11 @@ class CharacterRepository( ...@@ -33,6 +31,11 @@ class CharacterRepository(
fun get(name: String): LiveData<DndCharacter> { fun get(name: String): LiveData<DndCharacter> {
return characterDao.getCharacterLiveDataByName(name).map { it.toDomainModel() } 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 { private fun DndCharacter.toRoomDomain(): RoomCharacter {
......
...@@ -30,4 +30,8 @@ class CharacterViewModel: ViewModel() { ...@@ -30,4 +30,8 @@ class CharacterViewModel: ViewModel() {
fun get(name: String): LiveData<DndCharacter> { fun get(name: String): LiveData<DndCharacter> {
return repo.get(name) return repo.get(name)
} }
fun update(character: DndCharacter) = viewModelScope.launch {
repo.update(character)
}
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment