feat: working on the history binding
This commit is contained in:
parent
aa6b35f422
commit
14cd51c65b
|
@ -8,4 +8,6 @@ class APIEndpoint {
|
||||||
|
|
||||||
static const String quiz = "/quiz";
|
static const String quiz = "/quiz";
|
||||||
static const String userQuiz = "/quiz/user";
|
static const String userQuiz = "/quiz/user";
|
||||||
|
|
||||||
|
static const String historyQuiz = "/history";
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,42 @@
|
||||||
|
class QuizHistory {
|
||||||
|
final String quizId;
|
||||||
|
final String answerId;
|
||||||
|
final String title;
|
||||||
|
final String description;
|
||||||
|
final int totalCorrect;
|
||||||
|
final int totalQuestion;
|
||||||
|
final String date;
|
||||||
|
|
||||||
|
QuizHistory({
|
||||||
|
required this.quizId,
|
||||||
|
required this.answerId,
|
||||||
|
required this.title,
|
||||||
|
required this.description,
|
||||||
|
required this.totalCorrect,
|
||||||
|
required this.totalQuestion,
|
||||||
|
required this.date,
|
||||||
|
});
|
||||||
|
|
||||||
|
factory QuizHistory.fromJson(Map<String, dynamic> json) {
|
||||||
|
return QuizHistory(
|
||||||
|
quizId: json['quiz_id'],
|
||||||
|
answerId: json['answer_id'],
|
||||||
|
title: json['title'],
|
||||||
|
description: json['description'],
|
||||||
|
totalCorrect: json['total_correct'],
|
||||||
|
totalQuestion: json['total_question'],
|
||||||
|
date: json["date"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, dynamic> toJson() {
|
||||||
|
return {
|
||||||
|
'quiz_id': quizId,
|
||||||
|
'answer_id': answerId,
|
||||||
|
'title': title,
|
||||||
|
'description': description,
|
||||||
|
'total_correct': totalCorrect,
|
||||||
|
'total_question': totalQuestion,
|
||||||
|
'date': date
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,31 @@
|
||||||
|
import 'package:dio/dio.dart';
|
||||||
|
import 'package:get/get.dart';
|
||||||
|
import 'package:quiz_app/core/endpoint/api_endpoint.dart';
|
||||||
|
import 'package:quiz_app/core/utils/logger.dart';
|
||||||
|
import 'package:quiz_app/data/models/base/base_model.dart';
|
||||||
|
import 'package:quiz_app/data/models/history/quiz_history.dart';
|
||||||
|
import 'package:quiz_app/data/providers/dio_client.dart';
|
||||||
|
|
||||||
|
class HistoryService extends GetxService {
|
||||||
|
late final Dio _dio;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void onInit() {
|
||||||
|
_dio = Get.find<ApiClient>().dio;
|
||||||
|
super.onInit();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<List<QuizHistory>?> getHistory(String userId) async {
|
||||||
|
try {
|
||||||
|
final result = await _dio.get("${APIEndpoint.historyQuiz}/$userId");
|
||||||
|
|
||||||
|
final parsedResponse = BaseResponseModel<List<QuizHistory>>.fromJson(
|
||||||
|
result.data,
|
||||||
|
(data) => (data as List).map((e) => QuizHistory.fromJson(e as Map<String, dynamic>)).toList(),
|
||||||
|
);
|
||||||
|
return parsedResponse.data;
|
||||||
|
} catch (e, stacktrace) {
|
||||||
|
logC.e(e, stackTrace: stacktrace);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,9 +1,12 @@
|
||||||
import 'package:get/get.dart';
|
import 'package:get/get.dart';
|
||||||
|
import 'package:quiz_app/data/controllers/user_controller.dart';
|
||||||
|
import 'package:quiz_app/data/services/history_service.dart';
|
||||||
import 'package:quiz_app/feature/history/controller/history_controller.dart';
|
import 'package:quiz_app/feature/history/controller/history_controller.dart';
|
||||||
|
|
||||||
class HistoryBinding extends Bindings {
|
class HistoryBinding extends Bindings {
|
||||||
@override
|
@override
|
||||||
void dependencies() {
|
void dependencies() {
|
||||||
Get.lazyPut(() => HistoryController());
|
Get.lazyPut<HistoryService>(() => HistoryService());
|
||||||
|
Get.lazyPut(() => HistoryController(Get.find<HistoryService>(), Get.find<UserController>()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,23 +1,15 @@
|
||||||
import 'package:get/get.dart';
|
import 'package:get/get.dart';
|
||||||
|
import 'package:quiz_app/data/controllers/user_controller.dart';
|
||||||
class HistoryItem {
|
import 'package:quiz_app/data/models/history/quiz_history.dart';
|
||||||
final String title;
|
import 'package:quiz_app/data/services/history_service.dart';
|
||||||
final String date;
|
|
||||||
final int score;
|
|
||||||
final int totalQuestions;
|
|
||||||
final String duration;
|
|
||||||
|
|
||||||
HistoryItem({
|
|
||||||
required this.title,
|
|
||||||
required this.date,
|
|
||||||
required this.score,
|
|
||||||
required this.totalQuestions,
|
|
||||||
required this.duration,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
class HistoryController extends GetxController {
|
class HistoryController extends GetxController {
|
||||||
final historyList = <HistoryItem>[].obs;
|
HistoryService _historyService;
|
||||||
|
UserController _userController;
|
||||||
|
|
||||||
|
HistoryController(this._historyService, this._userController);
|
||||||
|
|
||||||
|
final historyList = <QuizHistory>[].obs;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void onInit() {
|
void onInit() {
|
||||||
|
@ -25,22 +17,7 @@ class HistoryController extends GetxController {
|
||||||
loadDummyHistory();
|
loadDummyHistory();
|
||||||
}
|
}
|
||||||
|
|
||||||
void loadDummyHistory() {
|
void loadDummyHistory() async {
|
||||||
historyList.value = [
|
historyList.value = await _historyService.getHistory(_userController.userData!.id) ?? [];
|
||||||
HistoryItem(
|
|
||||||
title: "Fisika Dasar",
|
|
||||||
date: "24 April 2025",
|
|
||||||
score: 8,
|
|
||||||
totalQuestions: 10,
|
|
||||||
duration: "5m 21s",
|
|
||||||
),
|
|
||||||
HistoryItem(
|
|
||||||
title: "Sejarah Indonesia",
|
|
||||||
date: "22 April 2025",
|
|
||||||
score: 7,
|
|
||||||
totalQuestions: 10,
|
|
||||||
duration: "4m 35s",
|
|
||||||
),
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:get/get.dart';
|
import 'package:get/get.dart';
|
||||||
|
import 'package:quiz_app/data/models/history/quiz_history.dart';
|
||||||
import 'package:quiz_app/feature/history/controller/history_controller.dart';
|
import 'package:quiz_app/feature/history/controller/history_controller.dart';
|
||||||
|
|
||||||
class HistoryView extends GetView<HistoryController> {
|
class HistoryView extends GetView<HistoryController> {
|
||||||
|
@ -60,7 +61,7 @@ class HistoryView extends GetView<HistoryController> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget _buildHistoryCard(HistoryItem item) {
|
Widget _buildHistoryCard(QuizHistory item) {
|
||||||
return Container(
|
return Container(
|
||||||
margin: const EdgeInsets.only(bottom: 16),
|
margin: const EdgeInsets.only(bottom: 16),
|
||||||
padding: const EdgeInsets.all(16),
|
padding: const EdgeInsets.all(16),
|
||||||
|
@ -106,14 +107,14 @@ class HistoryView extends GetView<HistoryController> {
|
||||||
const Icon(Icons.check_circle, size: 14, color: Colors.green),
|
const Icon(Icons.check_circle, size: 14, color: Colors.green),
|
||||||
const SizedBox(width: 4),
|
const SizedBox(width: 4),
|
||||||
Text(
|
Text(
|
||||||
"Skor: ${item.score}/${item.totalQuestions}",
|
"Skor: ${item.totalCorrect}/${item.totalQuestion}",
|
||||||
style: const TextStyle(fontSize: 12),
|
style: const TextStyle(fontSize: 12),
|
||||||
),
|
),
|
||||||
const SizedBox(width: 16),
|
const SizedBox(width: 16),
|
||||||
const Icon(Icons.timer, size: 14, color: Colors.grey),
|
const Icon(Icons.timer, size: 14, color: Colors.grey),
|
||||||
const SizedBox(width: 4),
|
const SizedBox(width: 4),
|
||||||
Text(
|
Text(
|
||||||
item.duration,
|
"3 menit",
|
||||||
style: const TextStyle(fontSize: 12),
|
style: const TextStyle(fontSize: 12),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
|
|
@ -136,6 +136,7 @@ class QuizPlayController extends GetxController {
|
||||||
|
|
||||||
void _finishQuiz() async {
|
void _finishQuiz() async {
|
||||||
_timer?.cancel();
|
_timer?.cancel();
|
||||||
|
|
||||||
AppDialog.showMessage(Get.context!, "Yeay semua soal selesai");
|
AppDialog.showMessage(Get.context!, "Yeay semua soal selesai");
|
||||||
await Future.delayed(Duration(seconds: 2));
|
await Future.delayed(Duration(seconds: 2));
|
||||||
Get.offAllNamed(
|
Get.offAllNamed(
|
||||||
|
|
Loading…
Reference in New Issue