Skip to content

Instantly share code, notes, and snippets.

@kartikarora
Created October 24, 2024 23:04
Show Gist options
  • Save kartikarora/42278623d34b1bb712a747fab0d711aa to your computer and use it in GitHub Desktop.
Save kartikarora/42278623d34b1bb712a747fab0d711aa to your computer and use it in GitHub Desktop.
Activity 8 Step 1
suspend fun getSingleQuote(topicName: String?): Quote? {
if (topicName == null) return null
val response =
generativeModel.generateContent("Give me a single quote on the topic of $topicName. Do not use any religious quotes.")
val quote = response.text?.let {
Quote(text = it.trim())
}
return quote
}
suspend fun getSingleQuote(topicName: String): Quote? {
val quote = topics.value.firstOrNull { it.name == topicName }?.quotes?.firstOrNull()
return quote ?: geminiInterface.getSingleQuote(topicName)
}
MotivateMeTheme {
...
val generatedTopics by viewModel.generatedTopics.collectAsStateWithLifecycle()
TopicScreen(
topics = topics + generatedTopics,
...
coroutineScope.launch {
val quote = async(Dispatchers.IO) {
viewModel.showLoading.emit(true)
viewModel.getSingleQuote(topicName)
}
...
prefs[QuoteWidget.KEY_QUOTE] = quote.await()?.text ?: "Quote not found"
}
...
}
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment