DEV Community

Discussion on: Translatable Strings In View Models, Without Android SDK imports

Collapse
 
ychescale9 profile image
Yang

But this won’t be type-safe if you just add a vararg at the end and you lose the lint check you get when doing this directly with context.getString(...).

Or do you mean adding a function for each string resource that takes some argument(s)? Wouldn’t this make the mapper not generic any more and you’ll have to maintain / test it often?

I’ve given up messing with string / any other resources in ViewModel (AndroidViewModel sucks) directly 😅 and instead just expose streams of states for Fragment / Activity to render which includes mapping the states to the correct string / color resources etc. This way you don’t need to mock your string mapper in your ViewModelTest and your UI tests will cover your string resource loading anyway.