From 2545498dc2341526801788d30d608c499c6c0cd2 Mon Sep 17 00:00:00 2001 From: Robin Schanbacher Date: Sun, 4 Oct 2026 17:17:23 +0200 Subject: [PATCH] feat: navigation feat: remove word --- app/build.gradle.kts | 1 + .../de/schanbro/weekfocus/MainActivity.kt | 190 ++++++++++-------- .../weekfocus/screens/WordListScreen.kt | 169 ++++++++++++++++ app/src/main/res/layout/widget_main.xml | 3 +- app/src/main/res/values-en/strings.xml | 5 +- app/src/main/res/values/strings.xml | 5 +- app/src/main/res/xml/widget_info.xml | 2 +- 7 files changed, 283 insertions(+), 92 deletions(-) create mode 100644 app/src/main/java/de/schanbro/weekfocus/screens/WordListScreen.kt diff --git a/app/build.gradle.kts b/app/build.gradle.kts index f9a06a1..8abe9fe 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -79,4 +79,5 @@ dependencies { androidTestImplementation(libs.androidx.junit) debugImplementation(libs.androidx.compose.ui.test.manifest) debugImplementation(libs.androidx.compose.ui.tooling) + implementation("androidx.compose.material:material-icons-extended") } \ 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 8ceb187..80aa7ab 100644 --- a/app/src/main/java/de/schanbro/weekfocus/MainActivity.kt +++ b/app/src/main/java/de/schanbro/weekfocus/MainActivity.kt @@ -1,22 +1,29 @@ package de.schanbro.weekfocus +import android.appwidget.AppWidgetManager +import android.content.ComponentName 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.material.icons.Icons +import androidx.compose.material.icons.filled.Home +import androidx.compose.material.icons.filled.List +import androidx.compose.material.icons.filled.Settings +import androidx.compose.material.icons.outlined.Home +import androidx.compose.material.icons.outlined.List +import androidx.compose.material.icons.outlined.Settings import androidx.compose.material3.* import androidx.compose.runtime.* +import androidx.compose.ui.Alignment 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 +import androidx.compose.ui.input.nestedscroll.nestedScroll import androidx.compose.ui.res.stringResource +import de.schanbro.weekfocus.ui.theme.WeekFocusTheme + +import de.schanbro.weekfocus.screens.WordListScreen class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { @@ -24,99 +31,108 @@ class MainActivity : ComponentActivity() { enableEdgeToEdge() setContent { WeekFocusTheme { - Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding -> - WordListScreen( - modifier = Modifier.padding(innerPadding) + MainScreen() + } + } + } +} + +@OptIn(ExperimentalMaterial3Api::class) +@Composable +fun MainScreen() { + var selectedTab by remember { mutableIntStateOf(0) } + val scrollBehavior = TopAppBarDefaults.exitUntilCollapsedScrollBehavior() + + Scaffold( + modifier = Modifier + .fillMaxSize() + .nestedScroll(scrollBehavior.nestedScrollConnection), + topBar = { + LargeTopAppBar( + title = { + Text( + when (selectedTab) { + 0 -> stringResource(R.string.title_home) + 1 -> stringResource(R.string.title_words) + 2 -> stringResource(R.string.title_settings) + else -> stringResource(R.string.app_name) + } ) - } + }, + scrollBehavior = scrollBehavior, + colors = TopAppBarDefaults.largeTopAppBarColors( + containerColor = MaterialTheme.colorScheme.surface, + scrolledContainerColor = MaterialTheme.colorScheme.surfaceContainer + ) + ) + }, + bottomBar = { + NavigationBar { + NavigationBarItem( + selected = selectedTab == 0, + onClick = { selectedTab = 0 }, + icon = { + Icon( + imageVector = if (selectedTab == 0) Icons.Filled.Home else Icons.Outlined.Home, + contentDescription = stringResource(R.string.title_home) + ) + }, + label = { Text(stringResource(R.string.title_home)) } + ) + NavigationBarItem( + selected = selectedTab == 1, + onClick = { selectedTab = 1 }, + icon = { + Icon( + imageVector = if (selectedTab == 1) Icons.Filled.List else Icons.Outlined.List, + contentDescription = stringResource(R.string.title_words) + ) + }, + label = { Text(stringResource(R.string.title_words)) } + ) + NavigationBarItem( + selected = selectedTab == 2, + onClick = { selectedTab = 2 }, + icon = { + Icon( + imageVector = if (selectedTab == 2) Icons.Filled.Settings else Icons.Outlined.Settings, + contentDescription = stringResource(R.string.title_settings) + ) + }, + label = { Text(stringResource(R.string.title_settings)) } + ) + } + } + ) { innerPadding -> + Surface( + modifier = Modifier + .fillMaxSize() + .padding(innerPadding), + color = MaterialTheme.colorScheme.background + ) { + when (selectedTab) { + 0 -> PlaceholderScreen(title = stringResource(R.string.title_weekly_focus)) + 1 -> WordListScreen() + 2 -> PlaceholderScreen(title = stringResource(R.string.title_settings)) } } } } @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().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) +fun PlaceholderScreen(title: String) { + Box( + modifier = Modifier.fillMaxSize(), + contentAlignment = Alignment.Center ) { - // Eingabefeld für ein neues Wort - OutlinedTextField( - value = textInput, - onValueChange = { textInput = it }, - label = { Text(stringResource(R.string.hint_new_word)) }, - 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(stringResource(R.string.hint_add_word)) - } - - Spacer(modifier = Modifier.height(24.dp)) - Text( - text = stringResource(R.string.title_saved_words), - style = MaterialTheme.typography.titleMedium + text = title, + style = MaterialTheme.typography.headlineMedium, + color = MaterialTheme.colorScheme.onSurfaceVariant ) - - Spacer(modifier = Modifier.height(8.dp)) - - // Liste der Wörter - if (wordList.isEmpty()) { - Text( - text = stringResource(R.string.msg_no_words_yet), - 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) diff --git a/app/src/main/java/de/schanbro/weekfocus/screens/WordListScreen.kt b/app/src/main/java/de/schanbro/weekfocus/screens/WordListScreen.kt new file mode 100644 index 0000000..8b39497 --- /dev/null +++ b/app/src/main/java/de/schanbro/weekfocus/screens/WordListScreen.kt @@ -0,0 +1,169 @@ +package de.schanbro.weekfocus.screens + +import android.content.Context +import androidx.compose.animation.AnimatedVisibility +import androidx.compose.animation.fadeIn +import androidx.compose.animation.fadeOut +import androidx.compose.foundation.gestures.detectTapGestures +import androidx.compose.foundation.layout.* +import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.foundation.lazy.itemsIndexed +import androidx.compose.foundation.text.KeyboardActions +import androidx.compose.foundation.text.KeyboardOptions +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.filled.Add +import androidx.compose.material.icons.filled.Delete +import androidx.compose.material3.* +import androidx.compose.runtime.* +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.input.pointer.pointerInput +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.platform.LocalFocusManager +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.text.input.ImeAction +import androidx.compose.ui.unit.dp +import de.schanbro.weekfocus.R +import de.schanbro.weekfocus.updateWidget + +@Composable +fun WordListScreen(modifier: Modifier = Modifier) { + val context = LocalContext.current + val focusManager = LocalFocusManager.current // FocusManager holen + + val sharedPrefs = remember { + context.getSharedPreferences("WeekFocusPrefs", Context.MODE_PRIVATE) + } + + var textInput by remember { mutableStateOf("") } + val wordList = remember { + mutableStateListOf().apply { + val savedSet = sharedPrefs.getStringSet("words_key", emptySet()) ?: emptySet() + addAll(savedSet) + } + } + + fun saveWords() { + sharedPrefs.edit().putStringSet("words_key", wordList.toSet()).apply() + } + + fun addCurrentWord() { + val trimmed = textInput.trim() + if (trimmed.isNotEmpty()) { + wordList.add(0, trimmed) + saveWords() + updateWidget(context) + textInput = "" + focusManager.clearFocus() // Fokus aufheben & Tastatur schließen + } + } + + Column( + modifier = modifier + .fillMaxSize() + // Auf Klicks im leeren Bereich reagieren -> Fokus aufheben + .pointerInput(Unit) { + detectTapGestures(onTap = { + focusManager.clearFocus() + }) + } + .padding(horizontal = 16.dp) + ) { + Column( + modifier = Modifier + .fillMaxWidth() + .padding(vertical = 8.dp), + verticalArrangement = Arrangement.spacedBy(8.dp) + ) { + OutlinedTextField( + value = textInput, + onValueChange = { textInput = it }, + label = { Text(stringResource(R.string.hint_new_word)) }, + modifier = Modifier.fillMaxWidth(), + singleLine = true, + shape = MaterialTheme.shapes.medium, + // "Fertig" / "Hinzufügen" Taste auf der Tastatur konfigurieren + keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done), + keyboardActions = KeyboardActions( + onDone = { addCurrentWord() } + ) + ) + + Button( + onClick = { addCurrentWord() }, + modifier = Modifier + .fillMaxWidth() + .height(50.dp), + shape = MaterialTheme.shapes.medium + ) { + Icon(Icons.Default.Add, contentDescription = null) + Spacer(Modifier.width(8.dp)) + Text(stringResource(R.string.hint_add_word)) + } + } + + Spacer(modifier = Modifier.height(16.dp)) + + AnimatedVisibility( + visible = wordList.isEmpty(), + enter = fadeIn(), + exit = fadeOut() + ) { + Box( + modifier = Modifier + .fillMaxWidth() + .padding(top = 32.dp), + contentAlignment = Alignment.Center + ) { + Text( + text = stringResource(R.string.msg_no_words_yet), + style = MaterialTheme.typography.bodyLarge, + color = MaterialTheme.colorScheme.onSurfaceVariant + ) + } + } + + LazyColumn( + verticalArrangement = Arrangement.spacedBy(8.dp), + contentPadding = PaddingValues(bottom = 16.dp) + ) { + itemsIndexed(wordList) { index, word -> + ElevatedCard( + modifier = Modifier.fillMaxWidth(), + shape = MaterialTheme.shapes.medium, + colors = CardDefaults.elevatedCardColors( + containerColor = MaterialTheme.colorScheme.surfaceContainerLow + ) + ) { + Row( + modifier = Modifier + .fillMaxWidth() + .padding(horizontal = 16.dp, vertical = 12.dp), + verticalAlignment = Alignment.CenterVertically, + horizontalArrangement = Arrangement.SpaceBetween + ) { + Text( + text = word, + style = MaterialTheme.typography.bodyLarge, + color = MaterialTheme.colorScheme.onSurface, + modifier = Modifier.weight(1f) + ) + IconButton( + onClick = { + wordList.removeAt(index) + saveWords() + updateWidget(context) + } + ) { + Icon( + imageVector = Icons.Default.Delete, + contentDescription = "Entfernen", + tint = MaterialTheme.colorScheme.error + ) + } + } + } + } + } + } +} \ 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 index 9b8a8ae..332c05a 100644 --- a/app/src/main/res/layout/widget_main.xml +++ b/app/src/main/res/layout/widget_main.xml @@ -11,10 +11,9 @@ diff --git a/app/src/main/res/values-en/strings.xml b/app/src/main/res/values-en/strings.xml index 76beed8..3e0936a 100644 --- a/app/src/main/res/values-en/strings.xml +++ b/app/src/main/res/values-en/strings.xml @@ -5,5 +5,8 @@ Saved words: No words yet. No words - Weekly Focus + Weekly Focus + Settings + Words + Home diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5d4bb60..ac58458 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -6,5 +6,8 @@ Gespeicherte Wörter: Noch keine Wörter vorhanden. Keine Wörter - Wochenfokus + Wochenfokus + Einstellungen + Wörter + Start diff --git a/app/src/main/res/xml/widget_info.xml b/app/src/main/res/xml/widget_info.xml index 0cf94df..a6935c1 100644 --- a/app/src/main/res/xml/widget_info.xml +++ b/app/src/main/res/xml/widget_info.xml @@ -7,5 +7,5 @@ android:resizeMode="horizontal|vertical" android:widgetCategory="home_screen" android:targetCellWidth="2" - android:targetCellHeight="2"> + android:targetCellHeight="1"> \ No newline at end of file -- 2.54.1