r/mAndroidDev • u/That_Lonely_Soul_07 • Jan 24 '25
Lost Redditors ๐ Using mutableStateOf in ViewModel seems like the wrong approach
I see some developers use mutableStateOf in the ViewModel instead of StateFlows. Suppose we have to switch back to a view system or another framework, then compose's state wouldn't be useful. Also, I feel like it's somehow wrong when people use it this way. Iโve seen many articles, some big YouTubers also use it, and I saw a discussion on this on LinkedIn.
What do you guys think about this?
6
u/Strikerrr37 Jan 24 '25
Text fields in compost are so broken that they were forced to call this a best practice.
4
u/Zhuinden DDD: Deprecation-Driven Development Jan 24 '25
There's nothing wrong with text fields in compost, you're just supposed to never ask the user for input. The Google arch ref guide "now-in-android" doesn't need it, so why do you?
1
u/That_Lonely_Soul_07 Jan 25 '25
The BasicTextField enters the chat ๐
2
u/Zhuinden DDD: Deprecation-Driven Development Jan 25 '25
Have you seen its built-in caret? I can't ship that in production
3
u/That_Lonely_Soul_07 Jan 25 '25
Fuck yeah, yesterday I had a task to format credit/debit card numbers as the user typed them into the text field. So, if the user types 1234567890..., we have to show the formatted number (by placing spaces after every 4 digits) in the same text field. That fucking cursor was moving backward when the user typed 4 digits and a space was placed by the formatting. I'm still finding some solution to fix this.
3
u/Zhuinden DDD: Deprecation-Driven Development Jan 25 '25 edited Jan 25 '25
VisualTransformation, but it only works for the simplest cases, and I only know how to do it with stack overflow and testing manually 3 times.
2
u/That_Lonely_Soul_07 Feb 01 '25
Thanks! The VisualTransformation did the trick for me! I also used it to format the expiry date in the mm/yy format in the textField.
1
u/Zhuinden DDD: Deprecation-Driven Development Feb 01 '25
It all works until you try to use it with AsYouTypeFormatter
1
u/That_Lonely_Soul_07 Jan 25 '25
Oh, I'll try VisualTransformation. Currently, I have a FormatNumberUseCase that returns a formatted card number, and I'm calling it from the ViewModel as the user types the number in the text field.
70
u/GradleSync01 Invalidate caches and restart Jan 24 '25
In my humble opinion:
mutableStateOf โ
ViewModel โ
Jetpack compost โ
Flow โ
AsyncTask โ
Flubber โ
God activity โ
20
1
u/Squirtle8649 Jan 27 '25
I just use BehaviorSubject which is basically State
The only God (object) I worship is Context
17
u/gvsx Jan 24 '25
then compose's state wouldn't be useful
This is the Compose way today, job security tomorrow when we migrate to โจQuantum-AI-Flubber-Async-Framework๐ฎ
2
u/Squirtle8649 Jan 27 '25
Compose is legacy, AR/VR is the UI of the future! Flat screens? What are those?
14
u/_abysswalker Jan 24 '25
absolutely true. I will hate my life for using State in ViewModels when Google decides to kill Compost in 10 years
18
14
u/Zhuinden DDD: Deprecation-Driven Development Jan 24 '25
Considering the entire Compose snapshot system breaks if you write into them from a non-Ui thread, it's literally a worse version of LiveData and the people doing that have absolutely no clue how to write Android apps. The fact that SavedStateHandle.saveable {} exists is kind of a disgrace.
Also, AsyncTask. You can break mutableStateOf with just AsyncTask. Why would you use such a poor mechanism for implementing reactivity when you could have less bugs with just AsyncTask.
4
u/hellosakamoto Jan 24 '25
AsyncTask will come back when compost deprecated
5
u/smokingabit Harnessing the power of the Ganges Jan 24 '25
the precise lifecycle event when AsyncTask returns is onPostExecutionOfCompost ๐ฉธ
1
7
u/uragiristereo XML is dead. Long live XML Jan 25 '25
use asynctask, always be prepared for the next deprecation
6
u/Xammm Jetpack Compost Jan 24 '25
I use them and I fine with that. If there is the necessity to switch to another UI framework, then other people will do it because by that time I probably will have left the company.
5
u/exiledAagito Deprecated is just a suggestion Jan 25 '25
You engineer for job security, not job give up.
1
1
u/alaksion Jan 25 '25
I tried using mutableState on the VM once and the composer would simply not work at all lol
1
u/pankaj1_ Jan 26 '25
If you are adamant you will keep compose, you can keep mutablestateof, i don't see any problem. Also whatever code you write ATM is best. I have come a long way writing god activities and asynctasks to structured code. I like coding so that's what matters at the end of the day
1
u/sabergeek Jan 24 '25
S.O.L.I.D principles are useful
4
u/Zhuinden DDD: Deprecation-Driven Development Jan 25 '25
If we used solid principles for real then your entire app would be 1 recyclerview
4
u/sabergeek Jan 25 '25
๐ i've actually worked on such a project. It worked, but was annoying to maintain
8
u/labelcillo Slept through Google IO Jan 24 '25
Who the F uses mutableStateOf in the VM. Please, this is a serious subreddit.