forked from Schanbro/WeekFocus
feat: create word
feat: display random word (per week)
This commit is contained in:
1 parent
6368714721
commit
73e4fd2957
5 files changed
+181
-17
No files matched your search
@@ -1,16 +1,20 @@
|
||||
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.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.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.tooling.preview.Preview
|
||||
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() {
|
||||
@@ -20,8 +24,7 @@ class MainActivity : ComponentActivity() {
|
||||
setContent {
|
||||
WeekFocusTheme {
|
||||
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
|
||||
Greeting(
|
||||
name = "Android",
|
||||
WordListScreen(
|
||||
modifier = Modifier.padding(innerPadding)
|
||||
)
|
||||
}
|
||||
@@ -31,17 +34,94 @@ class MainActivity : ComponentActivity() {
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun Greeting(name: String, modifier: Modifier = Modifier) {
|
||||
Text(
|
||||
text = "Hello $name!",
|
||||
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)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Preview(showBackground = true)
|
||||
@Composable
|
||||
fun GreetingPreview() {
|
||||
WeekFocusTheme {
|
||||
Greeting("Android")
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user