Created
October 24, 2024 23:04
-
-
Save kartikarora/42278623d34b1bb712a747fab0d711aa to your computer and use it in GitHub Desktop.
Activity 8 Step 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
suspend fun getSingleQuote(topicName: String): Quote? { | |
val quote = topics.value.firstOrNull { it.name == topicName }?.quotes?.firstOrNull() | |
return quote ?: geminiInterface.getSingleQuote(topicName) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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