Skip to content

Instantly share code, notes, and snippets.

View kartikarora's full-sized avatar
🎯
Focusing

Kartik Arora kartikarora

🎯
Focusing
View GitHub Profile
@kartikarora
kartikarora / FakeSleepData.kt
Created November 10, 2024 01:18
Fake Sleep Data
/*
* 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
@kartikarora
kartikarora / QuoteWidgetReceiver.kt
Created October 24, 2024 23:08
Activity 9 Step 2
override fun onUpdate(
context: Context,
appWidgetManager: AppWidgetManager,
appWidgetIds: IntArray,
) {
super.onUpdate(context, appWidgetManager, appWidgetIds)
QuoteWidgetWorker.enqueue(context)
}
override fun onDeleted(context: Context, appWidgetIds: IntArray) {
@kartikarora
kartikarora / QuoteWidgetWorker.kt
Created October 24, 2024 23:07
Activity 9 Step 1
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) {
@kartikarora
kartikarora / QuoteWidget.kt
Last active October 24, 2024 23:09
Activity 8 Step 2
...
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
@kartikarora
kartikarora / GeminiInterface.kt
Created October 24, 2024 23:04
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
}
@kartikarora
kartikarora / DataRepository.kt
Created October 24, 2024 22:40
Activity 7 Step 3
@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 = "::"
}
@kartikarora
kartikarora / GeminiInterface.kt
Last active October 24, 2024 22:36
Activity 7 Task 2
@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
)
@kartikarora
kartikarora / build.gradle.kts
Created October 24, 2024 16:57
Gemini SDK Dependencies
dependencies {
...
// Gemini SDK
implementation(libs.generativeai)
...
}
dependencies {
implementation(libs.my.spectacular)
}
[versions]
mySpectacularLibraryVersion = "1.2.3"
[libraries]
my-spectacular = { group = "my.spectacular", name = "library", version.ref = "mySpectacularLibraryVersion" }