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 composer: String?,
|
||||||
val year: Int?,
|
val year: Int?,
|
||||||
val genre: String?,
|
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)) {
|
Row(modifier = Modifier.padding(16.dp)) {
|
||||||
// Linkes Vorschaubild
|
// Linkes Vorschaubild
|
||||||
|
AsyncImage(
|
||||||
note.images.firstOrNull()?.let { uriString ->
|
model = note.imagePreview,
|
||||||
AsyncImage(
|
contentDescription = "Vorschaubild",
|
||||||
model = uriString,
|
modifier = Modifier
|
||||||
contentDescription = "Vorschaubild",
|
.size(120.dp)
|
||||||
modifier = Modifier
|
.clip(RoundedCornerShape(12.dp))
|
||||||
.size(120.dp)
|
)
|
||||||
.clip(RoundedCornerShape(12.dp))
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Rechte Info-Spalte
|
// Rechte Info-Spalte
|
||||||
Column(
|
Column(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.weight(1f)
|
.weight(1f)
|
||||||
.align(Alignment.CenterVertically)
|
.align(Alignment.CenterVertically)
|
||||||
|
.padding(start=16.dp)
|
||||||
) {
|
) {
|
||||||
Text(
|
Text(
|
||||||
text = note.title,
|
text = note.title,
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package come.stormborntales.notevault.ui.viewmodel
|
package come.stormborntales.notevault.ui.viewmodel
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.graphics.Bitmap
|
||||||
|
import android.graphics.BitmapFactory
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import android.webkit.MimeTypeMap
|
import android.webkit.MimeTypeMap
|
||||||
@ -12,12 +14,31 @@ import come.stormborntales.notevault.data.repository.NoteRepository
|
|||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
import java.io.InputStream
|
||||||
|
import androidx.core.graphics.scale
|
||||||
|
|
||||||
class NoteViewModel(
|
class NoteViewModel(
|
||||||
private val repository: NoteRepository,
|
private val repository: NoteRepository,
|
||||||
) : ViewModel() {
|
) : ViewModel() {
|
||||||
val notes = repository.allNotes.asLiveData()
|
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) {
|
fun addNote(context: Context, title: String, composer: String?, year: Int?, genre: String?, description: String?, selectedUris: List<Uri>, onDone:() -> Unit) {
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
viewModelScope.launch(Dispatchers.IO) {
|
||||||
val copiedUris = selectedUris.mapNotNull { uri ->
|
val copiedUris = selectedUris.mapNotNull { uri ->
|
||||||
@ -41,13 +62,16 @@ class NoteViewModel(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (copiedUris.isNotEmpty()) {
|
if (copiedUris.isNotEmpty()) {
|
||||||
|
val preview_image_path = createPreviewImage(context, uri = selectedUris[0])
|
||||||
|
|
||||||
val note = NoteEntity(
|
val note = NoteEntity(
|
||||||
title = title,
|
title = title,
|
||||||
images = copiedUris, // muss als List<String> gespeichert sein
|
images = copiedUris, // muss als List<String> gespeichert sein
|
||||||
composer = composer,
|
composer = composer,
|
||||||
year = year,
|
year = year,
|
||||||
genre = genre,
|
genre = genre,
|
||||||
description = description
|
description = description,
|
||||||
|
imagePreview = preview_image_path.toString()
|
||||||
)
|
)
|
||||||
|
|
||||||
repository.insert(note)
|
repository.insert(note)
|
||||||
|
Loading…
Reference in New Issue
Block a user