|
1 | | -import*asCRfrom'typings' |
| 1 | +import*asvscodefrom'vscode' |
| 2 | +import*asTfrom'typings' |
2 | 3 | import*asTTfrom'typings/tutorial' |
| 4 | +importStoragefrom'../../storage' |
3 | 5 |
|
4 | | -constdefaultValue:CR.Position={ |
| 6 | +constdefaultValue:T.Position={ |
5 | 7 | levelId:'', |
6 | 8 | stepId:null, |
| 9 | +complete:false, |
7 | 10 | } |
8 | 11 |
|
9 | 12 | // position |
10 | 13 | classPosition{ |
11 | | -privatevalue:CR.Position |
| 14 | +privatevalue:T.Position |
| 15 | +privatestorage: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=newStorage<T.Position>({ |
| 21 | +key:`coderoad:position:${tutorial.id}:${tutorial.version}`, |
| 22 | +storage:workspaceState, |
| 23 | + defaultValue, |
| 24 | +}) |
| 25 | +} |
| 26 | +asyncinitPosition(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 | +letinitLevel=tutorial.levels.length ?tutorial.levels[0] :null |
| 31 | +returnthis.set({ |
| 32 | +levelId:initLevel?.id||'', |
| 33 | +stepId:initLevel?.steps.length ?initLevel.steps[0].id :null, |
| 34 | +complete:false, |
| 35 | +}) |
| 36 | +} |
| 37 | +asynccontinuePosition(workspaceState:vscode.Memento,tutorial:TT.Tutorial):Promise<T.Position>{ |
| 38 | +this.setTutorial(workspaceState,tutorial) |
| 39 | +letposition:T.Position=(awaitthis.storage?.get())||defaultValue |
| 40 | +returnthis.set(position) |
| 41 | +} |
15 | 42 | publicget=()=>{ |
16 | 43 | returnthis.value |
17 | 44 | } |
18 | | -publicset=(value:CR.Position)=>{ |
| 45 | +publicset=(value:T.Position)=>{ |
19 | 46 | this.value=value |
| 47 | +this.storage?.set(value) |
| 48 | +returnthis.value |
20 | 49 | } |
21 | 50 | publicreset=()=>{ |
22 | | -this.value=defaultValue |
23 | | -} |
24 | | -// calculate the current position based on the saved progress |
25 | | -publicsetPositionFromProgress=(tutorial:TT.Tutorial,progress:CR.Progress):CR.Position=>{ |
26 | | -// tutorial already completed |
27 | | -// TODO handle start again? |
28 | | -if(progress.complete){ |
29 | | -returnthis.value |
30 | | -} |
31 | | - |
32 | | -if(!tutorial||!tutorial.levels){ |
33 | | -thrownewError('Error setting position from progress') |
34 | | -} |
35 | | - |
36 | | -// get level |
37 | | -const{ levels}=tutorial |
38 | | -constlastLevelIndex:number|undefined=levels.findIndex((l:TT.Level)=>!progress.levels[l.id]) |
39 | | -if(lastLevelIndex>=levels.length){ |
40 | | -thrownewError('Error setting progress level') |
41 | | -} |
42 | | - |
43 | | -// get step |
44 | | -constcurrentLevel:TT.Level=levels[lastLevelIndex] |
45 | | -if(!currentLevel){ |
46 | | -// tutorial complete but not reached completed view |
47 | | -constfinalLevel=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 | | -letcurrentStepId: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 | | -constlastStepIndex:number|undefined=steps.findIndex((s:TT.Step)=>!progress.steps[s.id]) |
62 | | -if(lastStepIndex>=steps.length){ |
63 | | -thrownewError('Error setting progress step') |
64 | | -} |
65 | | -// handle position when last step is complete but "continue" not yet selected |
66 | | -constadjustedLastStepIndex=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 | | -returnthis.value |
| 51 | +returnthis.set(defaultValue) |
76 | 52 | } |
77 | 53 | } |
78 | 54 |
|
|