Skip to content

Commit

Permalink
Added reaction on removed dive.
Browse files Browse the repository at this point in the history
  • Loading branch information
jirkapok committed Mar 23, 2024
1 parent 8377765 commit 20030fa
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
25 changes: 22 additions & 3 deletions projects/planner/src/app/shared/diff/profileComparatorService.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
import { Injectable } from '@angular/core';
import { DiveSchedule, DiveSchedules } from '../dive.schedules';
import { DiveResults } from '../diveresults';
import { Observable, Subject } from 'rxjs';
import { Observable, Subject, takeUntil } from 'rxjs';
import { ConsumptionByMix, IConsumedMix } from 'scuba-physics';
import { ComparedWaypoint } from '../ComparedWaypoint';
import { WayPoint } from '../models';
import { ReloadDispatcher } from '../reloadDispatcher';
import { Streamed } from '../streamed';

@Injectable()
export class ProfileComparatorService {
export class ProfileComparatorService extends Streamed {
private _profileAIndex = 0;
private _profileBIndex = 0;
private _onSelectionChanged: Subject<void> = new Subject<void>();
private _selectionChanged$: Observable<void>;

constructor(private schedules: DiveSchedules) {
constructor(private schedules: DiveSchedules, private dispatcher: ReloadDispatcher) {
super();
this._selectionChanged$ = this._onSelectionChanged.asObservable();

if(this.hasTwoProfiles) {
this.selectProfile(1);
}

// In case dive removed
this.dispatcher.depthChanged$.pipe(takeUntil(this.unsubscribe$))
.subscribe(() => this.resetSelection());
}

public get profileAIndex(): number {
Expand Down Expand Up @@ -134,6 +141,18 @@ export class ProfileComparatorService {
}, 100);
});
}

private resetSelection(): void {
const maxIndex = this.schedules.length;

if(this._profileAIndex >= maxIndex) {
this._profileAIndex = 0;
}

if(this._profileBIndex >= maxIndex) {
this._profileBIndex = 0;
}
}
}

class WaypointsDiffContext {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,21 @@ describe('ProfileComparison service', () => {
});

it('Only 1 dive compares with it self', () => {
expect(sut.profileA).toEqual(schedules.dives[0]);
expect(sut.profileB).toEqual(schedules.dives[0]);
expect(sut.profileAIndex).toEqual(0);
expect(sut.profileBIndex).toEqual(0);
});

it('Two dives compares first two dives', () => {
schedules.add();
const newSut = new ProfileComparatorService(schedules);
expect(newSut.profileA).toEqual(schedules.dives[0]);
expect(newSut.profileB).toEqual(schedules.dives[1]);
const newSut = new ProfileComparatorService(schedules, new ReloadDispatcher());
expect(newSut.profileAIndex).toEqual(0);
expect(newSut.profileBIndex).toEqual(1);
});

it('Has only one profile', () => {
expect(sut.hasTwoProfiles).toBeFalsy();
});

// TODO add reaction on event when profile was removed

it('Has two profiles', () => {
schedules.add();
expect(sut.hasTwoProfiles).toBeTruthy();
Expand Down Expand Up @@ -141,8 +139,8 @@ describe('ProfileComparison service', () => {
});

it('Selects Profile', () => {
expect(sut.profileA).toEqual(schedules.dives[expectedA]);
expect(sut.profileB).toEqual(schedules.dives[expectedB]);
expect(sut.profileAIndex).toEqual(expectedA);
expect(sut.profileBIndex).toEqual(expectedB);
});

it('Selection change event received', () => {
Expand All @@ -152,8 +150,19 @@ describe('ProfileComparison service', () => {
it('Switch already selected profiles', () => {
sut.selectProfile(expectedB);

expect(sut.profileA).toEqual(schedules.dives[expectedB]);
expect(sut.profileB).toEqual(schedules.dives[expectedA]);
expect(sut.profileAIndex).toEqual(expectedB);
expect(sut.profileBIndex).toEqual(expectedA);
});

it('Remove selected profile A resets selection to first profile', () => {
const added = schedules.add();
sut.selectProfile(added.index -1);
sut.selectProfile(added.index);
schedules.remove(added);
schedules.remove(schedules.dives[2]);

expect(sut.profileAIndex).toEqual(0);
expect(sut.profileBIndex).toEqual(0);
});
});
});

0 comments on commit 20030fa

Please sign in to comment.