Last active
October 24, 2024 22:36
-
-
Save kartikarora/f33bf118f921b27b742e9f0f02e391db to your computer and use it in GitHub Desktop.
Activity 7 Task 2
This file contains 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
@Singleton | |
class GeminiInterface @Inject constructor() { | |
private val apiKey get() = "YOUR_API_KEY" | |
private val splittingDelimiter = "::" | |
private val generativeModel by lazy { | |
GenerativeModel( | |
modelName = "gemini-1.5-flash-002", | |
apiKey = apiKey | |
) | |
} | |
suspend fun generateTopics(): List<Topic> { | |
val response = | |
generativeModel.generateContent( | |
"Give 3 topics for writing quotes about. Make sure they are 2 words each, capitalised. Separate each of them by `$splittingDelimiter` only." | |
) | |
val topics = response.text?.split(splittingDelimiter)?.map { | |
Topic( | |
name = it.trim(), | |
isGenerative = true, | |
quotes = emptyList() | |
) | |
} ?: emptyList() | |
return topics | |
} | |
suspend fun getQuotes(topicName: String): List<Quote> { | |
val response = | |
generativeModel.generateContent("Give me quotes on the topic of $topicName. I need at only 3 quotes. Separate each quote by `$splittingDelimiter` only.") | |
val quotes = response.text?.split(splittingDelimiter)?.map { | |
Quote(text = it.trim()) | |
} ?: emptyList() | |
return quotes | |
} | |
} |
This file contains 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
composable<Home> { | |
... | |
val generatedTopics by viewModel.generatedTopics.collectAsStateWithLifecycle() | |
TopicScreen( | |
topics = topics + generatedTopics, | |
onTopicClick = { topicName -> | |
navController.navigate(QuotesDestination(topicName)) | |
}, | |
modifier = Modifier.fillMaxSize() | |
) | |
} | |
composable<QuotesDestination> { backStackEntry -> | |
... | |
val quotes by viewModel.quotes.collectAsStateWithLifecycle() | |
LaunchedEffect(topicName) { | |
viewModel.getQuotes(topicName) | |
} | |
QuoteScreen( | |
topicName = topicName, | |
quotes = quotes, | |
onNavigateBack = { navController.popBackStack() }, | |
modifier = Modifier.fillMaxSize() | |
) | |
} |
This file contains 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
private val _generatedTopics = MutableStateFlow<List<Topic>>(emptyList()) | |
val generatedTopics: StateFlow<List<Topic>> = _generatedTopics | |
private val _quotes = MutableStateFlow<List<Quote>>(emptyList()) | |
val quotes: StateFlow<List<Quote>> = _quotes | |
init { | |
generateTopics() | |
} | |
fun getQuotes(topicName: String) { | |
val quotes = topics.value.firstOrNull { it.name == topicName }?.quotes ?: emptyList() | |
if (quotes.isEmpty()) { | |
generateQuotes(topicName) | |
} else { | |
_quotes.value = quotes | |
} | |
} | |
private fun generateTopics() { | |
viewModelScope.launch(Dispatchers.IO) { | |
showLoading.emit(true) | |
val newTopics = geminiInterface.generateTopics() | |
_generatedTopics.emit(newTopics) | |
showLoading.emit(false) | |
} | |
} | |
private fun generateQuotes(topicName: String) { | |
viewModelScope.launch(Dispatchers.IO) { | |
showLoading.emit(true) | |
val newQuotes = geminiInterface.getQuotes(topicName) | |
_quotes.emit(newQuotes) | |
showLoading.emit(false) | |
} | |
} |
This file contains 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
@Composable | |
fun TopicBlock( | |
topic: Topic, | |
modifier: Modifier = Modifier | |
) { | |
Card( | |
modifier = modifier, | |
colors = CardColors( | |
containerColor = MaterialTheme.colorScheme.secondaryContainer, | |
contentColor = MaterialTheme.colorScheme.onSecondaryContainer, | |
disabledContainerColor = MaterialTheme.colorScheme.surfaceContainer, | |
disabledContentColor = MaterialTheme.colorScheme.onSurface, | |
) | |
) { | |
Row( | |
verticalAlignment = Alignment.CenterVertically, | |
modifier = Modifier.padding(16.dp) | |
) { | |
if (topic.isGenerative) { | |
Icon( | |
painter = painterResource(R.drawable.ic_sparkle), | |
contentDescription = "sparkle icon", | |
modifier = Modifier.padding(end = 16.dp) | |
) | |
} | |
Text( | |
text = topic.name.capitalize(Locale.current), | |
style = MaterialTheme.typography.bodyLarge | |
) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment