Skip to content

Commit

Permalink
Get most recent drug from database to perform API call to label endpo…
Browse files Browse the repository at this point in the history
…int.
  • Loading branch information
sweisss committed Mar 19, 2024
1 parent 1d87596 commit 5906b73
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ interface SearchedDrugDAO {

@Query("SELECT * FROM SearchedDrug ORDER BY timestamp DESC")
fun getAllSearchedDrugs(): Flow<List<SearchedDrug>>

@Query("SELECT * FROM SearchedDrug ORDER BY timestamp DESC LIMIT 1")
fun getMostRecentSearchedDrug(): Flow<List<SearchedDrug>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ class SearchedDrugRepository(
suspend fun deleteSearchedDrugByName(name: String) = dao.deleteDrugByName(name)

fun getAllSearchedDrugs() = dao.getAllSearchedDrugs()

fun getMostRecentSearchedDrug() = dao.getMostRecentSearchedDrug()
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class SearchedDrugViewModel(application: Application) : AndroidViewModel(applica

val searchedDrugs = repository.getAllSearchedDrugs().asLiveData()

val mostRecentSearchedDrug = repository.getMostRecentSearchedDrug().asLiveData()

fun deleteDrugByName(name: String) {
viewModelScope.launch {
repository.deleteSearchedDrugByName(name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ import androidx.navigation.fragment.findNavController
import com.example.cs492_finalproject_rxwatch.R
import com.example.cs492_finalproject_rxwatch.data.DrugInteractionsDisplay
import com.example.cs492_finalproject_rxwatch.data.Manufacturers
import com.example.cs492_finalproject_rxwatch.data.database.SearchedDrugViewModel
import com.google.android.material.progressindicator.CircularProgressIndicator

class InteractingDrugsListFragment : Fragment(R.layout.drug_report_fragment) {
private val viewModel: DrugInformationViewModel by viewModels()
private val searchedDrugsViewModel: SearchedDrugViewModel by viewModels()
private val adapter = DrugInteractionsAdapter(::onDrugInfoItemClick)

private lateinit var searchResultsListRV: RecyclerView
Expand Down Expand Up @@ -45,85 +47,93 @@ class InteractingDrugsListFragment : Fragment(R.layout.drug_report_fragment) {
* However, Retrofit appears to be URL encoding the `+` signs, so it gets double encoded.
* Replacing the `+` signs with a space ` ` appears to fix the issue.
* */
val exampleDrug = "hydrocodone"
val exampleQuery = "drug_interactions:$exampleDrug AND _exists_:openfda.generic_name"
viewModel.loadDrugInteractions(exampleQuery)

searchResultsListRV = view.findViewById(R.id.rv_search_results)
searchResultsListRV.layoutManager = LinearLayoutManager(requireContext())
searchResultsListRV.setHasFixedSize(true)
searchResultsListRV.adapter = adapter

viewModel.searchResults.observe(viewLifecycleOwner) { drugInformationResults ->
if (drugInformationResults != null) {

drugsInfoView.visibility = View.VISIBLE

Log.d("InteractingDrugsListFragment", "Search Results: $drugInformationResults")
Log.d("InteractingDrugsListFragment", "Search Results List of DrugInfo: ${drugInformationResults.results}")

val drugInfoList = drugInformationResults.results

/*
* Build a map of the manufacturer (mfr) drug names to the drug interactions
* and nest it within a map of the generic drug names.
* This helps condense the search results, since many manufacturers make the same drug.
* */
val drugInteractionsMap = mutableMapOf<String, MutableMap<String, String>>()

drugInfoList.forEach { drugInfo ->
drugInfo.openFDA?.genericName?.forEach{ genericName ->
val mfrToInteractionsMap = drugInteractionsMap.getOrPut(genericName) {
mutableMapOf()
}
drugInfo.openFDA.manufacturerName?.forEach { mfrName ->
val interactionsString = drugInfo.drugInteractionsString?.joinToString(
separator = "; ")
?: "No interactions found"
mfrToInteractionsMap[mfrName] = interactionsString
searchedDrugsViewModel.mostRecentSearchedDrug.observe(viewLifecycleOwner) { drug ->
val searchedDrug = drug[0].drugName
Log.d("InteractingDrugsListFragment", "Searched drug outside observe: $searchedDrug")

val query = "drug_interactions:$searchedDrug AND _exists_:openfda.generic_name"
Log.d("InteractingDrugsListFragment", "Query: $query")

viewModel.loadDrugInteractions(query)

searchResultsListRV = view.findViewById(R.id.rv_search_results)
searchResultsListRV.layoutManager = LinearLayoutManager(requireContext())
searchResultsListRV.setHasFixedSize(true)
searchResultsListRV.adapter = adapter

viewModel.searchResults.observe(viewLifecycleOwner) { drugInformationResults ->
if (drugInformationResults != null) {

drugsInfoView.visibility = View.VISIBLE

val drugInfoList = drugInformationResults.results

/*
* Build a map of the manufacturer (mfr) drug names to the drug interactions
* and nest it within a map of the generic drug names.
* This helps condense the search results, since many manufacturers make the same drug.
* */
val drugInteractionsMap = mutableMapOf<String, MutableMap<String, String>>()

drugInfoList.forEach { drugInfo ->
drugInfo.openFDA?.genericName?.forEach { genericName ->
val mfrToInteractionsMap = drugInteractionsMap.getOrPut(genericName) {
mutableMapOf()
}
drugInfo.openFDA.manufacturerName?.forEach { mfrName ->
val interactionsString =
drugInfo.drugInteractionsString?.joinToString(
separator = "; "
)
?: "No interactions found"
mfrToInteractionsMap[mfrName] = interactionsString
}
}
}
}

// ****** Logging logic for debugging and not necessary for app functionality ******
Log.d("InteractingDrugsListFragment", "drugInteractionsMap: $drugInteractionsMap")
var totalMfrCount = 0
drugInteractionsMap.forEach { (_, mfrMap) ->
totalMfrCount += mfrMap.size
}
val genericCount = drugInteractionsMap.size
val avgMfrsPerGeneric = if (genericCount > 0 ) totalMfrCount.toDouble() / genericCount else 0.0
Log.d("InteractingDrugsListFragment", "Total Generic Names: $genericCount")
Log.d("InteractingDrugsListFragment", "Total Mfr Names: $totalMfrCount")
Log.d("InteractingDrugsListFragment", "Avg Mfr name per Generic Name: $avgMfrsPerGeneric")
// *********************************************************************************

// Convert the nested map into nested data classes
// so the adapter can more easily work with it
val displayList = drugInteractionsMap.map { (genericName, mfrMap) ->
DrugInteractionsDisplay(
genericName = genericName,
manufacturerName = mfrMap.map { (mfrName, interactions) ->
Manufacturers(mfrName, interactions)
}
// ****** Logging logic for debugging and not necessary for app functionality ******
var totalMfrCount = 0
drugInteractionsMap.forEach { (_, mfrMap) ->
totalMfrCount += mfrMap.size
}
val genericCount = drugInteractionsMap.size
val avgMfrsPerGeneric =
if (genericCount > 0) totalMfrCount.toDouble() / genericCount else 0.0
Log.d("InteractingDrugsListFragment", "Total Generic Names: $genericCount")
Log.d("InteractingDrugsListFragment", "Total Mfr Names: $totalMfrCount")
Log.d(
"InteractingDrugsListFragment",
"Avg Mfr name per Generic Name: $avgMfrsPerGeneric"
)
}
// *********************************************************************************

// Convert the nested map into nested data classes
// so the adapter can more easily work with it
val displayList = drugInteractionsMap.map { (genericName, mfrMap) ->
DrugInteractionsDisplay(
genericName = genericName,
manufacturerName = mfrMap.map { (mfrName, interactions) ->
Manufacturers(mfrName, interactions)
}
)
}

// Send the new nested data classes to the adapter
adapter.updateDrugInteractionsList(displayList)
// Send the new nested data classes to the adapter
adapter.updateDrugInteractionsList(displayList)

searchResultsListRV.visibility = View.VISIBLE
searchResultsListRV.scrollToPosition(0)
searchResultsListRV.visibility = View.VISIBLE
searchResultsListRV.scrollToPosition(0)

// Prep and share the list of drugs
shareButton.setOnClickListener {
val shareText = buildDrugListShareString(exampleDrug, displayList)
val intent = Intent().apply {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_TEXT, shareText)
type = "text/plain"
// Prep and share the list of drugs
shareButton.setOnClickListener {
val shareText = buildDrugListShareString(searchedDrug, displayList)
val intent = Intent().apply {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_TEXT, shareText)
type = "text/plain"
}
startActivity(Intent.createChooser(intent, null))
}
startActivity(Intent.createChooser(intent, null))
}
}
}
Expand All @@ -141,15 +151,6 @@ class InteractingDrugsListFragment : Fragment(R.layout.drug_report_fragment) {

}

// TODO Implement the onResume() method
// override fun onResume() {
// super.onResume()
//
// val exampleDrug = "hydrocodone"
// val exampleQuery = "drug_interactions:$exampleDrug AND _exists_:openfda.generic_name"
// viewModel.loadDrugInteractions(exampleQuery)
// }

private fun onDrugInfoItemClick(drugInfo: DrugInteractionsDisplay) {
Log.d("InteractingDrugsListFragment", "Item clicked: $drugInfo")
}
Expand Down

0 comments on commit 5906b73

Please sign in to comment.