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

Ahmed Mourad

04/11/2020, 7:23 AM
How do you "bind" your LiveData objects to a corresponding TextView, if you're supposed to expose all your LiveData objects as immutables? (Without DataBinding)
o

Orhan Tozan

04/11/2020, 11:09 AM
With viewmodel.livedata.observe ...
a

Ahmed Mourad

04/11/2020, 4:46 PM
@Orhan Tozan I mean the other way around, if we have a firstName
EditText
for example and want to keep our LD updated with its
text
.
o

Orhan Tozan

04/11/2020, 4:48 PM
Edittext.ontextchanged
a

Ahmed Mourad

04/11/2020, 5:13 PM
@Orhan Tozan So we make the LiveData mutable?
o

Orhan Tozan

04/11/2020, 5:29 PM
Give the viewmodel a method like
fun onUsernameTextFieldTextChange(newText: String)
👍 1
r

rkeazor

05/02/2020, 3:59 PM
Ummm your not suppose to Bind Live Data to Views. That's a Anti pattern. Your views are only suppose to observe your live data
o

Orhan Tozan

05/02/2020, 4:01 PM
Sure Views also need to be able to emit events. Something like a textinput can be abstracted away to simply two way binding.
r

rkeazor

05/02/2020, 4:16 PM
Yea but liveData isnt ment to handle those view events. Because think about it, the view gives data to live data just to be observed by the view all over agian. There is no real reason for that unless its triggering something else that is being observed... but it def should not just be emitting the same character that was entered into the text listener?
o

Orhan Tozan

05/02/2020, 4:18 PM
That's what MutableLiveData is for. Since something like a text input is really straight forward, the view could get the responsibility to update it's input
r

rkeazor

05/02/2020, 4:24 PM
Not exactly. Livedata , is a data holder that a lifecycle owner can observe.MutableLiveData is just a livedata that you can write to.. but if you end up with a pattern where the lifecycle owner gives a livedata data just so the owner can turn around and observe it, than you end up with a pattern like this (LifecycleOwner <----> livedata]... which to me seems like a clear anti pattern
o

Orhan Tozan

05/02/2020, 4:26 PM
It's called two-way binding yes, and isn't an anti pattern if used with decency. Android Docs cover this topic here: https://developer.android.com/topic/libraries/data-binding/two-way
r

rkeazor

05/02/2020, 4:27 PM
I thought we were talking about livedata
o

Orhan Tozan

05/02/2020, 4:27 PM
Yeah we are
r

rkeazor

05/02/2020, 4:28 PM
Two way databinding is something completely different
o

Orhan Tozan

05/02/2020, 4:28 PM
Two way binding is a concept on its own that exists outside of Android's databinding library. You can still apply the concept of two way binding with lets say regular view binding
r

rkeazor

05/02/2020, 4:29 PM
Mmmm yes but it's completely different
Livedata and 2 way databinding are not even the same concept
o

Orhan Tozan

05/02/2020, 4:31 PM
OP asked about how to bind livedata to a text input, which could be solved with two way binding
Two way binding isn't an anti pattern, that's my point
r

rkeazor

05/02/2020, 4:37 PM
Yes but not with live data
That component isnt ment for that
o

Orhan Tozan

05/02/2020, 4:38 PM
The linked docs perform two way binding with livedata
r

rkeazor

05/02/2020, 4:48 PM
Not the way your thinking. For instance 1 way data binding the data just updates the view correct. 2 way databinding the view can update the data and vise versa correct. But in the case of just using livedata unless your trying to observe the transmission for a particular reason, let's say another event happens when you type there is no real reason to use a livedata for that
Dont get me wrong not trying to say your idea is bad or 2 way databinding is bad. I love databinding lol, and your approach makes sense . But just saying the general idea of attaching a livedata to a view listener seems to be a bit of a anti pattern, unless it's to trigger another even in which the view needs to obserse
And if the livedata doesnt need to be observed than livedata is the wrong data structure to be used
o

Orhan Tozan

05/02/2020, 5:01 PM
I find it hard to understand your point lol, two way binding of livedata without databinding would just someting be like:
Copy code
viewmodel.textInputLiveData.observe(this) { text -> textViewInput.setText(text) }

textViewInput.onTextChangedListener { newText -> viewmodel.textInputLiveData.value = newText
}
r

rkeazor

05/02/2020, 5:03 PM
Lol ahhh wait that wouldnt that be a infinite loop or I'm trippong
That's def a infinite loop.like I said anti pattern
o

Orhan Tozan

05/02/2020, 5:04 PM
That would be an issue w/ the Android framework itself, an implementation detail from the viewmodel's standpoint
r

rkeazor

05/02/2020, 5:05 PM
Set value would trigger the textview onChangeListener which would update the live data which in turn would update the livedata all mover agian
This is exactly why I said it doesnt make sense. Livedata isnt ment for this
o

Orhan Tozan

05/02/2020, 5:06 PM
ViewModel should be able to expose MutableLiveData textfield livedata, just saying that hey this is the textfield just keep it up to date with the users input
r

rkeazor

05/02/2020, 5:07 PM
With the code snippet you just entered. You press one key on the keyboard and your program will be in a world of hurt lol.... that's not a issue of the android framework that's a misuse of a component
o

Orhan Tozan

05/02/2020, 5:08 PM
That's just android's framework not offering a listener for when text is input by a user
r

rkeazor

05/02/2020, 5:09 PM
It has onTextChangeListener.... it's not the framework . The code is incorrect because that's not how livedata is ment to be used
o

Orhan Tozan

05/02/2020, 5:10 PM
onTextChangeListener wouldn't be the right event, there should be like onTextInputListener, which doesnt exist
r

rkeazor

05/02/2020, 5:11 PM
Why, just so it could be re observed by live data?
Lol come on
o

Orhan Tozan

05/02/2020, 5:11 PM
Well, that's how MVVM works
r

rkeazor

05/02/2020, 5:11 PM
Absolutely not
o

Orhan Tozan

05/02/2020, 5:11 PM
Keep the state in the VM, instead of the view managing its own state
Well lol how would you handle this?
A screen with a text input
MVVM
go
r

rkeazor

05/02/2020, 5:14 PM
Mvvm every level is being observing something else. Umm I would ask first what he is trying to accomplish with the listener
o

Orhan Tozan

05/02/2020, 5:15 PM
Very simple, a screen with a text input, it should act like how you would think a text input acts
Let's say a login screen, but let's hide the login details for now, just handling text input
r

rkeazor

05/02/2020, 5:17 PM
Ok one sec let me get on my computer. My fingers hurt from typing on my phone lol
What does the login screen need to do
o

Orhan Tozan

05/02/2020, 5:21 PM
username and password text field w/ login button. correct username=123 & password=123. If login button pressed w/ correct credentials entered, go to other screen
r

rkeazor

05/02/2020, 5:32 PM
you dont even need a text change Listener in that example
sorry for the crazy sudo code
o

Orhan Tozan

05/02/2020, 5:34 PM
Yeah so with this example you are letting the view hold the state
You are letting the view hold the username and password text input state
r

rkeazor

05/02/2020, 5:36 PM
if you need to perverse through rotations , you don't need liveData for that. you can set the text directly in two strings in your Viewmodel
o

Orhan Tozan

05/02/2020, 5:36 PM
You want the view to be recreated at any time given the viewmodel. If a view would be recreated by your viewmodel, it would always start blank. The ViewModel should always reflect the View's state
r

rkeazor

05/02/2020, 5:36 PM
Your viewModel is lifecycle aware already
o

Orhan Tozan

05/02/2020, 5:37 PM
I'm talking about MVVM in general, I don't know the details how Android handles view retaining, but the fact is that your ViewModel doesn't reflect the state of your View
r

rkeazor

05/02/2020, 5:38 PM
Viewmodel and state are not the same thing
maybe your thinking Redux or MVI. VM , is not state
o

Orhan Tozan

05/02/2020, 5:39 PM
It is state
lol
r

rkeazor

05/02/2020, 5:39 PM
nope
o

Orhan Tozan

05/02/2020, 5:39 PM
It is state for the view
Your view can't be correctly build from your viewmodel
It would lose data
r

rkeazor

05/02/2020, 5:41 PM
Not really . you consider it state because the View is normally a reflection of what happen in the viewModel. But in essence that because the view observes the viewModel and updates accordingly . There is such thing as State. but they normally call it "State" lol
what
if data is in your viewModel it will survive configuration changes . thats for sure
o

Orhan Tozan

05/02/2020, 5:43 PM
A view should be able to be constructed at any time given the viewmodel at any point
I'm not talking about configuration changes, let's omit the Android details
I'm just talking about that, given I am a view builder and I want to build the login view, I need the login viewmodel for that
r

rkeazor

05/02/2020, 5:44 PM
Umm , maybe . maybe not lol .ViewModel and state are differenr
o

Orhan Tozan

05/02/2020, 5:45 PM
Right now my login viewmodel only tells me what I need to do on login button click, but not about what the username and password text input is
Your example is more MVC tbh
r

rkeazor

05/02/2020, 5:45 PM
Like consider this
A View and a State are a 1 to 1 relationship
lets keep this in mind
to clarify this
let me write some code
That is a View and its state
o

Orhan Tozan

05/02/2020, 5:48 PM
Yes
r

rkeazor

05/02/2020, 5:48 PM
HappyViewState can always rebuild HappyView yes
o

Orhan Tozan

05/02/2020, 5:48 PM
Yes
r

rkeazor

05/02/2020, 5:49 PM
However its a 1 to 1 relationship , if SadView comes in it may not have sunshine or a flower count
However ViewModel has a 1 to N relationship
o

Orhan Tozan

05/02/2020, 5:50 PM
Nope
r

rkeazor

05/02/2020, 5:50 PM
in which many views can use the same viewmodel
it does
even in android
o

Orhan Tozan

05/02/2020, 5:50 PM
I mean, it's very vague in what one describes to be a view
r

rkeazor

05/02/2020, 5:50 PM
You can share viewModels
o

Orhan Tozan

05/02/2020, 5:50 PM
But let's call it a screen
One screen = one viewmodel
r

rkeazor

05/02/2020, 5:50 PM
sure let me write more sudo code
nope
not at all
because views only observe viewModel
they don't base there state directly from them
This is just one example, majority agrees that 1 screen = 1 viewmodel
r

rkeazor

05/02/2020, 5:54 PM
Majority can agree, but it doesn't make it correct lol
o

Orhan Tozan

05/02/2020, 5:56 PM
Lol idk what you are trying to portray, but the point was that you couldn't build your view from your viewmodel
r

rkeazor

05/02/2020, 5:56 PM
The snippet up there is a 1 to 2 relationship right . both happyview and sadView used the same viewModel , and observe the countCloud
This is proof that ViewModel and state are not the same
o

Orhan Tozan

05/02/2020, 5:57 PM
That's a viewmodel with a too abstract data to begin with, viewmodel should say how the title, buttonText etc. look like directly instead of the view make decide that
But that's a different discussion
r

rkeazor

05/02/2020, 5:58 PM
lol it doesn't . if it did it would be called State lol
o

Orhan Tozan

05/02/2020, 5:58 PM
Instead of countClouds: Int, it should be screenTitle: String = "The count is $countClouds"
Model is state
r

rkeazor

05/02/2020, 5:58 PM
Its like me saying a Presenter is State lol
o

Orhan Tozan

05/02/2020, 5:58 PM
It is in the name, viewmodel
It is the model of the view
It is a serialized version of the View
r

rkeazor

05/02/2020, 5:59 PM
💀
lo
we just agreed that state and View is 1 to 1 . and I just showed you a situtation where viewModel and view can be 1 to many . If you can accept the premise why not the conclusion lol
hahaha its all fun I think we are off topic anyway. but its just my opinion
o

Orhan Tozan

05/02/2020, 6:01 PM
Dude lmao it's called viewmodel, not viewsmodel
😂 1
It's a model of a single view
r

rkeazor

05/02/2020, 6:02 PM
Sure it is lol
o

Orhan Tozan

05/02/2020, 6:02 PM
Why would you make a model for multiple views, that is just uneccessary breaking the seperation of concerns rule
or the SRP rule
r

rkeazor

05/02/2020, 6:03 PM
lol of course not
o

Orhan Tozan

05/02/2020, 6:04 PM
It's in the name man, viewmodel
the name itself states there is a 1-1 relation of view and model
Viewmodels, are not 1 to 1 lol
and they are not State
if they were they would be called State
o

Orhan Tozan

05/02/2020, 6:10 PM
Haha ok dude I give up, you can use it however you want, no problems w/ me
🤣 1
r

rkeazor

05/02/2020, 6:10 PM
you can conduct network calls from ViewModels, but you shouldn't from a state class
lol , i win . But Yea for the original question . Reconsider how the livedata is being utilize and make sure its within the right usecase
😂 2
o

Orhan Tozan

05/02/2020, 6:16 PM
long live viewsmodel
😂 1
r

rkeazor

05/02/2020, 6:18 PM
And the State , that wasn't a State . Sort of reminds me of Washington DC 😂
a

Ahmed Mourad

05/02/2020, 8:59 PM
I'm inclined to agree with @rkeazor here 😅 I mean, isn't that what shared viewmodels are for?
😅 1
20 Views