diff --git a/example/shared/src/commonMain/kotlin/App.kt b/example/shared/src/commonMain/kotlin/App.kt index 25006b1d..6cd473df 100644 --- a/example/shared/src/commonMain/kotlin/App.kt +++ b/example/shared/src/commonMain/kotlin/App.kt @@ -1,6 +1,7 @@ import androidx.compose.animation.animateContentSize import androidx.compose.foundation.Image import androidx.compose.foundation.border +import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.ExperimentalLayoutApi @@ -147,7 +148,7 @@ public fun App() { // LottieCompositionSpec.ResourceString("expr/move_horizontal.json") // LottieCompositionSpec.ResourceString("expr/wiggle.json") // LottieCompositionSpec.ResourceString("expr/noise.json") -// LottieCompositionSpec.ResourceString(WONDERS) + LottieCompositionSpec.ResourceString(TEST) // // LottieCompositionSpec.Url( // "https://assets-v2.lottiefiles.com/a/9286b092-117a-11ee-b857-2712bc869389/WSepKUr5be.lottie" @@ -179,9 +180,15 @@ public fun App() { contentAlignment = Alignment.Center ) { + var isPlaying by remember { + mutableStateOf(true) + } + val progress = animateLottieCompositionAsState( iterations = Compottie.IterateForever, composition = composition.value, + isPlaying = isPlaying, + restartOnPlay = false ) val painter = rememberLottiePainter( @@ -205,6 +212,7 @@ public fun App() { modifier = Modifier .fillMaxSize() .opacityGrid() + .clickable { isPlaying = !isPlaying } ,painter = painter, contentDescription = null ) diff --git a/example/shared/src/commonMain/kotlin/lottiefiles/LottieFilesScreen.kt b/example/shared/src/commonMain/kotlin/lottiefiles/LottieFilesScreen.kt index 95615453..eb1c3a81 100644 --- a/example/shared/src/commonMain/kotlin/lottiefiles/LottieFilesScreen.kt +++ b/example/shared/src/commonMain/kotlin/lottiefiles/LottieFilesScreen.kt @@ -27,14 +27,19 @@ import androidx.compose.foundation.lazy.grid.items import androidx.compose.foundation.lazy.grid.rememberLazyGridState import androidx.compose.foundation.shape.CircleShape import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.automirrored.filled.Sort import androidx.compose.material.icons.filled.Close +import androidx.compose.material.icons.filled.Done import androidx.compose.material.icons.filled.Person import androidx.compose.material.icons.filled.Search import androidx.compose.material.icons.outlined.FileDownload import androidx.compose.material.icons.rounded.ArrowBackIos import androidx.compose.material.icons.rounded.ArrowForwardIos +import androidx.compose.material3.AssistChip import androidx.compose.material3.Card import androidx.compose.material3.CardDefaults +import androidx.compose.material3.DropdownMenu +import androidx.compose.material3.DropdownMenuItem import androidx.compose.material3.ElevatedCard import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.Icon @@ -53,6 +58,7 @@ import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember +import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier @@ -108,15 +114,79 @@ internal fun LottieFilesScreen( } } - Surface { + Surface(modifier) { BoxWithConstraints { + + val isWideScreen = constraints.maxWidth > LocalDensity.current.run { + 400.dp.toPx() + } Column( - modifier = modifier, horizontalAlignment = Alignment.CenterHorizontally ) { - SearchBar( - viewModel = viewModel - ) + Row( + verticalAlignment = Alignment.CenterVertically, + horizontalArrangement = Arrangement.spacedBy(12.dp), + modifier = Modifier + .fillMaxWidth() + .padding(horizontal = 12.dp) + ) { + SearchBar( + modifier = Modifier + .weight(1f) + .padding(bottom = 12.dp), + viewModel = viewModel + ) + + var sortExpanded by rememberSaveable { + mutableStateOf(false) + } + + val sort by viewModel.sortOrder.collectAsState() + + Box { + AssistChip( + onClick = { + sortExpanded = true + }, + leadingIcon = { + Icon( + imageVector = Icons.AutoMirrored.Filled.Sort, + contentDescription = null + ) + }, + label = { + Text(if (isWideScreen) sort.name else sort.name.take(1)) + } + ) + DropdownMenu( + expanded = sortExpanded, + onDismissRequest = { + sortExpanded = false + } + ) { + SortOrder.entries.forEach { + DropdownMenuItem( + leadingIcon = if (it == sort){ + { + Icon( + imageVector = Icons.Default.Done, + contentDescription = "Selected" + ) + } + } else null, + text = { + Text(it.name) + }, + onClick = { + sortExpanded = false + viewModel.onSortOrderChanged(it) + } + ) + } + } + } + } + val files = viewModel.files.collectAsState().value diff --git a/example/shared/src/commonMain/kotlin/lottiefiles/LottieFilesViewModel.kt b/example/shared/src/commonMain/kotlin/lottiefiles/LottieFilesViewModel.kt index bcc3f9c8..83a83ffe 100644 --- a/example/shared/src/commonMain/kotlin/lottiefiles/LottieFilesViewModel.kt +++ b/example/shared/src/commonMain/kotlin/lottiefiles/LottieFilesViewModel.kt @@ -70,12 +70,6 @@ internal class LottieFilesViewModel() : ViewModel() { Triple(q, s, p) }.collectLatest { (q, s, p) -> - if (prevSearch != null && prevSearch != q){ - prevSearch = q - _page.value = 1 - _pageCount.value = 1 - return@collectLatest - } try { val resp = httpClient.get( "https://lottiefiles.com/api/search/get-animations" @@ -95,6 +89,8 @@ internal class LottieFilesViewModel() : ViewModel() { _files.value = json.decodeFromJsonElement>(files) _pageCount.value = resp.get("originalPageCount")?.jsonPrimitive?.intOrNull ?: 0 + _page.value = if (prevSearch == q) p else 1 + prevSearch = q } catch (t: Throwable) { t.printStackTrace() } @@ -124,6 +120,10 @@ internal class LottieFilesViewModel() : ViewModel() { } } + fun onSortOrderChanged(sortOrder: SortOrder){ + _sortOrder.value = sortOrder + } + fun onSearch(query: String) { _search.value = query }