Compare commits
No commits in common. "7e04c31ae384f9115b03ac328caaa663472ee84e" and "821c81cac2b74ff1c91a2e13d9256df453d398f8" have entirely different histories.
7e04c31ae3
...
821c81cac2
|
|
@ -10,7 +10,6 @@ import androidx.room.Entity
|
|||
import androidx.room.Insert
|
||||
import androidx.room.OnConflictStrategy
|
||||
import androidx.room.PrimaryKey
|
||||
import androidx.room.Query
|
||||
import java.io.File
|
||||
|
||||
@Entity
|
||||
|
|
@ -19,8 +18,7 @@ data class Recipe(
|
|||
val title: String,
|
||||
val preview: String?,
|
||||
val lastModified: Long,
|
||||
val content: String,
|
||||
val hash: Int
|
||||
val content: String
|
||||
) {
|
||||
fun previewImage(ctx: Context): Bitmap? {
|
||||
if (this.preview != null) {
|
||||
|
|
@ -43,7 +41,4 @@ interface RecipeDao {
|
|||
|
||||
@Delete
|
||||
fun delete(recipe: Recipe)
|
||||
|
||||
@Query("SELECT EXISTS(SELECT hash FROM recipe WHERE recipe.hash = :hash LIMIT 1)")
|
||||
fun hasHash(hash: Int): Boolean
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,18 +36,13 @@ class RecipesView : ViewModel() {
|
|||
fun setRecipes(recipes: List<RecipeWithTags>) {
|
||||
_recipes.update { recipes.sortedBy { it.recipe.title } }
|
||||
|
||||
val filtersMap = mutableMapOf<String, Int>()
|
||||
val filters = arrayListOf<TagFilter>()
|
||||
|
||||
_recipes.value.stream()
|
||||
.filter { it.tags.isNotEmpty() }
|
||||
.forEach {
|
||||
it.tags.forEach { tag ->
|
||||
val count = filtersMap[tag.name] ?: 0
|
||||
filtersMap[tag.name] = count + 1;
|
||||
it.tags.forEach {tag -> filters.add(TagFilter(tag.name))}
|
||||
}
|
||||
}
|
||||
|
||||
val filters = filtersMap.map { it -> TagFilter(it.key, count = it.value) }.toList()
|
||||
|
||||
_tagFilters.update { filters.distinct() }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
package xyz.pixelatedw.recipe.data
|
||||
|
||||
data class TagFilter(val tag: String, var checked: Boolean = false, var count: Int = 0) {
|
||||
data class TagFilter(val tag: String, var checked: Boolean = false) {
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,11 +33,11 @@ fun RecipePreview(entry: RecipeWithTags, previewUri: Bitmap?, onClick: () -> Uni
|
|||
bitmap = previewUri.asImageBitmap(),
|
||||
contentDescription = "Recipe image",
|
||||
contentScale = ContentScale.Crop,
|
||||
modifier = Modifier.size(256.dp).padding(top = 16.dp, bottom = 8.dp),
|
||||
modifier = Modifier.size(256.dp).padding(top = 16.dp, bottom = 16.dp),
|
||||
)
|
||||
}
|
||||
}
|
||||
Row(horizontalArrangement = Arrangement.Center, modifier = Modifier.fillMaxWidth().padding(bottom = 8.dp)) {
|
||||
Row(horizontalArrangement = Arrangement.Center, modifier = Modifier.fillMaxWidth()) {
|
||||
Text(
|
||||
text = entry.recipe.title,
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
|
|
|
|||
|
|
@ -1,15 +1,22 @@
|
|||
package xyz.pixelatedw.recipe.ui.components
|
||||
|
||||
import androidx.collection.arraySetOf
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.fillMaxHeight
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.wrapContentSize
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.grid.GridCells
|
||||
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
|
||||
import androidx.compose.foundation.lazy.grid.items
|
||||
import androidx.compose.foundation.lazy.items
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material3.ButtonDefaults
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.Card
|
||||
import androidx.compose.material3.Checkbox
|
||||
import androidx.compose.material3.Text
|
||||
|
|
@ -18,14 +25,17 @@ import androidx.compose.runtime.Composable
|
|||
import androidx.compose.runtime.collectAsState
|
||||
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.graphics.Color
|
||||
import androidx.compose.ui.text.style.TextAlign
|
||||
import androidx.compose.ui.unit.TextUnit
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.em
|
||||
import androidx.compose.ui.unit.sp
|
||||
import androidx.compose.ui.window.Dialog
|
||||
import xyz.pixelatedw.recipe.data.RecipesView
|
||||
import xyz.pixelatedw.recipe.data.TagFilter
|
||||
|
||||
@Composable
|
||||
fun TagFilterDialog(onAccept: ( ) -> Unit, view: RecipesView) {
|
||||
|
|
@ -44,45 +54,24 @@ fun TagFilterDialog(onAccept: () -> Unit, view: RecipesView) {
|
|||
modifier = Modifier
|
||||
.fillMaxHeight(0.9f)
|
||||
.padding(start = 16.dp, end = 16.dp, bottom = 16.dp)
|
||||
.wrapContentSize(Alignment.TopStart)
|
||||
) {
|
||||
.wrapContentSize(Alignment.TopStart)) {
|
||||
items(filters.value) { tag ->
|
||||
// TODO This doesn't really feel right lmao, but for now it works
|
||||
val filterChecked = remember { mutableStateOf(tag.checked) }
|
||||
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
Checkbox(checked = filterChecked.value, onCheckedChange = {
|
||||
filterChecked.value = !tag.checked
|
||||
view.setTagFilterState(tag.tag, !tag.checked)
|
||||
})
|
||||
TextButton(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
colors = ButtonDefaults.buttonColors(
|
||||
contentColor = Color.White,
|
||||
containerColor = Color.Transparent
|
||||
),
|
||||
onClick = {
|
||||
filterChecked.value = !tag.checked
|
||||
view.setTagFilterState(tag.tag, !tag.checked)
|
||||
}
|
||||
) {
|
||||
Text(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
text = tag.tag + " (" + tag.count + ")",
|
||||
textAlign = TextAlign.Left
|
||||
)
|
||||
}
|
||||
Text(tag.tag)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Row(
|
||||
modifier = Modifier
|
||||
Row(modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(end = 16.dp), horizontalArrangement = Arrangement.End
|
||||
) {
|
||||
.padding(end = 16.dp), horizontalArrangement = Arrangement.End) {
|
||||
TextButton(onClick = onAccept) {
|
||||
Text("Done")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -65,12 +65,6 @@ private fun parseRecipe(ctx: Context, db: AppDatabase, file: DocumentFile) {
|
|||
val inputStream = ctx.contentResolver.openInputStream(file.uri)
|
||||
val reader = BufferedReader(InputStreamReader(inputStream))
|
||||
val text = reader.readText()
|
||||
|
||||
val hash = text.hashCode() // Probably not the best way but its stable and works
|
||||
if (db.recipeDao().hasHash(hash)) {
|
||||
return
|
||||
}
|
||||
|
||||
val lines = text.lines()
|
||||
|
||||
val sb = StringBuilder()
|
||||
|
|
@ -139,11 +133,12 @@ private fun parseRecipe(ctx: Context, db: AppDatabase, file: DocumentFile) {
|
|||
title = recipeTitle,
|
||||
preview = recipePreview,
|
||||
lastModified = lastModified,
|
||||
content = content,
|
||||
hash = hash
|
||||
content = content
|
||||
)
|
||||
|
||||
db.recipeDao().insert(recipe)
|
||||
|
||||
println(recipe)
|
||||
|
||||
reader.close()
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue