incyclist-services 1.9.11 → 1.9.13

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.
Files changed (48) hide show
  1. package/lib/cjs/apps/googleMaps/consts.js +10 -0
  2. package/lib/cjs/apps/googleMaps/index.js +1 -0
  3. package/lib/cjs/apps/googleMaps/service.js +46 -1
  4. package/lib/cjs/maps/MapArea/options.js +2 -3
  5. package/lib/cjs/ride/display/service.js +95 -0
  6. package/lib/cjs/ride/index.js +1 -0
  7. package/lib/cjs/ride/page/service.js +69 -1
  8. package/lib/cjs/ride/route/FreeRideDisplayService.js +36 -5
  9. package/lib/cjs/ride/route/GpxDisplayService.js +46 -1
  10. package/lib/cjs/ride/view-restriction/index.js +18 -0
  11. package/lib/cjs/ride/view-restriction/service.js +221 -0
  12. package/lib/cjs/ride/view-restriction/types.js +2 -0
  13. package/lib/cjs/routes/free-ride/option-labels.js +141 -0
  14. package/lib/cjs/routes/free-ride/service.js +217 -117
  15. package/lib/cjs/settings/display/ride/service.js +22 -4
  16. package/lib/esm/apps/googleMaps/consts.js +7 -0
  17. package/lib/esm/apps/googleMaps/index.js +1 -0
  18. package/lib/esm/apps/googleMaps/service.js +46 -1
  19. package/lib/esm/maps/MapArea/options.js +2 -3
  20. package/lib/esm/ride/display/service.js +95 -0
  21. package/lib/esm/ride/index.js +1 -0
  22. package/lib/esm/ride/page/service.js +69 -1
  23. package/lib/esm/ride/route/FreeRideDisplayService.js +36 -5
  24. package/lib/esm/ride/route/GpxDisplayService.js +46 -1
  25. package/lib/esm/ride/view-restriction/index.js +2 -0
  26. package/lib/esm/ride/view-restriction/service.js +217 -0
  27. package/lib/esm/ride/view-restriction/types.js +1 -0
  28. package/lib/esm/routes/free-ride/service.js +217 -117
  29. package/lib/esm/settings/display/ride/service.js +20 -2
  30. package/lib/types/apps/googleMaps/consts.d.ts +7 -0
  31. package/lib/types/apps/googleMaps/index.d.ts +1 -0
  32. package/lib/types/apps/googleMaps/service.d.ts +8 -0
  33. package/lib/types/maps/MapArea/types.d.ts +1 -0
  34. package/lib/types/ride/base/types.d.ts +5 -1
  35. package/lib/types/ride/display/service.d.ts +15 -0
  36. package/lib/types/ride/index.d.ts +1 -0
  37. package/lib/types/ride/page/service.d.ts +10 -0
  38. package/lib/types/ride/page/types.d.ts +10 -0
  39. package/lib/types/ride/route/FreeRideDisplayService.d.ts +4 -1
  40. package/lib/types/ride/route/GpxDisplayService.d.ts +5 -0
  41. package/lib/types/ride/view-restriction/index.d.ts +2 -0
  42. package/lib/types/ride/view-restriction/service.d.ts +30 -0
  43. package/lib/types/ride/view-restriction/types.d.ts +57 -0
  44. package/lib/types/routes/free-ride/service.d.ts +18 -2
  45. package/lib/types/routes/list/types.d.ts +11 -0
  46. package/lib/types/settings/display/ride/service.d.ts +1 -0
  47. package/lib/types/settings/display/ride/types.d.ts +4 -0
  48. package/package.json +3 -3
@@ -0,0 +1,221 @@
1
+ "use strict";
2
+ var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
3
+ var useValue = arguments.length > 2;
4
+ for (var i = 0; i < initializers.length; i++) {
5
+ value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
6
+ }
7
+ return useValue ? value : void 0;
8
+ };
9
+ var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
10
+ function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
11
+ var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
12
+ var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
13
+ var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
14
+ var _, done = false;
15
+ for (var i = decorators.length - 1; i >= 0; i--) {
16
+ var context = {};
17
+ for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
18
+ for (var p in contextIn.access) context.access[p] = contextIn.access[p];
19
+ context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
20
+ var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
21
+ if (kind === "accessor") {
22
+ if (result === void 0) continue;
23
+ if (result === null || typeof result !== "object") throw new TypeError("Object expected");
24
+ if (_ = accept(result.get)) descriptor.get = _;
25
+ if (_ = accept(result.set)) descriptor.set = _;
26
+ if (_ = accept(result.init)) initializers.unshift(_);
27
+ }
28
+ else if (_ = accept(result)) {
29
+ if (kind === "field") initializers.unshift(_);
30
+ else descriptor[key] = _;
31
+ }
32
+ }
33
+ if (target) Object.defineProperty(target, contextIn.name, descriptor);
34
+ done = true;
35
+ };
36
+ Object.defineProperty(exports, "__esModule", { value: true });
37
+ exports.useRideViewRestriction = exports.RideViewRestrictionService = void 0;
38
+ const api_1 = require("../../api");
39
+ const consts_1 = require("../../apps/googleMaps/consts");
40
+ const service_1 = require("../../apps/googleMaps/service");
41
+ const decorators_1 = require("../../base/decorators");
42
+ const service_2 = require("../../base/service");
43
+ const types_1 = require("../../base/types");
44
+ const service_3 = require("../../appstate/service");
45
+ const NOTICE_OFF_STATE_KEY = 'simulatedRideViewNoticeOff';
46
+ const DIALOG_TITLE = 'Street View in simulated rides';
47
+ const DIALOG_BODY = 'Street View and Satellite View use Google Maps, and Google charges Incyclist for every ride that uses them. To keep Incyclist free, these views come with rides on a connected device. A simulated ride shows the Map instead.';
48
+ const DEVICE_TEXT = 'Connect your smart trainer, power meter or speed sensor before you start. Street View then works as usual.';
49
+ const DEVICE_TEXT_MOBILE = 'To get Street View: connect your smart trainer, power meter or speed sensor before you start.';
50
+ const OWN_KEY_TEXT = `With your own key, Street View and Satellite View also work in simulated rides. ${consts_1.MAPS_KEY_TEXTS.billing}`;
51
+ const VIEW_NAMES = { sv: 'Street View', sat: 'Satellite View' };
52
+ let RideViewRestrictionService = (() => {
53
+ let _classDecorators = [types_1.Singleton];
54
+ let _classDescriptor;
55
+ let _classExtraInitializers = [];
56
+ let _classThis;
57
+ let _classSuper = service_2.IncyclistService;
58
+ let _instanceExtraInitializers = [];
59
+ let _getAppState_decorators;
60
+ let _getGoogleMaps_decorators;
61
+ let _getBindings_decorators;
62
+ var RideViewRestrictionService = class extends _classSuper {
63
+ static { _classThis = this; }
64
+ static {
65
+ const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
66
+ _getAppState_decorators = [decorators_1.Injectable];
67
+ _getGoogleMaps_decorators = [decorators_1.Injectable];
68
+ _getBindings_decorators = [decorators_1.Injectable];
69
+ __esDecorate(this, null, _getAppState_decorators, { kind: "method", name: "getAppState", static: false, private: false, access: { has: obj => "getAppState" in obj, get: obj => obj.getAppState }, metadata: _metadata }, null, _instanceExtraInitializers);
70
+ __esDecorate(this, null, _getGoogleMaps_decorators, { kind: "method", name: "getGoogleMaps", static: false, private: false, access: { has: obj => "getGoogleMaps" in obj, get: obj => obj.getGoogleMaps }, metadata: _metadata }, null, _instanceExtraInitializers);
71
+ __esDecorate(this, null, _getBindings_decorators, { kind: "method", name: "getBindings", static: false, private: false, access: { has: obj => "getBindings" in obj, get: obj => obj.getBindings }, metadata: _metadata }, null, _instanceExtraInitializers);
72
+ __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
73
+ RideViewRestrictionService = _classThis = _classDescriptor.value;
74
+ if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
75
+ __runInitializers(_classThis, _classExtraInitializers);
76
+ }
77
+ restriction = __runInitializers(this, _instanceExtraInitializers);
78
+ noticeState = 'inactive';
79
+ noticeId = 0;
80
+ constructor() {
81
+ super('RideViewRestriction');
82
+ }
83
+ beginRide(props) {
84
+ try {
85
+ this.restriction = undefined;
86
+ this.noticeState = 'inactive';
87
+ this.noticeId++;
88
+ if (props.simulated && !this.getGoogleMaps().hasActivePersonalApiKey()) {
89
+ this.restriction = { cause: 'simulation', preferredView: props.preferredView };
90
+ if (this.isNoticeWanted(this.restriction))
91
+ this.noticeState = 'armed';
92
+ }
93
+ this.logEvent({ message: 'ride view restriction', restricted: this.restriction !== undefined, noticeState: this.noticeState });
94
+ }
95
+ catch (err) {
96
+ this.logError(err, 'beginRide');
97
+ }
98
+ this.emit('changed');
99
+ }
100
+ onRideStarted() {
101
+ if (this.noticeState !== 'armed')
102
+ return;
103
+ this.noticeState = 'due';
104
+ this.emit('changed');
105
+ }
106
+ endRide() {
107
+ this.restriction = undefined;
108
+ this.noticeState = 'inactive';
109
+ this.emit('changed');
110
+ }
111
+ getActiveRestriction() {
112
+ return this.restriction;
113
+ }
114
+ getNotice() {
115
+ const id = this.noticeId;
116
+ if (this.noticeState === 'due' && this.restriction) {
117
+ const view = VIEW_NAMES[this.restriction.preferredView];
118
+ return {
119
+ id, state: 'message',
120
+ text: `${view} isn't available in simulated rides. Showing the Map instead.`,
121
+ actions: [
122
+ { id: 'how-to', label: `How to get ${view}` },
123
+ { id: 'dont-show-again', label: "Don't show this again" },
124
+ ]
125
+ };
126
+ }
127
+ if (this.noticeState === 'acknowledged') {
128
+ return {
129
+ id, state: 'acknowledged',
130
+ actions: [{ id: 'change-this', label: "We won't show this again. Change this" }]
131
+ };
132
+ }
133
+ return undefined;
134
+ }
135
+ onNoticeAction(id) {
136
+ if (id === 'dont-show-again') {
137
+ this.setReminderEnabled(false);
138
+ if (this.noticeState === 'due')
139
+ this.noticeState = 'acknowledged';
140
+ }
141
+ else if (this.noticeState === 'due' || this.noticeState === 'acknowledged') {
142
+ this.noticeState = 'closed';
143
+ }
144
+ this.emit('changed');
145
+ }
146
+ onNoticeClosed() {
147
+ if (this.noticeState !== 'due' && this.noticeState !== 'acknowledged')
148
+ return;
149
+ this.noticeState = 'closed';
150
+ this.emit('changed');
151
+ }
152
+ isReminderEnabled() {
153
+ return this.getAppState().getPersistedState(NOTICE_OFF_STATE_KEY) !== true;
154
+ }
155
+ setReminderEnabled(enabled) {
156
+ try {
157
+ this.getAppState().setPersistedState(NOTICE_OFF_STATE_KEY, !enabled);
158
+ }
159
+ catch (err) {
160
+ this.logError(err, 'setReminderEnabled');
161
+ }
162
+ }
163
+ getDialogProps(props = {}) {
164
+ const options = [this.getDeviceOption(props.inRide === true)];
165
+ if (!this.isMobile())
166
+ options.push(this.getOwnKeyOption());
167
+ const dialog = { title: DIALOG_TITLE, body: DIALOG_BODY, options };
168
+ if (this.restriction)
169
+ dialog.reminder = { label: 'Remind me in simulated rides', checked: this.isReminderEnabled() };
170
+ return dialog;
171
+ }
172
+ isNoticeWanted(restriction) {
173
+ return (restriction.preferredView === 'sv' || restriction.preferredView === 'sat') && this.isReminderEnabled();
174
+ }
175
+ getDeviceOption(inRide) {
176
+ const mobile = this.isMobile();
177
+ const option = {
178
+ id: 'device',
179
+ text: mobile ? DEVICE_TEXT_MOBILE : DEVICE_TEXT,
180
+ };
181
+ if (!mobile)
182
+ option.heading = 'Ride with a device';
183
+ if (inRide) {
184
+ option.discard = {
185
+ label: 'Discard ride and connect a device',
186
+ confirm: { question: 'Discard this simulated ride?', confirmLabel: 'Discard and connect', cancelLabel: 'Keep riding' }
187
+ };
188
+ }
189
+ return option;
190
+ }
191
+ getOwnKeyOption() {
192
+ const option = {
193
+ id: 'own-key',
194
+ heading: 'Or use your own Google Maps API key',
195
+ text: OWN_KEY_TEXT,
196
+ helpLink: { label: consts_1.MAPS_KEY_TEXTS.helpLabel, url: consts_1.MAPS_KEY_HELP_URL },
197
+ };
198
+ if (this.getGoogleMaps().isPersonalApiKeyPendingRestart())
199
+ option.pendingRestart = { text: consts_1.MAPS_KEY_TEXTS.pendingRestart };
200
+ else
201
+ option.addKey = { label: 'Add my key' };
202
+ return option;
203
+ }
204
+ isMobile() {
205
+ return this.getBindings()?.appInfo?.getChannel() === 'mobile';
206
+ }
207
+ getAppState() {
208
+ return (0, service_3.useAppState)();
209
+ }
210
+ getGoogleMaps() {
211
+ return (0, service_1.useGoogleMaps)();
212
+ }
213
+ getBindings() {
214
+ return (0, api_1.getBindings)();
215
+ }
216
+ };
217
+ return RideViewRestrictionService = _classThis;
218
+ })();
219
+ exports.RideViewRestrictionService = RideViewRestrictionService;
220
+ const useRideViewRestriction = () => new RideViewRestrictionService();
221
+ exports.useRideViewRestriction = useRideViewRestriction;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,141 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getLayoutOrder = exports.buildOptionLabels = exports.getRoadLabel = exports.deriveManeuvers = exports.getCompassWord = exports.normalizeAngle = exports.FALLBACK_LABEL = exports.MANEUVER_TEXT = void 0;
4
+ exports.MANEUVER_TEXT = {
5
+ 'straight': 'Straight',
6
+ 'slight-left': 'Slight left',
7
+ 'slight-right': 'Slight right',
8
+ 'left': 'Left',
9
+ 'right': 'Right',
10
+ 'sharp-left': 'Sharp left',
11
+ 'sharp-right': 'Sharp right',
12
+ 'keep-left': 'Keep left',
13
+ 'keep-right': 'Keep right',
14
+ 'u-turn': 'Turn around',
15
+ 'roundabout': 'Roundabout',
16
+ 'unknown': 'Road'
17
+ };
18
+ const COMPASS_WORDS = ['North', 'Northeast', 'East', 'Southeast', 'South', 'Southwest', 'West', 'Northwest'];
19
+ exports.FALLBACK_LABEL = {
20
+ roundabout: 'Roundabout',
21
+ slip: 'Slip road',
22
+ unnamed: 'Unnamed road'
23
+ };
24
+ const normalizeAngle = (angle) => {
25
+ if (!Number.isFinite(angle))
26
+ return angle;
27
+ let a = angle % 360;
28
+ if (a > 180)
29
+ a -= 360;
30
+ if (a <= -180)
31
+ a += 360;
32
+ return a;
33
+ };
34
+ exports.normalizeAngle = normalizeAngle;
35
+ const getCompassWord = (heading) => {
36
+ if (!Number.isFinite(heading))
37
+ return undefined;
38
+ const normalized = ((heading % 360) + 360) % 360;
39
+ return COMPASS_WORDS[Math.round(normalized / 45) % 8];
40
+ };
41
+ exports.getCompassWord = getCompassWord;
42
+ const bucket = (angle) => {
43
+ const abs = Math.abs(angle);
44
+ const right = angle > 0;
45
+ if (abs <= 20)
46
+ return 'straight';
47
+ if (abs <= 60)
48
+ return right ? 'slight-right' : 'slight-left';
49
+ if (abs <= 135)
50
+ return right ? 'right' : 'left';
51
+ if (abs <= 170)
52
+ return right ? 'sharp-right' : 'sharp-left';
53
+ return 'u-turn';
54
+ };
55
+ const deriveManeuvers = (options) => {
56
+ const result = options.map(o => {
57
+ if (o.roundabout)
58
+ return 'roundabout';
59
+ if (!Number.isFinite(o.direction))
60
+ return 'unknown';
61
+ return bucket(o.direction);
62
+ });
63
+ const groups = new Map();
64
+ result.forEach((m, idx) => {
65
+ if (m === 'roundabout' || m === 'unknown' || m === 'u-turn')
66
+ return;
67
+ groups.set(m, [...(groups.get(m) ?? []), idx]);
68
+ });
69
+ groups.forEach(indexes => {
70
+ if (indexes.length < 2)
71
+ return;
72
+ const sorted = [...indexes].sort((a, b) => options[a].direction - options[b].direction);
73
+ result[sorted[0]] = 'keep-left';
74
+ result[sorted.at(-1)] = 'keep-right';
75
+ });
76
+ return result;
77
+ };
78
+ exports.deriveManeuvers = deriveManeuvers;
79
+ const getRoadLabel = (tags) => {
80
+ const roadName = tags?.name || tags?.ref || undefined;
81
+ if (roadName)
82
+ return { roadName, roadLabel: roadName };
83
+ if (tags?.junction === 'roundabout')
84
+ return { roadLabel: exports.FALLBACK_LABEL.roundabout };
85
+ if (tags?.highway?.endsWith('_link'))
86
+ return { roadLabel: exports.FALLBACK_LABEL.slip };
87
+ return { roadLabel: exports.FALLBACK_LABEL.unnamed };
88
+ };
89
+ exports.getRoadLabel = getRoadLabel;
90
+ const getDegradedText = (compass, idx) => compass ? `Heading ${compass.toLowerCase()}` : `Road ${idx + 1}`;
91
+ const FORK_SIDES = {
92
+ 2: ['left fork', 'right fork'],
93
+ 3: ['left fork', 'middle fork', 'right fork']
94
+ };
95
+ const buildOptionLabels = (options) => {
96
+ const maneuvers = (0, exports.deriveManeuvers)(options);
97
+ const labels = options.map((o, idx) => {
98
+ const maneuver = maneuvers[idx];
99
+ const degraded = maneuver === 'unknown';
100
+ const { roadName, roadLabel } = (0, exports.getRoadLabel)(o.tags);
101
+ return {
102
+ maneuver,
103
+ maneuverText: degraded ? getDegradedText(o.compass, idx) : exports.MANEUVER_TEXT[maneuver],
104
+ roadName,
105
+ roadLabel,
106
+ compass: o.compass,
107
+ degraded
108
+ };
109
+ });
110
+ const key = (l) => `${l.maneuverText}|${l.roadLabel}`;
111
+ const collisions = () => {
112
+ const groups = new Map();
113
+ labels.forEach((l, idx) => groups.set(key(l), [...(groups.get(key(l)) ?? []), idx]));
114
+ return [...groups.values()].filter(g => g.length > 1);
115
+ };
116
+ collisions().forEach(group => {
117
+ const compasses = group.map(idx => labels[idx].compass);
118
+ if (compasses.some(c => !c) || new Set(compasses).size !== group.length)
119
+ return;
120
+ group.forEach(idx => { labels[idx].roadLabel = `${labels[idx].roadLabel}, ${labels[idx].compass?.toLowerCase()}`; });
121
+ });
122
+ collisions().forEach(group => {
123
+ const sides = FORK_SIDES[group.length];
124
+ if (!sides || group.some(idx => !Number.isFinite(options[idx].direction)))
125
+ return;
126
+ const sorted = [...group].sort((a, b) => options[a].direction - options[b].direction);
127
+ sorted.forEach((idx, i) => { labels[idx].roadLabel = `${labels[idx].roadLabel}, ${sides[i]}`; });
128
+ });
129
+ return labels;
130
+ };
131
+ exports.buildOptionLabels = buildOptionLabels;
132
+ const getLayoutOrder = (options) => {
133
+ const indexes = options.map((_o, idx) => idx);
134
+ if (options.some(o => o.roundabout))
135
+ return indexes;
136
+ const valid = (idx) => Number.isFinite(options[idx].direction);
137
+ const withAngle = indexes.filter(valid).sort((a, b) => options[a].direction - options[b].direction);
138
+ const withoutAngle = indexes.filter(idx => !valid(idx));
139
+ return [...withAngle, ...withoutAngle];
140
+ };
141
+ exports.getLayoutOrder = getLayoutOrder;