incyclist-services 1.1.97 → 1.1.99

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -17,7 +17,6 @@ export interface Card<T> {
17
17
  export declare class CardList<T> {
18
18
  protected id: string;
19
19
  protected title: string;
20
- protected selected: Card<T>;
21
20
  protected hovered: Card<T>;
22
21
  protected cards: Array<Card<T>>;
23
22
  constructor(id: string, title: string);
@@ -25,8 +24,6 @@ export declare class CardList<T> {
25
24
  getId(): string;
26
25
  getTitle(): string;
27
26
  getCards(): Array<Card<T>>;
28
- getSelected(): T;
29
- select(card: Card<T>): void;
30
27
  getHovered(): Card<T>;
31
28
  hover(card: Card<T>): void;
32
29
  add(card: Card<T>): void;
@@ -19,12 +19,6 @@ class CardList {
19
19
  getCards() {
20
20
  return this.cards;
21
21
  }
22
- getSelected() {
23
- return this.selected.getData();
24
- }
25
- select(card) {
26
- this.selected = card;
27
- }
28
22
  getHovered() {
29
23
  return this.hovered;
30
24
  }
@@ -35,15 +29,10 @@ class CardList {
35
29
  this.cards.push(card);
36
30
  }
37
31
  remove(card) {
38
- console.log('~~~ remove', this.cards);
39
32
  const idx = this.cards.findIndex(c => c.equals(card));
40
33
  if (idx !== -1) {
41
- if (this.selected && card.equals(this.selected)) {
42
- this.selected = undefined;
43
- }
44
34
  this.cards.splice(idx, 1);
45
35
  }
46
- console.log('~~~ remove done', this.cards);
47
36
  }
48
37
  }
49
38
  exports.CardList = CardList;
@@ -4,7 +4,7 @@ export interface IListService<T> {
4
4
  close(): void;
5
5
  getLists(): Array<CardList<T>>;
6
6
  }
7
- export type ListEvent = 'started' | 'stopped' | 'loaded' | 'updated' | 'selected';
7
+ export type ListEvent = 'started' | 'stopped' | 'loaded' | 'updated' | 'selected' | 'loading';
8
8
  export declare class ListObserver<T> extends Observer {
9
9
  protected service: IListService<T>;
10
10
  constructor(service: IListService<T>);
@@ -58,8 +58,8 @@ class PromiseObserver extends Observer {
58
58
  }
59
59
  start() {
60
60
  return __awaiter(this, void 0, void 0, function* () {
61
- yield (0, utils_1.waitNextTick)();
62
- return new Promise((resolve, reject) => {
61
+ return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
62
+ yield (0, utils_1.waitNextTick)();
63
63
  this.emitter.emit('started');
64
64
  this.promise
65
65
  .then((res) => {
@@ -67,10 +67,9 @@ class PromiseObserver extends Observer {
67
67
  resolve(res);
68
68
  })
69
69
  .catch(err => {
70
- this.emitter.emit('error', err);
71
70
  reject(err);
72
71
  });
73
- });
72
+ }));
74
73
  });
75
74
  }
76
75
  wait() {
@@ -352,29 +352,35 @@ class DeviceConfigurationService extends events_1.default {
352
352
  });
353
353
  }
354
354
  getDeviceConfigurationInfo() {
355
- const { capabilities = [], devices = [] } = this.settings;
356
- const { adapters } = this;
357
- const info = capabilities.map(c => {
358
- const ci = {
359
- capability: c.capability,
360
- disabled: c.disabled || false,
361
- devices: c.devices.filter(udid => devices.find(d => d.udid === udid)).map(udid => {
362
- const adapter = adapters[udid];
363
- const device = devices.find(d => d.udid === udid);
364
- const mode = device.mode;
365
- const modeSetting = device.modes ? device.modes[mode] : undefined;
366
- const interfaceName = device.settings.interface;
367
- const name = (device === null || device === void 0 ? void 0 : device.displayName) || (adapter === null || adapter === void 0 ? void 0 : adapter.getUniqueName()) || (adapter === null || adapter === void 0 ? void 0 : adapter.getName());
368
- return { udid, name, interface: interfaceName, selected: udid === c.selected, mode, modeSetting };
369
- })
370
- };
371
- return ci;
372
- });
373
355
  const configuration = {};
374
- info.forEach(ci => {
375
- const c = ci.capability.toString();
376
- configuration[c] = ci;
377
- });
356
+ try {
357
+ const { capabilities = [], devices = [] } = this.settings;
358
+ const { adapters } = this;
359
+ const info = capabilities.map(c => {
360
+ var _a;
361
+ const ci = {
362
+ capability: c.capability,
363
+ disabled: c.disabled || false,
364
+ devices: ((_a = c.devices) !== null && _a !== void 0 ? _a : []).filter(udid => devices.find(d => d.udid === udid)).map(udid => {
365
+ const adapter = adapters[udid];
366
+ const device = devices.find(d => d.udid === udid);
367
+ const mode = device.mode;
368
+ const modeSetting = device.modes ? device.modes[mode] : undefined;
369
+ const interfaceName = device.settings.interface;
370
+ const name = (device === null || device === void 0 ? void 0 : device.displayName) || (adapter === null || adapter === void 0 ? void 0 : adapter.getUniqueName()) || (adapter === null || adapter === void 0 ? void 0 : adapter.getName());
371
+ return { udid, name, interface: interfaceName, selected: udid === c.selected, mode, modeSetting };
372
+ })
373
+ };
374
+ return ci;
375
+ });
376
+ info.forEach(ci => {
377
+ const c = ci.capability.toString();
378
+ configuration[c] = ci;
379
+ });
380
+ }
381
+ catch (err) {
382
+ this.logError(err, 'getDeviceConfigurationInfo');
383
+ }
378
384
  return configuration;
379
385
  }
380
386
  emitCapabiltyChanged(capability) {
@@ -31,6 +31,7 @@ export declare class UserSettingsService {
31
31
  updateSettings(data: any): Promise<boolean>;
32
32
  save(): Promise<void>;
33
33
  onAppClose(): Promise<void>;
34
+ protected reset(): void;
34
35
  }
35
36
  export declare const useUserSettings: () => UserSettingsService;
36
37
  export declare const initUserSettings: (binding: IUserSettingsBinding) => UserSettingsService;
@@ -194,6 +194,9 @@ class UserSettingsService {
194
194
  this.binding.save(this.settings, true);
195
195
  });
196
196
  }
197
+ reset() {
198
+ delete UserSettingsService._instance;
199
+ }
197
200
  }
198
201
  exports.UserSettingsService = UserSettingsService;
199
202
  const useUserSettings = () => UserSettingsService.getInstance();
@@ -1,6 +1,5 @@
1
1
  import { FileInfo } from "../../../api";
2
2
  import { Card } from "../../../base/cardlist";
3
- import { ImportFilter } from "../../../base/cardlist/types";
4
3
  import { PromiseObserver } from "../../../base/types/observer";
5
4
  import { AppStatus } from "../../../routes/base/types";
6
5
  import { WP } from "../types";
@@ -13,16 +12,16 @@ export declare class ActiveImportCard extends BaseCard implements Card<WP> {
13
12
  protected deleteObserver: PromiseObserver<void>;
14
13
  constructor(file: FileInfo);
15
14
  setVisible(): void;
15
+ canDelete(): boolean;
16
16
  canStart(status: AppStatus): boolean;
17
17
  delete(): PromiseObserver<boolean>;
18
18
  setError(error: Error): void;
19
- setData(): void;
20
- emitUpdate(): void;
21
19
  getData(): any;
22
20
  getCardType(): WorkoutCardType;
23
21
  getId(): string;
24
- getFilters(): Array<ImportFilter>;
25
22
  getTitle(): string;
26
23
  getDisplayProperties(): ActiveImportProps;
27
24
  retry(): void;
25
+ protected emitUpdate(): void;
26
+ protected getWorkoutList(): import("../service").WorkoutListService;
28
27
  }
@@ -15,14 +15,18 @@ class ActiveImportCard extends base_1.BaseCard {
15
15
  setVisible() {
16
16
  this.visible = true;
17
17
  }
18
+ canDelete() {
19
+ return true;
20
+ }
18
21
  canStart(status) {
19
22
  return false;
20
23
  }
21
24
  delete() {
22
- const list = (0, service_1.useWorkoutList)().getLists().find(l => l.getId() === 'myRoutes');
25
+ const listService = this.getWorkoutList();
26
+ const list = listService.getLists().find(l => l.getId() === 'myWorkouts');
23
27
  if (list) {
24
28
  list.remove(this);
25
- (0, service_1.useWorkoutList)().emitLists('updated');
29
+ listService.emitLists('updated');
26
30
  }
27
31
  return observer_1.PromiseObserver.alwaysReturning(true);
28
32
  }
@@ -30,11 +34,6 @@ class ActiveImportCard extends base_1.BaseCard {
30
34
  this.error = error;
31
35
  this.emitUpdate();
32
36
  }
33
- setData() {
34
- }
35
- emitUpdate() {
36
- this.cardObserver.emit('update', this.getDisplayProperties());
37
- }
38
37
  getData() {
39
38
  return undefined;
40
39
  }
@@ -45,9 +44,6 @@ class ActiveImportCard extends base_1.BaseCard {
45
44
  const { type, url, filename, name } = this.file || {};
46
45
  return type === 'file' ? filename || name : url;
47
46
  }
48
- getFilters() {
49
- return types_1.DEFAULT_FILTERS;
50
- }
51
47
  getTitle() {
52
48
  return types_1.DEFAULT_TITLE;
53
49
  }
@@ -60,7 +56,13 @@ class ActiveImportCard extends base_1.BaseCard {
60
56
  retry() {
61
57
  this.error = null;
62
58
  this.emitUpdate();
63
- (0, service_1.useWorkoutList)().import(this.file, this);
59
+ this.getWorkoutList().import(this.file, { card: this });
60
+ }
61
+ emitUpdate() {
62
+ this.cardObserver.emit('update', this.getDisplayProperties());
63
+ }
64
+ getWorkoutList() {
65
+ return (0, service_1.useWorkoutList)();
64
66
  }
65
67
  }
66
68
  exports.ActiveImportCard = ActiveImportCard;
@@ -3,7 +3,7 @@ import { Card, CardList } from "../../../base/cardlist";
3
3
  import { Observer, PromiseObserver } from "../../../base/types/observer";
4
4
  import { Workout } from "../../base/model/Workout";
5
5
  import { BaseCard } from "./base";
6
- import { WorkoutCardType, WorkoutSettings } from "./types";
6
+ import { WorkoutCardDisplayProperties, WorkoutCardType, WorkoutSettings, WorkoutSettingsDisplayProps } from "./types";
7
7
  import { WorkoutsDbLoader } from "../loaders/db";
8
8
  export declare class WorkoutCard extends BaseCard implements Card<Workout> {
9
9
  protected workout: Workout;
@@ -15,39 +15,22 @@ export declare class WorkoutCard extends BaseCard implements Card<Workout> {
15
15
  constructor(workout: Workout, props?: {
16
16
  list?: CardList<Workout>;
17
17
  });
18
- openSettings(): {
19
- settings: any;
20
- ftpRequired: any;
21
- canStart: any;
22
- duration: any;
23
- categories: any[];
24
- category: any;
25
- };
18
+ openSettings(): WorkoutSettingsDisplayProps;
26
19
  select(settings?: WorkoutSettings): void;
27
20
  unselect(): void;
28
21
  move(targetListName: string): void;
29
22
  save(): Promise<void>;
30
- setList(list: CardList<Workout>): void;
31
23
  delete(): PromiseObserver<boolean>;
32
- protected _delete(): Promise<boolean>;
33
24
  getId(): string;
25
+ getTitle(): string;
34
26
  update(workout: Workout): void;
35
27
  getData(): Workout;
36
- setData(data: Workout): void;
37
28
  getCardType(): WorkoutCardType;
38
- getDisplayProperties(): {
39
- title: string;
40
- workout: Workout;
41
- ftp: any;
42
- duration: string;
43
- canDelete: boolean;
44
- visible: boolean;
45
- selected: boolean;
46
- observer: Observer;
47
- };
29
+ getDisplayProperties(): WorkoutCardDisplayProperties;
48
30
  enableDelete(enabled?: boolean): void;
49
31
  canDelete(): boolean;
50
32
  setVisible(visible: boolean): void;
33
+ protected _delete(): Promise<boolean>;
51
34
  protected calculateDuration(): string;
52
35
  protected deleteFromUIList(): void;
53
36
  protected logError(err: Error, fn: string): void;
@@ -25,12 +25,12 @@ class WorkoutCard extends base_1.BaseCard {
25
25
  const { list } = props || {};
26
26
  this.workout = workout;
27
27
  this.list = list;
28
- this.deleteable = false;
28
+ this.deleteable = true;
29
29
  this.initialized = false;
30
30
  this.logger = new gd_eventlog_1.EventLogger('WorkoutCard');
31
31
  }
32
32
  openSettings() {
33
- var _a;
33
+ var _a, _b;
34
34
  let settings, canStart, duration, ftpRequired, categories = [], category;
35
35
  try {
36
36
  const service = (0, service_1.useWorkoutList)();
@@ -41,8 +41,8 @@ class WorkoutCard extends base_1.BaseCard {
41
41
  return (s.type == 'step' && ((_a = s.power) === null || _a === void 0 ? void 0 : _a.type) === 'pct of FTP') ||
42
42
  (s.type == 'segment' && (s === null || s === void 0 ? void 0 : s.steps.find(s => { var _a; return (s.type == 'step' && ((_a = s.power) === null || _a === void 0 ? void 0 : _a.type) === 'pct of FTP'); })));
43
43
  }));
44
- settings = service.getStartSettings();
45
- categories = ((_a = service.getLists()) !== null && _a !== void 0 ? _a : []).map(l => l.getTitle());
44
+ settings = (_a = service.getStartSettings()) !== null && _a !== void 0 ? _a : {};
45
+ categories = ((_b = service.getLists()) !== null && _b !== void 0 ? _b : []).map(l => l.getTitle());
46
46
  category = this.list.getTitle();
47
47
  }
48
48
  catch (err) {
@@ -67,8 +67,9 @@ class WorkoutCard extends base_1.BaseCard {
67
67
  return;
68
68
  const service = (0, service_1.getWorkoutList)();
69
69
  const newList = service.moveCard(this, this.list, targetListName);
70
- if (newList)
70
+ if (newList) {
71
71
  this.list = newList;
72
+ }
72
73
  this.workout.category = { name: targetListName, index: newList === null || newList === void 0 ? void 0 : newList.length };
73
74
  this.save();
74
75
  }
@@ -82,9 +83,6 @@ class WorkoutCard extends base_1.BaseCard {
82
83
  }
83
84
  });
84
85
  }
85
- setList(list) {
86
- this.list = list;
87
- }
88
86
  delete() {
89
87
  try {
90
88
  const service = (0, service_1.getWorkoutList)();
@@ -98,39 +96,15 @@ class WorkoutCard extends base_1.BaseCard {
98
96
  this.logError(err, 'delete');
99
97
  }
100
98
  }
101
- _delete() {
102
- return __awaiter(this, void 0, void 0, function* () {
103
- yield (0, utils_1.waitNextTick)();
104
- let deleted = false;
105
- try {
106
- this.deleteObserver.emit('started');
107
- this.emitUpdate();
108
- if (this.list.getId() === 'myWorkouts') {
109
- yield this.getRepo().delete(this.workout);
110
- }
111
- else {
112
- }
113
- this.deleteFromUIList();
114
- (0, service_1.getWorkoutList)().emitLists('updated');
115
- deleted = true;
116
- }
117
- catch (err) {
118
- deleted = false;
119
- }
120
- finally {
121
- this.deleteObserver.emit('done', deleted);
122
- (0, utils_1.waitNextTick)().then(() => {
123
- delete this.deleteObserver;
124
- this.emitUpdate();
125
- });
126
- }
127
- return deleted;
128
- });
129
- }
130
99
  getId() {
131
100
  return this.workout.id;
132
101
  }
102
+ getTitle() {
103
+ return this.workout.name;
104
+ }
133
105
  update(workout) {
106
+ if (!(0, valid_1.valid)(workout))
107
+ return;
134
108
  try {
135
109
  this.workout = workout;
136
110
  this.save();
@@ -143,20 +117,18 @@ class WorkoutCard extends base_1.BaseCard {
143
117
  getData() {
144
118
  return this.workout;
145
119
  }
146
- setData(data) {
147
- try {
148
- this.workout = data;
149
- }
150
- catch (err) {
151
- this.logError(err, 'setData');
152
- }
153
- }
154
120
  getCardType() {
155
121
  return "Workout";
156
122
  }
157
123
  getDisplayProperties() {
158
124
  const userSettings = (0, settings_1.useUserSettings)();
159
- const user = userSettings.get('user', {});
125
+ let user;
126
+ try {
127
+ user = userSettings.get('user', {});
128
+ }
129
+ catch (_a) {
130
+ user = {};
131
+ }
160
132
  const duration = this.calculateDuration();
161
133
  return { title: this.workout.name, workout: this.workout, ftp: user === null || user === void 0 ? void 0 : user.ftp, duration,
162
134
  canDelete: this.deleteable, visible: this.visible, selected: this.isSelected(),
@@ -179,6 +151,29 @@ class WorkoutCard extends base_1.BaseCard {
179
151
  this.logError(err, 'setVisible');
180
152
  }
181
153
  }
154
+ _delete() {
155
+ return __awaiter(this, void 0, void 0, function* () {
156
+ yield (0, utils_1.waitNextTick)();
157
+ let deleted = false;
158
+ try {
159
+ this.deleteObserver.emit('started');
160
+ yield this.getRepo().delete(this.workout);
161
+ this.deleteFromUIList();
162
+ (0, service_1.getWorkoutList)().emitLists('updated');
163
+ deleted = true;
164
+ }
165
+ catch (err) {
166
+ deleted = false;
167
+ }
168
+ finally {
169
+ this.deleteObserver.emit('done', deleted);
170
+ (0, utils_1.waitNextTick)().then(() => {
171
+ delete this.deleteObserver;
172
+ });
173
+ }
174
+ return deleted;
175
+ });
176
+ }
182
177
  calculateDuration() {
183
178
  const { duration } = this.workout;
184
179
  if (duration < 120)
@@ -215,6 +210,7 @@ class WorkoutCard extends base_1.BaseCard {
215
210
  const selectedWorkout = service.getSelected();
216
211
  if ((selectedWorkout === null || selectedWorkout === void 0 ? void 0 : selectedWorkout.id) === this.workout.id)
217
212
  return true;
213
+ return false;
218
214
  }
219
215
  }
220
216
  exports.WorkoutCard = WorkoutCard;
@@ -7,9 +7,9 @@ import { BaseCard } from "./base";
7
7
  import { WorkoutCardType, WorkoutImportProps } from "./types";
8
8
  export declare class WorkoutImportCard extends BaseCard implements Card<Workout> {
9
9
  setVisible(): void;
10
+ canDelete(): boolean;
10
11
  canStart(status: AppStatus): boolean;
11
12
  delete(): PromiseObserver<boolean>;
12
- setData(): void;
13
13
  getData(): any;
14
14
  getCardType(): WorkoutCardType;
15
15
  getId(): string;
@@ -8,14 +8,15 @@ class WorkoutImportCard extends base_1.BaseCard {
8
8
  setVisible() {
9
9
  this.visible = true;
10
10
  }
11
+ canDelete() {
12
+ return false;
13
+ }
11
14
  canStart(status) {
12
15
  return false;
13
16
  }
14
17
  delete() {
15
18
  return observer_1.PromiseObserver.alwaysReturning(false);
16
19
  }
17
- setData() {
18
- }
19
20
  getData() {
20
21
  return undefined;
21
22
  }
@@ -0,0 +1,3 @@
1
+ export * from './WorkoutCard';
2
+ export * from './WorkoutImportCard';
3
+ export * from './types';
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./WorkoutCard"), exports);
18
+ __exportStar(require("./WorkoutImportCard"), exports);
19
+ __exportStar(require("./types"), exports);
@@ -1,5 +1,6 @@
1
1
  import { ImportFilter } from "../../../base/cardlist/types";
2
2
  import { Observer } from "../../../base/types/observer";
3
+ import { Workout } from "../../base/model";
3
4
  export type WorkoutCardType = 'WorkoutImport' | 'Workout' | 'ActiveWorkoutImport';
4
5
  export declare const DEFAULT_TITLE = "Import Workout";
5
6
  export declare const DEFAULT_FILTERS: {
@@ -21,3 +22,21 @@ export interface WorkoutSettings {
21
22
  ftp?: number;
22
23
  useErgMode?: boolean;
23
24
  }
25
+ export interface WorkoutSettingsDisplayProps {
26
+ settings: WorkoutSettings;
27
+ ftpRequired: boolean;
28
+ canStart: boolean;
29
+ duration: string;
30
+ categories: string[];
31
+ category: string;
32
+ }
33
+ export interface WorkoutCardDisplayProperties {
34
+ title: string;
35
+ workout: Workout;
36
+ ftp: number;
37
+ duration: string;
38
+ canDelete: boolean;
39
+ visible: boolean;
40
+ selected: boolean;
41
+ observer: Observer;
42
+ }
@@ -1 +1,2 @@
1
1
  export * from './service';
2
+ export * from './cards';
@@ -15,3 +15,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./service"), exports);
18
+ __exportStar(require("./cards"), exports);
@@ -5,9 +5,10 @@ import { IListService, ListObserver } from "../../base/types";
5
5
  import { PromiseObserver } from "../../base/types/observer";
6
6
  import { Plan, Workout } from "../base/model/Workout";
7
7
  import { ActiveImportCard } from "./cards/ActiveImportCard";
8
+ import { WorkoutCard } from "./cards/WorkoutCard";
8
9
  import { WorkoutSettings } from "./cards/types";
9
10
  import { WorkoutsDbLoader } from "./loaders/db";
10
- import { WP } from "./types";
11
+ import { WP, WorkoutSettingsDisplayProps } from "./types";
11
12
  export declare class WorkoutListService extends IncyclistService implements IListService<Workout | Plan> {
12
13
  protected myWorkouts: CardList<WP>;
13
14
  protected lists: Array<CardList<WP>>;
@@ -19,30 +20,27 @@ export declare class WorkoutListService extends IncyclistService implements ILis
19
20
  protected screenProps: any;
20
21
  protected language: string;
21
22
  protected selectedWorkout: Workout;
22
- protected startSettings: WorkoutSettings;
23
+ protected ftp: number;
23
24
  constructor();
24
- setLanguage(language: string): void;
25
- getLanguage(): string;
26
25
  getSelected(): Workout;
27
26
  setScreenProps(props: any): void;
28
27
  getScreenProps(): any;
29
- getStartSettings(): WorkoutSettings;
30
- setStartSettings(settings: WorkoutSettings): void;
31
28
  open(): {
32
29
  observer: ListObserver<WP>;
33
30
  lists: Array<CardList<WP>>;
34
31
  };
35
- openSettings(): {
36
- observer: ListObserver<WP>;
37
- workouts: CardList<Workout>;
38
- };
39
32
  close(): void;
33
+ openSettings(): WorkoutSettingsDisplayProps;
34
+ getStartSettings(): WorkoutSettings;
35
+ setStartSettings(settings: WorkoutSettings): void;
40
36
  onResize(): void;
41
37
  onCarouselInitialized(list: CardList<WP>, item: any, itemsInSlide: any): void;
42
38
  onCarouselUpdated(list: any, item: any, itemsInSlide: any): void;
43
39
  preload(): PromiseObserver<void>;
44
- import(info: FileInfo | Array<FileInfo>, retry?: ActiveImportCard): void;
45
- importSingle(info: FileInfo): Promise<Workout>;
40
+ import(info: FileInfo | Array<FileInfo>, props: {
41
+ card?: ActiveImportCard;
42
+ showImportCards?: boolean;
43
+ }): Promise<void>;
46
44
  addList(name: string): CardList<WP>;
47
45
  getLists(forUi?: boolean): Array<CardList<WP>>;
48
46
  emitLists(event: 'loaded' | 'updated'): void;
@@ -52,10 +50,11 @@ export declare class WorkoutListService extends IncyclistService implements ILis
52
50
  unselectCard(card: Card<WP>): void;
53
51
  moveCard(card: Card<WP>, source: CardList<WP>, target: string | CardList<WP>): CardList<WP>;
54
52
  canDisplayStart(): boolean;
53
+ protected createSettingsList(): CardList<WP>;
55
54
  protected resetLists(): void;
56
55
  protected resetCards(): void;
57
56
  protected loadWorkouts(): Promise<void>;
58
- protected addItem(item: WP): void;
57
+ protected addItem(item: WP): Card<WP>;
59
58
  protected updateItem(item: WP): Promise<void>;
60
59
  protected selectList(item: WP): CardList<WP>;
61
60
  protected findCard(target: WP | string): {
@@ -63,9 +62,18 @@ export declare class WorkoutListService extends IncyclistService implements ILis
63
62
  list: CardList<WP>;
64
63
  };
65
64
  protected addImportCard(file: FileInfo): ActiveImportCard;
66
- protected initStartSettings(): void;
67
- protected updateStartSettings(): void;
68
- createSettingsList(): CardList<WP>;
65
+ protected _import(info: FileInfo): Promise<WorkoutCard>;
66
+ protected parse(info: FileInfo): Promise<Workout>;
67
+ protected addImportCards(retry: ActiveImportCard, files: FileInfo[]): ActiveImportCard[];
68
+ protected registerUserChangeHandler(): void;
69
+ protected onUserUpdate(update: any): void;
70
+ protected emitStartEvent(): void;
71
+ protected emitLoadingEvent(): void;
72
+ protected emitLoadedEvent(): void;
73
+ protected getUserSettings(): import("../../settings").UserSettingsService;
74
+ protected getRouteList(): import("../../routes").RouteListService;
75
+ protected getInitTimeout(): number;
76
+ protected getRepo(): WorkoutsDbLoader;
69
77
  }
70
78
  export declare const useWorkoutList: () => WorkoutListService;
71
79
  export declare const getWorkoutList: () => WorkoutListService;
@@ -54,6 +54,7 @@ const types_1 = require("../../base/types");
54
54
  const observer_1 = require("../../base/types/observer");
55
55
  const routes_1 = require("../../routes");
56
56
  const settings_1 = require("../../settings");
57
+ const utils_1 = require("../../utils");
57
58
  const valid_1 = require("../../utils/valid");
58
59
  const parsers_1 = require("../base/parsers");
59
60
  const ActiveImportCard_1 = require("./cards/ActiveImportCard");
@@ -72,85 +73,88 @@ let WorkoutListService = (() => {
72
73
  this.initialized = false;
73
74
  this.myWorkouts = new cardlist_1.CardList('myWorkouts', 'My Workouts');
74
75
  this.lists = [this.myWorkouts];
75
- this.db = new db_1.WorkoutsDbLoader();
76
76
  this.items = [];
77
+ this.language = 'en';
77
78
  this.myWorkouts.add(new WorkoutImportCard_1.WorkoutImportCard());
79
+ this.registerUserChangeHandler();
78
80
  }
79
- setLanguage(language) { this.language = language; }
80
- getLanguage() { return this.language; }
81
81
  getSelected() { return this.selectedWorkout; }
82
82
  setScreenProps(props) { this.screenProps = props; }
83
83
  getScreenProps() { return this.screenProps; }
84
- getStartSettings() {
85
- if (!this.startSettings) {
86
- this.initStartSettings();
87
- }
88
- return this.startSettings;
89
- }
90
- setStartSettings(settings) {
91
- this.startSettings = settings;
92
- this.updateStartSettings();
93
- }
94
84
  open() {
95
- var _a;
85
+ let lists = null;
96
86
  try {
97
87
  this.logEvent({ message: 'open workout list' });
98
- const hasLists = ((_a = this.getLists()) === null || _a === void 0 ? void 0 : _a.length) > 0;
99
- const emitStartEvent = () => __awaiter(this, void 0, void 0, function* () {
100
- process.nextTick(() => {
101
- var _a;
102
- (_a = this.observer) === null || _a === void 0 ? void 0 : _a.emit('started');
103
- });
104
- });
105
- const emitLoadedEvent = () => __awaiter(this, void 0, void 0, function* () {
106
- process.nextTick(() => {
107
- this.emitLists('loaded');
108
- });
109
- });
88
+ lists = this.getLists();
110
89
  this.resetLists();
111
90
  if (!this.observer) {
112
91
  this.observer = new types_1.ListObserver(this);
113
- emitStartEvent();
92
+ this.emitStartEvent();
114
93
  }
115
- if (!this.initialized && !this.preloadObserver) {
116
- this.preload();
94
+ if (!this.initialized) {
95
+ if (!this.preloadObserver)
96
+ this.preload();
97
+ return { observer: this.observer, lists: null };
117
98
  }
118
- if (this.initialized && !hasLists)
119
- emitLoadedEvent();
120
- else
99
+ (0, utils_1.waitNextTick)().then(() => {
121
100
  this.emitLists('updated');
101
+ });
122
102
  }
123
103
  catch (err) {
124
104
  this.logError(err, 'open');
125
105
  }
126
- return { observer: this.observer, lists: this.getLists() };
106
+ return { observer: this.observer, lists };
107
+ }
108
+ close() {
127
109
  }
128
110
  openSettings() {
129
111
  let workouts;
112
+ let settings;
113
+ let selected;
130
114
  try {
131
115
  this.logEvent({ message: 'open workout settings' });
132
- const emitStartEvent = () => __awaiter(this, void 0, void 0, function* () {
133
- process.nextTick(() => {
134
- var _a;
135
- (_a = this.observer) === null || _a === void 0 ? void 0 : _a.emit('started');
136
- });
137
- });
138
116
  if (!this.observer) {
139
117
  this.observer = new types_1.ListObserver(this);
140
- emitStartEvent();
118
+ this.emitStartEvent();
141
119
  }
142
- if (!this.initialized && !this.preloadObserver) {
143
- this.preload();
120
+ if (!this.initialized) {
121
+ if (!this.preloadObserver)
122
+ this.preload();
123
+ return { observer: this.observer, workouts: null };
144
124
  }
145
125
  workouts = this.createSettingsList();
126
+ selected = this.getSelected();
127
+ settings = this.getStartSettings();
146
128
  }
147
129
  catch (err) {
148
130
  this.logError(err, 'openSettings');
149
131
  }
150
- console.log('~~~ returning', { observer: this.observer, workouts });
151
- return { observer: this.observer, workouts };
132
+ return { observer: this.observer, workouts, selected, settings };
152
133
  }
153
- close() {
134
+ getStartSettings() {
135
+ var _a, _b, _c;
136
+ let ftp = (_a = this.ftp) !== null && _a !== void 0 ? _a : 200;
137
+ let useErgMode = true;
138
+ try {
139
+ const userSettings = this.getUserSettings();
140
+ useErgMode = userSettings.get('preferences.useErgMode', true);
141
+ const user = userSettings.get('user', undefined);
142
+ ftp = (_c = (_b = this.ftp) !== null && _b !== void 0 ? _b : user === null || user === void 0 ? void 0 : user.ftp) !== null && _c !== void 0 ? _c : 200;
143
+ }
144
+ catch (err) {
145
+ this.logError(err, 'getStartSettings');
146
+ }
147
+ return { ftp, useErgMode };
148
+ }
149
+ setStartSettings(settings) {
150
+ try {
151
+ const userSettings = this.getUserSettings();
152
+ userSettings.set('preferences.useErgMode', settings === null || settings === void 0 ? void 0 : settings.useErgMode);
153
+ this.ftp = settings === null || settings === void 0 ? void 0 : settings.ftp;
154
+ }
155
+ catch (err) {
156
+ this.logError(err, 'setStartSettings');
157
+ }
154
158
  }
155
159
  onResize() {
156
160
  try {
@@ -161,22 +165,28 @@ let WorkoutListService = (() => {
161
165
  }
162
166
  }
163
167
  onCarouselInitialized(list, item, itemsInSlide) {
168
+ var _a;
169
+ if (list === undefined || list === null)
170
+ return;
164
171
  try {
165
- list.getCards().forEach((card, idx) => {
172
+ const cards = (_a = list.getCards()) !== null && _a !== void 0 ? _a : [];
173
+ cards.forEach((card, idx) => {
166
174
  card.setInitialized(true);
167
175
  if (idx < item + itemsInSlide + 2) {
168
176
  card.setVisible(true);
169
177
  }
170
178
  });
171
- setTimeout(() => { this.onCarouselUpdated(list, item, itemsInSlide); }, 1000);
179
+ setTimeout(() => { this.onCarouselUpdated(list, item, itemsInSlide); }, this.getInitTimeout());
172
180
  }
173
181
  catch (err) {
174
182
  this.logError(err, 'onCarouselInitialized');
175
183
  }
176
184
  }
177
185
  onCarouselUpdated(list, item, itemsInSlide) {
186
+ var _a;
178
187
  try {
179
- list.getCards().forEach((card, idx) => {
188
+ const cards = (_a = list.getCards()) !== null && _a !== void 0 ? _a : [];
189
+ cards.forEach((card, idx) => {
180
190
  if (idx >= item && idx < item + itemsInSlide + 10 && !card.isVisible()) {
181
191
  card.setVisible(true);
182
192
  }
@@ -189,6 +199,7 @@ let WorkoutListService = (() => {
189
199
  preload() {
190
200
  try {
191
201
  this.logEvent({ message: 'preload workout list' });
202
+ this.emitLoadingEvent();
192
203
  if (!this.preloadObserver) {
193
204
  const promise = this.loadWorkouts();
194
205
  this.preloadObserver = new observer_1.PromiseObserver(promise);
@@ -204,6 +215,7 @@ let WorkoutListService = (() => {
204
215
  })
205
216
  .catch((err) => {
206
217
  this.logError(err, 'preload');
218
+ process.nextTick(() => { delete this.preloadObserver; });
207
219
  });
208
220
  }
209
221
  }
@@ -212,67 +224,43 @@ let WorkoutListService = (() => {
212
224
  }
213
225
  return this.preloadObserver;
214
226
  }
215
- import(info, retry) {
216
- try {
217
- const files = Array.isArray(info) ? info : [info];
218
- const importCards = [];
219
- if (!retry) {
220
- files.forEach((file) => {
227
+ import(info, props) {
228
+ return __awaiter(this, void 0, void 0, function* () {
229
+ try {
230
+ const { card, showImportCards = true } = props !== null && props !== void 0 ? props : {};
231
+ const files = Array.isArray(info) ? info : [info];
232
+ const importCards = showImportCards ? this.addImportCards(card, files) : [];
233
+ const doImport = (file, idx) => __awaiter(this, void 0, void 0, function* () {
221
234
  if (!file)
222
235
  return;
223
- const card = this.addImportCard(file);
224
- importCards.push(card);
225
- this.emitLists('updated');
226
- });
227
- }
228
- else {
229
- importCards.push(retry);
230
- }
231
- files.forEach((file, idx) => __awaiter(this, void 0, void 0, function* () {
232
- if (!file)
233
- return;
234
- const importCard = importCards[idx];
235
- try {
236
- const workout = yield parsers_1.WorkoutParser.parse(file);
237
- const existing = this.findCard(workout);
238
- if (existing) {
239
- existing.list.remove(existing.card);
236
+ const importCard = showImportCards ? importCards[idx] : null;
237
+ try {
238
+ yield this._import(file);
239
+ this.myWorkouts.remove(importCard);
240
+ this.emitLists('updated');
240
241
  }
241
- else {
242
- this.items.push(workout);
242
+ catch (err) {
243
+ if (importCard)
244
+ importCard.setError(err);
245
+ else
246
+ throw err;
243
247
  }
244
- const card = new WorkoutCard_1.WorkoutCard(workout, { list: this.myWorkouts });
245
- card.save();
246
- card.enableDelete();
247
- this.myWorkouts.add(card);
248
- this.myWorkouts.remove(importCard);
249
- card.enableDelete(true);
250
- this.emitLists('updated');
251
- }
252
- catch (err) {
253
- importCard.setError(err);
248
+ });
249
+ const promises = [];
250
+ files.forEach((file, idx) => __awaiter(this, void 0, void 0, function* () {
251
+ promises.push(doImport(file, idx));
252
+ }));
253
+ const res = yield Promise.allSettled(promises);
254
+ const rejected = res.filter(pr => pr.status === 'rejected');
255
+ if (rejected) {
256
+ throw (rejected[0].reason);
254
257
  }
255
- }));
256
- }
257
- catch (err) {
258
- this.logError(err, 'import', info);
259
- }
260
- }
261
- importSingle(info) {
262
- return __awaiter(this, void 0, void 0, function* () {
263
- const workout = yield parsers_1.WorkoutParser.parse(info);
264
- const existing = this.findCard(workout);
265
- if (existing) {
266
- existing.list.remove(existing.card);
267
258
  }
268
- else {
269
- this.items.push(workout);
259
+ catch (err) {
260
+ if ((props === null || props === void 0 ? void 0 : props.showImportCards) === false)
261
+ throw err;
262
+ this.logError(err, 'import', info);
270
263
  }
271
- const card = new WorkoutCard_1.WorkoutCard(workout, { list: this.myWorkouts });
272
- card.save();
273
- card.enableDelete(true);
274
- this.myWorkouts.add(card);
275
- return workout;
276
264
  });
277
265
  }
278
266
  addList(name) {
@@ -328,7 +316,6 @@ let WorkoutListService = (() => {
328
316
  }
329
317
  moveCard(card, source, target) {
330
318
  try {
331
- source.remove(card);
332
319
  let targetList;
333
320
  if (typeof target === 'string') {
334
321
  targetList = this.lists.find(l => l.getTitle() === target);
@@ -338,6 +325,7 @@ let WorkoutListService = (() => {
338
325
  else {
339
326
  targetList = target;
340
327
  }
328
+ source.remove(card);
341
329
  targetList.add(card);
342
330
  this.emitLists('updated');
343
331
  return targetList;
@@ -347,9 +335,22 @@ let WorkoutListService = (() => {
347
335
  }
348
336
  }
349
337
  canDisplayStart() {
350
- const routes = (0, routes_1.getRouteList)();
338
+ const routes = this.getRouteList();
351
339
  return (0, valid_1.valid)(routes.getSelected());
352
340
  }
341
+ createSettingsList() {
342
+ const list = new cardlist_1.CardList('settings', 'Workouts');
343
+ list.add(new WorkoutImportCard_1.WorkoutImportCard());
344
+ const sorted = this.items
345
+ .sort((a, b) => a.name > b.name ? 1 : -1);
346
+ sorted.forEach(i => {
347
+ if (i.type === 'workout') {
348
+ const card = new WorkoutCard_1.WorkoutCard(i, { list });
349
+ list.add(card);
350
+ }
351
+ });
352
+ return list;
353
+ }
353
354
  resetLists() {
354
355
  var _a;
355
356
  (_a = this.getLists(false)) === null || _a === void 0 ? void 0 : _a.forEach(list => {
@@ -362,8 +363,12 @@ let WorkoutListService = (() => {
362
363
  });
363
364
  }
364
365
  resetCards() {
365
- this.getLists().forEach(list => {
366
- list.getCards().forEach((card) => {
366
+ var _a;
367
+ const lists = (_a = this.getLists(false)) !== null && _a !== void 0 ? _a : [];
368
+ lists.forEach(list => {
369
+ var _a;
370
+ const cards = (_a = list.getCards()) !== null && _a !== void 0 ? _a : [];
371
+ cards.forEach((card) => {
367
372
  card.reset();
368
373
  });
369
374
  });
@@ -372,7 +377,7 @@ let WorkoutListService = (() => {
372
377
  return __awaiter(this, void 0, void 0, function* () {
373
378
  return new Promise((done, reject) => {
374
379
  try {
375
- const observer = this.db.load();
380
+ const observer = this.getRepo().load();
376
381
  const add = this.addItem.bind(this);
377
382
  const update = this.updateItem.bind(this);
378
383
  observer.on('workout.added', add);
@@ -381,7 +386,6 @@ let WorkoutListService = (() => {
381
386
  }
382
387
  catch (err) {
383
388
  reject(err);
384
- console.log('~~~ ERROR', err);
385
389
  }
386
390
  });
387
391
  });
@@ -398,6 +402,7 @@ let WorkoutListService = (() => {
398
402
  card.enableDelete(true);
399
403
  this.items.push(item);
400
404
  this.emitLists('updated');
405
+ return card;
401
406
  }
402
407
  updateItem(item) {
403
408
  return __awaiter(this, void 0, void 0, function* () {
@@ -464,30 +469,87 @@ let WorkoutListService = (() => {
464
469
  this.emitLists('updated');
465
470
  return card;
466
471
  }
467
- initStartSettings() {
468
- var _a;
469
- const userSettings = (0, settings_1.useUserSettings)();
470
- const useErgMode = userSettings.get('preferences.useErgMode', true);
471
- const ftp = (_a = userSettings.get('user', undefined)) === null || _a === void 0 ? void 0 : _a.ftp;
472
- this.startSettings = { ftp, useErgMode };
472
+ _import(info) {
473
+ return __awaiter(this, void 0, void 0, function* () {
474
+ const workout = yield this.parse(info);
475
+ const existing = this.findCard(workout);
476
+ if (existing) {
477
+ existing.list.remove(existing.card);
478
+ }
479
+ else {
480
+ this.items.push(workout);
481
+ }
482
+ const card = new WorkoutCard_1.WorkoutCard(workout, { list: this.myWorkouts });
483
+ card.save();
484
+ card.enableDelete(true);
485
+ this.myWorkouts.add(card);
486
+ return card;
487
+ });
473
488
  }
474
- updateStartSettings() {
475
- var _a;
476
- const userSettings = (0, settings_1.useUserSettings)();
477
- userSettings.set('preferences.useErgMode', (_a = this.startSettings) === null || _a === void 0 ? void 0 : _a.useErgMode);
489
+ parse(info) {
490
+ return __awaiter(this, void 0, void 0, function* () {
491
+ return yield parsers_1.WorkoutParser.parse(info);
492
+ });
478
493
  }
479
- createSettingsList() {
480
- const list = new cardlist_1.CardList('settings', 'Workouts');
481
- list.add(new WorkoutImportCard_1.WorkoutImportCard());
482
- const sorted = this.items
483
- .sort((a, b) => a.name > b.name ? 1 : -1);
484
- sorted.forEach(i => {
485
- if (i.type === 'workout') {
486
- const card = new WorkoutCard_1.WorkoutCard(i, { list });
487
- list.add(card);
488
- }
494
+ addImportCards(retry, files) {
495
+ const importCards = [];
496
+ if (!retry) {
497
+ files.forEach((file) => {
498
+ if (!file)
499
+ return;
500
+ const card = this.addImportCard(file);
501
+ importCards.push(card);
502
+ this.emitLists('updated');
503
+ });
504
+ }
505
+ else {
506
+ importCards.push(retry);
507
+ }
508
+ return importCards;
509
+ }
510
+ registerUserChangeHandler() {
511
+ const userSettings = this.getUserSettings();
512
+ if (userSettings) {
513
+ const observer = userSettings.requestNotifyOnChange('workouts', 'user');
514
+ observer === null || observer === void 0 ? void 0 : observer.on('changed', (update) => {
515
+ this.onUserUpdate(update);
516
+ });
517
+ }
518
+ }
519
+ onUserUpdate(update) {
520
+ if (this.ftp !== (update === null || update === void 0 ? void 0 : update.ftp) && (0, valid_1.valid)(update === null || update === void 0 ? void 0 : update.ftp))
521
+ this.ftp = update.ftp;
522
+ }
523
+ emitStartEvent() {
524
+ process.nextTick(() => {
525
+ var _a;
526
+ (_a = this.observer) === null || _a === void 0 ? void 0 : _a.emit('started');
489
527
  });
490
- return list;
528
+ }
529
+ emitLoadingEvent() {
530
+ process.nextTick(() => {
531
+ var _a;
532
+ (_a = this.observer) === null || _a === void 0 ? void 0 : _a.emit('loading');
533
+ });
534
+ }
535
+ emitLoadedEvent() {
536
+ process.nextTick(() => {
537
+ this.emitLists('loaded');
538
+ });
539
+ }
540
+ getUserSettings() {
541
+ return (0, settings_1.useUserSettings)();
542
+ }
543
+ getRouteList() {
544
+ return (0, routes_1.useRouteList)();
545
+ }
546
+ getInitTimeout() {
547
+ return 1000;
548
+ }
549
+ getRepo() {
550
+ if (!this.db)
551
+ this.db = new db_1.WorkoutsDbLoader();
552
+ return this.db;
491
553
  }
492
554
  };
493
555
  __setFunctionName(_classThis, "WorkoutListService");
@@ -1,2 +1,11 @@
1
+ import { CardList } from "../../base/cardlist";
2
+ import { ListObserver } from "../../base/types";
1
3
  import { Workout, Plan } from "../base/model";
4
+ import { WorkoutSettings } from "./cards";
2
5
  export type WP = Workout | Plan;
6
+ export interface WorkoutSettingsDisplayProps {
7
+ observer: ListObserver<WP>;
8
+ workouts: CardList<Workout>;
9
+ selected?: Workout;
10
+ settings?: WorkoutSettings;
11
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "incyclist-services",
3
- "version": "1.1.97",
3
+ "version": "1.1.99",
4
4
  "peerDependencies": {
5
5
  "gd-eventlog": "^0.1.26"
6
6
  },