https://kotlinlang.org logo
#android-architecture
Title
# android-architecture
o

oday

01/16/2019, 3:35 PM
can I use MVVM without LiveData?
👌 7
s

Sinan Kozak

01/16/2019, 3:39 PM
Yes
o

oday

01/16/2019, 3:40 PM
why not @commanderpepper
s

Sinan Kozak

01/16/2019, 3:41 PM
LiveData is just a data type which also let you observe with lifecycleowner. So it should be okey to use it without VM
o

oday

01/16/2019, 3:41 PM
yes makes sense
c

commanderpepper

01/16/2019, 3:41 PM
I gave a thumbs down because I don't like single word responses. I'm not a fan of 'Yes' or 'No'.
o

oday

01/16/2019, 3:41 PM
so you are a fan of a single reaction emoji?
😆 2
🙂
you literally did the same as him
lol
s

Sinan Kozak

01/16/2019, 3:43 PM
I start with Yes to ensure you it is not a stupid question.
o

oday

01/16/2019, 3:43 PM
im aware
c

commanderpepper

01/16/2019, 3:43 PM
Yeah you're right, I didn't let him respond with greater detail. I'm sorry for the hasty emoji response.
o

oday

01/16/2019, 3:43 PM
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

Michael Bernier

01/16/2019, 3:48 PM
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

oday

01/16/2019, 3:48 PM
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

Michael Bernier

01/16/2019, 3:51 PM
pretty much, I just don’t use presenters with VM, i put all the logic in the repository for now
o

oday

01/16/2019, 3:52 PM
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

Michael Bernier

01/16/2019, 3:52 PM
pretty much
o

oday

01/16/2019, 3:52 PM
ok good
glad i went and asked what a viewmodel was 😛
do you use LiveData ?
m

Michael Bernier

01/16/2019, 3:54 PM
yes I do
g

ghedeon

01/16/2019, 3:55 PM
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

oday

01/16/2019, 3:56 PM
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

ghedeon

01/16/2019, 3:58 PM
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

oday

01/16/2019, 4:00 PM
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

ghedeon

01/16/2019, 4:05 PM
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

oday

01/16/2019, 4:08 PM
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

Michael Bernier

01/16/2019, 4:45 PM
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

oday

01/16/2019, 4:47 PM
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

Smorg

01/16/2019, 4:52 PM
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

oday

01/16/2019, 4:53 PM
yes yes, just my confusion
you can do LiveData without having to do databinding and XML shit
s

Smorg

01/16/2019, 4:55 PM
trust me, databinding is dope, try it and thank me later
👎 2
o

oday

01/16/2019, 4:55 PM
i cannot imagine going into an XML file and seeing something that has to do with non-graphical things
my only barrier
s

Smorg

01/16/2019, 4:56 PM
your activities/fragments will have the kind of leanness you have never seen before when you use it
o

oday

01/16/2019, 4:56 PM
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

ursus

01/16/2019, 5:04 PM
yes, use rx
24 Views