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
/* | |
* Copyright 2022 The Android Open Source Project | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* https://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software |
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
override fun onUpdate( | |
context: Context, | |
appWidgetManager: AppWidgetManager, | |
appWidgetIds: IntArray, | |
) { | |
super.onUpdate(context, appWidgetManager, appWidgetIds) | |
QuoteWidgetWorker.enqueue(context) | |
} | |
override fun onDeleted(context: Context, appWidgetIds: IntArray) { |
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
class QuoteWidgetWorker( | |
private val context: Context, | |
params: WorkerParameters, | |
) : CoroutineWorker(context, params) { | |
companion object { | |
private val uniqueWorkName = QuoteWidgetWorker::class.java.simpleName | |
fun enqueue(context: Context, force: Boolean = 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
... | |
val geminiInterface = GeminiInterface() | |
val scope = CoroutineScope(Dispatchers.IO) | |
val generatedQuote = scope.async(Dispatchers.IO) { | |
geminiInterface.getSingleQuote(currentTopicName) | |
} | |
updateAppWidgetState(context, glanceId) { prefs -> | |
val quote = sampleData.firstOrNull { | |
it.name == currentTopicName |
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
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 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 DataRepository @Inject constructor( | |
@ApplicationContext private val context: Context | |
) { | |
... | |
companion object { | |
private val GENERATED_TOPICS_KEY = stringPreferencesKey("generatedTopics") | |
private const val TOPICS_DATASTORE = "topics_datastore" | |
private const val SPLITTING_DELIMITER = "::" | |
} |
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 | |
) |
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
dependencies { | |
... | |
// Gemini SDK | |
implementation(libs.generativeai) | |
... | |
} |
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
dependencies { | |
implementation(libs.my.spectacular) | |
} |
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
[versions] | |
mySpectacularLibraryVersion = "1.2.3" | |
[libraries] | |
my-spectacular = { group = "my.spectacular", name = "library", version.ref = "mySpectacularLibraryVersion" } |
NewerOlder