can I use MVVM without LiveData?
# android-architecture
o
can I use MVVM without LiveData?
👌 7
s
Yes
o
why not @commanderpepper
s
LiveData is just a data type which also let you observe with lifecycleowner. So it should be okey to use it without VM
o
yes makes sense
c
I gave a thumbs down because I don't like single word responses. I'm not a fan of 'Yes' or 'No'.
o
so you are a fan of a single reaction emoji?
😆 2
🙂
you literally did the same as him
lol
s
I start with Yes to ensure you it is not a stupid question.
o
im aware
c
Yeah you're right, I didn't let him respond with greater detail. I'm sorry for the hasty emoji response.
o
its daunting to follow a damn tutorial video 80 episdoes on how to do clean arch
what’s this about presenters AND viewmodels in the same arch?
m
You could use RxJava, or nothing, but it depends if you go full on with ViewModel from Android Architecture component or your own implementation. In all cases for MVVM to work, you need to use the Observer design pattern
o
from google’s own intro to ViewModels video
i do use Rx
the point here is easy testing, and to do that I’ve found that i am slowly converging towards VM
because i started with the test, the rest is having to adhere to the same structure kind of
ohh ok ok, so the viewmodel is literally all the view stuff that used to sit inside the activity
now the activity just reads like a set of instructions
presenters hand off to repository for calls, give back to viewmodel for display/change
activity just orchestrates
m
pretty much, I just don’t use presenters with VM, i put all the logic in the repository for now
o
hmm
yea same same
useless to have a presenter
because…if you write a test for example, to test the presenter
all you’ll be doing is making sure the call went out to the repo
which is useless
m
pretty much
o
ok good
glad i went and asked what a viewmodel was 😛
do you use LiveData ?
m
yes I do
g
if you already use RX, LiveData won't give you much. Although, I'd still consider replacing your rx code in Views with LD. Because it's a bit more handy there. Also, if you want to move to channels from RX in the feature, you'll have less work to do.
o
I don’t have any Rx code in views
all my Rx only is for threading basically
you could take out the entirety of what I use Rx for, and put coroutines
just to be able to say “request, wait, receive, call view with result”
so I made like an observableuseCase class that does this for me, and now each endpoint call is a subclass of it
or a completableusecase
g
than it's an easy question. I'd NEVER use LiveData below presenters (ViewModels, etc) 🙂
coroutines won't replace your reactive code tho. Only Maybe, Single and Completable.
o
you mean in my use case you’d never use live data? given the way ive got things set up?
i mean cause it is reactive..what they’re preaching im already doing
couldn’t get the shtick
cause it’s already like that
g
assuming we're below presentation layer, ie data & domain. The decision tree is the following: is my code reactive? If not (returns
T
,
Maybe<T>
,
Single<T>
,
Completable
) just coroutines enough. If yes (returns stream of
T
), then either Rx or Channels. LiveData will work as well but it's like shooting yourself in the leg. Because the next minute you move to LD you'll want some operators, and it has none of them.
you're doomed to reinvent the wheel and write all by yourself
the
map
kotlin extension was added in alpha version of ktx livedata library recently. It`s like RxJava 0.0.0-alpha-there-was-an-attempt-1 version.
o
wow
yea I didnt know that
map and filter are extremely essential to the results
and a host of others
ok, thanks a lot
if you’re still there @ghedeon, since I am not using LiveData, I have nowhere to post back from inside the viewmodel, do I just let it make the call to my endpoint method (returns Observable)? I can then …pass the viewmodel to the presenter which makes the call there and passes back to the view?..
ahh no, the viewmodel is going to be like the activity, i just move the shit there
I dont know
do I just again give it a reference to my view interface? which my activity implements?
that don’t sound right
m
your activity only subcribes to changes
your viewmodel doesn’t know about the activity
your xml use data binding to display data
it can also listen to LiveData directly
coroutine can replace threading in Rx
kotlin already has transforms that can replace most rx transform
o
given this though, without wanting to change anything https://pastebin.com/MBF2Ujuy
how can I do it, have the data return back to the activity where it’s displayed
I can wrap it in another observable
s
Quick question, why would you need a presenter and a viewmodel for an activity/feature, are they not meant to perform the same function (with a slightly different implementation)? You can still have all the logic in the viewmodel and do the normal command subscription in your activity/fragment
o
yes yes, just my confusion
you can do LiveData without having to do databinding and XML shit
s
trust me, databinding is dope, try it and thank me later
👎 2
o
i cannot imagine going into an XML file and seeing something that has to do with non-graphical things
my only barrier
s
your activities/fragments will have the kind of leanness you have never seen before when you use it
o
i guess though
if you switch out where you want your data to end up, you just carry a tag with the XML view and now that new view will host your data
one step at atime
u
yes, use rx