|
| 1 | +/* |
| 2 | + * Nextcloud Talk - Android Client |
| 3 | + * |
| 4 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 5 | + * SPDX-License-Identifier: GPL-3.0-or-later |
| 6 | + */ |
| 7 | + |
| 8 | +package com.nextcloud.talk.conversationlist.ui |
| 9 | + |
| 10 | +import androidx.compose.animation.AnimatedVisibility |
| 11 | +import androidx.compose.animation.core.RepeatMode |
| 12 | +import androidx.compose.animation.core.animateFloat |
| 13 | +import androidx.compose.animation.core.infiniteRepeatable |
| 14 | +import androidx.compose.animation.core.rememberInfiniteTransition |
| 15 | +import androidx.compose.animation.core.tween |
| 16 | +import androidx.compose.animation.fadeIn |
| 17 | +import androidx.compose.animation.fadeOut |
| 18 | +import androidx.compose.foundation.background |
| 19 | +import androidx.compose.foundation.layout.Box |
| 20 | +import androidx.compose.foundation.layout.Column |
| 21 | +import androidx.compose.foundation.layout.Row |
| 22 | +import androidx.compose.foundation.layout.Spacer |
| 23 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 24 | +import androidx.compose.foundation.layout.height |
| 25 | +import androidx.compose.foundation.layout.padding |
| 26 | +import androidx.compose.foundation.layout.size |
| 27 | +import androidx.compose.foundation.layout.width |
| 28 | +import androidx.compose.foundation.shape.CircleShape |
| 29 | +import androidx.compose.foundation.shape.RoundedCornerShape |
| 30 | +import androidx.compose.material3.MaterialTheme |
| 31 | +import androidx.compose.runtime.Composable |
| 32 | +import androidx.compose.runtime.getValue |
| 33 | +import androidx.compose.ui.Alignment |
| 34 | +import androidx.compose.ui.Modifier |
| 35 | +import androidx.compose.ui.draw.clip |
| 36 | +import androidx.compose.ui.tooling.preview.Preview |
| 37 | +import androidx.compose.ui.unit.dp |
| 38 | +import android.content.res.Configuration |
| 39 | +import androidx.compose.foundation.isSystemInDarkTheme |
| 40 | +import androidx.compose.material3.darkColorScheme |
| 41 | +import androidx.compose.material3.lightColorScheme |
| 42 | + |
| 43 | +private const val SHIMMER_ITEM_COUNT = 4 |
| 44 | +private const val SHIMMER_ANIM_DURATION_MS = 800 |
| 45 | +private const val SHIMMER_ALPHA_MIN = 0.2f |
| 46 | +private const val TITLE_WIDTH = 0.6f |
| 47 | +private const val SUBLINE_WIDTH = 0.9f |
| 48 | + |
| 49 | +private const val SHIMMER_ALPHA_MAX = TITLE_WIDTH |
| 50 | + |
| 51 | +/** |
| 52 | + * Top-level wrapper rendered by the shimmer_compose_view ComposeView. |
| 53 | + * Animates in/out via [AnimatedVisibility] and shows skeleton placeholder rows |
| 54 | + * while the conversation list is loading for the first time. |
| 55 | + */ |
| 56 | +@Composable |
| 57 | +fun ConversationListSkeleton(isVisible: Boolean, itemCount: Int = SHIMMER_ITEM_COUNT) { |
| 58 | + AnimatedVisibility( |
| 59 | + visible = isVisible, |
| 60 | + enter = fadeIn(), |
| 61 | + exit = fadeOut() |
| 62 | + ) { |
| 63 | + val infiniteTransition = rememberInfiniteTransition(label = "shimmer") |
| 64 | + val shimmerAlpha by infiniteTransition.animateFloat( |
| 65 | + initialValue = SHIMMER_ALPHA_MIN, |
| 66 | + targetValue = SHIMMER_ALPHA_MAX, |
| 67 | + animationSpec = infiniteRepeatable( |
| 68 | + animation = tween(durationMillis = SHIMMER_ANIM_DURATION_MS), |
| 69 | + repeatMode = RepeatMode.Reverse |
| 70 | + ), |
| 71 | + label = "shimmerAlpha" |
| 72 | + ) |
| 73 | + val shimmerColor = MaterialTheme.colorScheme.onSurface.copy(alpha = shimmerAlpha) |
| 74 | + |
| 75 | + Column { |
| 76 | + repeat(itemCount) { |
| 77 | + ShimmerConversationItem(shimmerColor = shimmerColor) |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +@Composable |
| 84 | +private fun ShimmerConversationItem(shimmerColor: androidx.compose.ui.graphics.Color) { |
| 85 | + Row( |
| 86 | + modifier = Modifier |
| 87 | + .fillMaxWidth() |
| 88 | + .padding(horizontal = 16.dp, vertical = 8.dp), |
| 89 | + verticalAlignment = Alignment.CenterVertically |
| 90 | + ) { |
| 91 | + Box( |
| 92 | + modifier = Modifier |
| 93 | + .size(48.dp) |
| 94 | + .clip(CircleShape) |
| 95 | + .background(shimmerColor) |
| 96 | + ) |
| 97 | + |
| 98 | + Spacer(modifier = Modifier.width(16.dp)) |
| 99 | + |
| 100 | + Column(modifier = Modifier.weight(1f)) { |
| 101 | + Box( |
| 102 | + modifier = Modifier |
| 103 | + .fillMaxWidth(TITLE_WIDTH) |
| 104 | + .height(14.dp) |
| 105 | + .clip(RoundedCornerShape(4.dp)) |
| 106 | + .background(shimmerColor) |
| 107 | + ) |
| 108 | + |
| 109 | + Spacer(modifier = Modifier.height(6.dp)) |
| 110 | + |
| 111 | + Box( |
| 112 | + modifier = Modifier |
| 113 | + .fillMaxWidth(SUBLINE_WIDTH) |
| 114 | + .height(12.dp) |
| 115 | + .clip(RoundedCornerShape(4.dp)) |
| 116 | + .background(shimmerColor) |
| 117 | + ) |
| 118 | + } |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +@Preview(showBackground = true, name = "Shimmer – visible") |
| 123 | +@Preview(showBackground = true, name = "Shimmer – visible (dark)", uiMode = Configuration.UI_MODE_NIGHT_YES) |
| 124 | +@Composable |
| 125 | +private fun ShimmerVisibleDarkPreview() { |
| 126 | + val colorScheme = if (isSystemInDarkTheme()) darkColorScheme() else lightColorScheme() |
| 127 | + MaterialTheme(colorScheme = colorScheme) { |
| 128 | + ConversationListSkeleton(isVisible = true) |
| 129 | + } |
| 130 | +} |
0 commit comments