ADD: Further Performance Improvements by storing a special preview image
This commit is contained in:
parent
0a33c12cde
commit
dc5be58b30
@ -11,5 +11,6 @@ data class NoteEntity(
|
||||
val composer: String?,
|
||||
val year: Int?,
|
||||
val genre: String?,
|
||||
val description: String?
|
||||
val description: String?,
|
||||
val imagePreview: String
|
||||
)
|
||||
|
@ -57,22 +57,20 @@ fun NoteCard(note: NoteEntity) {
|
||||
) {
|
||||
Row(modifier = Modifier.padding(16.dp)) {
|
||||
// Linkes Vorschaubild
|
||||
|
||||
note.images.firstOrNull()?.let { uriString ->
|
||||
AsyncImage(
|
||||
model = uriString,
|
||||
contentDescription = "Vorschaubild",
|
||||
modifier = Modifier
|
||||
.size(120.dp)
|
||||
.clip(RoundedCornerShape(12.dp))
|
||||
)
|
||||
}
|
||||
AsyncImage(
|
||||
model = note.imagePreview,
|
||||
contentDescription = "Vorschaubild",
|
||||
modifier = Modifier
|
||||
.size(120.dp)
|
||||
.clip(RoundedCornerShape(12.dp))
|
||||
)
|
||||
|
||||
// Rechte Info-Spalte
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.weight(1f)
|
||||
.align(Alignment.CenterVertically)
|
||||
.padding(start=16.dp)
|
||||
) {
|
||||
Text(
|
||||
text = note.title,
|
||||
|
@ -1,6 +1,8 @@
|
||||
package come.stormborntales.notevault.ui.viewmodel
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.BitmapFactory
|
||||
import android.net.Uri
|
||||
import android.util.Log
|
||||
import android.webkit.MimeTypeMap
|
||||
@ -12,12 +14,31 @@ import come.stormborntales.notevault.data.repository.NoteRepository
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import java.io.File
|
||||
import java.io.InputStream
|
||||
import androidx.core.graphics.scale
|
||||
|
||||
class NoteViewModel(
|
||||
private val repository: NoteRepository,
|
||||
) : ViewModel() {
|
||||
val notes = repository.allNotes.asLiveData()
|
||||
|
||||
fun createPreviewImage(context: Context, uri: Uri): File? {
|
||||
return try {
|
||||
val inputStream = context.contentResolver.openInputStream(uri)
|
||||
val originalBitmap = BitmapFactory.decodeStream(inputStream) ?: return null
|
||||
|
||||
val previewBitmap = originalBitmap.scale(128, 128, false)
|
||||
val previewFile = File(context.filesDir, "preview_${System.currentTimeMillis()}.jpg")
|
||||
previewFile.outputStream().use { out ->
|
||||
previewBitmap.compress(Bitmap.CompressFormat.JPEG, 75, out)
|
||||
}
|
||||
previewFile
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
fun addNote(context: Context, title: String, composer: String?, year: Int?, genre: String?, description: String?, selectedUris: List<Uri>, onDone:() -> Unit) {
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
val copiedUris = selectedUris.mapNotNull { uri ->
|
||||
@ -41,13 +62,16 @@ class NoteViewModel(
|
||||
}
|
||||
|
||||
if (copiedUris.isNotEmpty()) {
|
||||
val preview_image_path = createPreviewImage(context, uri = selectedUris[0])
|
||||
|
||||
val note = NoteEntity(
|
||||
title = title,
|
||||
images = copiedUris, // muss als List<String> gespeichert sein
|
||||
composer = composer,
|
||||
year = year,
|
||||
genre = genre,
|
||||
description = description
|
||||
description = description,
|
||||
imagePreview = preview_image_path.toString()
|
||||
)
|
||||
|
||||
repository.insert(note)
|
||||
|
Loading…
Reference in New Issue
Block a user