I guess the easiest way is having a `lateinit var`...
# android-architecture
d
I guess the easiest way is having a
lateinit var
that's initialized when the viewmodel is injected in the Fragment... but that's a bit of a brittle way to do things?
s
Register and unregister the Broadcast receiver from the fragment and post received values to the view model ?
d
You mean providing a
MutableLiveData
from the
ViewModel
that the view can post to? It wouldn't have been a bad idea... but in my case, I need to combine that
Flow
with one I'm getting from a repository...
s
Maybe you could expose a broadcast receiver created in your VM that you would register/unregister in your fragment ? You might be able to get away without leaking your context 🤔. But I’d rather handle Broadcast receiver in the view part and treat it as if it was view inputs
d
I'm really trying to get rid of
Context
from my VM to make unit tests... having a Flow exposed instead of
BroadcastReceiver
code makes it easy to mock... I guess for now a
lateinit var
is the way to go... just wondering what everybody else does in this case. Your idea for
MutableLiveData
might also come in handy in other situations... thanks!
s
I wouldn’t use a
MutableLiveData
. I’m not sure it is a good practice to push from the view to the ViewModel using a MutableLiveData. Just expose a method in your view model 🤷
🙈 1
d
Yeah,
LiveData
is really to manage View lifecycles...
h
I would do it with wrapper class implementing some interface and injecting it in VM
d
Still with a
lateinit var
though... since I need to register that
BroadcastReceiver
...?
Just instead of having it on the
ViewModel
, I'd need to inject the class implementing the interface...
h
you can implement ViewModelFactory and pass that interface with constructor
i mean you can have
val
d
Yeah but the actual
var
containing the
Flow
needs to be initialized in
onCreateView()
of the Fragment so that the viewmodel can listen to it...
So either the injection must be done after creating the wrapper class in
onCreateView()
or just having a
lateinit var
just does the same?
h
Why you want to do it in
onCreateView
?
d
Because I need a
Context
to register that receiver with the lifecycle of the
Fragment
...
h
Your wrapper interface implementation can have context provided by constructor and you can use it to register broadcastreceiver
and the context can be even applicationContext
d
Yeah, I see what you mean now... I guess it could be cleaner in a sense, I just wonder if its worth the overhead unless there's more than one field... thanks!
h
if you want to not have platform specific code in
ViewModel
for testing purposes it worth 🙂
d
So for that I have a Flow... see the code I posted before (fromBroadcast) 😉
For testing I just use flowOf in the lateinit var...
a
You can extend AndoroidViewModel instead of ViewModel and inject application for the context (it accepts application as a param) and then mock it, Androidx testing core library provides now support for mocking Andoroid components