forked from Schanbro/WeekFocus
127 lines
3.9 KiB
Kotlin
127 lines
3.9 KiB
Kotlin
package de.schanbro.weekfocus
|
|
|
|
import android.content.Context
|
|
import android.os.Bundle
|
|
import androidx.activity.ComponentActivity
|
|
import androidx.activity.compose.setContent
|
|
import androidx.activity.enableEdgeToEdge
|
|
import androidx.compose.foundation.layout.*
|
|
import androidx.compose.foundation.lazy.LazyColumn
|
|
import androidx.compose.foundation.lazy.items
|
|
import androidx.compose.material3.*
|
|
import androidx.compose.runtime.*
|
|
import androidx.compose.ui.Modifier
|
|
import androidx.compose.ui.platform.LocalContext
|
|
import androidx.compose.ui.unit.dp
|
|
import android.appwidget.AppWidgetManager
|
|
import android.content.ComponentName
|
|
import de.schanbro.weekfocus.ui.theme.WeekFocusTheme
|
|
|
|
class MainActivity : ComponentActivity() {
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
super.onCreate(savedInstanceState)
|
|
enableEdgeToEdge()
|
|
setContent {
|
|
WeekFocusTheme {
|
|
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
|
|
WordListScreen(
|
|
modifier = Modifier.padding(innerPadding)
|
|
)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
@Composable
|
|
fun WordListScreen(modifier: Modifier = Modifier) {
|
|
val context = LocalContext.current
|
|
val sharedPrefs = remember {
|
|
context.getSharedPreferences("WeekFocusPrefs", Context.MODE_PRIVATE)
|
|
}
|
|
|
|
// Wörter aus SharedPreferences laden und Zustand verwalten
|
|
var textInput by remember { mutableStateOf("") }
|
|
val wordList = remember {
|
|
mutableStateListOf<String>().apply {
|
|
val savedSet = sharedPrefs.getStringSet("words_key", emptySet()) ?: emptySet()
|
|
addAll(savedSet)
|
|
}
|
|
}
|
|
|
|
// Funktion zum Speichern in SharedPreferences
|
|
fun saveWords() {
|
|
sharedPrefs.edit().putStringSet("words_key", wordList.toSet()).apply()
|
|
}
|
|
|
|
Column(
|
|
modifier = modifier
|
|
.fillMaxSize()
|
|
.padding(16.dp)
|
|
) {
|
|
// Eingabefeld für ein neues Wort
|
|
OutlinedTextField(
|
|
value = textInput,
|
|
onValueChange = { textInput = it },
|
|
label = { Text("Neues Wort eingeben...") },
|
|
modifier = Modifier.fillMaxWidth(),
|
|
singleLine = true
|
|
)
|
|
|
|
Spacer(modifier = Modifier.height(8.dp))
|
|
|
|
// Button zum Hinzufügen
|
|
Button(
|
|
onClick = {
|
|
val trimmed = textInput.trim()
|
|
if (trimmed.isNotEmpty()) {
|
|
wordList.add(trimmed)
|
|
saveWords()
|
|
updateWidget(context)
|
|
textInput = ""
|
|
}
|
|
},
|
|
modifier = Modifier.fillMaxWidth()
|
|
) {
|
|
Text("Wort hinzufügen")
|
|
}
|
|
|
|
Spacer(modifier = Modifier.height(24.dp))
|
|
|
|
Text(
|
|
text = "Gespeicherte Wörter:",
|
|
style = MaterialTheme.typography.titleMedium
|
|
)
|
|
|
|
Spacer(modifier = Modifier.height(8.dp))
|
|
|
|
// Liste der Wörter
|
|
if (wordList.isEmpty()) {
|
|
Text(
|
|
text = "Noch keine Wörter vorhanden.",
|
|
style = MaterialTheme.typography.bodyMedium
|
|
)
|
|
} else {
|
|
LazyColumn {
|
|
items(wordList) { word ->
|
|
Text(
|
|
text = "• $word",
|
|
style = MaterialTheme.typography.bodyLarge,
|
|
modifier = Modifier.padding(vertical = 4.dp)
|
|
)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Hilfsfunktion zum Aktualisieren des Widgets
|
|
fun updateWidget(context: Context) {
|
|
val appWidgetManager = AppWidgetManager.getInstance(context)
|
|
val componentName = ComponentName(context, SimpleWidget::class.java)
|
|
val appWidgetIds = appWidgetManager.getAppWidgetIds(componentName)
|
|
|
|
for (appWidgetId in appWidgetIds) {
|
|
SimpleWidget.updateAppWidget(context, appWidgetManager, appWidgetId)
|
|
}
|
|
} |