forked from Schanbro/WeekFocus
feat: add home screen
refactor: change navigation method
This commit is contained in:
1 parent
792047eae4
commit
c56ff94a47
5 files changed
+478
-107
No files matched your search
@@ -0,0 +1,328 @@
|
||||
package de.schanbro.weekfocus.screens
|
||||
|
||||
import android.appwidget.AppWidgetManager
|
||||
import android.content.ComponentName
|
||||
import android.content.Context
|
||||
import android.text.format.DateUtils
|
||||
import androidx.compose.animation.AnimatedContent
|
||||
import androidx.compose.animation.fadeIn
|
||||
import androidx.compose.animation.fadeOut
|
||||
import androidx.compose.animation.slideInHorizontally
|
||||
import androidx.compose.animation.slideOutHorizontally
|
||||
import androidx.compose.animation.togetherWith
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.automirrored.filled.KeyboardArrowLeft
|
||||
import androidx.compose.material.icons.automirrored.filled.KeyboardArrowRight
|
||||
import androidx.compose.material.icons.filled.Check
|
||||
import androidx.compose.material3.*
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.hapticfeedback.HapticFeedbackType
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.platform.LocalHapticFeedback
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.text.style.TextAlign
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import androidx.lifecycle.compose.LifecycleEventEffect
|
||||
import de.schanbro.weekfocus.R
|
||||
import de.schanbro.weekfocus.SimpleWidget
|
||||
import de.schanbro.weekfocus.WeekInfo
|
||||
import de.schanbro.weekfocus.WordOfWeek
|
||||
import java.time.LocalDate
|
||||
import java.time.ZoneId
|
||||
|
||||
@Composable
|
||||
fun HomeScreen(
|
||||
onOpenWords: () -> Unit,
|
||||
modifier: Modifier = Modifier
|
||||
) {
|
||||
val context = LocalContext.current
|
||||
val haptic = LocalHapticFeedback.current
|
||||
|
||||
var words by remember { mutableStateOf(WordOfWeek.loadWords(context)) }
|
||||
var widgetPlaced by remember { mutableStateOf(true) } // true = kein Flackern beim Start
|
||||
var weekOffset by rememberSaveable { mutableIntStateOf(0) }
|
||||
val canPinWidget = remember {
|
||||
AppWidgetManager.getInstance(context).isRequestPinAppWidgetSupported
|
||||
}
|
||||
|
||||
// Beim Zurückkehren (Wörter-Tab, Launcher, Widget) neu laden
|
||||
LifecycleEventEffect(Lifecycle.Event.ON_RESUME) {
|
||||
words = WordOfWeek.loadWords(context)
|
||||
widgetPlaced = isWidgetPlaced(context)
|
||||
}
|
||||
|
||||
fun changeOffset(new: Int) {
|
||||
haptic.performHapticFeedback(HapticFeedbackType.TextHandleMove)
|
||||
weekOffset = new
|
||||
}
|
||||
|
||||
Column(
|
||||
modifier = modifier
|
||||
.fillMaxSize()
|
||||
.verticalScroll(rememberScrollState())
|
||||
.padding(horizontal = 16.dp, vertical = 8.dp),
|
||||
verticalArrangement = Arrangement.spacedBy(16.dp)
|
||||
) {
|
||||
if (words.isEmpty()) {
|
||||
EmptyCard(onOpenWords)
|
||||
} else {
|
||||
WordHeroCard(words, weekOffset, ::changeOffset)
|
||||
WeeksCard(words, weekOffset, ::changeOffset)
|
||||
}
|
||||
|
||||
if (!widgetPlaced && canPinWidget) {
|
||||
WidgetCard(
|
||||
onAdd = {
|
||||
AppWidgetManager.getInstance(context).requestPinAppWidget(
|
||||
ComponentName(context, SimpleWidget::class.java), null, null
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
Spacer(Modifier.height(8.dp))
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun WordHeroCard(
|
||||
words: List<String>,
|
||||
weekOffset: Int,
|
||||
onOffsetChange: (Int) -> Unit
|
||||
) {
|
||||
val context = LocalContext.current
|
||||
val week = WordOfWeek.weekOf(weekOffset)
|
||||
val onColor = MaterialTheme.colorScheme.onPrimaryContainer
|
||||
|
||||
Card(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
shape = MaterialTheme.shapes.extraLarge,
|
||||
colors = CardDefaults.cardColors(
|
||||
containerColor = MaterialTheme.colorScheme.primaryContainer,
|
||||
contentColor = onColor
|
||||
)
|
||||
) {
|
||||
Column(
|
||||
modifier = Modifier.padding(16.dp),
|
||||
horizontalAlignment = Alignment.CenterHorizontally
|
||||
) {
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
IconButton(onClick = { onOffsetChange(weekOffset - 1) }) {
|
||||
Icon(
|
||||
Icons.AutoMirrored.Filled.KeyboardArrowLeft,
|
||||
contentDescription = stringResource(R.string.home_cd_previous)
|
||||
)
|
||||
}
|
||||
Column(
|
||||
modifier = Modifier.weight(1f),
|
||||
horizontalAlignment = Alignment.CenterHorizontally
|
||||
) {
|
||||
Text(
|
||||
text = weekLabel(weekOffset, week),
|
||||
style = MaterialTheme.typography.labelLarge
|
||||
)
|
||||
Text(
|
||||
text = stringResource(
|
||||
R.string.home_week_range,
|
||||
week.weekNumber,
|
||||
formatRange(context, week)
|
||||
),
|
||||
style = MaterialTheme.typography.bodySmall
|
||||
)
|
||||
}
|
||||
IconButton(onClick = { onOffsetChange(weekOffset + 1) }) {
|
||||
Icon(
|
||||
Icons.AutoMirrored.Filled.KeyboardArrowRight,
|
||||
contentDescription = stringResource(R.string.home_cd_next)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
AnimatedContent(
|
||||
targetState = weekOffset,
|
||||
transitionSpec = {
|
||||
val dir = if (targetState > initialState) 1 else -1
|
||||
(slideInHorizontally { width -> width * dir } + fadeIn()) togetherWith
|
||||
(slideOutHorizontally { width -> -width * dir } + fadeOut())
|
||||
},
|
||||
label = "word"
|
||||
) { offset ->
|
||||
Text(
|
||||
text = WordOfWeek.wordFor(words, WordOfWeek.weekOf(offset)).orEmpty(),
|
||||
style = MaterialTheme.typography.displaySmall,
|
||||
fontWeight = FontWeight.Bold,
|
||||
textAlign = TextAlign.Center,
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(vertical = 32.dp, horizontal = 8.dp)
|
||||
)
|
||||
}
|
||||
|
||||
if (weekOffset == 0) {
|
||||
val day = remember { LocalDate.now().dayOfWeek.value } // 1 = Mo ... 7 = So
|
||||
LinearProgressIndicator(
|
||||
progress = { day / 7f },
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
color = onColor,
|
||||
trackColor = onColor.copy(alpha = 0.15f)
|
||||
)
|
||||
Spacer(Modifier.height(8.dp))
|
||||
Text(
|
||||
text = stringResource(R.string.home_day_progress, day),
|
||||
style = MaterialTheme.typography.bodySmall
|
||||
)
|
||||
} else {
|
||||
FilledTonalButton(onClick = { onOffsetChange(0) }) {
|
||||
Text(stringResource(R.string.home_back_to_current))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun WeeksCard(
|
||||
words: List<String>,
|
||||
selectedOffset: Int,
|
||||
onOffsetChange: (Int) -> Unit
|
||||
) {
|
||||
val context = LocalContext.current
|
||||
|
||||
Column {
|
||||
Text(
|
||||
text = stringResource(R.string.home_more_weeks),
|
||||
style = MaterialTheme.typography.labelLarge,
|
||||
color = MaterialTheme.colorScheme.primary,
|
||||
modifier = Modifier.padding(start = 4.dp, bottom = 8.dp)
|
||||
)
|
||||
Card(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
shape = MaterialTheme.shapes.large,
|
||||
colors = CardDefaults.cardColors(
|
||||
containerColor = MaterialTheme.colorScheme.surfaceContainerLow
|
||||
)
|
||||
) {
|
||||
listOf(-2, -1, 0, 1, 2, 3).forEach { offset ->
|
||||
val week = WordOfWeek.weekOf(offset)
|
||||
ListItem(
|
||||
overlineContent = {
|
||||
Text(
|
||||
stringResource(
|
||||
R.string.home_week_range,
|
||||
week.weekNumber,
|
||||
formatRange(context, week)
|
||||
)
|
||||
)
|
||||
},
|
||||
headlineContent = {
|
||||
Text(WordOfWeek.wordFor(words, week).orEmpty())
|
||||
},
|
||||
trailingContent = {
|
||||
if (offset == selectedOffset) {
|
||||
Icon(Icons.Default.Check, contentDescription = null)
|
||||
}
|
||||
},
|
||||
colors = ListItemDefaults.colors(containerColor = Color.Transparent),
|
||||
modifier = Modifier.clickable { onOffsetChange(offset) }
|
||||
)
|
||||
}
|
||||
}
|
||||
Text(
|
||||
text = stringResource(R.string.home_preview_hint),
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
modifier = Modifier.padding(start = 4.dp, top = 8.dp)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun EmptyCard(onOpenWords: () -> Unit) {
|
||||
Card(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
shape = MaterialTheme.shapes.extraLarge,
|
||||
colors = CardDefaults.cardColors(
|
||||
containerColor = MaterialTheme.colorScheme.surfaceContainerLow
|
||||
)
|
||||
) {
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(24.dp),
|
||||
horizontalAlignment = Alignment.CenterHorizontally,
|
||||
verticalArrangement = Arrangement.spacedBy(16.dp)
|
||||
) {
|
||||
Text(
|
||||
text = stringResource(R.string.msg_no_words_yet),
|
||||
style = MaterialTheme.typography.bodyLarge,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||
)
|
||||
Button(onClick = onOpenWords) {
|
||||
Text(stringResource(R.string.hint_add_word))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun WidgetCard(onAdd: () -> Unit) {
|
||||
Card(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
shape = MaterialTheme.shapes.large,
|
||||
colors = CardDefaults.cardColors(
|
||||
containerColor = MaterialTheme.colorScheme.secondaryContainer,
|
||||
contentColor = MaterialTheme.colorScheme.onSecondaryContainer
|
||||
)
|
||||
) {
|
||||
Column(Modifier.padding(16.dp), verticalArrangement = Arrangement.spacedBy(4.dp)) {
|
||||
Text(
|
||||
text = stringResource(R.string.home_widget_title),
|
||||
style = MaterialTheme.typography.titleMedium
|
||||
)
|
||||
Text(
|
||||
text = stringResource(R.string.home_widget_message),
|
||||
style = MaterialTheme.typography.bodyMedium
|
||||
)
|
||||
Spacer(Modifier.height(8.dp))
|
||||
FilledTonalButton(onClick = onAdd) {
|
||||
Text(stringResource(R.string.home_widget_action))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun weekLabel(offset: Int, week: WeekInfo): String = when (offset) {
|
||||
0 -> stringResource(R.string.home_this_week)
|
||||
1 -> stringResource(R.string.home_next_week)
|
||||
-1 -> stringResource(R.string.home_last_week)
|
||||
else -> stringResource(R.string.home_calendar_week, week.weekNumber)
|
||||
}
|
||||
|
||||
private fun formatRange(context: Context, week: WeekInfo): String {
|
||||
val zone = ZoneId.systemDefault()
|
||||
val start = week.monday.atStartOfDay(zone).toInstant().toEpochMilli()
|
||||
val end = week.sunday.atStartOfDay(zone).toInstant().toEpochMilli()
|
||||
return DateUtils.formatDateRange(
|
||||
context, start, end,
|
||||
DateUtils.FORMAT_SHOW_DATE or DateUtils.FORMAT_ABBREV_MONTH or DateUtils.FORMAT_NO_YEAR
|
||||
)
|
||||
}
|
||||
|
||||
private fun isWidgetPlaced(context: Context): Boolean =
|
||||
AppWidgetManager.getInstance(context)
|
||||
.getAppWidgetIds(ComponentName(context, SimpleWidget::class.java))
|
||||
.isNotEmpty()
|
||||
Reference in new issue
Block a user