Office Hours Transcript: 2021-05-20

Scott joined

hello

hello, Scott!

 

how can I help you today?

I entered an invalid nickname for chat, and the message it showed me was confusing. Are you in control of the message?

nope – that’s handled by hack.chat itself

ok

 

I’m trying to use ViewModel in some new projects. I’ve got a couple of questions around that.

OK, fire away!

I have 2 fragments sharing a ViewModel. They are used by the same activity

 

MapFragment and ListFragment

 

The list of entries is retrieved in the ViewModel

 

MapFragment shows little icons on the map and ListFragment shows some data about the objects

 

I want to add a new LiveData object to the ViewModel to hold a "selected entry"

 

so, if I tap the entry in the list, I can move the map camera to that entry in the MapFragment.

 

so I’m basically just using the ViewModel in this case to transfer data between the 2 fragments.

 

is this an appropriate use of ViewModel?

using ViewModel to share common data between two fragments? yes

 

using a separate LiveData for "selected entry"? I’m skeptical

 

what are you using for rendering your list? RecyclerView?

yeah

are you using ListAdapter? or how are you handling updates to the contents of the list?

oh I guess I could just have the second livedata be an int pointing to the location in the list

or, "selected" is actually part of the state of an entry in the list itself, particularly if the list needs to know what is selected (e.g., to render the row differently)

nah the list doesn’t care. It will just receive the click event and send you back to the map

oh, OK

I’m using an extension of RecyclerView.Adapter<?>

 

…to display the list

given the "list doesn’t care about the selection" aspect, whether your second LiveData is of the position or of the actual model object depends a bit on the semantics of the click

 

if the click means "I want the map to move to the Nth item", even if what that item is changes between the click event and when the map does something, model it as an Int

 

if, instead, the click means "I want the map to move to this data", then I would model it as a LiveData of the model object

I will do the latter

 

test

Markdown FTW!

:)

 

next question

 

ViewModel is created by the wizard in Android Studio with a dummy LiveObject instantiated using Kotlin by lazy and MutableLiveData<?>().also

 

by lazy I think means "instantiate the object when someone comes asking for it". Is that right?

yeah, but that will be immediately, so I suspect that the lazy aspect is not helping you much

 

at least assuming I am interpreting how you are using also()

 

(er, correctly interpreting)

private val dummy: MutableLiveData<String>()

 

this says a property getter or setter is expected

 

I’m trying to create this without using lazy

private val dummy = MutableLiveData<String>()

 

my guess is that I am missing some subtlety here, though

no that was it

 

so simple

OK, cool!

private val dummy: MutableLiveData<String> by lazy { MutableLiveData<String>() }

 

so this says whenever it’s time, make dummy MutableLiveData<String>()

since the MutableLiveData constructor is cheap, I’m uncertain that you really need the extra lazy complexity

I agree

 

So, the wizard sets it up with .also after the value is set.

"the wizard"?

Android Studio wizard

 

I think if you create a new fragment with view model and spits this stuff out

yeah, I tend to avoid those wizards to the greatest extent possible

I usually do too, but I’m new to these architecture objects and kotlin so I wanted to get something to work with.

so, the .also

y’know… there’s a book that demonstrates using these objects with Kotlin… :-)

I know

 

I’m struggling to see why the .also is helpful

let me create a scrap project and try to get the same results that you’re seeing – give me a minute

 

OK, it wasn’t the "Fragment (with ViewModel)" template – that gave me a ViewModel but no LiveData

maybe it was the ViewModel guide on d.android.com

 

I’m assuming that’s what you’re referring to

yes!

my guess is that they are trying to avoid the overhead of the loadUsers() call, in the off chance that this viewmodel is created and users is never referenced

 

that’s the reason for the lazy bit, anyway

 

the also() is because by lazy needs to evaluate to a MutableLiveData<String>, but they also want to do this somewhat-unrelated thing

 

frankly, it’s too cute by half, particularly for a introductory guide

yeah. I understand it now though. Thank you for walkthroug.

 

Next question

 

Should I avoid using Volley?

I don’t get the sense that it is maintained much

is Retrofit an appropriate alternative?

I would consider OkHttp to be more of a direct Volley equivalent

 

Retrofit wraps OkHttp for REST operations

 

OkHttp (with or without Retrofit) is significantly more popular than Volley at this point

ok. I’m new to https requests.

 

and REST apis

 

I’ve done 2 job application code challenges now and they both ask for basically the same thing.

 

use this REST api to make a little app.

that’s one of the reasons why I have a chapter in Elements of Android Jetpack that demonstrates that sort of thing, using Retrofit as it turns out

maybe I should read the books

 

In future office hour, if I were the only one present, would you be willing to look at a public github repo of mine while I ask you questions about it?

sure, I’ve done that before

that would be great.

 

I’m out of time tonight thought. Gotta finish this application.

 

thanks for the help!

you’re welcome!

bye bye

Scott left