@@ -17,6 +17,7 @@ import androidx.lifecycle.ViewModelProvider
1717import androidx.lifecycle.lifecycleScope
1818import com.nlinterface.R
1919import com.nlinterface.databinding.ActivityMainBinding
20+ import com.nlinterface.dataclasses.PlaceDetailsItem
2021import com.nlinterface.utility.LLMAppConnector
2122import com.nlinterface.utility.OnSwipeTouchListener
2223import com.nlinterface.viewmodels.VoiceOnlyViewModel
@@ -300,9 +301,23 @@ class VoiceOnlyActivity: AppCompatActivity() {
300301 startActivity(intent)
301302 }
302303 " navigation" -> {
303- viewModel.sayAndAwait(getString(R .string.place_details))
304- val intent = Intent (this @VoiceOnlyActivity, PlaceDetailsActivity ::class .java)
305- startActivity(intent)
304+ if (needsAdditionalData) {
305+ // Retrieve current place details list as additional data (JSON)
306+ val placeList = getCurrentPlaceList()
307+ val token = LLMAppConnector .getInstance.authenticate()
308+ val prefixedData = " <App>$placeList "
309+ val apiResponse = LLMAppConnector .getInstance.sendCommandToLLM(prefixedData, token)
310+ val (response, _) = LLMAppConnector .getInstance.parseResponse(apiResponse)
311+ if (response != null ) {
312+ processNavigationUpdates(response)
313+ } else {
314+ viewModel.sayAndAwait(" Sorry, I couldn't process your navigation request." )
315+ }
316+ } else {
317+ viewModel.sayAndAwait(getString(R .string.place_details))
318+ val intent = Intent (this @VoiceOnlyActivity, PlaceDetailsActivity ::class .java)
319+ startActivity(intent)
320+ }
306321 }
307322 " object-and-hand-recognition" -> {
308323 viewModel.sayAndAwait(getString(R .string.classification))
@@ -368,6 +383,59 @@ class VoiceOnlyActivity: AppCompatActivity() {
368383
369384 viewModel.sayAndAwait(" $addMessage$removeMessage " )
370385 }
386+
387+ // Helper to get the current place list from the saved PlaceDetailsItem JSON file.
388+ private fun getCurrentPlaceList (): String {
389+ val placeListFile = File (applicationContext.filesDir, " PlaceDetailsItemList.json" )
390+ if (! placeListFile.exists() || placeListFile.length() == 0L ) {
391+ return " {}"
392+ }
393+ return try {
394+ placeListFile.readText()
395+ } catch (e: Exception ) {
396+ " {}"
397+ }
398+ }
399+
400+ private suspend fun processNavigationUpdates (response : String ) {
401+ // Process only the content after the <LLM> tag if it exists.
402+ val processedResponse = if (response.contains(" <LLM>" )) {
403+ response.substringAfter(" <LLM>" )
404+ } else {
405+ response
406+ }
407+
408+ // Regex to capture removals of the format: [-1]PlaceID
409+ val removalPattern = " \\ [-1\\ ](\\ S+)" .toRegex()
410+ val removals = removalPattern.findAll(processedResponse).map { it.groupValues[1 ] }.toList()
411+
412+ if (removals.isNotEmpty()) {
413+ // Start PlaceDetailsActivity with the removal IDs passed in the intent.
414+ val intent = Intent (this @VoiceOnlyActivity, PlaceDetailsActivity ::class .java)
415+ intent.putStringArrayListExtra(" PLACE_IDS_TO_REMOVE" , ArrayList (removals))
416+ startActivity(intent)
417+
418+ // Retrieve the currently stored PlaceDetailsItem list from JSON.
419+ val placeListJson = getCurrentPlaceList()
420+ val placeNames = try {
421+ // Using the PlaceDetailsItem list from PlaceDetailsActivity.
422+ val type = object : com.google.gson.reflect.TypeToken <List <PlaceDetailsItem >>() {}.type
423+ val places: List <PlaceDetailsItem > = com.google.gson.Gson ().fromJson(placeListJson, type)
424+ // Map each removal ID to its corresponding store name (or fallback to the removal ID).
425+ removals.map { removalId ->
426+ places.find { it.placeID == removalId }?.storeName ? : removalId
427+ }
428+ } catch (e: Exception ) {
429+ removals // Fallback if JSON parsing fails.
430+ }
431+
432+ // Announce removal by speaking only the supermarket names.
433+ viewModel.sayAndAwait(" Removing supermarkets with names: ${placeNames.joinToString(" , " )} ." )
434+ } else {
435+ viewModel.sayAndAwait(response)
436+ }
437+ }
438+
371439 private fun startListening () {
372440 viewModel.handleSTTSpeechBegin()
373441 }
0 commit comments