Skip to content

Commit 1f49dd5

Browse files
authored
fix: old state when returning to block page (#871)
* fix: old state when returning to block page * feat: add on willpop scope * fix: remove debug value
1 parent 441f380 commit 1f49dd5

File tree

2 files changed

+113
-77
lines changed

2 files changed

+113
-77
lines changed

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

Lines changed: 88 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -113,91 +113,104 @@ class ChallengeView extends StatelessWidget {
113113
),
114114
);
115115

116-
return Scaffold(
117-
appBar: !model.hideAppBar
118-
? AppBar(
119-
automaticallyImplyLeading: false,
120-
title: challenge.files.length == 1
121-
? const Text('Editor')
122-
: null,
123-
actions: [
124-
if (model.showPreview)
125-
Expanded(
126-
child: Container(
127-
decoration: model.showPreview ? decoration : null,
116+
return WillPopScope(
117+
onWillPop: () async {
118+
model.updateProgressOnPop(context);
119+
120+
return Future.value(true);
121+
},
122+
child: Scaffold(
123+
appBar: !model.hideAppBar
124+
? AppBar(
125+
leading: IconButton(
126+
icon: const Icon(Icons.arrow_back_ios),
127+
onPressed: () async {
128+
model.updateProgressOnPop(context);
129+
},
130+
),
131+
title: challenge.files.length == 1
132+
? const Text('Editor')
133+
: null,
134+
actions: [
135+
if (model.showPreview)
136+
Expanded(
128137
child: Container(
129138
decoration:
130-
model.showConsole ? decoration : null,
131-
child: ElevatedButton(
132-
onPressed: () {},
133-
child: const Text('Preview'),
139+
model.showPreview ? decoration : null,
140+
child: Container(
141+
decoration:
142+
model.showConsole ? decoration : null,
143+
child: ElevatedButton(
144+
onPressed: () {},
145+
child: const Text('Preview'),
146+
),
134147
),
135148
),
136149
),
137-
),
138-
if (model.showPreview)
139-
Expanded(
140-
child: ElevatedButton(
141-
child: const Text('Console'),
142-
onPressed: () {
143-
model.consoleSnackbar();
144-
},
150+
if (model.showPreview)
151+
Expanded(
152+
child: ElevatedButton(
153+
child: const Text('Console'),
154+
onPressed: () {
155+
model.consoleSnackbar();
156+
},
157+
),
145158
),
146-
),
147-
if (!model.showPreview && challenge.files.length > 1)
148-
for (ChallengeFile file in challenge.files)
149-
customTabBar(
150-
model,
151-
challenge,
152-
file,
153-
editor,
154-
)
155-
],
156-
)
157-
: null,
158-
bottomNavigationBar: Padding(
159-
padding: EdgeInsets.only(
160-
bottom: MediaQuery.of(context).viewInsets.bottom,
161-
),
162-
child: customBottomBar(
163-
model,
164-
challenge,
165-
editor,
166-
context,
159+
if (!model.showPreview && challenge.files.length > 1)
160+
for (ChallengeFile file in challenge.files)
161+
customTabBar(
162+
model,
163+
challenge,
164+
file,
165+
editor,
166+
)
167+
],
168+
)
169+
: null,
170+
bottomNavigationBar: Padding(
171+
padding: EdgeInsets.only(
172+
bottom: MediaQuery.of(context).viewInsets.bottom,
173+
),
174+
child: customBottomBar(
175+
model,
176+
challenge,
177+
editor,
178+
context,
179+
),
167180
),
168-
),
169-
body: !model.showPreview
170-
? Column(
171-
children: [
172-
if (model.showPanel && !keyboard)
173-
DynamicPanel(
174-
challenge: challenge,
175-
model: model,
176-
panel: model.panelType,
177-
maxChallenges: maxChallenges,
178-
challengesCompleted: challengesCompleted,
179-
editor: editor,
180-
),
181-
Expanded(child: editor)
182-
],
183-
)
184-
: Column(
185-
children: [
186-
if (model.showPanel && !keyboard)
187-
DynamicPanel(
181+
body: !model.showPreview
182+
? Column(
183+
children: [
184+
if (model.showPanel && !keyboard)
185+
DynamicPanel(
186+
challenge: challenge,
187+
model: model,
188+
panel: model.panelType,
189+
maxChallenges: maxChallenges,
190+
challengesCompleted: challengesCompleted,
191+
editor: editor,
192+
),
193+
Expanded(child: editor)
194+
],
195+
)
196+
: Column(
197+
children: [
198+
if (model.showPanel && !keyboard)
199+
DynamicPanel(
200+
challenge: challenge,
201+
model: model,
202+
panel: model.panelType,
203+
maxChallenges: maxChallenges,
204+
challengesCompleted: challengesCompleted,
205+
editor: editor,
206+
),
207+
ProjectPreview(
188208
challenge: challenge,
189209
model: model,
190-
panel: model.panelType,
191-
maxChallenges: maxChallenges,
192-
challengesCompleted: challengesCompleted,
193-
editor: editor,
194210
),
195-
ProjectPreview(
196-
challenge: challenge,
197-
model: model,
198-
),
199-
],
200-
),
211+
],
212+
),
213+
),
201214
);
202215
}
203216

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'dart:convert';
2+
import 'package:flutter/material.dart';
23
import 'package:flutter_code_editor/editor/editor.dart';
34
import 'package:flutter_code_editor/enums/syntax.dart';
45
import 'package:flutter_code_editor/models/editor_options.dart';
@@ -15,6 +16,7 @@ import 'package:freecodecamp/service/learn/learn_file_service.dart';
1516
import 'package:freecodecamp/service/learn/learn_offline_service.dart';
1617
import 'package:freecodecamp/service/learn/learn_service.dart';
1718
import 'package:freecodecamp/service/authentication/authentication_service.dart';
19+
import 'package:freecodecamp/ui/views/learn/superblock/superblock_view.dart';
1820
import 'package:freecodecamp/ui/views/learn/test_runner.dart';
1921
import 'package:freecodecamp/ui/widgets/setup_dialog_ui.dart';
2022
import 'package:html/dom.dart' as dom;
@@ -89,7 +91,7 @@ class ChallengeViewModel extends BaseViewModel {
8991
final NavigationService _navigationService = locator<NavigationService>();
9092
final LearnFileService fileService = locator<LearnFileService>();
9193
final LearnService learnService = locator<LearnService>();
92-
final _learnOfflineService = locator<LearnOfflineService>();
94+
final learnOfflineService = locator<LearnOfflineService>();
9395

9496
set setCurrentSelectedFile(String value) {
9597
_currentSelectedFile = value;
@@ -190,7 +192,7 @@ class ChallengeViewModel extends BaseViewModel {
190192
setupDialogUi();
191193
learnService.init();
192194

193-
setChallenge = _learnOfflineService.getChallenge(url, challengeId);
195+
setChallenge = learnOfflineService.getChallenge(url, challengeId);
194196
Challenge challenge = await _challenge!;
195197

196198
List<ChallengeFile> currentEditedChallenge = challenge.files
@@ -416,6 +418,27 @@ class ChallengeViewModel extends BaseViewModel {
416418
}
417419
}
418420

421+
void updateProgressOnPop(BuildContext context) async {
422+
learnOfflineService.hasInternet().then(
423+
(value) => Navigator.pushReplacement(
424+
context,
425+
PageRouteBuilder(
426+
transitionDuration: Duration.zero,
427+
pageBuilder: (
428+
context,
429+
animation1,
430+
animation2,
431+
) =>
432+
SuperBlockView(
433+
superBlockDashedName: block!.superBlock.dashedName,
434+
superBlockName: block!.superBlock.name,
435+
hasInternet: value,
436+
),
437+
),
438+
),
439+
);
440+
}
441+
419442
void passChallenge(Challenge? challenge) async {
420443
SharedPreferences prefs = await SharedPreferences.getInstance();
421444
if (challenge != null) {

0 commit comments

Comments
 (0)