Skip to content

Conversation

@JoseAlcerreca
Copy link
Contributor

No description provided.

Change-Id: I18d6c90b947fc2cc98c7d2c2683f1954ace200ed
Change-Id: I10a769b54254f94ff67bf9905bc1b736bd6d1b1f
Change-Id: I7f2adb8970cba9f2a91467367e4e39328c609771
Change-Id: Ibad4143a1765e3418ddbda1f8bfdcfc7bf42d63d
Change-Id: I2e9becb5f7afaecc936f70b98c34f8b803eb847e
Copy link
Contributor

@manuelvicnt manuelvicnt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for making the changes! This looks great

Added a comment about the usage of StateFlow. Thanks!


private val _userData = MutableLiveData<ProfileScreenState>()
val userData: LiveData<ProfileScreenState> = _userData
private val _userData = MutableStateFlow<ProfileScreenState?>(null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pattern is very common when migrating from LD to StateFlow but I'd discourage having a StateFlow of a nullable type. What about creating an empty ProfileScreenState for this?

Suggested change
private val _userData = MutableStateFlow<ProfileScreenState?>(null)
private val _userData = MutableStateFlow<ProfileScreenState>(
ProfileScreenState("", "", "", "", "") // Empty user to begin with
)

The view then should check userId.isEmpty() for showing the error state, or even better, have that function as an extension function on ProfileScreenState to indicate if it's the object is properly constructed or not.

The benefits of doing this might not be obvious for this example, but it's the right pattern to use. In this way, you avoid that you can emit null again for this StateFlow and handling nullability when collecting

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we're trying to avoid null too much, this is an acceptable case for it. An empty screen state doesn't mean the same thing as null. In the view I can do:

                        if (userData == null) {
                            ProfileError()

and display better data to the user than an empty profile. Or I would have to create an extension ProfileScreenState.isEmpty() or something similar which is wasteful.

In a real app you would probably have this data wrapped into a resource sealed class. If there's a big problem with nullable types I would do that first.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ping!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine, leave it like this 🤷‍♂️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants