nextNoteVault #23
@ -1,39 +1,81 @@
 | 
			
		||||
package come.stormborntales.notevault
 | 
			
		||||
 | 
			
		||||
import android.content.Context
 | 
			
		||||
import android.net.Uri
 | 
			
		||||
import android.os.Bundle
 | 
			
		||||
import android.util.Log
 | 
			
		||||
import androidx.activity.ComponentActivity
 | 
			
		||||
import androidx.activity.compose.setContent
 | 
			
		||||
import androidx.activity.enableEdgeToEdge
 | 
			
		||||
import androidx.activity.result.ActivityResultLauncher
 | 
			
		||||
import androidx.activity.result.PickVisualMediaRequest
 | 
			
		||||
import androidx.activity.result.contract.ActivityResultContracts
 | 
			
		||||
import androidx.compose.foundation.layout.fillMaxSize
 | 
			
		||||
import androidx.compose.foundation.layout.padding
 | 
			
		||||
import androidx.compose.material3.Scaffold
 | 
			
		||||
import androidx.compose.material3.Text
 | 
			
		||||
import androidx.compose.runtime.Composable
 | 
			
		||||
import androidx.compose.runtime.mutableStateListOf
 | 
			
		||||
import androidx.compose.runtime.remember
 | 
			
		||||
import androidx.compose.ui.Modifier
 | 
			
		||||
import androidx.compose.ui.tooling.preview.Preview
 | 
			
		||||
import come.stormborntales.notevault.ui.screens.MainScreen
 | 
			
		||||
import come.stormborntales.notevault.ui.theme.NoteVaultTheme
 | 
			
		||||
import java.io.File
 | 
			
		||||
import java.io.InputStream
 | 
			
		||||
 | 
			
		||||
class MainActivity : ComponentActivity() {
 | 
			
		||||
    private lateinit var imagePickerLauncher: ActivityResultLauncher<PickVisualMediaRequest>
 | 
			
		||||
    // Definiere notes als MutableStateList für die gesamte Aktivität
 | 
			
		||||
    private val notes = mutableStateListOf<Uri>()
 | 
			
		||||
    override fun onCreate(savedInstanceState: Bundle?) {
 | 
			
		||||
        super.onCreate(savedInstanceState)
 | 
			
		||||
        enableEdgeToEdge()
 | 
			
		||||
 | 
			
		||||
        // Launcher für die Bildauswahl einrichten
 | 
			
		||||
        imagePickerLauncher = registerForActivityResult(ActivityResultContracts.PickVisualMedia()) { uri ->
 | 
			
		||||
            uri?.let {
 | 
			
		||||
                // Hier kannst du die Logik zum Speichern und Anzeigen des Bildes hinzufügen
 | 
			
		||||
                val context: Context = applicationContext
 | 
			
		||||
                val inputStream: InputStream? = contentResolver.openInputStream(uri)
 | 
			
		||||
                val outputFile = File(context.filesDir, "note_${System.currentTimeMillis()}.jpg")
 | 
			
		||||
 | 
			
		||||
                inputStream?.use { input ->
 | 
			
		||||
                    outputFile.outputStream().use { output ->
 | 
			
		||||
                        input.copyTo(output)
 | 
			
		||||
                        addNoteToList(notes, it)
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                Log.d("MainActivity", "Bild gespeichert: ${outputFile.absolutePath}")
 | 
			
		||||
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        setContent {
 | 
			
		||||
            // Initialisiere die Notenliste als State
 | 
			
		||||
            val notes = remember { mutableStateListOf<Uri>() }
 | 
			
		||||
            // Launcher für die Bildauswahl einrichten
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
            NoteVaultTheme {
 | 
			
		||||
                MainScreen(
 | 
			
		||||
                    onAddNoteClicked = {
 | 
			
		||||
                        // Hier kommt später deine Logik zum Öffnen der Galerie oder eines Dialogs.
 | 
			
		||||
                        // Zum Beispiel kannst du hier später ein Intent einbauen, um ein Bild zu wählen.
 | 
			
		||||
                        Log.d("MainActivity", "FAB geklickt! Hier könnte die Galerie geöffnet werden.")
 | 
			
		||||
                    }
 | 
			
		||||
                        // Öffne die Galerie zum Auswählen eines Bildes
 | 
			
		||||
                        imagePickerLauncher.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageAndVideo))
 | 
			
		||||
                    },
 | 
			
		||||
                    notes = notes,
 | 
			
		||||
                )
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
// Funktion zum Hinzufügen von Noten zur Liste
 | 
			
		||||
private fun addNoteToList(notes: MutableList<Uri>, uri: Uri) {
 | 
			
		||||
    // Hier wird das Bild zur Liste hinzugefügt
 | 
			
		||||
    notes.add(uri)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@Composable
 | 
			
		||||
fun Greeting(name: String, modifier: Modifier = Modifier) {
 | 
			
		||||
 | 
			
		||||
@ -1,5 +1,11 @@
 | 
			
		||||
package come.stormborntales.notevault.ui.screens
 | 
			
		||||
 | 
			
		||||
import android.content.Context
 | 
			
		||||
import android.graphics.BitmapFactory
 | 
			
		||||
import android.graphics.ImageDecoder
 | 
			
		||||
import android.media.Image
 | 
			
		||||
import android.net.Uri
 | 
			
		||||
import androidx.compose.foundation.Image
 | 
			
		||||
import androidx.compose.foundation.layout.fillMaxSize
 | 
			
		||||
import androidx.compose.foundation.layout.fillMaxWidth
 | 
			
		||||
import androidx.compose.foundation.layout.padding
 | 
			
		||||
@ -8,17 +14,34 @@ import androidx.compose.foundation.lazy.items
 | 
			
		||||
import androidx.compose.material.icons.Icons
 | 
			
		||||
import androidx.compose.material.icons.filled.Add
 | 
			
		||||
import androidx.compose.material3.*
 | 
			
		||||
import androidx.compose.runtime.Composable
 | 
			
		||||
import androidx.compose.runtime.*
 | 
			
		||||
import androidx.compose.ui.Modifier
 | 
			
		||||
import androidx.compose.ui.graphics.ImageBitmap
 | 
			
		||||
import androidx.compose.ui.graphics.asImageBitmap
 | 
			
		||||
import androidx.compose.ui.platform.LocalContext
 | 
			
		||||
import androidx.compose.ui.unit.dp
 | 
			
		||||
import java.io.InputStream
 | 
			
		||||
 | 
			
		||||
fun loadImageBitmap(context: Context, uri: Uri): ImageBitmap? {
 | 
			
		||||
    return try {
 | 
			
		||||
        val inputStream: InputStream? = context.contentResolver.openInputStream(uri)
 | 
			
		||||
        val bitmap = BitmapFactory.decodeStream(inputStream)
 | 
			
		||||
        bitmap?.asImageBitmap()
 | 
			
		||||
    } catch (e: Exception) {
 | 
			
		||||
        e.printStackTrace()
 | 
			
		||||
        null
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@OptIn(ExperimentalMaterial3Api::class)
 | 
			
		||||
@Composable
 | 
			
		||||
fun MainScreen(
 | 
			
		||||
    onAddNoteClicked: () -> Unit // Übergib hier deine Logik für den Import
 | 
			
		||||
    onAddNoteClicked: () -> Unit, // Übergib hier deine Logik für den Import
 | 
			
		||||
    notes: MutableList<Uri>, // Liste der Notenbilder (hier als URI),
 | 
			
		||||
) {
 | 
			
		||||
    val dummyNotes = listOf("Notenblatt 1", "Notenblatt 2", "Notenblatt 3")
 | 
			
		||||
 | 
			
		||||
    val dummyNotes = remember { mutableStateListOf<Uri>().apply { addAll(notes) } }
 | 
			
		||||
    // Hole den aktuellen Context
 | 
			
		||||
    val context = LocalContext.current
 | 
			
		||||
    Scaffold(
 | 
			
		||||
        topBar = {
 | 
			
		||||
            TopAppBar(
 | 
			
		||||
@ -40,19 +63,22 @@ fun MainScreen(
 | 
			
		||||
                .fillMaxSize()
 | 
			
		||||
                .padding(16.dp)
 | 
			
		||||
        ) {
 | 
			
		||||
            items(dummyNotes) { note ->
 | 
			
		||||
                Card(
 | 
			
		||||
                    modifier = Modifier
 | 
			
		||||
                        .fillMaxWidth()
 | 
			
		||||
                        .padding(vertical = 8.dp),
 | 
			
		||||
                    elevation = CardDefaults.cardElevation(defaultElevation = 4.dp)
 | 
			
		||||
                ) {
 | 
			
		||||
                    Text(
 | 
			
		||||
                        text = note,
 | 
			
		||||
                        modifier = Modifier.padding(16.dp)
 | 
			
		||||
            items(dummyNotes) { noteUri ->
 | 
			
		||||
                // Lade das Bild mit der Hilfsfunktion
 | 
			
		||||
                val imageBitmap = loadImageBitmap(context, noteUri)
 | 
			
		||||
                if (imageBitmap != null) {
 | 
			
		||||
                    Image(
 | 
			
		||||
                        bitmap = imageBitmap,
 | 
			
		||||
                        contentDescription = "Notenblatt",
 | 
			
		||||
                        modifier = Modifier
 | 
			
		||||
                            .fillMaxWidth()
 | 
			
		||||
                            .padding(8.dp)
 | 
			
		||||
                    )
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user