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
hiring-french
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
t

Tim Malseed

03/22/2023, 10:37 PM
Hey team, I’m still dealing with this compose nav problem, and it’s really doing my head in 😭 The app launches and shows the ‘start destination’, the home screen. It sees that the ‘viewState’ is ‘unauthorized’, so it navigates to the login screen (via launchedEffect, keyed on the viewState) We go to the login screen, and perform login, then pop the login screen off the back stack. As far as I can tell, this is all following navigation best practice. But, after popping back to the home screen, the viewState is still ‘unauthorized’ momentarily. Because we’ve navigated back to the home screen, the launched effect fires again, and we navigate once again to the login screen. It sees we’re authorized, navigates back to home. And now the home view state is up to date (authorized), and we render the home content. So, the navigation is home -> login -> home -> login -> home, when it should be home -> login -> home.
There are a couple of things that might be contributing. The viewState is derived in the ViewModel, from a StateFlow, which uses
stateIn(viewModelScope, whileSubscribed(5000), ViewState.Loading)
p

Pablichjenkov

03/22/2023, 10:54 PM
Make sure you update the authorization data/token before popping the login screen.
t

Tim Malseed

03/22/2023, 10:55 PM
The login screen calls a suspend function to log in, and waits until that has completed before popping the back stack.
That log in function changes the ‘authorization state’ to ‘authorized’. This is exposed via a stateflow, the same stateflow that the home screen view model’s ‘view state’ is derived from
p

Pablichjenkov

03/22/2023, 11:00 PM
You mean login() right?
So what's the problem the state flow is not updating the value, does it have some operator applied that is buffering the events
Soon as you update the State flow from the login() function call it should override the previous state
t

Tim Malseed

03/22/2023, 11:17 PM
So, to demonstrate the issue by way of logs:
10:11:56.288  onDestinationChanged: home_route // start destination

10:11:56.450  AuthorizationState: Unauthorized // User is unauthorized

10:11:56.524  ViewState: Loading // Initial view state

10:11:56.803  ViewState: Login // Because user is unauthorized

10:11:56.803  navController.navigate(login_route) // Navigate to login screen

10:11:56.806  onDestinationChanged: login_route // User arrives. Presses 'login'

10:12:05.158  AuthorizationState: Authorized // Auth state changes to 'authorized'

10:12:05.726  navController.popBackStack() // go back to home screen

10:12:05.727  onDestinationChanged: home_route // we're back

10:12:05.745  ViewState: Login // ViewState is Login? Should be 'Ready'

10:12:05.745  navController.navigate(login_route) // Navigate back to the login screen

10:12:05.750  onDestinationChanged: login_route // Go to login screen

10:12:05.786  navController.popBackStack() // Oh, user is authorized, pop back again

10:12:05.788  onDestinationChanged: home_route // Back to home

10:12:05.837  ViewState: Ready // Now the home screen has the up-to-date view state
The stateflow from which ‘view state’ is derived is up-to-date. But when we go back to the home screen, the ‘viewState’ itself is stale
I’m starting to think I need to do something dumb, like `delay(50)`before calling
popBackStack()
p

Pablichjenkov

03/22/2023, 11:30 PM
What about adding another state, 'AwaitingLoginResult'. You would set the ViewState in that value soon as you navigate to the login screen. In that state you could show a loader or simply do nothing, just wait.
t

Tim Malseed

03/22/2023, 11:32 PM
Yeah, that’s a better solution. Still, it feels like these are workarounds for a more fundamental problem
p

Pablichjenkov

03/22/2023, 11:36 PM
Some problems that are easily modeled with an event based system are harder to model in a representational state system. The only advantage I may see in the state modeling is testability. But right, you pretty much end up writing a state machine for any elemental things
I would rename Login State to RequestLogin State
t

Tim Malseed

03/22/2023, 11:42 PM
I was hoping to have the view state derived from the user auth state. So, user auth is the ‘source of truth’. But, having to add in some intermediate state, like ‘AwaitingLoginResult’ means I now need to imperatively update the view state (as well as having it derived from the auth state). Maybe I’m thinking about this wrong, but it feels like a recipe for different bugs
Hmm, maybe not. I can’t actually think of a scenario where this would cause the view state to somehow be incorrect
p

Pablichjenkov

03/22/2023, 11:47 PM
I see it as a state machine with multiple inputs. Authorization data is one input but there could be other inputs like user events or so. The State of the View is derived from the response the machine reacts to any input change in the given state
In a perfect world only user auth would be sufficient but usually there are other variables involved.
Perhaps don't create an AwaitingLoginResult state, perhaps go to an Idle state, could be more convenient
Idle simply do nothing
t

Tim Malseed

03/22/2023, 11:55 PM
Idle, or ‘loading’ is already the initial view state. I wonder if I can use something similar to
collectAsStateWithLifecycle()
, which resets its state when the lifecycle changes
In other words, if we come back to the home screen, and so its lifecycle changes to ‘started’, the view state is reset to ‘loading’
a

Alex Vanyo

03/23/2023, 12:54 AM
If you’re using
WhileSubscribed(5000)
, the previous value of the
StateFlow
will be cached and it won’t expire due to the default value for
replayExpiration
, the second parameter of
WhileSubscribed
So even if
Loading
is the default state, it’ll be caching the last one seen upon returning. Maybe for authentication state in particular, it’d be better to not cache that last value? Maybe set
replayExpiration
to
0
, and maybe also
stopTimeoutMillis
to
0
too specifically for the authentication:
WhileSubscribed(0, 0)
That would ensure that you’re refreshing the flow upon going back to the home screen, which will cause the initial
Loading
state again
t

Tim Malseed

03/23/2023, 12:58 AM
I tried setting
stopTimeoutMillis
to 0, but I didn’t even consider this
replayExpiration
parameter! That seems to solve the problem
I need to have a read of the
WhileSubscribed
docs and think about this a bit more
Alright, I guess this is a gotcha to watch out for. If you have a launched effect keyed by some state derived from a stateflow, consider how caching/replay of the sharingStarted property might affect that launched effect
c

Colton Idle

03/26/2023, 2:26 PM
I had to do this in my app recently. What I did was move the "authorized" state out of the viewModel and into "AppState" and then I only ever have a single source of truth on whether the user is auth'd or not. Works great!
t

Tim Malseed

03/28/2023, 3:14 AM
I don’t mind that suggestion - although it’s not like the current approach has multiple sources of truth around whether the user is authed