feat: working on library data from network done
This commit is contained in:
parent
e4ac170a21
commit
5b1f579b13
|
@ -7,4 +7,5 @@ class APIEndpoint {
|
||||||
static const String register = "/register";
|
static const String register = "/register";
|
||||||
|
|
||||||
static const String quiz = "/quiz";
|
static const String quiz = "/quiz";
|
||||||
|
static const String userQuiz = "/quiz/user";
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
class BaseResponseModel<T> {
|
class BaseResponseModel<T> {
|
||||||
final String message;
|
final String message;
|
||||||
final T? data;
|
final T? data;
|
||||||
final dynamic meta;
|
final MetaModel? meta;
|
||||||
|
|
||||||
BaseResponseModel({
|
BaseResponseModel({
|
||||||
required this.message,
|
required this.message,
|
||||||
|
@ -11,12 +11,44 @@ class BaseResponseModel<T> {
|
||||||
|
|
||||||
factory BaseResponseModel.fromJson(
|
factory BaseResponseModel.fromJson(
|
||||||
Map<String, dynamic> json,
|
Map<String, dynamic> json,
|
||||||
T Function(Map<String, dynamic>) fromJsonT,
|
T Function(dynamic) fromJsonT,
|
||||||
) {
|
) {
|
||||||
return BaseResponseModel<T>(
|
return BaseResponseModel<T>(
|
||||||
message: json['message'],
|
message: json['message'],
|
||||||
data: json['data'] != null ? fromJsonT(json['data']) : null,
|
data: json['data'] != null ? fromJsonT(json['data']) : null,
|
||||||
meta: json['meta'],
|
meta: json['meta'] != null ? MetaModel.fromJson(json['meta']) : null,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class MetaModel {
|
||||||
|
final int totalPage;
|
||||||
|
final int currentPage;
|
||||||
|
final int totalData;
|
||||||
|
final int totalAllData;
|
||||||
|
|
||||||
|
MetaModel({
|
||||||
|
required this.totalPage,
|
||||||
|
required this.currentPage,
|
||||||
|
required this.totalData,
|
||||||
|
required this.totalAllData,
|
||||||
|
});
|
||||||
|
|
||||||
|
factory MetaModel.fromJson(Map<String, dynamic> json) {
|
||||||
|
return MetaModel(
|
||||||
|
totalPage: json['total_page'],
|
||||||
|
currentPage: json['current_page'],
|
||||||
|
totalData: json['total_data'],
|
||||||
|
totalAllData: json['total_all_data'],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, dynamic> toJson() {
|
||||||
|
return {
|
||||||
|
'total_page': totalPage,
|
||||||
|
'current_page': currentPage,
|
||||||
|
'total_data': totalData,
|
||||||
|
'total_all_data': totalAllData,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,83 @@
|
||||||
|
class QuizData {
|
||||||
|
final String authorId;
|
||||||
|
final String title;
|
||||||
|
final String? description;
|
||||||
|
final bool isPublic;
|
||||||
|
final String? date;
|
||||||
|
final int totalQuiz;
|
||||||
|
final int limitDuration;
|
||||||
|
final List<QuestionListing> questionListings;
|
||||||
|
|
||||||
|
QuizData({
|
||||||
|
required this.authorId,
|
||||||
|
required this.title,
|
||||||
|
this.description,
|
||||||
|
required this.isPublic,
|
||||||
|
this.date,
|
||||||
|
required this.totalQuiz,
|
||||||
|
required this.limitDuration,
|
||||||
|
required this.questionListings,
|
||||||
|
});
|
||||||
|
|
||||||
|
factory QuizData.fromJson(Map<String, dynamic> json) {
|
||||||
|
return QuizData(
|
||||||
|
authorId: json['author_id'],
|
||||||
|
title: json['title'],
|
||||||
|
description: json['description'],
|
||||||
|
isPublic: json['is_public'],
|
||||||
|
date: json['date'],
|
||||||
|
totalQuiz: json['total_quiz'],
|
||||||
|
limitDuration: json['limit_duration'],
|
||||||
|
questionListings: (json['question_listings'] as List).map((e) => QuestionListing.fromJson(e)).toList(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, dynamic> toJson() {
|
||||||
|
return {
|
||||||
|
'author_id': authorId,
|
||||||
|
'title': title,
|
||||||
|
'description': description,
|
||||||
|
'is_public': isPublic,
|
||||||
|
'date': date,
|
||||||
|
'total_quiz': totalQuiz,
|
||||||
|
'limit_duration': limitDuration,
|
||||||
|
'question_listings': questionListings.map((e) => e.toJson()).toList(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class QuestionListing {
|
||||||
|
final String question;
|
||||||
|
final String targetAnswer;
|
||||||
|
final int duration;
|
||||||
|
final String type;
|
||||||
|
final List<String>? options;
|
||||||
|
|
||||||
|
QuestionListing({
|
||||||
|
required this.question,
|
||||||
|
required this.targetAnswer,
|
||||||
|
required this.duration,
|
||||||
|
required this.type,
|
||||||
|
this.options,
|
||||||
|
});
|
||||||
|
|
||||||
|
factory QuestionListing.fromJson(Map<String, dynamic> json) {
|
||||||
|
return QuestionListing(
|
||||||
|
question: json['question'],
|
||||||
|
targetAnswer: json['target_answer'],
|
||||||
|
duration: json['duration'],
|
||||||
|
type: json['type'],
|
||||||
|
options: json['options'] != null ? List<String>.from(json['options']) : null,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, dynamic> toJson() {
|
||||||
|
return {
|
||||||
|
'question': question,
|
||||||
|
'target_answer': targetAnswer,
|
||||||
|
'duration': duration,
|
||||||
|
'type': type,
|
||||||
|
'options': options,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,9 @@
|
||||||
import 'package:dio/dio.dart';
|
import 'package:dio/dio.dart';
|
||||||
import 'package:get/get.dart';
|
import 'package:get/get.dart';
|
||||||
import 'package:quiz_app/core/endpoint/api_endpoint.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/quiz/library_quiz_model.dart';
|
||||||
import 'package:quiz_app/data/models/quiz/question_create_request.dart';
|
import 'package:quiz_app/data/models/quiz/question_create_request.dart';
|
||||||
import 'package:quiz_app/data/providers/dio_client.dart';
|
import 'package:quiz_app/data/providers/dio_client.dart';
|
||||||
|
|
||||||
|
@ -26,7 +29,24 @@ class QuizService extends GetxService {
|
||||||
throw Exception("Quiz creation failed");
|
throw Exception("Quiz creation failed");
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
logC.e("Quiz creation error: $e");
|
||||||
throw Exception("Quiz creation error: $e");
|
throw Exception("Quiz creation error: $e");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<List<QuizData>> userQuiz(String userId, int page) async {
|
||||||
|
try {
|
||||||
|
final response = await _dio.get("${APIEndpoint.userQuiz}/$userId?page=$page");
|
||||||
|
|
||||||
|
final parsedResponse = BaseResponseModel<List<QuizData>>.fromJson(
|
||||||
|
response.data,
|
||||||
|
(data) => (data as List).map((e) => QuizData.fromJson(e as Map<String, dynamic>)).toList(),
|
||||||
|
);
|
||||||
|
|
||||||
|
return parsedResponse.data ?? [];
|
||||||
|
} catch (e) {
|
||||||
|
logC.e("Error fetching user quizzes: $e");
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,13 @@
|
||||||
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/quiz_service.dart';
|
||||||
import 'package:quiz_app/feature/library/controller/library_controller.dart';
|
import 'package:quiz_app/feature/library/controller/library_controller.dart';
|
||||||
|
|
||||||
class LibraryBinding extends Bindings {
|
class LibraryBinding extends Bindings {
|
||||||
@override
|
@override
|
||||||
void dependencies() {
|
void dependencies() {
|
||||||
Get.lazyPut<LibraryController>(() => LibraryController());
|
Get.lazyPut<QuizService>(() => QuizService());
|
||||||
|
|
||||||
|
Get.lazyPut<LibraryController>(() => LibraryController(Get.find<QuizService>(), Get.find<UserController>()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,27 +1,39 @@
|
||||||
import 'package:get/get.dart';
|
import 'package:get/get.dart';
|
||||||
|
import 'package:quiz_app/data/controllers/user_controller.dart';
|
||||||
|
import 'package:quiz_app/data/models/quiz/library_quiz_model.dart';
|
||||||
|
import 'package:quiz_app/data/services/quiz_service.dart';
|
||||||
|
|
||||||
class LibraryController extends GetxController {
|
class LibraryController extends GetxController {
|
||||||
RxList<Map<String, dynamic>> quizList = <Map<String, dynamic>>[].obs;
|
RxList<QuizData> quizs = <QuizData>[].obs;
|
||||||
|
RxBool isLoading = true.obs;
|
||||||
|
RxString emptyMessage = "".obs;
|
||||||
|
|
||||||
|
final QuizService _quizService;
|
||||||
|
final UserController _userController;
|
||||||
|
LibraryController(this._quizService, this._userController);
|
||||||
|
|
||||||
|
int currentPage = 1;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void onInit() {
|
void onInit() {
|
||||||
|
loadUserQuiz();
|
||||||
super.onInit();
|
super.onInit();
|
||||||
loadDummyQuiz();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loadDummyQuiz() {
|
void loadUserQuiz() async {
|
||||||
quizList.assignAll([
|
try {
|
||||||
{
|
isLoading.value = true;
|
||||||
"author_id": "user_12345",
|
List<QuizData> data = await _quizService.userQuiz(_userController.userData!.id, currentPage);
|
||||||
"title": "Sejarah Indonesia - Kerajaan Hindu Budha",
|
if (data.isEmpty) {
|
||||||
"description": "Kuis ini membahas kerajaan-kerajaan Hindu Budha di Indonesia seperti Kutai, Sriwijaya, dan Majapahit.",
|
emptyMessage.value = "Kamu belum membuat soal.";
|
||||||
"is_public": true,
|
} else {
|
||||||
"date": "2025-04-25 10:00:00",
|
quizs.addAll(data);
|
||||||
"total_quiz": 3,
|
}
|
||||||
"limit_duration": 900,
|
} catch (e) {
|
||||||
},
|
emptyMessage.value = "Terjadi kesalahan saat memuat data.";
|
||||||
// Tambahkan data dummy lain kalau mau
|
} finally {
|
||||||
]);
|
isLoading.value = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
String formatDuration(int seconds) {
|
String formatDuration(int seconds) {
|
||||||
|
@ -32,7 +44,7 @@ class LibraryController extends GetxController {
|
||||||
String formatDate(String dateString) {
|
String formatDate(String dateString) {
|
||||||
try {
|
try {
|
||||||
// DateTime date = DateTime.parse(dateString);
|
// DateTime date = DateTime.parse(dateString);
|
||||||
return "19-04-2025";
|
return "19-04-2025"; // Ini kamu hardcode, pastikan nanti parse bener
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return '-';
|
return '-';
|
||||||
}
|
}
|
||||||
|
|
|
@ -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/quiz/library_quiz_model.dart';
|
||||||
import 'package:quiz_app/feature/library/controller/library_controller.dart';
|
import 'package:quiz_app/feature/library/controller/library_controller.dart';
|
||||||
|
|
||||||
class LibraryView extends GetView<LibraryController> {
|
class LibraryView extends GetView<LibraryController> {
|
||||||
|
@ -33,15 +34,40 @@ class LibraryView extends GetView<LibraryController> {
|
||||||
),
|
),
|
||||||
const SizedBox(height: 20),
|
const SizedBox(height: 20),
|
||||||
Expanded(
|
Expanded(
|
||||||
child: Obx(
|
child: Obx(() {
|
||||||
() => ListView.builder(
|
if (controller.isLoading.value) {
|
||||||
itemCount: controller.quizList.length,
|
return const Center(
|
||||||
|
child: Column(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
children: [
|
||||||
|
CircularProgressIndicator(),
|
||||||
|
SizedBox(height: 12),
|
||||||
|
Text(
|
||||||
|
"Memuat data...",
|
||||||
|
style: TextStyle(color: Colors.grey, fontSize: 14),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (controller.quizs.isEmpty) {
|
||||||
|
return const Center(
|
||||||
|
child: Text(
|
||||||
|
"Belum ada soal tersedia.",
|
||||||
|
style: TextStyle(color: Colors.grey, fontSize: 14),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ListView.builder(
|
||||||
|
itemCount: controller.quizs.length,
|
||||||
itemBuilder: (context, index) {
|
itemBuilder: (context, index) {
|
||||||
final quiz = controller.quizList[index];
|
final quiz = controller.quizs[index];
|
||||||
return _buildQuizCard(quiz);
|
return _buildQuizCard(quiz);
|
||||||
},
|
},
|
||||||
),
|
);
|
||||||
),
|
}),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
@ -50,7 +76,7 @@ class LibraryView extends GetView<LibraryController> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget _buildQuizCard(Map<String, dynamic> quiz) {
|
Widget _buildQuizCard(QuizData quiz) {
|
||||||
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),
|
||||||
|
@ -82,7 +108,7 @@ class LibraryView extends GetView<LibraryController> {
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
children: [
|
children: [
|
||||||
Text(
|
Text(
|
||||||
quiz['title'] ?? '-',
|
quiz.title,
|
||||||
style: const TextStyle(
|
style: const TextStyle(
|
||||||
fontWeight: FontWeight.bold,
|
fontWeight: FontWeight.bold,
|
||||||
fontSize: 16,
|
fontSize: 16,
|
||||||
|
@ -93,7 +119,7 @@ class LibraryView extends GetView<LibraryController> {
|
||||||
),
|
),
|
||||||
const SizedBox(height: 4),
|
const SizedBox(height: 4),
|
||||||
Text(
|
Text(
|
||||||
quiz['description'] ?? '-',
|
quiz.description ?? "",
|
||||||
style: const TextStyle(
|
style: const TextStyle(
|
||||||
color: Colors.grey,
|
color: Colors.grey,
|
||||||
fontSize: 12,
|
fontSize: 12,
|
||||||
|
@ -107,21 +133,21 @@ class LibraryView extends GetView<LibraryController> {
|
||||||
const Icon(Icons.calendar_today_rounded, size: 14, color: Colors.grey),
|
const Icon(Icons.calendar_today_rounded, size: 14, color: Colors.grey),
|
||||||
const SizedBox(width: 4),
|
const SizedBox(width: 4),
|
||||||
Text(
|
Text(
|
||||||
controller.formatDate(quiz['date']),
|
controller.formatDate(quiz.date ?? ""),
|
||||||
style: const TextStyle(fontSize: 12, color: Colors.grey),
|
style: const TextStyle(fontSize: 12, color: Colors.grey),
|
||||||
),
|
),
|
||||||
const SizedBox(width: 12),
|
const SizedBox(width: 12),
|
||||||
const Icon(Icons.list, size: 14, color: Colors.grey),
|
const Icon(Icons.list, size: 14, color: Colors.grey),
|
||||||
const SizedBox(width: 4),
|
const SizedBox(width: 4),
|
||||||
Text(
|
Text(
|
||||||
'${quiz['total_quiz']} Quizzes',
|
'${quiz.totalQuiz} Quizzes',
|
||||||
style: const TextStyle(fontSize: 12, color: Colors.grey),
|
style: const TextStyle(fontSize: 12, color: Colors.grey),
|
||||||
),
|
),
|
||||||
const SizedBox(width: 12),
|
const SizedBox(width: 12),
|
||||||
const Icon(Icons.access_time, size: 14, color: Colors.grey),
|
const Icon(Icons.access_time, size: 14, color: Colors.grey),
|
||||||
const SizedBox(width: 4),
|
const SizedBox(width: 4),
|
||||||
Text(
|
Text(
|
||||||
controller.formatDuration(quiz['limit_duration'] ?? 0),
|
controller.formatDuration(quiz.limitDuration),
|
||||||
style: const TextStyle(fontSize: 12, color: Colors.grey),
|
style: const TextStyle(fontSize: 12, color: Colors.grey),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
|
|
@ -74,23 +74,28 @@ class QuizPreviewController extends GetxController {
|
||||||
List<QuestionListing> _mapQuestionsToListings(List<QuestionData> questions) {
|
List<QuestionListing> _mapQuestionsToListings(List<QuestionData> questions) {
|
||||||
return questions.map((q) {
|
return questions.map((q) {
|
||||||
String typeString;
|
String typeString;
|
||||||
|
String answer = "";
|
||||||
switch (q.type) {
|
switch (q.type) {
|
||||||
case QuestionType.fillTheBlank:
|
case QuestionType.fillTheBlank:
|
||||||
typeString = 'fill_the_blank';
|
typeString = 'fill_the_blank';
|
||||||
|
answer = q.answer ?? "";
|
||||||
break;
|
break;
|
||||||
case QuestionType.option:
|
case QuestionType.option:
|
||||||
typeString = 'option';
|
typeString = 'option';
|
||||||
|
answer = q.correctAnswerIndex.toString();
|
||||||
break;
|
break;
|
||||||
case QuestionType.trueOrFalse:
|
case QuestionType.trueOrFalse:
|
||||||
typeString = 'true_false';
|
typeString = 'true_false';
|
||||||
|
answer = q.answer ?? "";
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
typeString = 'fill_the_blank';
|
typeString = 'fill_the_blank';
|
||||||
|
answer = q.answer ?? "";
|
||||||
}
|
}
|
||||||
|
|
||||||
return QuestionListing(
|
return QuestionListing(
|
||||||
question: q.question ?? '',
|
question: q.question ?? '',
|
||||||
targetAnswer: q.answer ?? '',
|
targetAnswer: answer,
|
||||||
duration: 30,
|
duration: 30,
|
||||||
type: typeString,
|
type: typeString,
|
||||||
options: q.options?.map((o) => o.text).toList(),
|
options: q.options?.map((o) => o.text).toList(),
|
||||||
|
|
Loading…
Reference in New Issue