@@ -16,7 +16,7 @@ import androidx.compose.foundation.layout.heightIn
1616import androidx.compose.foundation.layout.padding
1717import androidx.compose.foundation.layout.widthIn
1818import androidx.compose.foundation.lazy.LazyColumn
19- import androidx.compose.foundation.lazy.items
19+ import androidx.compose.foundation.lazy.itemsIndexed
2020import androidx.compose.foundation.text.KeyboardActions
2121import androidx.compose.foundation.text.KeyboardOptions
2222import androidx.compose.material.icons.Icons
@@ -53,13 +53,17 @@ import androidx.lifecycle.setViewTreeViewModelStoreOwner
5353import androidx.savedstate.setViewTreeSavedStateRegistryOwner
5454import org.nitri.opentopo.MapFragment
5555import org.nitri.opentopo.R
56+ import org.nitri.opentopo.analytics.AnalyticsProvider
57+ import org.nitri.opentopo.analytics.resultPositionBucket
5658import org.nitri.opentopo.model.PlaceSearchResult
5759import org.nitri.opentopo.ui.theme.OpenTopoTheme
5860import org.nitri.opentopo.viewmodel.PlaceSearchUiState
5961import org.nitri.opentopo.viewmodel.PlaceSearchViewModel
6062
6163class PlaceSearchDialogFragment : DialogFragment () {
6264
65+ private val analytics by lazy { AnalyticsProvider .get(requireContext()) }
66+
6367 private val viewModel: PlaceSearchViewModel by viewModels {
6468 val focusLon = arguments?.getDouble(ARG_FOCUS_LON )
6569 val focusLat = arguments?.getDouble(ARG_FOCUS_LAT )
@@ -72,10 +76,20 @@ class PlaceSearchDialogFragment : DialogFragment() {
7276 }
7377 },
7478 focusLon = focusLon,
75- focusLat = focusLat
79+ focusLat = focusLat,
80+ analytics = analytics
7681 )
7782 }
7883
84+ override fun onCreate (savedInstanceState : Bundle ? ) {
85+ super .onCreate(savedInstanceState)
86+ if (savedInstanceState == null ) {
87+ analytics.trackOrsSearchOpened(
88+ arguments?.containsKey(ARG_FOCUS_LON ) == true && arguments?.containsKey(ARG_FOCUS_LAT ) == true
89+ )
90+ }
91+ }
92+
7993 override fun onCreateDialog (savedInstanceState : Bundle ? ): Dialog {
8094 val composeView = ComposeView (requireContext()).apply {
8195 setViewCompositionStrategy(ViewCompositionStrategy .DisposeOnViewTreeLifecycleDestroyed )
@@ -84,7 +98,8 @@ class PlaceSearchDialogFragment : DialogFragment() {
8498 PlaceSearchDialogContent (
8599 viewModel = viewModel,
86100 isInitialLaunch = savedInstanceState == null ,
87- onResultSelected = { result ->
101+ onResultSelected = { result, index, resultCount ->
102+ analytics.trackOrsSearchSelection(resultPositionBucket(index), resultCount.coerceAtMost(10 ))
88103 setFragmentResult(
89104 REQUEST_KEY ,
90105 Bundle ().apply {
@@ -146,7 +161,7 @@ class PlaceSearchDialogFragment : DialogFragment() {
146161private fun PlaceSearchDialogContent (
147162 viewModel : PlaceSearchViewModel ,
148163 isInitialLaunch : Boolean ,
149- onResultSelected : (PlaceSearchResult ) -> Unit ,
164+ onResultSelected : (PlaceSearchResult , Int , Int ) -> Unit ,
150165 onDismiss : () -> Unit
151166) {
152167 val query by viewModel.query.collectAsState()
@@ -249,14 +264,14 @@ private fun PlaceSearchDialogContent(
249264 .heightIn(max = 280 .dp)
250265 .padding(vertical = 8 .dp)
251266 ) {
252- items (
267+ itemsIndexed (
253268 items = state.results,
254- key = { it .stableId }
255- ) { result ->
269+ key = { _, item -> item .stableId }
270+ ) { index, result ->
256271 Column (
257272 modifier = Modifier
258273 .fillMaxWidth()
259- .clickable { onResultSelected(result) }
274+ .clickable { onResultSelected(result, index, state.results.size ) }
260275 .padding(vertical = 12 .dp, horizontal = 4 .dp)
261276 ) {
262277 Text (
0 commit comments