Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/search tag #41

Merged
merged 2 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.github.swent.echo.compose.components

import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.github.swent.echo.data.model.Tag
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class MainScreenTagsTest {
@get:Rule val composeTestRule = createComposeRule()
private var tagClicked: Tag? = null

@Before
fun setUp() {
tagClicked = null
}

@Test
fun showTagText() {
val tagList = listOf(Tag("Tag1", "Sports"), Tag("Tag2", "Music"))
composeTestRule.setContent { TagUI(tags = tagList, null) {} }
composeTestRule.onNodeWithText("Sports").assertExists()
composeTestRule.onNodeWithText("Music").assertExists()
}

@Test
fun clickTag() {
val tagList = listOf(Tag("Tag1", "Sports"), Tag("Tag2", "Music"))
composeTestRule.setContent { TagUI(tags = tagList, null) { tagClicked = it } }
composeTestRule.onNodeWithText("Sports").performClick()
assert(tagClicked != null && tagClicked!!.name == "Sports")
composeTestRule.onNodeWithText("Music").performClick()
assert(tagClicked != null && tagClicked!!.name == "Music")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.github.swent.echo

@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.FUNCTION)
internal annotation class ExcludeFromJacocoGeneratedReport
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
@file:JvmName("MainScreenTagsFilterKt")

package com.github.swent.echo.compose.components

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.FilledTonalButton
import androidx.compose.material3.MaterialTheme
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.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.github.swent.echo.ExcludeFromJacocoGeneratedReport
import com.github.swent.echo.data.model.Tag

/*
* TagUI is a composable that displays a tag.
* tags: List of tags to display, fetch the most recent or the most common tags from the repository.
*/

@Composable
fun TagUI(tags: List<Tag>, selectedTagId: String?, onTagClick: (Tag) -> Unit) {
val lazyListState = rememberLazyListState()
var selectTag by remember { mutableStateOf(selectedTagId) }

Column(modifier = Modifier) {
LazyRow(
modifier = Modifier.padding(8.dp),
state = lazyListState,
horizontalArrangement = Arrangement.spacedBy(8.dp)
) {
items(tags) { tag ->
TagItem(
tag = tag,
isSelected = tag.tagId == selectTag,
onClick = {
selectTag = tag.tagId
onTagClick(tag)
}
)
}
}
}
}

/*
* TagItem is a composable that highlights the selected tag.
*/
@Composable
fun TagItem(tag: Tag, isSelected: Boolean, onClick: () -> Unit) {
val colors = MaterialTheme.colorScheme
val backgroundColor = if (isSelected) colors.primary else colors.inverseOnSurface
val contentColor = if (isSelected) colors.onPrimary else colors.onSurface

FilledTonalButton(
onClick = onClick,
contentPadding = PaddingValues(0.dp),
colors =
ButtonDefaults.buttonColors(
containerColor = backgroundColor,
contentColor = contentColor,
)
) {
Text(
text = tag.name,
color = contentColor,
fontSize = 16.sp,
)
}
}
// excluding this preview function
@ExcludeFromJacocoGeneratedReport
@Preview
@Composable
fun PreviewTagUI() {
val tags =
listOf(
Tag("SP", "Sports"),
Tag("MU", "Music"),
Tag("SO", "Social"),
Tag("AR", "Arts"),
Tag("GA", "Games"),
Tag("PO", "Politics"),
Tag("TE", "Tech")
)
MaterialTheme { TagUI(tags = tags, null) {} }
}