-
Notifications
You must be signed in to change notification settings - Fork 0
Lab 20
Nate Ebel edited this page Jul 19, 2022
·
3 revisions
-
Setup Google Maps api key
- Follow this link to create an api key for Google maps
- Add the api key to your user's gradle.properties file as
GOOGLE_MAPS_API_KEY="<your key>"
- Rebuild your project
-
Hoist Main screen state into a
ViewModel
- Create a class named
MainViewModel
in themain
package - Create a
UiState
data class withinMainViewModel
that includes a single propertylocations: List<Location>
- Expose a
StateFlow<UiState>
as a property namedstate
fromMainViewModel
- Get an instance of
MainViewModel
fromMainScreen()
by addingviewModel: MainViewModel = viewModel()
as a function parameter - Collect
MainViewModel.state
as ComposeState
by addingval state by viewModel.state.collectAsState()
to the beginning ofMainScreen()
- Update
MainScreenContent
to take aList<Location>
namedlocations
and replaceitems(LOCATIONS){}
withitems(state.locations){}
- Pass
state.locations
toMainScreenContent
- Create a class named
-
Hoist Map screen state into a
ViewModel
- Create a class named
MapViewModel
in themap
package - Create a
UiState
data class withinMapViewModel
that includs 3 properties:title: String
location: Location
markerLabel: String
zoom: Float
- Expose a
StateFlow<UiState>
as a property namedstate
fromMapViewModel
- Add a constructor property to
MapViewModel
namedlocation: Location
and use the to initialize thestate
property - Within the same file as
MapViewModel
create a class namedMapViewModelFactory
that implmentsViewModelProvider.Factory
and returns an instance ofMapViewModel
within the overriddencreate()
method (see hints) - Add a new parameter to
MapScreen
after thelocation
parameter. This new parameter should be namedviewModel: MapViewModel = viewModel()
- Pass
factory = MapViewModelFactory(location)
to theviewModel()
call - Collect
MapViewModel.state
as ComposeState
by addingval state by viewModel.state.collectAsState()
to the beginning ofMapScreen()
- Use that
state
variable, to populate the UI ofMapScreen()
- Create a class named
class MapViewModelFactory(private val location: Location) : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
return MapViewModel(location) as T
}
}