Skip to content

Commit ea4a6df

Browse files
committed
Integrated advanced navigation LLM integration
- retrieval of place details and ability to remove them
1 parent e4654d9 commit ea4a6df

2 files changed

Lines changed: 90 additions & 4 deletions

File tree

app/src/main/java/com/nlinterface/activities/PlaceDetailsActivity.kt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,29 @@ class PlaceDetailsActivity : AppCompatActivity(), PlaceDetailsItemCallback {
105105
placeDetailsItemList = viewModel.placeDetailsItemList
106106
placeDetailsAdapter = PlaceDetailsAdapter(placeDetailsItemList, this)
107107

108-
109108
configureAutocompleteFragment()
110109
viewPagerSetUp()
111110
configureTTS()
112111
configureSTT()
112+
113+
val removalIds = intent.getStringArrayListExtra("PLACE_IDS_TO_REMOVE")
114+
if (!removalIds.isNullOrEmpty()) {
115+
val removedNames = mutableListOf<String>()
116+
removalIds.forEach { removalId ->
117+
val itemToRemove = placeDetailsItemList.find { it.placeID == removalId }
118+
if (itemToRemove != null) {
119+
// Delete the item from the ViewModel list and update storage
120+
viewModel.deletePlaceDetailsItem(itemToRemove)
121+
// Also remove it from the local list and notify the adapter
122+
val index = placeDetailsItemList.indexOf(itemToRemove)
123+
if (index != -1) {
124+
placeDetailsItemList.removeAt(index)
125+
placeDetailsAdapter.notifyItemRemoved(index)
126+
}
127+
removedNames.add(itemToRemove.storeName)
128+
}
129+
}
130+
}
113131
}
114132

115133

app/src/main/java/com/nlinterface/activities/VoiceOnlyActivity.kt

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import androidx.lifecycle.ViewModelProvider
1717
import androidx.lifecycle.lifecycleScope
1818
import com.nlinterface.R
1919
import com.nlinterface.databinding.ActivityMainBinding
20+
import com.nlinterface.dataclasses.PlaceDetailsItem
2021
import com.nlinterface.utility.LLMAppConnector
2122
import com.nlinterface.utility.OnSwipeTouchListener
2223
import 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

Comments
 (0)