Office Hours — Today, April 3

Thursday, April 1

Apr 3
3:55 PM
Mark M.
has entered the room
Mark M.
turned on guest access
4:05 PM
trocchietto_Ivano
has entered the room
Mark M.
hello, Ivano!
how can I help you today?
trocchietto_Ivano
Hi Mark
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
I have two questions
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)
my aim is to insert in Room this List and showing it in the UI
the first problem is, that I found not possible to return from Retrofit a Flow<List<Developer>>>
but only a List<Developer>
Mark M.
that is because a Web service request is a one-time thing
Flow implies a series of events over time
hence, Retrofit focuses its coroutines support on simple suspend functions
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
but cannot "collect" in the UI
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!)
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
as flow
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
Observable, like Flow, models a series of events
a Web service call is a single event, not a series of events
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
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!
thanks
Mark M.
they would emit view states (loading, content, error)
I will be updating my book to use that approach this year
probably Version 0.5, in a few months
trocchietto_Ivano
Cool thanks, are newbe questions but doing my baby steps with new stack
thanks
Hi Anon
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?
Anon: do you have a question?
Anon: if you come up with a question, let me know!
in the meantime...
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)
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
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
sorry mark I can use also fine with me as expression?
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
ok thanks see you later
4:30 PM
anon
has left the room
4:35 PM
Scott W.
has entered the room
Scott W.
Hello
Mark M.
hello, Scott!
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
trying to use jetpack Navigation in Kotlin
my nav_graph.xml says "No NavHostFragments found"
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?
is this a compile error? an IDE message? a runtime crash? something else?
Scott W.
the... xml renderer for the nav_graph file
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
yeah
so I have an Activity, MainActivity, which I am expecting to house all of the fragments in this app.
Then I have 6 empty fragments I've made through the nav_graph editor.
MainActivity has an xml file with a fragment element
Mark M.
does the app run?
if it does, this is an IDE bug, and you can ignore it
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
like the activity is being remade over and over
I'm using 4.1.3
Mark M.
OK, does your activity layout have something in it that refers to NavHostFragment?
for example, in https://gitlab.com/commonsguy/cw-jetpack-kotlin..., I have a FragmentContainerView that loads NavHostFragment
Scott W.
yes
View paste
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setupActionBarWithNavController((supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment).navController)
this is the only thing in my main activity
yes I have FragmentContainerView in the xml layout
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
can you describe "like the activity is being remade over and over" a bit more?
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.
these elements pop into view, then fade out
this process is looping
Mark M.
OK, that's very strange
Scott W.
View paste (45 more lines)
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/nav_graph"
    app:startDestination="@id/mainActivity">

    <activity
        android:id="@+id/mainActivity"
        android:name="com.completewordproblems.fourthgrade.MainActivity"
        android:label="Start Screen"
        tools:layout="@layout/activity_main" />
    <fragment
        android:id="@+id/myNavigationFragment"
        android:name="com.completewordproblems.fourthgrade.MyNavigationFragment"
...
this is the nav graph xml
Mark M.
that <activity> element frightens me
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...
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
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
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
the layout's FragmentHostView loads the NavHostFragment
the NavHostFragment loads the nav graph from the app:navGraph attribute in the <FragmentHostView> element
and the NavHostFragment shows your startDestination
Scott W.
got it
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
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
OK, remove it
setupActionBarWithNavController() is going to try to set up an action bar, assuming that your theme calls for one
4:55 PM
Scott W.
the theme inherits from Theme.MaterialComponents...DarkActionBar
Mark M.
OK, then that will display a dark action bar
if you do not want that, you would also need to switch themes, plus remove the setupActionBarWithNavController() call
(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
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
nice I can debug room with this db inspector, I am rfeally in love with intelliJ
have a nice evening Mark, bye Scott
Mark M.
you too!
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
so I've change the theme to no action bar
5:00 PM
Scott W.
removed the toolbar from layout.xml
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)
Navigation offers integrations with a few stock options
after that, you're on your own
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
maybe it's in my fragment layout
Mark M.
if you have not yet removed setupActionBarWithNavController(), you would need to remove that
(though I am not aware of it somehow magically having the ability to add a bar...)
but, yeah, check the fragment layout to see if you have a Toolbar there
Scott W.
oh wow
ok yeah removing that line did it
View paste
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
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
I'm happy to be useful
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

Thursday, April 1

 

Office Hours

People in this transcript

  • anon
  • Mark Murphy
  • Scott Wehby
  • trocchietto_Ivano