Skip to content

Commit

Permalink
fix: deleting history entries (closes #292)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro committed Jul 24, 2023
1 parent 637f7aa commit 4f5e1b8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
10 changes: 6 additions & 4 deletions app/src/main/java/com/bnyro/translate/ui/models/HistoryModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package com.bnyro.translate.ui.models

import android.annotation.SuppressLint
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
Expand All @@ -26,24 +27,25 @@ import com.bnyro.translate.DatabaseHolder.Companion.Db
import com.bnyro.translate.db.obj.HistoryItem
import com.bnyro.translate.ext.query

@SuppressLint("MutableCollectionMutableState")
class HistoryModel : ViewModel() {
var history = mutableStateListOf<HistoryItem>()
var history by mutableStateOf(listOf<HistoryItem>())

fun fetchHistory() {
query {
history.addAll(Db.historyDao().getAll().reversed())
history = history.plus(Db.historyDao().getAll().reversed())
}
}

fun clearHistory() {
query {
Db.historyDao().deleteAll()
history.clear()
history = emptyList()
}
}

fun deleteHistoryItem(historyItem: HistoryItem) {
history.removeAll { it.id == historyItem.id }
history = history.filter { it.id != historyItem.id }
query {
Db.historyDao().delete(historyItem)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.Delete
Expand Down Expand Up @@ -126,11 +124,10 @@ fun HistoryScreen(
}

if (filteredHistory.isNotEmpty()) {
val scrollState = rememberScrollState()
Column(
modifier = Modifier.fillMaxSize().verticalScroll(scrollState)
LazyColumn(
modifier = Modifier.fillMaxSize()
) {
filteredHistory.forEach {
items(filteredHistory) {
HistoryRow(navController, translationModel, it) {
viewModel.deleteHistoryItem(it)
}
Expand Down

0 comments on commit 4f5e1b8

Please sign in to comment.