Skip to content

Commit

Permalink
fix: old state when returning to block page (#871)
Browse files Browse the repository at this point in the history
* fix: old state when returning to block page

* feat: add on willpop scope

* fix: remove debug value
  • Loading branch information
Sembauke authored Mar 25, 2023
1 parent 441f380 commit 1f49dd5
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 77 deletions.
163 changes: 88 additions & 75 deletions mobile-app/lib/ui/views/learn/challenge/challenge_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -113,91 +113,104 @@ class ChallengeView extends StatelessWidget {
),
);

return Scaffold(
appBar: !model.hideAppBar
? AppBar(
automaticallyImplyLeading: false,
title: challenge.files.length == 1
? const Text('Editor')
: null,
actions: [
if (model.showPreview)
Expanded(
child: Container(
decoration: model.showPreview ? decoration : null,
return WillPopScope(
onWillPop: () async {
model.updateProgressOnPop(context);

return Future.value(true);
},
child: Scaffold(
appBar: !model.hideAppBar
? AppBar(
leading: IconButton(
icon: const Icon(Icons.arrow_back_ios),
onPressed: () async {
model.updateProgressOnPop(context);
},
),
title: challenge.files.length == 1
? const Text('Editor')
: null,
actions: [
if (model.showPreview)
Expanded(
child: Container(
decoration:
model.showConsole ? decoration : null,
child: ElevatedButton(
onPressed: () {},
child: const Text('Preview'),
model.showPreview ? decoration : null,
child: Container(
decoration:
model.showConsole ? decoration : null,
child: ElevatedButton(
onPressed: () {},
child: const Text('Preview'),
),
),
),
),
),
if (model.showPreview)
Expanded(
child: ElevatedButton(
child: const Text('Console'),
onPressed: () {
model.consoleSnackbar();
},
if (model.showPreview)
Expanded(
child: ElevatedButton(
child: const Text('Console'),
onPressed: () {
model.consoleSnackbar();
},
),
),
),
if (!model.showPreview && challenge.files.length > 1)
for (ChallengeFile file in challenge.files)
customTabBar(
model,
challenge,
file,
editor,
)
],
)
: null,
bottomNavigationBar: Padding(
padding: EdgeInsets.only(
bottom: MediaQuery.of(context).viewInsets.bottom,
),
child: customBottomBar(
model,
challenge,
editor,
context,
if (!model.showPreview && challenge.files.length > 1)
for (ChallengeFile file in challenge.files)
customTabBar(
model,
challenge,
file,
editor,
)
],
)
: null,
bottomNavigationBar: Padding(
padding: EdgeInsets.only(
bottom: MediaQuery.of(context).viewInsets.bottom,
),
child: customBottomBar(
model,
challenge,
editor,
context,
),
),
),
body: !model.showPreview
? Column(
children: [
if (model.showPanel && !keyboard)
DynamicPanel(
challenge: challenge,
model: model,
panel: model.panelType,
maxChallenges: maxChallenges,
challengesCompleted: challengesCompleted,
editor: editor,
),
Expanded(child: editor)
],
)
: Column(
children: [
if (model.showPanel && !keyboard)
DynamicPanel(
body: !model.showPreview
? Column(
children: [
if (model.showPanel && !keyboard)
DynamicPanel(
challenge: challenge,
model: model,
panel: model.panelType,
maxChallenges: maxChallenges,
challengesCompleted: challengesCompleted,
editor: editor,
),
Expanded(child: editor)
],
)
: Column(
children: [
if (model.showPanel && !keyboard)
DynamicPanel(
challenge: challenge,
model: model,
panel: model.panelType,
maxChallenges: maxChallenges,
challengesCompleted: challengesCompleted,
editor: editor,
),
ProjectPreview(
challenge: challenge,
model: model,
panel: model.panelType,
maxChallenges: maxChallenges,
challengesCompleted: challengesCompleted,
editor: editor,
),
ProjectPreview(
challenge: challenge,
model: model,
),
],
),
],
),
),
);
}

Expand Down
27 changes: 25 additions & 2 deletions mobile-app/lib/ui/views/learn/challenge/challenge_viewmodel.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter_code_editor/editor/editor.dart';
import 'package:flutter_code_editor/enums/syntax.dart';
import 'package:flutter_code_editor/models/editor_options.dart';
Expand All @@ -15,6 +16,7 @@ import 'package:freecodecamp/service/learn/learn_file_service.dart';
import 'package:freecodecamp/service/learn/learn_offline_service.dart';
import 'package:freecodecamp/service/learn/learn_service.dart';
import 'package:freecodecamp/service/authentication/authentication_service.dart';
import 'package:freecodecamp/ui/views/learn/superblock/superblock_view.dart';
import 'package:freecodecamp/ui/views/learn/test_runner.dart';
import 'package:freecodecamp/ui/widgets/setup_dialog_ui.dart';
import 'package:html/dom.dart' as dom;
Expand Down Expand Up @@ -89,7 +91,7 @@ class ChallengeViewModel extends BaseViewModel {
final NavigationService _navigationService = locator<NavigationService>();
final LearnFileService fileService = locator<LearnFileService>();
final LearnService learnService = locator<LearnService>();
final _learnOfflineService = locator<LearnOfflineService>();
final learnOfflineService = locator<LearnOfflineService>();

set setCurrentSelectedFile(String value) {
_currentSelectedFile = value;
Expand Down Expand Up @@ -190,7 +192,7 @@ class ChallengeViewModel extends BaseViewModel {
setupDialogUi();
learnService.init();

setChallenge = _learnOfflineService.getChallenge(url, challengeId);
setChallenge = learnOfflineService.getChallenge(url, challengeId);
Challenge challenge = await _challenge!;

List<ChallengeFile> currentEditedChallenge = challenge.files
Expand Down Expand Up @@ -416,6 +418,27 @@ class ChallengeViewModel extends BaseViewModel {
}
}

void updateProgressOnPop(BuildContext context) async {
learnOfflineService.hasInternet().then(
(value) => Navigator.pushReplacement(
context,
PageRouteBuilder(
transitionDuration: Duration.zero,
pageBuilder: (
context,
animation1,
animation2,
) =>
SuperBlockView(
superBlockDashedName: block!.superBlock.dashedName,
superBlockName: block!.superBlock.name,
hasInternet: value,
),
),
),
);
}

void passChallenge(Challenge? challenge) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
if (challenge != null) {
Expand Down

0 comments on commit 1f49dd5

Please sign in to comment.