Skip to content
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
Expand Up @@ -342,7 +342,7 @@ fun HomeScreen(

if (isTagsSelectionActive) {
TagSelectionBottomSheet(
tags = viewModel.allTags.collectAsStateWithLifecycle().value,
tagsWithCount = viewModel.allTagsWithCount.collectAsStateWithLifecycle().value,
selectedTag = selectedTag.value,
dismissBottomSheet = {
isTagsSelectionActive = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import com.yogeshpaliyal.deepr.GetAllTagsWithCount
import com.yogeshpaliyal.deepr.R
import com.yogeshpaliyal.deepr.Tags
import com.yogeshpaliyal.deepr.ui.components.ClearInputIconButton
Expand All @@ -53,7 +54,7 @@ import compose.icons.tablericons.Trash
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun TagSelectionBottomSheet(
tags: List<Tags>,
tagsWithCount: List<GetAllTagsWithCount>,
selectedTag: Tags?,
dismissBottomSheet: () -> Unit,
setTagFilter: (Tags?) -> Unit,
Expand All @@ -63,8 +64,8 @@ fun TagSelectionBottomSheet(
modifier: Modifier = Modifier,
) {
val modalBottomSheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true)
var isTagEditEnable by remember { mutableStateOf<Tags?>(null) }
var isTagDeleteEnable by remember { mutableStateOf<Tags?>(null) }
var isTagEditEnable by remember { mutableStateOf<GetAllTagsWithCount?>(null) }
var isTagDeleteEnable by remember { mutableStateOf<GetAllTagsWithCount?>(null) }
var tagEditError by remember { mutableStateOf<String?>(null) }
val context = LocalContext.current
var newTagName by remember { mutableStateOf("") }
Expand Down Expand Up @@ -108,7 +109,7 @@ fun TagSelectionBottomSheet(
},
confirmButton = {
Button(onClick = {
val result = editTag(tag)
val result = editTag(Tags(tag.id, tag.name))
if (result.isFailure) {
val exception = result.exceptionOrNull()
when (exception) {
Expand Down Expand Up @@ -155,7 +156,7 @@ fun TagSelectionBottomSheet(
},
confirmButton = {
Button(onClick = {
val result = deleteTag(tag)
val result = deleteTag(Tags(tag.id, tag.name))
if (result.isFailure) {
Toast
.makeText(
Expand Down Expand Up @@ -213,7 +214,7 @@ fun TagSelectionBottomSheet(
onClick = {
val trimmedTagName = newTagName.trim()
if (trimmedTagName.isNotBlank()) {
val existingTag = tags.find { it.name.equals(trimmedTagName, ignoreCase = true) }
val existingTag = tagsWithCount.find { it.name.equals(trimmedTagName, ignoreCase = true) }

if (existingTag != null) {
Toast
Expand Down Expand Up @@ -263,14 +264,14 @@ fun TagSelectionBottomSheet(
},
)
}
items(tags) { tag ->
items(tagsWithCount) { tag ->
ListItem(
modifier =
Modifier.clickable {
setTagFilter(tag)
setTagFilter(Tags(tag.id, tag.name))
dismissBottomSheet()
},
headlineContent = { Text(tag.name) },
headlineContent = { Text("${tag.name} (${tag.linkCount})") },
trailingContent = {
Row {
IconButton(onClick = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import app.cash.sqldelight.coroutines.asFlow
import app.cash.sqldelight.coroutines.mapToList
import app.cash.sqldelight.coroutines.mapToOneOrNull
import com.yogeshpaliyal.deepr.DeeprQueries
import com.yogeshpaliyal.deepr.GetAllTagsWithCount
import com.yogeshpaliyal.deepr.GetLinksAndTags
import com.yogeshpaliyal.deepr.Tags
import com.yogeshpaliyal.deepr.backup.ExportRepository
Expand Down Expand Up @@ -83,6 +84,14 @@ class AccountViewModel(
viewModelScope.coroutineContext,
).stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), listOf())

val allTagsWithCount: StateFlow<List<GetAllTagsWithCount>> =
deeprQueries
.getAllTagsWithCount()
.asFlow()
.mapToList(
viewModelScope.coroutineContext,
).stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), listOf())

val countOfLinks: StateFlow<Long?> =
deeprQueries
.countOfLinks()
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/sqldelight/com/yogeshpaliyal/deepr/Deepr.sq
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ SELECT * FROM Tags WHERE name = ?;
getAllTags:
SELECT * FROM Tags ORDER BY name;

getAllTagsWithCount:
SELECT
Tags.id,
Tags.name,
COUNT(LinkTags.linkId) AS linkCount
FROM Tags
LEFT JOIN LinkTags ON Tags.id = LinkTags.tagId
GROUP BY Tags.id, Tags.name
ORDER BY Tags.name;

countOfLinks:
SELECT count(*) FROM Deepr;

Expand Down