https://kotlinlang.org logo
Channels
100daysofcode
100daysofkotlin
100daysofkotlin-2021
advent-of-code
aem
ai
alexa
algeria
algolialibraries
amsterdam
android
android-architecture
android-databinding
android-studio
androidgithubprojects
androidthings
androidx
androidx-xprocessing
anime
anko
announcements
apollo-kotlin
appintro
arabic
argentina
arkenv
arksemdevteam
armenia
arrow
arrow-contributors
arrow-meta
ass
atlanta
atm17
atrium
austin
australia
austria
awesome-kotlin
ballast
bangladesh
barcelona
bayarea
bazel
beepiz-libraries
belgium
benchmarks
berlin
big-data
books
boston
brazil
brikk
budapest
build
build-tools
bulgaria
bydgoszcz
cambodia
canada
carrat
carrat-dev
carrat-feed
chicago
chile
china
chucker
cincinnati-user-group
cli
clikt
cloudfoundry
cn
cobalt
code-coverage
codeforces
codemash-precompiler
codereview
codingame
codingconventions
coimbatore
collaborations
colombia
colorado
communities
competitive-programming
competitivecoding
compiler
compose
compose-android
compose-desktop
compose-hiring
compose-ios
compose-mp
compose-ui-showcase
compose-wear
compose-web
confetti
connect-audit-events
corda
cork
coroutines
couchbase
coursera
croatia
cryptography
cscenter-course-2016
cucumber-bdd
cyprus
czech
dagger
data2viz
databinding
datascience
dckotlin
debugging
decompose
decouple
denmark
deprecated
detekt
detekt-hint
dev-core
dfw
docs-revamped
dokka
domain-driven-design
doodle
dsl
dublin
dutch
eap
eclipse
ecuador
edinburgh
education
effective-kotlin
effectivekotlin
emacs
embedded-kotlin
estatik
event21-community-content
events
exposed
failgood
fb-internal-demo
feed
firebase
flow
fluid-libraries
forkhandles
forum
fosdem
fp-in-kotlin
framework-elide
freenode
french
fritz2
fuchsia
functional
funktionale
gamedev
ge-kotlin
general-advice
georgia
geospatial
german-lang
getting-started
github-workflows-kt
glance
godot-kotlin
google-io
gradle
graphic
graphkool
graphql
graphql-kotlin
graviton-browser
greece
grpc
gsoc
gui
hackathons
hacktoberfest
hamburg
hamkrest
helios
helsinki
hexagon
hibernate
hikari-cp
hire-me
hiring
hongkong
hoplite
http4k
hungary
hyderabad
image-processing
india
indonesia
inkremental
intellij
intellij-plugins
intellij-tricks
internships
introduce-yourself
io
ios
iran
israel
istanbulcoders
italian
jackson-kotlin
jadx
japanese
jasync-sql
java-to-kotlin-refactoring
javadevelopers
javafx
javalin
javascript
jdbi
jhipster-kotlin
jobsworldwide
jpa
jshdq
juul-libraries
jvm-ir-backend-feedback
jxadapter
k2-early-adopters
kaal
kafka
kakao
kalasim
kapt
karachi
karg
karlsruhe
kash_shell
kaskade
kbuild
kdbc
kgen-doc-tools
kgraphql
kinta
klaxon
klock
kloudformation
kmdc
kmm-español
kmongo
knbt
knote
koalaql
koans
kobalt
kobweb
kodein
kodex
kohesive
koin
koin-dev
komapper
kondor-json
kong
kontent
kontributors
korau
korean
korge
korim
korio
korlibs
korte
kotest
kotest-contributors
kotless
kotlick
kotlin-asia
kotlin-beam
kotlin-by-example
kotlin-csv
kotlin-data-storage
kotlin-foundation
kotlin-fuel
kotlin-in-action
kotlin-inject
kotlin-latam
kotlin-logging
kotlin-multiplatform-contest
kotlin-mumbai
kotlin-native
kotlin-pakistan
kotlin-plugin
kotlin-pune
kotlin-roadmap
kotlin-samples
kotlin-sap
kotlin-serbia
kotlin-spark
kotlin-szeged
kotlin-website
kotlinacademy
kotlinbot
kotlinconf
kotlindl
kotlinforbeginners
kotlingforbeginners
kotlinlondon
kotlinmad
kotlinprogrammers
kotlinsu
kotlintest
kotlintest-devs
kotlintlv
kotlinultimatechallenge
kotlinx-datetime
kotlinx-files
kotlinx-html
kotrix
kotson
kovenant
kprompt
kraph
krawler
kroto-plus
ksp
ktcc
ktfmt
ktlint
ktor
ktp
kubed
kug-leads
kug-torino
kvision
kweb
lambdaworld_cadiz
lanark
language-evolution
language-proposals
latvia
leakcanary
leedskotlinusergroup
lets-have-fun
libgdx
libkgd
library-development
lincheck
linkeddata
lithuania
london
losangeles
lottie
love
lychee
macedonia
machinelearningbawas
madrid
malaysia
mathematics
meetkotlin
memes
meta
metro-detroit
mexico
miami
micronaut
minnesota
minutest
mirror
mockk
moko
moldova
monsterpuzzle
montreal
moonbean
morocco
motionlayout
mpapt
mu
multiplatform
mumbai
munich
mvikotlin
mvrx
myndocs-oauth2-server
naming
navigation-architecture-component
nepal
new-mexico
new-zealand
newname
nigeria
nodejs
norway
npm-publish
nyc
oceania
ohio-kotlin-users
oldenburg
oolong
opensource
orbit-mvi
osgi
otpisani
package-search
pakistan
panamá
pattern-matching
pbandk
pdx
peru
philippines
phoenix
pinoy
pocketgitclient
polish
popkorn
portugal
practical-functional-programming
proguard
prozis-android-backup
pyhsikal
python
python-contributors
quasar
random
re
react
reaktive
realm
realworldkotlin
reductor
reduks
redux
redux-kotlin
refactoring-to-kotlin
reflect
refreshversions
reports
result
rethink
revolver
rhein-main
rocksdb
romania
room
rpi-pico
rsocket
russian
russian_feed
russian-kotlinasfirst
rx
rxjava
san-diego
science
scotland
scrcast
scrimage
script
scripting
seattle
serialization
server
sg-user-group
singapore
skia-wasm-interop-temp
skrape-it
slovak
snake
sofl-user-group
southafrica
spacemacs
spain
spanish
speaking
spek
spin
splitties
spotify-mobius
spring
spring-security
squarelibraries
stackoverflow
stacks
stayhungrystayfoolish
stdlib
stlouis
strife-discord-lib
strikt
students
stuttgart
sudan
swagger-gradle-codegen
swarm
sweden
swing
swiss-user-group
switzerland
talking-kotlin
tallinn
tampa
teamcity
tegal
tempe
tensorflow
terminal
test
testing
testtestest
texas
tgbotapi
thailand
tornadofx
touchlab-tools
training
tricity-kotlin-user-group
trójmiasto
truth
tunisia
turkey
turkiye
twitter-feed
uae
udacityindia
uk
ukrainian
uniflow
unkonf
uruguay
utah
uuid
vancouver
vankotlin
vertx
videos
vienna
vietnam
vim
vkug
vuejs
web-mpp
webassembly
webrtc
wimix_sentry
wwdc
zircon
Powered by
Title
s

Stylianos Gakis

03/22/2023, 6:06 PM
Using
ColumnScope.AnimatedVisibility
gives this annoying problem of having to do double check for the state, since the content lambda does not give the current state. Like:
AnimatedVisibility(model != null) {
  if (model != null) {
    ActualComposable(model)
    Spacer(Modifier.height(20.dp))
  }
}
Since I am not sure if doing
!!
in there is safe, as the old content may still be animating out, and it will call
!!
on the null item to try and render the item as it’s leaving. What do you folks usually do for this? Do I just have to do this double null check or is there something I can do instead, some API I may be missing?
I can see one can do something like
AnimatedContent(
  targetState = model,
  label = "AnimatedContent",
  transitionSpec = {
    ContentTransform(
      targetContentEnter = fadeIn() + expandVertically(),
      initialContentExit = fadeOut() + shrinkVertically(),
    )
  },
) { model ->
  if (model != null) {
    ActualComposable()
  }
}
to use AnimatedContent which does in fact give me the current item inside the lamvda, but then I need to specify the ContentTransform myself and so on, which I do like that AnimatedVisibility on a Column puts a sane default there.
e

ephemient

03/22/2023, 6:30 PM
s

Stylianos Gakis

03/22/2023, 6:42 PM
Thanks, I +1 that one
a

Alex Vanyo

03/22/2023, 7:02 PM
That wouldn’t apply in this case though, it’s definitely possible that the content inside
AnimatedVisibility
is getting called, even if
model
is
null
. This is a case where the strict data flow of Compose makes the required code a bit more explicit for what’s actually happening. Let’s say that the animation for
AnimatedVisibility
takes 10 minutes to animate out (the world’s slowest remove animation) Even if you clear the model, something somewhere still needs to hold onto it to continue to render the UI for it for the next 10 minutes. The user could be doing other actions, other things could be changing, and the content inside the
AnimatedVisibility
is still being composed and updated.
s

Stylianos Gakis

03/22/2023, 7:07 PM
Hmm that’s a very valid point, and the example of having the animation take 10 minutes really makes this obvious. I really wonder what I should be doing there then. My content in there takes in this entire model which has quite a lot of state, so I can’t “fake” it just while it’s animating out. In theory, what I want in there is for the last known state to be saved as a screenshot, and have that thing animate out, as I don’t need it to be interactive, update and so on. But I know I can’t quite do that. I guess there isn’t a straight-forward solution to this as I see it hmm
a

Alex Vanyo

03/22/2023, 7:11 PM
The main documentation for
AnimatedVisibility
has a sample for managing a list where items can be removed with an animation out which might be interesting to take a look at: https://developer.android.com/reference/kotlin/androidx/compose/animation/package-summary#AnimatedVisibility(androidx[…]lin.String,kotlin.Function1) The core idea is that an item is only completely removed from everywhere once it has finished animated out.
In theory, what I want in there is for the last known state to be saved as a screenshot
What you could maybe sort of do is “cache” with
remember
the item to still render with, and keep that around even if the item is deleted higher up. Or have two lists: the “logical” list, which are the items that will be in the list if all animations resolve, and the “display” list, which might have additional items that are in the process of animating out.
s

Stylianos Gakis

03/22/2023, 7:21 PM
Yeah that solution from the link is interesting, but would definitely not want to introduce all this extra code to make this animation look a bit better, at least not in what I am working on rn which doesn’t need the animation to look that perfect. I like the idea of temporarily remembering the item for a bit longer, but again I’m sure if I look back at this in 1 month from now I’ll start wondering what the hell I was doing with this extra logic in the UI 😅 Thing is with all these, in a more animation-rich screen I’d probably justify doing it, but now I was trying to get an easy win by animating some portion of a column out instead of having it just disappear. But this “easy win” doesn’t end up being that easy after all unfortunately.
Thanks a lot for helping me by explaining the options I have btw, I appreciate it a lot, despite me saying that I am most likely going to decide not to use any of those at least right now. I’ll probably need it in the future 😊
Hey you know what, after a good night’s sleep, the solution became super obvious. I just pass a nullable model to
ActualComposable(model)
, and I handle the null case in there. In my case I can basically show most of what I was showing already (it’s a card) with the same height and all, so the animation won’t be jumpy. This model was needed for some of the text only in this case (“Foo” in the video), so the bad thing is just that that text shows nothing instantaneously when it starts leaving instead of also animating out, but I get this animation without adding any extra logic just for this animation like remebering stuff and so on. So that’s much better than what I first had anyway. Thanks so much for helping me out!
TIL
expandVertically
and
shrinkVertically
can take in
expandFrom = Alignment.CenterVertically
and
shrinkTowards = Alignment.CenterVertically
. Every time I do anything with the animation APIs it’s always so pleasant to work with to do these little touches that take like no effort, it’s insane.
e

efemoney

03/23/2023, 12:58 PM
if I look back at this in 1 month from now I’ll start wondering what the hell I was doing
Thats what comments are for 😅
s

Stylianos Gakis

03/23/2023, 5:07 PM
Thats what comments are for
The comments will start lying before you know it. They’re sneaky like that 😅