|
1 | | -import * as CR from 'typings' |
| 1 | +import * as vscode from 'vscode' |
| 2 | +import * as T from 'typings' |
2 | 3 | import * as TT from 'typings/tutorial' |
| 4 | +import Storage from '../../storage' |
3 | 5 |
|
4 | | -const defaultValue: CR.Position = { |
| 6 | +const defaultValue: T.Position = { |
5 | 7 | levelId: '', |
6 | 8 | stepId: null, |
| 9 | + complete: false, |
7 | 10 | } |
8 | 11 |
|
9 | 12 | // position |
10 | 13 | class Position { |
11 | | - private value: CR.Position |
| 14 | + private value: T.Position |
| 15 | + private storage: Storage<T.Position> | undefined |
12 | 16 | constructor() { |
13 | 17 | this.value = defaultValue |
14 | 18 | } |
| 19 | + setTutorial(workspaceState: vscode.Memento, tutorial: TT.Tutorial) { |
| 20 | + this.storage = new Storage<T.Position>({ |
| 21 | + key: `coderoad:position:${tutorial.id}:${tutorial.version}`, |
| 22 | + storage: workspaceState, |
| 23 | + defaultValue, |
| 24 | + }) |
| 25 | + } |
| 26 | + async initPosition(workspaceState: vscode.Memento, tutorial: TT.Tutorial): Promise<T.Position> { |
| 27 | + // set value from storage |
| 28 | + this.setTutorial(workspaceState, tutorial) |
| 29 | + // find first level & step id |
| 30 | + let initLevel = tutorial.levels.length ? tutorial.levels[0] : null |
| 31 | + return this.set({ |
| 32 | + levelId: initLevel?.id || '', |
| 33 | + stepId: initLevel?.steps.length ? initLevel.steps[0].id : null, |
| 34 | + complete: false, |
| 35 | + }) |
| 36 | + } |
| 37 | + async continuePosition(workspaceState: vscode.Memento, tutorial: TT.Tutorial): Promise<T.Position> { |
| 38 | + this.setTutorial(workspaceState, tutorial) |
| 39 | + let position: T.Position = (await this.storage?.get()) || defaultValue |
| 40 | + return this.set(position) |
| 41 | + } |
15 | 42 | public get = () => { |
16 | 43 | return this.value |
17 | 44 | } |
18 | | - public set = (value: CR.Position) => { |
| 45 | + public set = (value: T.Position) => { |
19 | 46 | this.value = value |
| 47 | + this.storage?.set(value) |
| 48 | + return this.value |
20 | 49 | } |
21 | 50 | public reset = () => { |
22 | | - this.value = defaultValue |
23 | | - } |
24 | | - // calculate the current position based on the saved progress |
25 | | - public setPositionFromProgress = (tutorial: TT.Tutorial, progress: CR.Progress): CR.Position => { |
26 | | - // tutorial already completed |
27 | | - // TODO handle start again? |
28 | | - if (progress.complete) { |
29 | | - return this.value |
30 | | - } |
31 | | - |
32 | | - if (!tutorial || !tutorial.levels) { |
33 | | - throw new Error('Error setting position from progress') |
34 | | - } |
35 | | - |
36 | | - // get level |
37 | | - const { levels } = tutorial |
38 | | - const lastLevelIndex: number | undefined = levels.findIndex((l: TT.Level) => !progress.levels[l.id]) |
39 | | - if (lastLevelIndex >= levels.length) { |
40 | | - throw new Error('Error setting progress level') |
41 | | - } |
42 | | - |
43 | | - // get step |
44 | | - const currentLevel: TT.Level = levels[lastLevelIndex] |
45 | | - if (!currentLevel) { |
46 | | - // tutorial complete but not reached completed view |
47 | | - const finalLevel = levels[levels.length - 1] |
48 | | - return { |
49 | | - levelId: finalLevel.id, |
50 | | - stepId: finalLevel.steps.length ? finalLevel.steps[finalLevel.steps.length - 1].id : null, |
51 | | - complete: true, |
52 | | - } |
53 | | - } |
54 | | - let currentStepId: string | null |
55 | | - if (!currentLevel.steps.length) { |
56 | | - // no steps available for level |
57 | | - currentStepId = null |
58 | | - } else { |
59 | | - // find current step id |
60 | | - const { steps } = currentLevel |
61 | | - const lastStepIndex: number | undefined = steps.findIndex((s: TT.Step) => !progress.steps[s.id]) |
62 | | - if (lastStepIndex >= steps.length) { |
63 | | - throw new Error('Error setting progress step') |
64 | | - } |
65 | | - // handle position when last step is complete but "continue" not yet selected |
66 | | - const adjustedLastStepIndex = lastStepIndex === -1 ? steps.length - 1 : lastStepIndex |
67 | | - currentStepId = steps[adjustedLastStepIndex].id |
68 | | - } |
69 | | - |
70 | | - this.value = { |
71 | | - levelId: currentLevel.id, |
72 | | - stepId: currentStepId, |
73 | | - } |
74 | | - |
75 | | - return this.value |
| 51 | + return this.set(defaultValue) |
76 | 52 | } |
77 | 53 | } |
78 | 54 |
|
|
0 commit comments