Trying to use Flow for the first time for a piece ...
# coroutines
c
Trying to use Flow for the first time for a piece of app state. my app state is basically whether or not the user is logged in. I'm creating my flow this way
Copy code
@Singleton
class AppUserManager
@Inject
constructor(@ApplicationContext val context: Context) {
    var loggedInFlow = flow<Boolean> { false }
...
and then there is a login call in the appUserManager
Copy code
fun login(token: String) {
    loggedInFlow = flow { true }
...
}
then in my apps ViewModel I listen to this flow via
Copy code
@HiltViewModel
class HomeScreenViewModel
@Inject
constructor(
    var service: ApiService,
    private val appUserManager: AppUserManager
) : ViewModel() {

    init {
        reload()
        viewModelScope.launch { appUserManager.loggedInFlow.collect {
            reload()
        } }
    }
...
This doesn't work. I'm assuming I'm missing something fundamental here about how to observe a flow. Any tips for me?
1
Think I got it. Used a stateFlow instead (which seems to have the behavior I want)
Copy code
val loggedInFlow = MutableStateFlow<Boolean>(false)
collecting is the same. and I just call loggedInFlow.value = whatever and it just emits super easily.
j
If you think about the first example more I think you may be able to figure out why it doesn't work.
☝🏼 1
And it has nothing to do with Flow, really
☝🏼 2
c
I think it's just setting the variable to a brand new flowable, therefore the collection happened on the first flowable but not the second?
1
🏅 2
💡 2
👌 5
🙏 1