Skip to content
Open
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,8 @@ The notification then says the command was sent or the service logged. When it c

Settings > Notifications has a push switch for the device you are on and a per-type checklist for these alerts, which offers only the types the car's drivetrain can produce: the plug-in alerts (low EV battery, charging complete, charging interrupted) are left out for a plain hybrid, low fuel for a full electric car, and the RDW type (APK, recalls and insurance) wherever `RDW_ENABLED` is off. Both are kept per device: a type switched off on your phone still reaches your laptop, and still shows up in the notification panel. The weekly and monthly summaries are the other way round: a device gets them, in push and in its notification panel, only once it has switched them on, and **All on** and **All off** leave them as they are. Only the switch turns push on or off; the checklist never asks the browser for permission.

With two or more cars on the account, a **Car** picker above the checklist chooses whether it is for all cars or for one. A type switched off for one car stays on for the others, and a type switched off for all cars stays off for every car, which a car's own list shows as locked. The list for all cars offers every type any of the cars can produce, and a car's own list the types that car can. Like the rest of the checklist, the choice is kept per device, so a phone can mute the doors of one car while a laptop hears about both. A notification that belongs to no car, such as a failed MG sign-in, follows the list for all cars. The summaries are asked for once, for the device, and can then be switched off for a single car.

Notification texts are written by the background worker, which has no browser to take a language from, so their language is a deployment setting: `NOTIFICATION_LANGUAGE=en` (default), `nl`, `es` or `pt`. The web UI's language selection does not affect them.

MG app messages are the exception: they are passed on as SAIC wrote them, in the language of your MG account. Each one is sent once, even though the gateway repeats its latest message every time it starts, because GarageStack remembers which message it last saw. Two kinds are left out: the "vehicle started" message, which the engine started alert already covers, and any message sent more than a day ago, so a new install does not announce whatever the account last received. A message that arrives while the worker is not running is not sent afterwards.
Expand Down
12 changes: 12 additions & 0 deletions documentation/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -9023,6 +9023,18 @@
"items": {
"type": "string"
}
},
"excludedCategoriesByVehicle": {
"type": [
"null",
"object"
],
"additionalProperties": {
"type": "array",
"items": {
"type": "string"
}
}
}
}
},
Expand Down
13 changes: 13 additions & 0 deletions frontend/src/assets/settings.css
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,19 @@
line-height: 1.25;
}

/* A type switched off for all cars, shown under one car: off, and not the car's to change. */
.notif-type-checklist__item--locked {
cursor: default;
}

.notif-type-checklist__item--locked:hover {
background: transparent;
}

.notif-type-checklist__note {
display: block;
}

/* ── Settings tabs ────────────────────────────────────────── */

/* The row sits beside the dialog title and scrolls sideways where it does not fit. Its width,
Expand Down
131 changes: 121 additions & 10 deletions frontend/src/components/settings/NotificationTypeSettings.vue
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
<script setup lang="ts">
import { computed } from 'vue'
import { computed, ref, watch } from 'vue'
import { useI18n } from 'vue-i18n'
import { useUiSettingsStore } from '@/stores/settingsUi'
import { useVehicleStore } from '@/stores/vehicle'
import { useRdwStore } from '@/stores/rdw'
import { useNotificationPushSync } from '@/composables/useNotificationPushSync'
import SettingsToggle from '../SettingsToggle.vue'
import SettingsSelect, { type SettingsSelectOption } from '../SettingsSelect.vue'
import {
isCategoryShown,
isOptIn,
isPerCarCategory,
NOTIFICATION_CATEGORY_IDS,
notificationCategoryIdsFor,
type NotificationCategoryId,
} from '@/utils/notificationCategories'
import { vehicleLabels } from '@/utils/vehicleLabels'
import type { Vehicle } from '@/services/vehicleApi'

// Push for this device, and the notification types it shows and gets pushed.
const { t } = useI18n()
Expand Down Expand Up @@ -56,14 +61,50 @@ const NOTIFICATION_CATEGORY_LABEL_KEYS: Record<NotificationCategoryId, string> =
'monthly-summary': 'monthlySummary',
}

// The list below is for every car, or, with more than one car on the account, for one of them:
// 'all' or a vehicle id as text. A type switched off for all cars stays off for each of them,
// so a car's own list only ever switches more off (NotificationCategories.IsWanted).
const ALL_CARS = 'all'
const scope = ref(ALL_CARS)
const scopeVehicleId = computed(() => (scope.value === ALL_CARS ? null : scope.value))

const scopeOptions = computed((): SettingsSelectOption<string>[] => [
{ value: ALL_CARS, label: t('notifications.scopeAll') },
...vehicleLabels(vehicleStore.vehicles).map((car) => ({
value: String(car.id),
label: car.label,
})),
])

// A car that left the account leaves nothing to pick: fall back to every car.
watch(
() => vehicleStore.vehicles,
(cars) => {
if (scopeVehicleId.value !== null && !cars.some((car) => String(car.id) === scope.value))
scope.value = ALL_CARS
},
)

function typeOf(car: Vehicle) {
return car.vehicleTypeOverride ?? car.vehicleType
}

// Only the types this drivetrain can actually produce: a plain hybrid never charges, so offering
// it a "charging complete" switch is a control that does nothing. The RDW's only where the
// deployment looks plates up.
const offeredNotificationTypes = computed(() =>
notificationCategoryIdsFor(vehicleStore.effectiveVehicleType).filter(
(id) => id !== 'rdw' || rdw.available === true,
),
)
// deployment looks plates up. For every car at once that is what any of them can produce, and for
// one car, what that one can.
const offeredNotificationTypes = computed(() => {
const cars = vehicleStore.vehicles
const shown = scopeVehicleId.value
const own = cars.find((car) => String(car.id) === shown)
let offered: readonly NotificationCategoryId[]
if (own) offered = notificationCategoryIdsFor(typeOf(own)).filter(isPerCarCategory)
else if (cars.length > 1) {
const union = new Set(cars.flatMap((car) => notificationCategoryIdsFor(typeOf(car))))
offered = NOTIFICATION_CATEGORY_IDS.filter((id) => union.has(id))
} else offered = notificationCategoryIdsFor(vehicleStore.effectiveVehicleType)
return offered.filter((id) => id !== 'rdw' || rdw.available === true)
})

const notificationTypeOptions = computed(() =>
offeredNotificationTypes.value.map((id) => ({
Expand All @@ -80,14 +121,44 @@ const offeredAlertTypes = computed(() =>
offeredNotificationTypes.value.filter((id) => !isOptIn(id)),
)

// What the picked car has off on top of the device's own list; nothing while the list is for all cars.
const carExclusions = computed((): readonly string[] =>
scopeVehicleId.value === null
? []
: (settings.notificationTypeExclusionsByVehicle[scopeVehicleId.value] ?? []),
)

// A car with nothing off has no entry, the same as a car this device never chose for.
function setCarExclusions(vehicleId: string, types: readonly string[]) {
const others = Object.entries(settings.notificationTypeExclusionsByVehicle).filter(
([id]) => id !== vehicleId,
)
settings.notificationTypeExclusionsByVehicle = Object.fromEntries(
types.length > 0 ? [...others, [vehicleId, [...types]]] : others,
)
}

function selectAllNotificationTypes() {
const offered = offeredAlertTypes.value as readonly string[]
if (scopeVehicleId.value !== null) {
setCarExclusions(
scopeVehicleId.value,
carExclusions.value.filter((excludedId) => !offered.includes(excludedId)),
)
return
}
settings.notificationTypeExclusions = settings.notificationTypeExclusions.filter(
(excludedId) => !offered.includes(excludedId),
)
}

function deselectAllNotificationTypes() {
if (scopeVehicleId.value !== null) {
setCarExclusions(scopeVehicleId.value, [
...new Set([...carExclusions.value, ...offeredAlertTypes.value]),
])
return
}
settings.notificationTypeExclusions = [
...new Set([...settings.notificationTypeExclusions, ...offeredAlertTypes.value]),
]
Expand All @@ -97,8 +168,17 @@ function without(list: readonly string[], id: string): string[] {
return list.filter((other) => other !== id)
}

// An opt-in type is switched on by being in the inclusions, any other off by being in the exclusions.
// For all cars, an opt-in type is switched on by being in the inclusions, any other off by being
// in the exclusions. For one car every type, an opt-in one too, is switched off by being in that
// car's list: the summary itself is still asked for once, for the device.
function toggleNotificationType(id: NotificationCategoryId, checked: boolean) {
if (scopeVehicleId.value !== null) {
setCarExclusions(
scopeVehicleId.value,
checked ? without(carExclusions.value, id) : [...without(carExclusions.value, id), id],
)
return
}
if (isOptIn(id)) {
settings.notificationTypeInclusions = checked
? [...without(settings.notificationTypeInclusions, id), id]
Expand All @@ -110,8 +190,19 @@ function toggleNotificationType(id: NotificationCategoryId, checked: boolean) {
: [...settings.notificationTypeExclusions, id]
}

// A type switched off for all cars cannot be switched on for one car, so its row for a car is
// shown off and cannot be changed, and says why.
const isOffForAllCars = (id: NotificationCategoryId) =>
scopeVehicleId.value !== null &&
!isCategoryShown(id, settings.notificationTypeExclusions, settings.notificationTypeInclusions)

const isShown = (id: NotificationCategoryId) =>
isCategoryShown(id, settings.notificationTypeExclusions, settings.notificationTypeInclusions)
isCategoryShown(
id,
settings.notificationTypeExclusions,
settings.notificationTypeInclusions,
carExclusions.value,
)
</script>

<template>
Expand Down Expand Up @@ -139,6 +230,16 @@ const isShown = (id: NotificationCategoryId) =>
<template #control><span /></template>
</SettingsToggle>

<!-- With more than one car, the list below is for all of them or for one. -->
<SettingsSelect
v-if="vehicleStore.hasMultipleVehicles"
id="settings-notification-scope"
v-model="scope"
:label="t('notifications.scope')"
:desc="t('notifications.scopeDesc')"
:options="scopeOptions"
/>

<SettingsToggle
class="settings-toggle--stacked"
:label="t('notifications.typeFilter')"
Expand Down Expand Up @@ -167,17 +268,27 @@ const isShown = (id: NotificationCategoryId) =>
v-for="opt in notificationTypeOptions"
:key="opt.value"
class="form-check form-switch notif-type-checklist__item"
:class="{ 'notif-type-checklist__item--locked': isOffForAllCars(opt.value) }"
>
<input
class="form-check-input"
type="checkbox"
role="switch"
:checked="isShown(opt.value)"
:disabled="isOffForAllCars(opt.value)"
@change="
toggleNotificationType(opt.value, ($event.target as HTMLInputElement).checked)
"
/>
<span class="notif-type-checklist__label">{{ opt.label }}</span>
<span class="notif-type-checklist__label">
{{ opt.label }}
<span
v-if="isOffForAllCars(opt.value)"
class="notif-type-checklist__note text-muted text-sm"
>
{{ t('notifications.offForAllCars') }}
</span>
</span>
</label>
</div>
</template>
Expand Down
Loading
Loading