Skip to content

Commit

Permalink
Merge pull request #403 from DanXi-Dev/question_version
Browse files Browse the repository at this point in the history
feat: add version for forum answer question api
  • Loading branch information
w568w authored Aug 9, 2024
2 parents ad692f4 + 4883bc6 commit cea4014
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
7 changes: 5 additions & 2 deletions lib/page/forum/quiz.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class OTQuizWidgetState extends State<OTQuizWidget> {
int questionIndex = 0, prevQuestionIndex = 0;
int displayIndex = 1, displayTotalIndex = 1;
late List<QuizQuestion>? questions;
late int version;
late List<int>? indexes;
List<QuizAnswer>? answers;
OTQuizDisplayTypes displayType = OTQuizDisplayTypes.WELCOME_PAGE;
Expand Down Expand Up @@ -71,8 +72,10 @@ class OTQuizWidgetState extends State<OTQuizWidget> {
child: PlatformElevatedButton(
padding: const EdgeInsets.all(20.0),
onPressed: () async {
questions = await ForumRepository.getInstance()
final result = await ForumRepository.getInstance()
.getPostRegisterQuestions();
questions = result.$1;
version = result.$2;
if (questions != null) {
indexes =
List.generate(questions!.length, (index) => index);
Expand Down Expand Up @@ -182,7 +185,7 @@ class OTQuizWidgetState extends State<OTQuizWidget> {
return;
} else {
final errorList =
await ForumRepository.getInstance().submitAnswers(answers!);
await ForumRepository.getInstance().submitAnswers(answers!, version);

// Have trouble submitting
if (errorList == null) {
Expand Down
18 changes: 9 additions & 9 deletions lib/repository/forum/forum_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -833,26 +833,26 @@ class ForumRepository extends BaseRepositoryWithDio {
return resp.statusCode;
}

Future<List<QuizQuestion>?> getPostRegisterQuestions() async {
Future<(List<QuizQuestion>?, int)> getPostRegisterQuestions() async {
final Response<Map<String, dynamic>> response = await dio.get(
"$_BASE_AUTH_URL/register/questions",
options: Options(headers: _tokenHeader));
final questionList = response.data?["questions"]
final List<QuizQuestion>? questionList = response.data?["questions"]
.map((e) => QuizQuestion.fromJson(e))
.toList();
final length = response.data?["spec"]["number_of_questions"] as int;

assert(questionList?.length == length);
return questionList.cast<QuizQuestion>();
.toList()
.cast<QuizQuestion>();
final int version = response.data?["version"];
return (questionList, version);
}

// Empty list means all-correct
Future<List<int>?> submitAnswers(List<QuizAnswer> answers) async {
Future<List<int>?> submitAnswers(
List<QuizAnswer> answers, int version) async {
final Response<Map<String, dynamic>> response = await dio.post(
"$_BASE_AUTH_URL/register/questions/_answer",
data: {
"answers": answers.map((e) => e.toJson()).toList(),
"version": 0
"version": version
},
options: Options(headers: _tokenHeader));

Expand Down
8 changes: 4 additions & 4 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
name: dan_xi
description: Maybe the best all-rounded service app for Fudan University students.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
publish_to: "none" # Remove this line if you wish to publish to pub.dev
version: 1.4.4+341

environment:
sdk: '>=3.0.0'
sdk: ">=3.0.0"

dependencies:
flutter:
Expand Down Expand Up @@ -70,8 +70,8 @@ dependencies:
file_picker: ^8.0.6
cached_network_image: ^3.2.1
flutter_typeahead: ^5.2.0
collection: '>=1.15.0 <2.0.0'
meta: '>=1.3.0 <2.0.0'
collection: ">=1.15.0 <2.0.0"
meta: ">=1.3.0 <2.0.0"
flutter_layout_grid: ^2.0.1
flutter_js: ^0.8.0
flutter_math_fork: ^0.7.2
Expand Down

0 comments on commit cea4014

Please sign in to comment.