Skip to content

Commit

Permalink
Add alerts to widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
steviek committed Jan 24, 2025
1 parent 3c5b43c commit 1b2550c
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.glance.ImageProvider
import androidx.glance.action.Action
import androidx.glance.action.clickable
import androidx.glance.appwidget.cornerRadius
import androidx.glance.unit.ColorProvider
import org.jetbrains.compose.resources.StringResource

@GlanceComposable
Expand All @@ -22,13 +23,14 @@ fun ImageButton(
contentDesc: StringResource,
onClick: Action,
isClickable: Boolean = true,
tintColor: ColorProvider = GlanceTheme.colors.primary
) {
Image(
modifier = modifier
.let { if (isClickable) it.clickable(onClick) else it }
.cornerRadius(200.dp),
provider = ImageProvider(srcResId),
contentDescription = stringResource(contentDesc),
colorFilter = ColorFilter.tint(GlanceTheme.colors.primary),
colorFilter = ColorFilter.tint(tintColor),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import androidx.glance.layout.fillMaxWidth
import androidx.glance.layout.height
import androidx.glance.layout.size
import androidx.glance.visibility
import com.sixbynine.transit.path.MainActivity.Companion.createAppWidgetLaunchAction
import com.sixbynine.transit.path.R.drawable
import com.sixbynine.transit.path.util.isFailure
import com.sixbynine.transit.path.util.isLoading
Expand All @@ -31,6 +32,7 @@ import com.sixbynine.transit.path.widget.glance.Text
import com.sixbynine.transit.path.widget.glance.stringResource
import com.sixbynine.transit.path.widget.startConfigurationActivityAction
import pathwidgetxplat.composeapp.generated.resources.Res.string
import pathwidgetxplat.composeapp.generated.resources.alerts
import pathwidgetxplat.composeapp.generated.resources.edit
import pathwidgetxplat.composeapp.generated.resources.error_long
import pathwidgetxplat.composeapp.generated.resources.error_short
Expand All @@ -50,15 +52,28 @@ fun WidgetFooter(
verticalAlignment = Alignment.CenterVertically,
modifier = modifier.fillMaxWidth()
) {
ImageButton(
modifier =
GlanceModifier
.visibility(if (VERSION.SDK_INT >= 31) Invisible else Visible),
isClickable = VERSION.SDK_INT < 31,
srcResId = drawable.ic_edit_inset,
contentDesc = string.edit,
onClick = startConfigurationActivityAction()
)
if (VERSION.SDK_INT >= 31) {
val alertCount = result.data?.globalAlerts?.size ?: 0
if (alertCount > 0) {
ImageButton(
srcResId = drawable.ic_warning_inset,
contentDesc = string.alerts,
onClick = createAppWidgetLaunchAction(),
tintColor = GlanceTheme.colors.error,
)
} else {
Spacer(GlanceModifier.size(40.dp))
}
} else {
// sorry old phones, I don't want to think about your real estate situation yet. You
// need this edit button.
ImageButton(
srcResId = drawable.ic_edit_inset,
contentDesc = string.edit,
onClick = startConfigurationActivityAction()
)
}


Spacer(modifier = GlanceModifier.defaultWeight().height(1.dp))

Expand Down
5 changes: 5 additions & 0 deletions composeApp/src/androidMain/res/drawable/ic_warning.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="#000000" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp">

<path android:fillColor="@android:color/white" android:pathData="M4.47,21h15.06c1.54,0 2.5,-1.67 1.73,-3L13.73,4.99c-0.77,-1.33 -2.69,-1.33 -3.46,0L2.74,18c-0.77,1.33 0.19,3 1.73,3zM12,14c-0.55,0 -1,-0.45 -1,-1v-2c0,-0.55 0.45,-1 1,-1s1,0.45 1,1v2c0,0.55 -0.45,1 -1,1zM13,18h-2v-2h2v2z"/>

</vector>
4 changes: 4 additions & 0 deletions composeApp/src/androidMain/res/drawable/ic_warning_inset.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/ic_warning"
android:inset="12dp"/>
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<resources>
<string name="add_station">Add station</string>
<string name="advanced_settings">Advanced settings</string>
<string name="alerts">Alerts</string>
<string name="and_or">and/or</string>
<string name="app_and_widget_settings">Settings that apply to the app and any widgets</string>
<string name="app_name">Departures for PATH</string>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<resources>
<string name="add_station">Añadir una estación</string>
<string name="advanced_settings">Configuraciones avanzadas</string>
<string name="alerts">Alertas</string>
<string name="and_or">y/o</string>
<string name="app_and_widget_settings">Configuraciones para la app y widgets</string>
<string name="app_name">Salidas para PATH</string>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<resources>
<string name="add_station">Add station</string>
<string name="advanced_settings">Advanced settings</string>
<string name="alerts">Alerts</string>
<string name="and_or">and/or</string>
<string name="app_and_widget_settings">Settings that apply to the app and any widgets</string>
<string name="app_name">Departures for PATH</string>
Expand Down
32 changes: 24 additions & 8 deletions iosApp/widget/DepartureBoardView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,31 @@ struct DepartureBoardView: View {
.frame(width: innerWidth, height: innerHeight)

HStack(alignment: .center, spacing: 0) {
Button(intent: RefreshIntent()) {
Image(systemName: "arrow.2.circlepath")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 24, height: 24)
if (entry.hasGlobalPathAlerts) {
// Show the error indicator if there are alerts. Clicking anywhere will open the app, which is fine.
let isDark = colorScheme == .dark
ZStack {
Image(systemName: "exclamationmark.triangle.fill")
.resizable()
.foregroundStyle(isDark ? .red : .orange)
.aspectRatio(contentMode: .fit)
.frame(width: 20, height: 20)
.padding(4)
}
.padding(4)

} else {
Button(intent: RefreshIntent()) {
Image(systemName: "arrow.2.circlepath")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 24, height: 24)
}
.padding(4)
.buttonStyle(.borderless)
.hidden()
}
.padding(4)
.buttonStyle(.borderless)
.hidden()


Spacer()

Expand Down
3 changes: 3 additions & 0 deletions iosApp/widget/widget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ struct Provider: AppIntentTimelineProvider {
data: nil,
hasError: false,
hasPathError: false,
hasGlobalPathAlerts: false,
dataFrom: Date()
)
return entry
Expand Down Expand Up @@ -109,6 +110,7 @@ struct Provider: AppIntentTimelineProvider {
data: widgetData,
hasError: hasError,
hasPathError: hasPathError,
hasGlobalPathAlerts: (widgetData?.globalAlerts.count ?? 0) > 0,
dataFrom: now
)
)
Expand All @@ -126,6 +128,7 @@ struct SimpleEntry: TimelineEntry {
let data: WidgetData?
let hasError: Bool
let hasPathError: Bool
let hasGlobalPathAlerts: Bool
let dataFrom: Date
}

Expand Down

0 comments on commit 1b2550c

Please sign in to comment.