Apr 3 | 3:55 PM |
Mark M. | has entered the room |
Mark M. | turned on guest access |
Apr 3 | 4:05 PM |
trocchietto_Ivano | has entered the room |
Mark M. |
hello, Ivano!
|
Mark M. |
how can I help you today?
|
trocchietto_Ivano |
Hi Mark
|
trocchietto_Ivano |
well I am trying to break your project on from Rxjava to Coroutines, to better understand how it works, because unfortunately I am not familiar with flows and room
|
trocchietto_Ivano |
I have two questions
|
Apr 3 | 4:10 PM |
trocchietto_Ivano |
I changed the rest endpoint so that instead to get info from the weather API, I am getting a simple JSON list of objects: basically a List<Developer> where Developer(name:String ,city: String)
|
trocchietto_Ivano |
my aim is to insert in Room this List and showing it in the UI
|
trocchietto_Ivano |
the first problem is, that I found not possible to return from Retrofit a Flow<List<Developer>>>
|
trocchietto_Ivano |
but only a List<Developer>
|
Mark M. |
that is because a Web service request is a one-time thing
|
Mark M. |
Flow implies a series of events over time
|
Mark M. |
hence, Retrofit focuses its coroutines support on simple suspend functions
|
Mark M. |
as that better models what you are doing at the REST level
|
trocchietto_Ivano |
yes, but when I use RXJava I always return Observable or Single from the Retrofit interface
|
Mark M. |
Single = suspend fun
|
trocchietto_Ivano |
I suspend the end point
|
trocchietto_Ivano |
but cannot "collect" in the UI
|
trocchietto_Ivano |
so I have to throw them in the DB before
|
Mark M. |
collect() is something that you do on a Flow
|
trocchietto_Ivano |
yes
|
anon | has entered the room |
trocchietto_Ivano |
so my question is:
|
Mark M. |
your ViewModel could use a StateFlow to emit a series of viewstates
|
trocchietto_Ivano |
yes
|
Mark M. |
(BTW, hello anon -- I will be with you shortly!)
|
Apr 3 | 4:15 PM |
trocchietto_Ivano |
but how can I implement this use case: As developer I want to retrieve a Retrofit List and show in the UI
|
trocchietto_Ivano |
as flow
|
trocchietto_Ivano |
as I did with Observable
|
Mark M. |
I cannot comment on your use of Observable with Retrofit -- to be honest, I doubt that I would accept that in a code review
|
Mark M. |
Observable, like Flow, models a series of events
|
Mark M. |
a Web service call is a single event, not a series of events
|
Mark M. |
so, from an RxJava standpoint, Single or Completable make sense to me, but not Observable
|
trocchietto_Ivano |
ahahah that is why i am trying to leave the company
|
Mark M. |
anyway, back to coroutines
|
Mark M. |
you would basically do exactly what I am doing in MainMotor, except that you would replace MutableLiveData and LiveData with MutableStateFlow and StateFlow
|
trocchietto_Ivano |
that return a value instead of LiveData!
|
trocchietto_Ivano |
thanks
|
Mark M. |
they would emit view states (loading, content, error)
|
Mark M. |
I will be updating my book to use that approach this year
|
Mark M. |
probably Version 0.5, in a few months
|
trocchietto_Ivano |
Cool thanks, are newbe questions but doing my baby steps with new stack
|
trocchietto_Ivano |
thanks
|
trocchietto_Ivano |
Hi Anon
|
Apr 3 | 4:20 PM |
Mark M. |
let me take a question from Anon, and I will be back with you shortly
|
trocchietto_Ivano |
cool
|
Mark M. |
Anon: hi! how can I help you today?
|
Mark M. |
Anon: do you have a question?
|
Mark M. |
Anon: if you come up with a question, let me know!
|
Mark M. |
in the meantime...
|
Mark M. |
Ivano: back to you! do you have another question?
|
trocchietto_Ivano |
(in the meantime I try to hack room db to insert a list and a fake autogenerated id)
|
Apr 3 | 4:25 PM |
trocchietto_Ivano |
really do not want to ask too many silly questions, and as Leonardo said, I have to practice making my hand dirty, not theorize
|
trocchietto_Ivano |
let me play with your room a tweenty minutes, eventually I come back toward the end of the session oki?
|
Mark M. |
fine by me!
|
trocchietto_Ivano |
thanks for your patience, see you later
|
trocchietto_Ivano |
sorry mark I can use also fine with me as expression?
|
trocchietto_Ivano |
in the netherlands everybody says fine with me
|
Mark M. |
that works too!
|
trocchietto_Ivano |
but well this is not a pc language, sorry for my weirdness, after all I am a dev
|
trocchietto_Ivano |
ok thanks see you later
|
Apr 3 | 4:30 PM |
anon | has left the room |
Apr 3 | 4:35 PM |
Scott W. | has entered the room |
Scott W. |
Hello
|
Mark M. |
hello, Scott!
|
Mark M. |
Ivano is doing some research right now, so... how can I help you?
|
Scott W. |
I'm trying a brand new thing and it's not going so well
|
Scott W. |
trying to use jetpack Navigation in Kotlin
|
Scott W. |
my nav_graph.xml says "No NavHostFragments found"
|
Scott W. |
I've been going through the example in your book, but no luck so far.
|
Mark M. |
when you say "says", what do you mean?
|
Mark M. |
is this a compile error? an IDE message? a runtime crash? something else?
|
Scott W. |
the... xml renderer for the nav_graph file
|
Scott W. |
not sure what that's technically called
|
Mark M. |
OK, so you are seeing this message in an editor tab of the IDE?
|
Scott W. |
for xml you have Code/Split/Design views
|
Scott W. |
yeah
|
Scott W. |
so I have an Activity, MainActivity, which I am expecting to house all of the fragments in this app.
|
Scott W. |
Then I have 6 empty fragments I've made through the nav_graph editor.
|
Scott W. |
MainActivity has an xml file with a fragment element
|
Mark M. | |
Mark M. |
does the app run?
|
Mark M. |
if it does, this is an IDE bug, and you can ignore it
|
Apr 3 | 4:40 PM |
Mark M. |
now, in Android Studio 4.1.3, I am no longer getting that error, so it is possible that they fixed it and just did not update the issue
|
Scott W. |
the app runs, but it's doing a weird thing
|
Scott W. |
like the activity is being remade over and over
|
Scott W. |
I'm using 4.1.3
|
Mark M. |
OK, does your activity layout have something in it that refers to NavHostFragment?
|
Mark M. |
for example, in https://gitlab.com/commonsguy/cw-jetpack-kotlin..., I have a FragmentContainerView that loads NavHostFragment
|
Scott W. |
yes
|
Scott W. |
View paste
|
Scott W. |
this is the only thing in my main activity
|
Scott W. |
yes I have FragmentContainerView in the xml layout
|
Scott W. |
I tried switching it to a fragment also, same result in the Host section of the nav graph
|
Mark M. |
OK, for the time being then, I would assume that the IDE message about not having a NavHostFragment is a red herring
|
Mark M. |
can you describe "like the activity is being remade over and over" a bit more?
|
Apr 3 | 4:45 PM |
Scott W. |
the title of the app is that the top of the screen, and toolbar is below it. Those are the only UI elements.
|
Scott W. |
these elements pop into view, then fade out
|
Scott W. |
this process is looping
|
Mark M. |
OK, that's very strange
|
Scott W. |
View paste
(45 more lines)
|
Scott W. |
this is the nav graph xml
|
Mark M. |
that <activity> element frightens me
|
Mark M. |
is that the activity that had the code snippet that you showed earlier?
|
Scott W. |
yes
|
Mark M. |
and so it is the activity that is loading the layout that is loading the nav graph that is loading the activity that is loading the layout that is loading the nav graph that is loading the activity that is loading the layout that is loading the nav graph that is...
|
Mark M. |
IOW, remove that <activity> element, and set your startDestination to be whatever fragment you want to show first
|
Scott W. |
ok I removed this and the loop has died
|
Apr 3 | 4:50 PM |
Mark M. |
IIRC, an <activity> element is if you are trying to exit this nav graph and go to another activity that either has its own nav graph or is not using Navigation
|
Scott W. |
ah
|
Scott W. |
so where is the connection from the activity to this nav graph? The layout.xml?
|
Mark M. |
yes
|
Scott W. |
ok
|
Mark M. |
the activity loads the layout
|
Mark M. |
the layout's FragmentHostView loads the NavHostFragment
|
Mark M. |
the NavHostFragment loads the nav graph from the app:navGraph attribute in the <FragmentHostView> element
|
Mark M. |
and the NavHostFragment shows your startDestination
|
Scott W. |
got it
|
Scott W. |
I didn't initially want to have a toolbar, but I included one to remove differences with your implementation to try solving the first problem
|
Scott W. |
How can I change onCreate to set this up without the toolbar?
|
Mark M. |
do you have a Toolbar in your layout?
|
Scott W. |
I do now because of the reasoning I mentioned. I'd like to remove it.
|
Mark M. |
your code refers to an action bar, not a Toolbar
|
Mark M. |
OK, remove it
|
Mark M. |
setupActionBarWithNavController() is going to try to set up an action bar, assuming that your theme calls for one
|
Apr 3 | 4:55 PM |
Scott W. |
the theme inherits from Theme.MaterialComponents...DarkActionBar
|
Mark M. |
OK, then that will display a dark action bar
|
Mark M. |
if you do not want that, you would also need to switch themes, plus remove the setupActionBarWithNavController() call
|
Mark M. |
(BTW, Ivano, if you are around and have a follow-up question, let me know!)
|
Scott W. |
what is the difference between ActionBar and Toolbar?
|
trocchietto_Ivano |
well we are short of time, and I have a lot of time to learn
|
Mark M. |
Ivano: we all do! :-)
|
trocchietto_Ivano |
I try again if does not work we can catch up next time
|
trocchietto_Ivano |
ehehe
|
Mark M. |
Scott: visually, they are the same -- an action bar is supplied by the theme, and a Toolbar is a widget that you place yourself
|
Scott W. |
ok
|
trocchietto_Ivano |
anyway no rush two small questions I am gonna solve myself, if not I will come back to you
|
Scott W. |
In the end I will probably use the ActionBar, but I would like to get this working without one.
|
trocchietto_Ivano |
I am seeing the light out from the tunnel of the flow
|
trocchietto_Ivano |
nice I can debug room with this db inspector, I am rfeally in love with intelliJ
|
trocchietto_Ivano |
have a nice evening Mark, bye Scott
|
Mark M. |
you too!
|
Mark M. |
Scott: anything quick before we wrap up the chat?
|
Scott W. |
bye bye! see you around here some other time mayber :)
|
trocchietto_Ivano |
thanks
|
Scott W. |
yeah
|
Scott W. |
so I've change the theme to no action bar
|
Apr 3 | 5:00 PM |
Scott W. |
removed the toolbar from layout.xml
|
Scott W. |
now I'd like to know how to change the onCreate to setup navigation without the actionbar
|
Mark M. |
there is nothing to set up, unless you have a UI affordance for navigation (e.g., bottom nav)
|
Mark M. |
Navigation offers integrations with a few stock options
|
Mark M. |
after that, you're on your own
|
Mark M. |
so, for example, in my chapter on Navigation, I do forward navigation when the user clicks on a RecyclerView row
|
Scott W. |
I'm still seeing my fragment title at the top of the screen
|
Scott W. |
maybe it's in my fragment layout
|
Mark M. |
if you have not yet removed setupActionBarWithNavController(), you would need to remove that
|
Mark M. |
(though I am not aware of it somehow magically having the ability to add a bar...)
|
Mark M. |
but, yeah, check the fragment layout to see if you have a Toolbar there
|
Scott W. |
oh wow
|
Scott W. |
ok yeah removing that line did it
|
Scott W. |
View paste
|
trocchietto_Ivano |
eheh
|
Scott W. |
just a lot less code than I thought would be needed
|
Mark M. |
OK, then, that's all for today's chat
|
Scott W. |
I've got room to work now. Thanks for the help
|
Mark M. |
next one is Tuesday in the 8:30am US Eastern time slot
|
Mark M. |
I'm happy to be useful
|
Mark M. |
and, have a pleasant day!
|
Scott W. |
bye bye
|
trocchietto_Ivano |
you too
|
trocchietto_Ivano | has left the room |
Scott W. | has left the room |
Mark M. | turned off guest access |