-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9d330d3
commit 2738c1b
Showing
28 changed files
with
770 additions
and
41 deletions.
There are no files selected for viewing
This file contains 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
24 changes: 24 additions & 0 deletions
24
application/frontend/src/app/core/actions/travel-simulator.actions.ts
This file contains 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,24 @@ | ||
/* | ||
Copyright 2024 Google LLC | ||
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. | ||
*/ | ||
|
||
import { createAction, props } from '@ngrx/store'; | ||
|
||
export const setTime = createAction('[Travel Simulator] Set time', props<{ time: number }>()); | ||
|
||
export const setActive = createAction( | ||
'[Travel Simulator] Set active', | ||
props<{ active: boolean }>() | ||
); |
This file contains 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 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 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 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
30 changes: 30 additions & 0 deletions
30
...ication/frontend/src/app/core/containers/travel-simulator/travel-simulator.component.html
This file contains 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,30 @@ | ||
<div class="d-flex align-items-center"> | ||
<mat-slide-toggle [checked]="active$ | async" (change)="onToggleActive($event)" color="primary"> | ||
Travel simulator | ||
</mat-slide-toggle> | ||
<div *ngIf="active$ | async" class="d-flex align-items-center simulator-contents"> | ||
<mat-divider [vertical]="true" class="divider"></mat-divider> | ||
<button | ||
mat-mini-fab | ||
color="primary" | ||
class="play-button" | ||
type="button" | ||
[disabled]="!(active$ | async)" | ||
(click)="animating ? onEndAnimate() : onBeginAnimate()"> | ||
<mat-icon [svgIcon]="animating ? 'pause' : 'play'"></mat-icon> | ||
</button> | ||
<mat-divider [vertical]="true" class="divider"></mat-divider> | ||
<div class="d-flex align-items-center speed-container"> | ||
<div>Speed:</div> | ||
<mat-radio-group | ||
aria-label="Animation speed" | ||
color="primary" | ||
[(ngModel)]="animationSpeedMultiple"> | ||
<mat-radio-button [value]="0.5">0.5x</mat-radio-button> | ||
<mat-radio-button [value]="1">1x</mat-radio-button> | ||
<mat-radio-button [value]="3">3x</mat-radio-button> | ||
<mat-radio-button [value]="5">5x</mat-radio-button> | ||
</mat-radio-group> | ||
</div> | ||
</div> | ||
</div> |
42 changes: 42 additions & 0 deletions
42
...ication/frontend/src/app/core/containers/travel-simulator/travel-simulator.component.scss
This file contains 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,42 @@ | ||
@import '../../../../styles/variables.scss'; | ||
|
||
:host { | ||
position: absolute; | ||
bottom: 10px; | ||
left: 10px; | ||
padding: 10px; | ||
} | ||
|
||
.simulator-toggle { | ||
font-size: 16px; | ||
line-height: 24px; | ||
} | ||
|
||
.simulator-contents { | ||
margin-left: 20px; | ||
gap: 20px; | ||
} | ||
|
||
.divider { | ||
height: 24px; | ||
} | ||
|
||
.play-button { | ||
box-shadow: none; | ||
height: 24px; | ||
width: 24px; | ||
|
||
.mat-button-wrapper { | ||
position: relative; | ||
top: -4px; | ||
} | ||
} | ||
|
||
.speed-container { | ||
gap: 10px; | ||
|
||
mat-radio-group { | ||
display: flex; | ||
gap: 10px; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...tion/frontend/src/app/core/containers/travel-simulator/travel-simulator.component.spec.ts
This file contains 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,51 @@ | ||
/* | ||
Copyright 2024 Google LLC | ||
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. | ||
*/ | ||
|
||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
|
||
import { TravelSimulatorComponent } from './travel-simulator.component'; | ||
import { provideMockStore } from '@ngrx/store/testing'; | ||
import ShipmentModelSelectors from '../../selectors/shipment-model.selectors'; | ||
import TravelSimulatorSelectors from '../../selectors/travel-simulator.selectors'; | ||
|
||
describe('TravelSimulatorComponent', () => { | ||
let component: TravelSimulatorComponent; | ||
let fixture: ComponentFixture<TravelSimulatorComponent>; | ||
|
||
beforeEach(async () => { | ||
await TestBed.configureTestingModule({ | ||
declarations: [TravelSimulatorComponent], | ||
providers: [ | ||
provideMockStore({ | ||
selectors: [ | ||
{ selector: ShipmentModelSelectors.selectGlobalStartTime, value: 0 }, | ||
{ selector: ShipmentModelSelectors.selectGlobalEndTime, value: 0 }, | ||
{ selector: TravelSimulatorSelectors.selectTime, value: 0 }, | ||
{ selector: TravelSimulatorSelectors.selectActive, value: false }, | ||
], | ||
}), | ||
], | ||
}).compileComponents(); | ||
|
||
fixture = TestBed.createComponent(TravelSimulatorComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
}); |
109 changes: 109 additions & 0 deletions
109
application/frontend/src/app/core/containers/travel-simulator/travel-simulator.component.ts
This file contains 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,109 @@ | ||
/* | ||
Copyright 2024 Google LLC | ||
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. | ||
*/ | ||
|
||
import { | ||
ChangeDetectionStrategy, | ||
ChangeDetectorRef, | ||
Component, | ||
HostBinding, | ||
OnDestroy, | ||
OnInit, | ||
} from '@angular/core'; | ||
import { Store, select } from '@ngrx/store'; | ||
import Long from 'long'; | ||
import { Observable, Subscription, interval } from 'rxjs'; | ||
import ShipmentModelSelectors from '../../selectors/shipment-model.selectors'; | ||
import { map } from 'rxjs/operators'; | ||
import { setActive, setTime } from '../../actions/travel-simulator.actions'; | ||
import TravelSimulatorSelectors from '../../selectors/travel-simulator.selectors'; | ||
import { MatSlideToggleChange } from '@angular/material/slide-toggle'; | ||
import { formatSecondsDate } from 'src/app/util/time-translation'; | ||
|
||
@Component({ | ||
selector: 'app-travel-simulator', | ||
templateUrl: './travel-simulator.component.html', | ||
styleUrls: ['./travel-simulator.component.scss'], | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class TravelSimulatorComponent implements OnInit, OnDestroy { | ||
@HostBinding('class.app-map-panel') readonly mapPanel = true; | ||
|
||
readonly animationRateMs: number = 33; | ||
readonly animationStepSize: number = 2; | ||
|
||
get animating(): boolean { | ||
return this.animationTimer$?.closed === false; | ||
} | ||
|
||
active$: Observable<boolean>; | ||
animationTimer$: Subscription; | ||
animationSpeedMultiple = 1; | ||
end$: Subscription; | ||
end: number; | ||
time$: Subscription; | ||
time: number; | ||
|
||
formatSecondsDate = formatSecondsDate; | ||
|
||
constructor(private store: Store, private detectorRef: ChangeDetectorRef) {} | ||
|
||
ngOnInit(): void { | ||
this.end$ = this.store | ||
.select(ShipmentModelSelectors.selectGlobalEndTime) | ||
.pipe(map((value) => Long.fromValue(value).toNumber())) | ||
.subscribe((end) => { | ||
this.end = end; | ||
this.detectorRef.markForCheck(); | ||
}); | ||
|
||
this.time$ = this.store.select(TravelSimulatorSelectors.selectTime).subscribe((time) => { | ||
this.time = time; | ||
this.detectorRef.markForCheck(); | ||
}); | ||
|
||
this.active$ = this.store.pipe(select(TravelSimulatorSelectors.selectActive)); | ||
} | ||
|
||
ngOnDestroy(): void { | ||
this.onEndAnimate(); | ||
this.time$.unsubscribe(); | ||
this.end$.unsubscribe(); | ||
} | ||
|
||
onBeginAnimate(): void { | ||
this.animationTimer$ = interval(this.animationRateMs).subscribe((_value) => { | ||
const newTime = this.time + this.animationStepSize * this.animationSpeedMultiple; | ||
if (newTime >= this.end) { | ||
this.onEndAnimate(); | ||
return; | ||
} | ||
this.store.dispatch(setTime({ time: newTime })); | ||
}); | ||
} | ||
|
||
onEndAnimate(): void { | ||
this.animationTimer$?.unsubscribe(); | ||
this.detectorRef.markForCheck(); | ||
} | ||
|
||
onToggleActive(event: MatSlideToggleChange): void { | ||
this.store.dispatch(setActive({ active: event.checked })); | ||
|
||
if (!event.checked) { | ||
this.onEndAnimate(); | ||
} | ||
} | ||
} |
This file contains 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
Oops, something went wrong.