Skip to content

Commit

Permalink
example improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhirkevich Alexander Y authored and Zhirkevich Alexander Y committed Jul 24, 2024
1 parent def299f commit 0e12a92
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 12 deletions.
10 changes: 9 additions & 1 deletion example/shared/src/commonMain/kotlin/App.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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(
Expand All @@ -205,6 +212,7 @@ public fun App() {
modifier = Modifier
.fillMaxSize()
.opacityGrid()
.clickable { isPlaying = !isPlaying }
,painter = painter,
contentDescription = null
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -95,6 +89,8 @@ internal class LottieFilesViewModel() : ViewModel() {

_files.value = json.decodeFromJsonElement<List<LottieFile>>(files)
_pageCount.value = resp.get("originalPageCount")?.jsonPrimitive?.intOrNull ?: 0
_page.value = if (prevSearch == q) p else 1
prevSearch = q
} catch (t: Throwable) {
t.printStackTrace()
}
Expand Down Expand Up @@ -124,6 +120,10 @@ internal class LottieFilesViewModel() : ViewModel() {
}
}

fun onSortOrderChanged(sortOrder: SortOrder){
_sortOrder.value = sortOrder
}

fun onSearch(query: String) {
_search.value = query
}
Expand Down

0 comments on commit 0e12a92

Please sign in to comment.