diff --git a/desktop/src/app/atlas/atlas.component.ts b/desktop/src/app/atlas/atlas.component.ts index c9a837156..96a517a40 100644 --- a/desktop/src/app/atlas/atlas.component.ts +++ b/desktop/src/app/atlas/atlas.component.ts @@ -12,6 +12,7 @@ import { ONE_DECIMAL_PLACE_FORMATTER, TWO_DIGITS_FORMATTER } from '../../shared/ import { AngularService } from '../../shared/services/angular.service' import { ApiService } from '../../shared/services/api.service' import { BrowserWindowService } from '../../shared/services/browser-window.service' +import { DeviceService } from '../../shared/services/device.service' import { ElectronService } from '../../shared/services/electron.service' import { PreferenceService } from '../../shared/services/preference.service' import { extractDate, extractTime } from '../../shared/types/angular.types' @@ -58,6 +59,7 @@ export class AtlasComponent implements OnInit, AfterContentInit, AfterViewInit, private readonly route = inject(ActivatedRoute) private readonly preferenceService = inject(PreferenceService) private readonly angularService = inject(AngularService) + private readonly deviceService = inject(DeviceService) protected readonly sun = structuredClone(DEFAULT_SUN) protected readonly moon = structuredClone(DEFAULT_MOON) @@ -538,7 +540,7 @@ export class AtlasComponent implements OnInit, AfterContentInit, AfterViewInit, this.minorPlanet.closeApproach.result = await this.api.closeApproachesOfMinorPlanets(this.minorPlanet.closeApproach.days, this.minorPlanet.closeApproach.lunarDistance, this.dateTimeAndLocation.dateTime) if (!this.minorPlanet.closeApproach.result.length) { - this.angularService.message('No close approaches found for the given days and lunar distance', 'warning') + this.angularService.message('No close approaches found for the given days and lunar distance', 'warn') } } @@ -974,25 +976,8 @@ export class AtlasComponent implements OnInit, AfterContentInit, AfterViewInit, } } - private async executeMount(action: (mount: Mount) => void | Promise) { - if (await this.angularService.confirm('Are you sure that you want to proceed?')) { - return false - } - + private async executeMount(action: (mount: Mount) => void | Promise, showConfirmation: boolean = true) { const mounts = await this.api.mounts() - - if (mounts.length === 1) { - await action(mounts[0]) - return true - } else { - const mount = await this.deviceMenu.show(mounts) - - if (mount && mount !== 'NONE' && mount.connected) { - await action(mount) - return true - } - } - - return false + return this.deviceService.executeAction(this.deviceMenu, mounts, action, showConfirmation) } } diff --git a/desktop/src/app/image/image.component.ts b/desktop/src/app/image/image.component.ts index 09a6300db..53ce41422 100644 --- a/desktop/src/app/image/image.component.ts +++ b/desktop/src/app/image/image.component.ts @@ -10,6 +10,7 @@ import { SEPARATOR_MENU_ITEM } from '../../shared/constants' import { AngularService } from '../../shared/services/angular.service' import { ApiService } from '../../shared/services/api.service' import { BrowserWindowService } from '../../shared/services/browser-window.service' +import { DeviceService } from '../../shared/services/device.service' import { ElectronService } from '../../shared/services/electron.service' import { PreferenceService } from '../../shared/services/preference.service' import { EquatorialCoordinateJ2000, filterAstronomicalObject } from '../../shared/types/atlas.types' @@ -68,6 +69,7 @@ export class ImageComponent implements AfterViewInit, OnDestroy { private readonly browserWindowService = inject(BrowserWindowService) private readonly preferenceService = inject(PreferenceService) private readonly angularService = inject(AngularService) + private readonly deviceService = inject(DeviceService) protected readonly preference = structuredClone(DEFAULT_IMAGE_PREFERENCE) protected readonly solver = structuredClone(DEFAULT_IMAGE_SOLVER_DIALOG) @@ -1485,46 +1487,12 @@ export class ImageComponent implements AfterViewInit, OnDestroy { } private async executeCamera(action: (camera: Camera) => void | Promise, showConfirmation: boolean = true) { - if (showConfirmation && (await this.angularService.confirm('Are you sure that you want to proceed?'))) { - return false - } - const cameras = await this.api.cameras() - - if (cameras.length === 1) { - await action(cameras[0]) - return true - } else { - const camera = await this.deviceMenu.show(cameras, undefined, 'CAMERA') - - if (camera && camera !== 'NONE' && camera.connected) { - await action(camera) - return true - } - } - - return false + return this.deviceService.executeAction(this.deviceMenu, cameras, action, showConfirmation) } private async executeMount(action: (mount: Mount) => void | Promise, showConfirmation: boolean = true) { - if (showConfirmation && (await this.angularService.confirm('Are you sure that you want to proceed?'))) { - return false - } - const mounts = await this.api.mounts() - - if (mounts.length === 1) { - await action(mounts[0]) - return true - } else { - const mount = await this.deviceMenu.show(mounts, undefined, 'MOUNT') - - if (mount && mount !== 'NONE' && mount.connected) { - await action(mount) - return true - } - } - - return false + return this.deviceService.executeAction(this.deviceMenu, mounts, action, showConfirmation) } } diff --git a/desktop/src/app/sequencer/sequencer.component.ts b/desktop/src/app/sequencer/sequencer.component.ts index 88a9e4416..a91d1e27b 100644 --- a/desktop/src/app/sequencer/sequencer.component.ts +++ b/desktop/src/app/sequencer/sequencer.component.ts @@ -365,7 +365,7 @@ export class SequencerComponent implements AfterContentInit, OnDestroy, Tickable private loadPlanFromJson(file: JsonFile) { if (!this.loadPlan(file.json)) { - this.angularService.message('No sequence found', 'warning') + this.angularService.message('No sequence found', 'warn') this.add() } diff --git a/desktop/src/shared/components/device-list-menu/device-list-menu.component.ts b/desktop/src/shared/components/device-list-menu/device-list-menu.component.ts index 6e3445cc6..093669583 100644 --- a/desktop/src/shared/components/device-list-menu/device-list-menu.component.ts +++ b/desktop/src/shared/components/device-list-menu/device-list-menu.component.ts @@ -57,7 +57,7 @@ export class DeviceListMenuComponent { return new Promise>((resolve) => { if (devices.length <= 0) { resolve(undefined) - this.angularService.message('Please connect your equipment first!', 'warning') + this.angularService.message('No equipment available to perform this action!', 'warn') return } diff --git a/desktop/src/shared/interceptors/error.interceptor.ts b/desktop/src/shared/interceptors/error.interceptor.ts index 9d9bb3751..b5322c8b0 100644 --- a/desktop/src/shared/interceptors/error.interceptor.ts +++ b/desktop/src/shared/interceptors/error.interceptor.ts @@ -5,7 +5,7 @@ import { AngularService } from '../services/angular.service' export interface ErrorResponse { message: string - type: 'success' | 'info' | 'warning' | 'error' + type: 'success' | 'info' | 'warn' | 'error' } @Injectable({ providedIn: 'root' }) diff --git a/desktop/src/shared/services/angular.service.ts b/desktop/src/shared/services/angular.service.ts index a4a977eb3..fad4bd720 100644 --- a/desktop/src/shared/services/angular.service.ts +++ b/desktop/src/shared/services/angular.service.ts @@ -38,7 +38,7 @@ export class AngularService { return ConfirmDialogComponent.open(this, message) } - message(text: string, severity: 'success' | 'info' | 'warning' | 'error' = 'success') { + message(text: string, severity: 'success' | 'info' | 'warn' | 'error' = 'success') { this.messageService.add({ severity, detail: text, life: 4000 }) } } diff --git a/desktop/src/shared/services/device.service.ts b/desktop/src/shared/services/device.service.ts new file mode 100644 index 000000000..aecb8e380 --- /dev/null +++ b/desktop/src/shared/services/device.service.ts @@ -0,0 +1,33 @@ +import { inject, Injectable } from '@angular/core' +import { DeviceListMenuComponent } from '../components/device-list-menu/device-list-menu.component' +import { Device } from '../types/device.types' +import { AngularService } from './angular.service' + +@Injectable({ providedIn: 'root' }) +export class DeviceService { + private readonly angularService = inject(AngularService) + + async executeAction(deviceMenu: DeviceListMenuComponent, devices: T[], action: (device: T) => void | Promise, showConfirmation: boolean = true) { + if (showConfirmation && (await this.angularService.confirm('Are you sure that you want to proceed?'))) { + return false + } + + if (!devices.length) { + this.angularService.message('No equipment available to perform this action!', 'warn') + return false + } else { + const device = devices.length === 1 ? devices[0] : await deviceMenu.show(devices, undefined, devices[0].type) + + if (device && device !== 'NONE') { + if (device.connected) { + await action(device) + return true + } else { + this.angularService.message('Your equipment must be connected to perform this action', 'error') + } + } + } + + return false + } +}