-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Add Recent Orders Widget #1567
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
Open
ashnohe
wants to merge
7
commits into
main
Choose a base branch
from
widget
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add Recent Orders Widget #1567
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0e2188b
Add Recent Orders Widget.
ashnohe a83eb8e
small refactor
ashnohe 8a2ffa1
acknowledge code review comments
ashnohe 5db0fb4
run spotless
ashnohe f009e2b
run spotless again
ashnohe 5e52729
Merge branch 'main' into widget
ashnohe 29bc987
run spotless agaain
ashnohe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
48 changes: 48 additions & 0 deletions
48
Jetsnack/app/src/main/java/com/example/jetsnack/widget/ActionDemonstrationActivity.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright 2023-2025 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 | ||
* 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. | ||
*/ | ||
|
||
package com.example.jetsnack.widget | ||
|
||
import androidx.activity.ComponentActivity | ||
import androidx.activity.compose.setContent | ||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.material3.Text | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.glance.action.ActionParameters | ||
|
||
internal val ActionSourceMessageKey = ActionParameters.Key<String>("actionSourceMessageKey") | ||
|
||
/** | ||
* Activity that is launched on clicks from different parts of sample widgets. Displays string | ||
* describing source of the click. | ||
*/ | ||
class ActionDemonstrationActivity : ComponentActivity() { | ||
|
||
override fun onResume() { | ||
super.onResume() | ||
setContent { | ||
Box( | ||
modifier = Modifier.fillMaxSize(), | ||
contentAlignment = Alignment.Center, | ||
) { | ||
val source = intent.getStringExtra(ActionSourceMessageKey.name) ?: "Unknown" | ||
Text("Launched from $source") | ||
} | ||
} | ||
} | ||
} |
130 changes: 130 additions & 0 deletions
130
Jetsnack/app/src/main/java/com/example/jetsnack/widget/RecentOrdersWidget.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/* | ||
* Copyright 2025 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 | ||
* 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. | ||
*/ | ||
|
||
package com.example.jetsnack.widget | ||
|
||
import android.content.Context | ||
import android.content.Intent | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.collectAsState | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.key | ||
import androidx.compose.ui.unit.DpSize | ||
import androidx.compose.ui.unit.dp | ||
import androidx.core.net.toUri | ||
import androidx.glance.GlanceId | ||
import androidx.glance.GlanceTheme | ||
import androidx.glance.LocalContext | ||
import androidx.glance.LocalSize | ||
import androidx.glance.appwidget.AppWidgetId | ||
import androidx.glance.appwidget.GlanceAppWidget | ||
import androidx.glance.appwidget.GlanceAppWidgetReceiver | ||
import androidx.glance.appwidget.SizeMode | ||
import androidx.glance.appwidget.action.actionStartActivity | ||
import androidx.glance.appwidget.provideContent | ||
import com.example.jetsnack.R | ||
import com.example.jetsnack.ui.MainActivity | ||
import com.example.jetsnack.widget.data.RecentOrdersDataRepository | ||
import com.example.jetsnack.widget.data.RecentOrdersDataRepository.Companion.getImageTextListDataRepo | ||
import com.example.jetsnack.widget.layout.ImageTextListItemData | ||
import com.example.jetsnack.widget.layout.ImageTextListLayout | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
|
||
class RecentOrdersWidget : GlanceAppWidget() { | ||
// Unlike the "Single" size mode, using "Exact" allows us to have better control over rendering in | ||
// different sizes. And, unlike the "Responsive" mode, it doesn't cause several views for each | ||
// supported size to be held in the widget host's memory. | ||
override val sizeMode: SizeMode = SizeMode.Exact | ||
|
||
override val previewSizeMode = SizeMode.Responsive( | ||
setOf( | ||
DpSize(256.dp, 115.dp), // 4x2 cell min size | ||
DpSize(260.dp, 180.dp), // Medium width layout, height with header | ||
), | ||
) | ||
|
||
override suspend fun provideGlance(context: Context, id: GlanceId) { | ||
val repo = getImageTextListDataRepo(id) | ||
|
||
val initialItems = withContext(Dispatchers.Default) { | ||
repo.load(context) | ||
} | ||
|
||
provideContent { | ||
GlanceTheme { | ||
val items by repo.data().collectAsState(initial = initialItems) | ||
|
||
key(LocalSize.current) { | ||
WidgetContent( | ||
items = items, | ||
shoppingCartActionIntent = Intent( | ||
context.applicationContext, | ||
MainActivity::class.java, | ||
) | ||
.setAction(Intent.ACTION_VIEW) | ||
.setFlags( | ||
Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK, | ||
) | ||
.setData("https://jetsnack.example.com/home/cart".toUri()), | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
fun WidgetContent(items: List<ImageTextListItemData>, shoppingCartActionIntent: Intent) { | ||
val context = LocalContext.current | ||
|
||
ImageTextListLayout( | ||
items = items, | ||
title = context.getString(R.string.widget_title), | ||
titleIconRes = R.drawable.widget_logo, | ||
titleBarActionIconRes = R.drawable.shopping_cart, | ||
titleBarActionIconContentDescription = context.getString( | ||
R.string.shopping_cart_button_label, | ||
), | ||
titleBarAction = actionStartActivity(shoppingCartActionIntent), | ||
shoppingCartActionIntent = shoppingCartActionIntent, | ||
) | ||
} | ||
|
||
override suspend fun providePreview(context: Context, widgetCategory: Int) { | ||
val repo = RecentOrdersDataRepository() | ||
val items = repo.load(context) | ||
|
||
provideContent { | ||
GlanceTheme { | ||
WidgetContent( | ||
items = items, | ||
shoppingCartActionIntent = Intent(), | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
class RecentOrdersWidgetReceiver : GlanceAppWidgetReceiver() { | ||
override val glanceAppWidget = RecentOrdersWidget() | ||
|
||
override fun onDeleted(context: Context, appWidgetIds: IntArray) { | ||
appWidgetIds.forEach { | ||
RecentOrdersDataRepository.cleanUp(AppWidgetId(appWidgetId = it)) | ||
} | ||
super.onDeleted(context, appWidgetIds) | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.