Skip to content

Commit

Permalink
Merge pull request #6 from sh1mj1/ui/deck-list
Browse files Browse the repository at this point in the history
UI/deck list add DeckItem, and refactor separating DeckItemDetail composeable
  • Loading branch information
sh1mj1 authored Jan 22, 2025
2 parents f8a91fd + 2369589 commit adf903a
Show file tree
Hide file tree
Showing 9 changed files with 255 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,10 @@ class DeckItemKtTest {
var expanded by remember { mutableStateOf(false) }

DeckItem(
deckId = "",
information = fakeDeckInformation,
onCardClick = {},
rank = 1,
deckName = "deckName",
winRate = "winRate",
share = "share",
pokemonImageUrls = emptyList(),
expanded = expanded,
onExpandClick = { expanded = !expanded },
cost = 1990,
pokemonTypes = emptyList(),
onExpandedChange = { expanded = !expanded },
)
}
val notExpandedIcon = composeTestRule.onNodeWithContentDescription("not expanded icon")
Expand All @@ -51,17 +44,10 @@ class DeckItemKtTest {
var expanded by remember { mutableStateOf(true) }

DeckItem(
deckId = "",
rank = 1,
deckName = "deckName",
winRate = "winRate",
share = "share",
pokemonImageUrls = emptyList(),
information = fakeDeckInformation,
expanded = expanded,
onExpandClick = { expanded = !expanded },
onExpandedChange = { expanded = !expanded },
onCardClick = {},
cost = 1990,
pokemonTypes = emptyList(),
)
}
val notExpandedIcon = composeTestRule.onNodeWithContentDescription("not expanded icon")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package tcg.pocket.dex.tierdecks

import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onAllNodesWithContentDescription
import androidx.compose.ui.test.onFirst
import androidx.compose.ui.test.onNodeWithContentDescription
import androidx.compose.ui.test.performClick
import org.junit.Rule
import org.junit.Test

class DeckListKtTest {
@get:Rule
val composeTestRule = createComposeRule()

private val sampleDeckItemsState =
fakeDecksInformation.map {
DeckItemState(it)
}

@Test
fun deckList_expandItem() {
composeTestRule.setContent {
DeckList(
deckItemsState = sampleDeckItemsState,
onExpandDeck = { itemState, expanded -> itemState.toggleExpanded() },
onDeckItemClick = {},
)
}

composeTestRule.onAllNodesWithContentDescription("not expanded icon").onFirst().performClick()

composeTestRule.onNodeWithContentDescription("expanded icon").assertIsDisplayed()
}
}
55 changes: 4 additions & 51 deletions app/src/main/java/tcg/pocket/dex/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,68 +5,21 @@ import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.material3.Surface
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import tcg.pocket.dex.tierdecks.DeckItem
import tcg.pocket.dex.tierdecks.fakePokemonTypeChipDataset
import tcg.pocket.dex.tierdecks.fakeSimpleUrl
import tcg.pocket.dex.tierdecks.TierDecksScreen
import tcg.pocket.dex.ui.theme.TcgPocketDexTheme

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
var expanded by rememberSaveable { mutableStateOf(true) }
TcgPocketDexTheme {
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
DeckItem(
deckId = "deckId",
rank = 1,
deckName = "Mewtwo ex Gardevoir",
winRate = "50.64%",
share = "17.57%",
pokemonImageUrls =
listOf(
fakeSimpleUrl(150),
fakeSimpleUrl(282),
),
cost = 1990,
pokemonTypes = fakePokemonTypeChipDataset,
expanded = expanded,
onExpandClick = { expanded = !expanded },
onCardClick = {},
modifier = Modifier.padding(innerPadding),
)
Surface(modifier = Modifier.fillMaxSize()) {
TierDecksScreen()
}
}
}
}
}

@Composable
fun Greeting(
name: String,
modifier: Modifier = Modifier,
) {
Text(
text = "Hello $name!",
modifier = modifier,
)
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
TcgPocketDexTheme {
Greeting("Android")
}
}
21 changes: 21 additions & 0 deletions app/src/main/java/tcg/pocket/dex/tierdecks/DeckInformation.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package tcg.pocket.dex.tierdecks

data class DeckInformation(
val simple: DeckSimpleInformation,
val detail: DeckDetailInformation,
)

data class DeckSimpleInformation(
val deckId: String,
val representativePokemonImageUrls: List<String>,
val rank: Int,
val deckName: String,
val winRate: String,
val share: String,
)

data class DeckDetailInformation(
val cost: Int,
val pokemonTypes: List<PokemonTypeChipData>,
val description: String,
)
58 changes: 23 additions & 35 deletions app/src/main/java/tcg/pocket/dex/tierdecks/DeckItem.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tcg.pocket.dex.tierdecks

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
Expand All @@ -21,6 +22,10 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
Expand All @@ -34,25 +39,18 @@ import tcg.pocket.dex.ui.theme.TcgPocketDexTheme

@Composable
fun DeckItem(
deckId: String,
rank: Int,
deckName: String,
winRate: String,
share: String,
pokemonImageUrls: List<String>,
cost: Int,
pokemonTypes: List<PokemonTypeChipData>,
information: DeckInformation,
expanded: Boolean = false,
onExpandClick: () -> Unit,
onExpandedChange: (Boolean) -> Unit,
onCardClick: (String) -> Unit,
modifier: Modifier = Modifier,
) {
Card(
modifier =
modifier
.padding(16.dp)
.fillMaxWidth()
.clickable { onCardClick(deckId) },
.padding(4.dp)
.clickable { onCardClick(information.simple.deckId) },
shape = RoundedCornerShape(8.dp),
) {
Column(
Expand All @@ -66,25 +64,25 @@ fun DeckItem(
verticalAlignment = Alignment.CenterVertically,
) {
RankText(
rank = rank,
rank = information.simple.rank,
modifier = Modifier.padding(end = 8.dp),
)

PokemonImageList(
pokemonImageUrls = pokemonImageUrls,
pokemonImageUrls = information.simple.representativePokemonImageUrls,
modifier = Modifier,
)

DeckItemInfoContent(
deckName = deckName,
winRate = winRate,
share = share,
deckName = information.simple.deckName,
winRate = information.simple.winRate,
share = information.simple.share,
modifier = Modifier.weight(1f),
)

IconButton(
modifier = Modifier.padding(0.dp),
onClick = onExpandClick,
onClick = { onExpandedChange(expanded) },
) {
if (expanded) {
Icon(
Expand All @@ -100,10 +98,10 @@ fun DeckItem(
}
}

if (expanded) {
AnimatedVisibility(expanded) {
DeckItemDetail(
cost = cost,
pokemonTypes = pokemonTypes,
cost = information.detail.cost,
pokemonTypes = information.detail.pokemonTypes,
modifier = Modifier,
)
}
Expand All @@ -118,7 +116,7 @@ private fun DeckItemDetail(
modifier: Modifier = Modifier,
) {
Column(
modifier = modifier.fillMaxWidth().padding(8.dp),
modifier = modifier.fillMaxWidth(),
) {
Row(
modifier = Modifier.fillMaxWidth(),
Expand Down Expand Up @@ -258,25 +256,15 @@ private fun DeckItemInfoContentPreview() {
@Preview(showBackground = true)
@Composable
private fun DeckItemPreview(
@PreviewParameter(DeckCardPreviewParameters::class) expanded: Boolean,
@PreviewParameter(DeckCardPreviewParameters::class) initialExpanded: Boolean,
) {
TcgPocketDexTheme {
var expanded by remember { mutableStateOf(initialExpanded) }
DeckItem(
deckId = "",
rank = 1,
deckName = "Mewtwo ex Gardevoir",
winRate = "50.67%",
share = "17.57%",
pokemonImageUrls =
listOf(
fakeSimpleUrl(150),
fakeSimpleUrl(282),
),
information = fakeDeckInformation,
expanded = expanded,
onExpandClick = {},
onExpandedChange = { expanded = !expanded },
onCardClick = {},
cost = 0,
pokemonTypes = fakePokemonTypeChipDataset,
)
}
}
Expand Down
17 changes: 17 additions & 0 deletions app/src/main/java/tcg/pocket/dex/tierdecks/DeckItemState.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package tcg.pocket.dex.tierdecks

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue

class DeckItemState(
val content: DeckInformation,
initialExpanded: Boolean = false,
) {
var expanded: Boolean by mutableStateOf(initialExpanded)
private set

fun toggleExpanded() {
expanded = !expanded
}
}
51 changes: 51 additions & 0 deletions app/src/main/java/tcg/pocket/dex/tierdecks/DeckList.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package tcg.pocket.dex.tierdecks

import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import tcg.pocket.dex.ui.theme.TcgPocketDexTheme

@Composable
fun DeckList(
deckItemsState: List<DeckItemState>,
onExpandDeck: (DeckItemState, Boolean) -> Unit,
onDeckItemClick: (String) -> Unit,
modifier: Modifier = Modifier,
) {
LazyColumn(
modifier =
modifier
.fillMaxWidth()
.padding(8.dp),
) {
items(
items = deckItemsState,
key = { deckItem -> deckItem.content.simple.deckId },
) { deckItemState ->

DeckItem(
information = deckItemState.content,
expanded = deckItemState.expanded,
onExpandedChange = { expanded -> onExpandDeck(deckItemState, expanded) },
onCardClick = onDeckItemClick,
)
}
}
}

@Preview
@Composable
private fun DeckListPreview() {
TcgPocketDexTheme {
DeckList(
deckItemsState = fakeDecksInformation.map(::DeckItemState),
onDeckItemClick = { },
onExpandDeck = { deckItemStata, _ -> },
)
}
}
Loading

0 comments on commit adf903a

Please sign in to comment.