Skip to content

Commit 35c171d

Browse files
fix(view, viewmodel, service): use full file name as file identifier (#1590)
* fix(view, viewmodel): use full file name as file identifier * Apply suggestion
1 parent 5dc72f4 commit 35c171d

4 files changed

Lines changed: 35 additions & 21 deletions

File tree

mobile-app/lib/service/learn/learn_file_service.dart

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import 'package:html/parser.dart';
77
import 'package:shared_preferences/shared_preferences.dart';
88

99
class LearnFileService {
10+
String getFullFileName(ChallengeFile file) {
11+
return '${file.name}.${file.ext.value}';
12+
}
13+
1014
// This function returns a specific file content from the cache.
1115
// If testing is enabled on the function it will return
1216
// the first file with the given file name.
@@ -20,11 +24,12 @@ class LearnFileService {
2024

2125
if (testing) {
2226
cache = challenge.files
23-
.firstWhere((element) => element.name == file.name)
27+
.firstWhere(
28+
(element) => getFullFileName(element) == getFullFileName(file))
2429
.contents;
2530
} else {
2631
SharedPreferences prefs = await SharedPreferences.getInstance();
27-
cache = prefs.getString('${challenge.id}.${file.name}');
32+
cache = prefs.getString('${challenge.id}.${getFullFileName(file)}');
2833
}
2934

3035
return cache ?? file.contents;
@@ -40,7 +45,7 @@ class LearnFileService {
4045
await prefs.setString('${challenge.id}.$currentSelectedFile', value);
4146
}
4247

43-
// This funciton returns the first file content with the given extension from the
48+
// This function returns the first file content with the given extension from the
4449
// cache. If testing is enabled it will return the file directly from the challenge.
4550
// If a CSS extension is put as a parameter it will return the first HTML file instead.
4651

@@ -67,7 +72,7 @@ class LearnFileService {
6772
SharedPreferences prefs = await SharedPreferences.getInstance();
6873

6974
fileContent = prefs.getString(
70-
'${challenge.id}.${firstChallenge[0].name}',
75+
'${challenge.id}.${getFullFileName(firstChallenge[0])}',
7176
) ??
7277
firstChallenge[0].contents;
7378
}
@@ -98,7 +103,7 @@ class LearnFileService {
98103

99104
if (fileWithEditableRegion.isNotEmpty) {
100105
cache = prefs.getString(
101-
'${challenge.id}.${fileWithEditableRegion[0].name}',
106+
'${challenge.id}.${getFullFileName(fileWithEditableRegion[0])}',
102107
) ??
103108
'';
104109

@@ -204,7 +209,7 @@ class LearnFileService {
204209
testing: testing,
205210
);
206211

207-
if (!await cssFileIsLinked(content, '${file.name}.${file.ext.name}')) {
212+
if (!await cssFileIsLinked(content, getFullFileName(file))) {
208213
continue;
209214
}
210215

@@ -246,7 +251,7 @@ class LearnFileService {
246251

247252
if (jsFiles.isNotEmpty) {
248253
for (ChallengeFile file in jsFiles) {
249-
String filename = '${file.name}.${file.ext.name}';
254+
String filename = getFullFileName(file);
250255
String? fileContents = await getExactFileFromCache(
251256
challenge,
252257
file,

mobile-app/lib/service/learn/learn_service.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ class LearnService {
122122
List challengeFiles = challenge.files.map((file) {
123123
return {
124124
'contents':
125-
prefs.getString('${challenge.id}.${file.name}') ?? file.contents,
125+
prefs.getString('${challenge.id}.${file.name}.${file.ext.value}') ??
126+
file.contents,
126127
'ext': file.ext.name,
127128
'history': file.history,
128129
'key': file.fileKey,

mobile-app/lib/ui/views/learn/challenge/challenge_view.dart

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -376,15 +376,17 @@ class ChallengeView extends StatelessWidget {
376376
items: [
377377
for (ChallengeFile file in challenge.files)
378378
DropdownMenuItem(
379-
value: file.name,
379+
value: model.getFullFileName(file),
380380
child: Text(
381-
'${file.name}.${file.ext.name}',
381+
model.getFullFileName(file),
382382
style: TextStyle(
383-
color: model.currentSelectedFile == file.name &&
383+
color: model.currentSelectedFile ==
384+
model.getFullFileName(file) &&
384385
!model.showTestsPanel
385386
? FccColors.blue50
386387
: Colors.white,
387-
fontWeight: model.currentSelectedFile == file.name &&
388+
fontWeight: model.currentSelectedFile ==
389+
model.getFullFileName(file) &&
388390
!model.showTestsPanel
389391
? FontWeight.bold
390392
: FontWeight.normal,
@@ -394,7 +396,8 @@ class ChallengeView extends StatelessWidget {
394396
],
395397
onChanged: (file) {
396398
model.setShowTestsPanel = false;
397-
model.setCurrentSelectedFile = file ?? challenge.files[0].name;
399+
model.setCurrentSelectedFile =
400+
file ?? model.getFullFileName(challenge.files[0]);
398401
model.setMounted = false;
399402
ChallengeFile currFile = model.currentFile(challenge);
400403
model.initFile(challenge, currFile);
@@ -404,13 +407,15 @@ class ChallengeView extends StatelessWidget {
404407
return challenge.files.map((file) {
405408
return Center(
406409
child: Text(
407-
'${file.name}.${file.ext.name}',
410+
model.getFullFileName(file),
408411
style: TextStyle(
409-
color: model.currentSelectedFile == file.name &&
412+
color: model.currentSelectedFile ==
413+
model.getFullFileName(file) &&
410414
!model.showTestsPanel
411415
? FccColors.blue50
412416
: Colors.white,
413-
fontWeight: model.currentSelectedFile == file.name &&
417+
fontWeight: model.currentSelectedFile ==
418+
model.getFullFileName(file) &&
414419
!model.showTestsPanel
415420
? FontWeight.bold
416421
: FontWeight.normal,

mobile-app/lib/ui/views/learn/challenge/challenge_viewmodel.dart

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ class ChallengeViewModel extends BaseViewModel {
318318
currFile,
319319
);
320320

321-
setCurrentSelectedFile = currFile.name;
321+
setCurrentSelectedFile = getFullFileName(currFile);
322322
setEditorText = fileContents;
323323
setEditorLanguage = currFile.ext.value;
324324
initEditor(challenge, currFile);
@@ -343,7 +343,7 @@ class ChallengeViewModel extends BaseViewModel {
343343
key: ValueKey(editorText),
344344
defaultLanguage: editorLanguage,
345345
defaultValue: editorText ?? '',
346-
path: '/${challenge.id}/${file.name}',
346+
path: '/${challenge.id}/${getFullFileName(file)}',
347347
options: options,
348348
);
349349

@@ -541,10 +541,14 @@ class ChallengeViewModel extends BaseViewModel {
541541
notifyListeners();
542542
}
543543

544+
String getFullFileName(ChallengeFile file) {
545+
return '${file.name}.${file.ext.value}';
546+
}
547+
544548
ChallengeFile currentFile(Challenge challenge) {
545549
if (currentSelectedFile.isNotEmpty) {
546550
ChallengeFile file = challenge.files.firstWhere(
547-
(file) => file.name == currentSelectedFile,
551+
(file) => getFullFileName(file) == currentSelectedFile,
548552
);
549553
return file;
550554
}
@@ -575,8 +579,7 @@ class ChallengeViewModel extends BaseViewModel {
575579
Challenge? currChallenge = challenge;
576580

577581
for (ChallengeFile file in currChallenge!.files) {
578-
await prefs.remove('${currChallenge.id}.${file.name}');
579-
await prefs.remove('${currChallenge.id}${file.name}');
582+
await prefs.remove('${currChallenge.id}.${getFullFileName(file)}');
580583
}
581584

582585
var challengeIndex = block!.challengeTiles.indexWhere(

0 commit comments

Comments
 (0)