Skip to content

Add widget of leetcode daily problem #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Get the latest snapshot of development version: https://t.ly/myQ6X
<img src="./ss/Goal-Status.png" alt="Screenshot 4" width="200"/>
<img src="./ss/Goal-Notification-1.png" alt="Screenshot 5" width="200"/>
<img src="./ss/Goal-Notification-2.png" alt="Screenshot 6" width="200"/>
<img src="./ss/widget.png" alt="Screenshot 6" width="200"/>
</p>

## Built With 🛠️
Expand Down
6 changes: 6 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ android {
excludes += "META-INF/gradle/incremental.annotation.processors"
}
}
composeOptions {
kotlinCompilerExtensionVersion = libs.versions.compose.compiler.get()
}
configurations.all {
exclude(group = "org.apache.httpcomponents", module = "httpclient")
}
Expand Down Expand Up @@ -84,6 +87,9 @@ dependencies {
implementation(libs.bytebeats.charts)
implementation(libs.androidx.splashscreen)
implementation(libs.androidx.material.icons.extended)
implementation(libs.androidx.glance)
implementation(libs.androidx.glance.appwidget)
implementation(libs.androidx.glance.material3)
kapt(libs.room.compiler)

testImplementation(libs.junit)
Expand Down
16 changes: 15 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,26 @@
<application
android:name=".LeetCodePlusApplication"
android:allowBackup="true"
android:usesCleartextTraffic="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@drawable/leetcode_logo"
android:label="@string/app_name"
android:networkSecurityConfig="@xml/network_security_config"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:usesCleartextTraffic="true"
tools:targetApi="31">

<activity
android:name=".ui.MainActivity"
android:exported="true"
android:theme="@style/Theme.LeetcodePlus">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="com.byteutility.dev.leetcode.plus.main" />

<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

Expand All @@ -34,6 +37,17 @@
android:authorities="${applicationId}.androidx-startup"
tools:node="remove" />

<receiver
android:name=".glance.LeetCodePlusGlanceAppWidgetReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/leetcode_plus_glance_appwidget_info" />
</receiver>

</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.byteutility.dev.leetcode.plus.glance

import androidx.glance.appwidget.GlanceAppWidget
import androidx.glance.appwidget.GlanceAppWidgetReceiver
import com.byteutility.dev.leetcode.plus.glance.ui.LeetcodePlusGlanceAppWidget

class LeetCodePlusGlanceAppWidgetReceiver : GlanceAppWidgetReceiver() {
override val glanceAppWidget: GlanceAppWidget = LeetcodePlusGlanceAppWidget()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package com.byteutility.dev.leetcode.plus.glance.ui

import android.content.Context
import android.content.Intent
import android.net.Uri
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.TextUnitType
import androidx.compose.ui.unit.dp
import androidx.glance.GlanceId
import androidx.glance.GlanceModifier
import androidx.glance.LocalContext
import androidx.glance.action.clickable
import androidx.glance.appwidget.GlanceAppWidget
import androidx.glance.appwidget.action.actionStartActivity
import androidx.glance.appwidget.cornerRadius
import androidx.glance.appwidget.provideContent
import androidx.glance.background
import androidx.glance.layout.Alignment
import androidx.glance.layout.Box
import androidx.glance.layout.Column
import androidx.glance.layout.Spacer
import androidx.glance.layout.fillMaxWidth
import androidx.glance.layout.height
import androidx.glance.layout.padding
import androidx.glance.text.FontWeight
import androidx.glance.text.Text
import androidx.glance.text.TextStyle
import androidx.glance.unit.ColorProvider
import com.byteutility.dev.leetcode.plus.data.datastore.UserDatastore
import com.byteutility.dev.leetcode.plus.data.model.LeetCodeProblem
import dagger.hilt.EntryPoint
import dagger.hilt.InstallIn
import dagger.hilt.android.EntryPointAccessors
import dagger.hilt.components.SingletonComponent

class LeetcodePlusGlanceAppWidget : GlanceAppWidget() {

override suspend fun provideGlance(
context: Context,
id: GlanceId
) {
provideContent {
val context = LocalContext.current
val datastore = EntryPointAccessors.fromApplication(
context.applicationContext,
UserDataStoreProviderEntryPoint::class.java
).userDatastore()
val dailyProblem = remember { mutableStateOf<LeetCodeProblem?>(null) }
var isLoading = remember { mutableStateOf(true) }
LaunchedEffect(Unit) {
datastore.getDailyProblem().collect {
dailyProblem.value = it
isLoading.value = false
}
}
if (isLoading.value) {

} else {
dailyProblem.value?.let {
DailyProblemWidget(it)
}
}
}
}

@Composable
private fun DailyProblemWidget(
problem: LeetCodeProblem
) {
val intent = Intent(
Intent.ACTION_VIEW,
Uri.parse("https://leetcode.com/problems/${problem.titleSlug}/description")
)

Box(
modifier = GlanceModifier
.padding(8.dp)
.background(ColorProvider(Color.White))
.fillMaxWidth()
.cornerRadius(16.dp)
) {
Box(
modifier = GlanceModifier
.padding(12.dp)
.background(MaterialTheme.colorScheme.errorContainer)
.fillMaxWidth()
.cornerRadius(12.dp)
.clickable(actionStartActivity(intent))
) {
Column(
horizontalAlignment = Alignment.Start,
) {
Text(
text = "Problem of the day",
style = TextStyle(
fontSize = TextUnit(16.0F, TextUnitType.Sp)
)
)
Spacer(modifier = GlanceModifier.height(4.dp))
Text(
text = problem.title,
style = TextStyle(
fontWeight = FontWeight.Bold,
fontSize = TextUnit(20.0F, TextUnitType.Sp)
)
)
}
}
}
}
}


@EntryPoint
@InstallIn(SingletonComponent::class)
interface UserDataStoreProviderEntryPoint {
fun userDatastore(): UserDatastore
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,18 @@
package com.byteutility.dev.leetcode.plus.network

import com.byteutility.dev.leetcode.plus.network.annotation.Format
import com.byteutility.dev.leetcode.plus.network.annotation.RequestFormat
import com.byteutility.dev.leetcode.plus.network.annotation.ResponseFormat
import com.byteutility.dev.leetcode.plus.network.requestVO.SampleRequestVo
import com.byteutility.dev.leetcode.plus.network.responseVo.DailyQuestionResponse
import com.byteutility.dev.leetcode.plus.network.responseVo.ProblemSetResponseVo
import com.byteutility.dev.leetcode.plus.network.responseVo.UserContestVo
import com.byteutility.dev.leetcode.plus.network.responseVo.UserProfileVo
import com.byteutility.dev.leetcode.plus.network.responseVo.UserSolvedVo
import com.byteutility.dev.leetcode.plus.network.responseVo.UserSubmissionVo
import retrofit2.http.Body
import retrofit2.http.GET
import retrofit2.http.HeaderMap
import retrofit2.http.POST
import retrofit2.http.Path
import retrofit2.http.Query
import retrofit2.http.Url

interface RestApiService {
@ResponseFormat(Format.JSON)
@RequestFormat(Format.JSON)
@POST
suspend fun samplePostRequest(
@Url fullUrl: String,
@HeaderMap headerMap: Map<String, String>,
@Body body: SampleRequestVo
)

@ResponseFormat(Format.JSON)
@GET("/problems")
Expand Down
20 changes: 20 additions & 0 deletions app/src/main/res/values/integers.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2023 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

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<resources>
<!-- Refresh widget every 1 hour -->
<integer name="widget_update_period_millis">3600000</integer>
</resources>
24 changes: 24 additions & 0 deletions app/src/main/res/xml/leetcode_plus_glance_appwidget_info.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2023 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

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:description="@string/app_name"
android:initialLayout="@layout/glance_default_loading_layout"
android:minHeight="65dp"
android:minWidth="250dp"
android:resizeMode="horizontal|vertical"
android:updatePeriodMillis="@integer/widget_update_period_millis"
android:widgetCategory="home_screen" />
5 changes: 5 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ coreKtx = "1.13.1"
lifecycleRuntimeKtx = "2.8.7"
activityCompose = "1.9.3"
composeBom = "2024.11.00"
compose-compiler = "1.5.4"
materialIconsExtended = "1.7.5"
retrofit = "2.11.0"
okhttp = "4.12.0"
Expand All @@ -22,6 +23,7 @@ secretsGradlePlugin = "2.0.1"
junit = "4.13.2"
junitVersion = "1.2.1"
espressoCore = "3.6.1"
androidx-glance = "1.1.1"

[libraries]
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
Expand Down Expand Up @@ -62,6 +64,9 @@ coil-network = { group = "io.coil-kt.coil3", name = "coil-network-okhttp", versi
bytebeats-charts = { group = "io.github.bytebeats", name = "compose-charts", version.ref = "bytebeats-charts" }
androidx-splashscreen = { group = "androidx.core", name = "core-splashscreen", version.ref = "androidx-splashscreen" }
secrets-gradle-plugin = { module = "com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin", version.ref = "secretsGradlePlugin" }
androidx-glance = { group = "androidx.glance", name = "glance", version.ref = "androidx-glance" }
androidx-glance-appwidget = { group = "androidx.glance", name = "glance-appwidget", version.ref = "androidx-glance" }
androidx-glance-material3 = { module = "androidx.glance:glance-material3", version.ref = "androidx-glance" }

[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
Expand Down
Binary file added ss/widget.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.