diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 1d27472..4f2cf1a 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -23,6 +23,16 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/java/de/schanbro/weekfocus/MainActivity.kt b/app/src/main/java/de/schanbro/weekfocus/MainActivity.kt index 49a97ed..da71da9 100644 --- a/app/src/main/java/de/schanbro/weekfocus/MainActivity.kt +++ b/app/src/main/java/de/schanbro/weekfocus/MainActivity.kt @@ -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().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) } } \ No newline at end of file diff --git a/app/src/main/java/de/schanbro/weekfocus/SimpleWidget.kt b/app/src/main/java/de/schanbro/weekfocus/SimpleWidget.kt new file mode 100644 index 0000000..891f8e7 --- /dev/null +++ b/app/src/main/java/de/schanbro/weekfocus/SimpleWidget.kt @@ -0,0 +1,56 @@ +package de.schanbro.weekfocus + +import android.appwidget.AppWidgetManager +import android.appwidget.AppWidgetProvider +import android.content.Context +import android.widget.RemoteViews +import java.util.Calendar +import java.util.Random + +class SimpleWidget : AppWidgetProvider() { + + override fun onUpdate( + context: Context, + appWidgetManager: AppWidgetManager, + appWidgetIds: IntArray + ) { + for (appWidgetId in appWidgetIds) { + updateAppWidget(context, appWidgetManager, appWidgetId) + } + } + + companion object { + fun updateAppWidget( + context: Context, + appWidgetManager: AppWidgetManager, + appWidgetId: Int + ) { + // 1. SharedPreferences auslesen + val sharedPrefs = context.getSharedPreferences("WeekFocusPrefs", Context.MODE_PRIVATE) + val savedSet = sharedPrefs.getStringSet("words_key", emptySet()) ?: emptySet() + val wordList = savedSet.toList() + + // 2. Aktuelle Kalenderwoche & Jahr bestimmen + val calendar = Calendar.getInstance() + val weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR) + val year = calendar.get(Calendar.YEAR) + + // 3. Wort für die aktuelle Kalenderwoche auswählen + val displayText = if (wordList.isNotEmpty()) { + // Kombinierter Seed aus Jahr und Kalenderwoche + val seed = (year * 100 + weekOfYear).toLong() + val random = Random(seed) + val randomIndex = random.nextInt(wordList.size) + wordList[randomIndex] + } else { + "Keine Wörter" + } + + // 4. RemoteViews aktualisieren + val views = RemoteViews(context.packageName, R.layout.widget_main) + views.setTextViewText(R.id.appwidget_text, displayText) + + appWidgetManager.updateAppWidget(appWidgetId, views) + } + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/widget_main.xml b/app/src/main/res/layout/widget_main.xml new file mode 100644 index 0000000..e4de4bf --- /dev/null +++ b/app/src/main/res/layout/widget_main.xml @@ -0,0 +1,10 @@ + + \ No newline at end of file diff --git a/app/src/main/res/xml/widget_info.xml b/app/src/main/res/xml/widget_info.xml new file mode 100644 index 0000000..b707b8f --- /dev/null +++ b/app/src/main/res/xml/widget_info.xml @@ -0,0 +1,8 @@ + + + \ No newline at end of file