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 quiz = "/quiz";
|
||||
static const String userQuiz = "/quiz/user";
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
class BaseResponseModel<T> {
|
||||
final String message;
|
||||
final T? data;
|
||||
final dynamic meta;
|
||||
final MetaModel? meta;
|
||||
|
||||
BaseResponseModel({
|
||||
required this.message,
|
||||
|
@ -11,12 +11,44 @@ class BaseResponseModel<T> {
|
|||
|
||||
factory BaseResponseModel.fromJson(
|
||||
Map<String, dynamic> json,
|
||||
T Function(Map<String, dynamic>) fromJsonT,
|
||||
T Function(dynamic) fromJsonT,
|
||||
) {
|
||||
return BaseResponseModel<T>(
|
||||
message: json['message'],
|
||||
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: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/quiz/library_quiz_model.dart';
|
||||
import 'package:quiz_app/data/models/quiz/question_create_request.dart';
|
||||
import 'package:quiz_app/data/providers/dio_client.dart';
|
||||
|
||||
|
@ -26,7 +29,24 @@ class QuizService extends GetxService {
|
|||
throw Exception("Quiz creation failed");
|
||||
}
|
||||
} catch (e) {
|
||||
logC.e("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: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';
|
||||
|
||||
class LibraryBinding extends Bindings {
|
||||
@override
|
||||
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: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 {
|
||||
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
|
||||
void onInit() {
|
||||
loadUserQuiz();
|
||||
super.onInit();
|
||||
loadDummyQuiz();
|
||||
}
|
||||
|
||||
void loadDummyQuiz() {
|
||||
quizList.assignAll([
|
||||
{
|
||||
"author_id": "user_12345",
|
||||
"title": "Sejarah Indonesia - Kerajaan Hindu Budha",
|
||||
"description": "Kuis ini membahas kerajaan-kerajaan Hindu Budha di Indonesia seperti Kutai, Sriwijaya, dan Majapahit.",
|
||||
"is_public": true,
|
||||
"date": "2025-04-25 10:00:00",
|
||||
"total_quiz": 3,
|
||||
"limit_duration": 900,
|
||||
},
|
||||
// Tambahkan data dummy lain kalau mau
|
||||
]);
|
||||
void loadUserQuiz() async {
|
||||
try {
|
||||
isLoading.value = true;
|
||||
List<QuizData> data = await _quizService.userQuiz(_userController.userData!.id, currentPage);
|
||||
if (data.isEmpty) {
|
||||
emptyMessage.value = "Kamu belum membuat soal.";
|
||||
} else {
|
||||
quizs.addAll(data);
|
||||
}
|
||||
} catch (e) {
|
||||
emptyMessage.value = "Terjadi kesalahan saat memuat data.";
|
||||
} finally {
|
||||
isLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
String formatDuration(int seconds) {
|
||||
|
@ -32,7 +44,7 @@ class LibraryController extends GetxController {
|
|||
String formatDate(String dateString) {
|
||||
try {
|
||||
// DateTime date = DateTime.parse(dateString);
|
||||
return "19-04-2025";
|
||||
return "19-04-2025"; // Ini kamu hardcode, pastikan nanti parse bener
|
||||
} catch (e) {
|
||||
return '-';
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import 'package:flutter/material.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';
|
||||
|
||||
class LibraryView extends GetView<LibraryController> {
|
||||
|
@ -33,15 +34,40 @@ class LibraryView extends GetView<LibraryController> {
|
|||
),
|
||||
const SizedBox(height: 20),
|
||||
Expanded(
|
||||
child: Obx(
|
||||
() => ListView.builder(
|
||||
itemCount: controller.quizList.length,
|
||||
child: Obx(() {
|
||||
if (controller.isLoading.value) {
|
||||
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) {
|
||||
final quiz = controller.quizList[index];
|
||||
final quiz = controller.quizs[index];
|
||||
return _buildQuizCard(quiz);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
@ -50,7 +76,7 @@ class LibraryView extends GetView<LibraryController> {
|
|||
);
|
||||
}
|
||||
|
||||
Widget _buildQuizCard(Map<String, dynamic> quiz) {
|
||||
Widget _buildQuizCard(QuizData quiz) {
|
||||
return Container(
|
||||
margin: const EdgeInsets.only(bottom: 16),
|
||||
padding: const EdgeInsets.all(16),
|
||||
|
@ -82,7 +108,7 @@ class LibraryView extends GetView<LibraryController> {
|
|||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
quiz['title'] ?? '-',
|
||||
quiz.title,
|
||||
style: const TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: 16,
|
||||
|
@ -93,7 +119,7 @@ class LibraryView extends GetView<LibraryController> {
|
|||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
quiz['description'] ?? '-',
|
||||
quiz.description ?? "",
|
||||
style: const TextStyle(
|
||||
color: Colors.grey,
|
||||
fontSize: 12,
|
||||
|
@ -107,21 +133,21 @@ class LibraryView extends GetView<LibraryController> {
|
|||
const Icon(Icons.calendar_today_rounded, size: 14, color: Colors.grey),
|
||||
const SizedBox(width: 4),
|
||||
Text(
|
||||
controller.formatDate(quiz['date']),
|
||||
controller.formatDate(quiz.date ?? ""),
|
||||
style: const TextStyle(fontSize: 12, color: Colors.grey),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
const Icon(Icons.list, size: 14, color: Colors.grey),
|
||||
const SizedBox(width: 4),
|
||||
Text(
|
||||
'${quiz['total_quiz']} Quizzes',
|
||||
'${quiz.totalQuiz} Quizzes',
|
||||
style: const TextStyle(fontSize: 12, color: Colors.grey),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
const Icon(Icons.access_time, size: 14, color: Colors.grey),
|
||||
const SizedBox(width: 4),
|
||||
Text(
|
||||
controller.formatDuration(quiz['limit_duration'] ?? 0),
|
||||
controller.formatDuration(quiz.limitDuration),
|
||||
style: const TextStyle(fontSize: 12, color: Colors.grey),
|
||||
),
|
||||
],
|
||||
|
|
|
@ -74,23 +74,28 @@ class QuizPreviewController extends GetxController {
|
|||
List<QuestionListing> _mapQuestionsToListings(List<QuestionData> questions) {
|
||||
return questions.map((q) {
|
||||
String typeString;
|
||||
String answer = "";
|
||||
switch (q.type) {
|
||||
case QuestionType.fillTheBlank:
|
||||
typeString = 'fill_the_blank';
|
||||
answer = q.answer ?? "";
|
||||
break;
|
||||
case QuestionType.option:
|
||||
typeString = 'option';
|
||||
answer = q.correctAnswerIndex.toString();
|
||||
break;
|
||||
case QuestionType.trueOrFalse:
|
||||
typeString = 'true_false';
|
||||
answer = q.answer ?? "";
|
||||
break;
|
||||
default:
|
||||
typeString = 'fill_the_blank';
|
||||
answer = q.answer ?? "";
|
||||
}
|
||||
|
||||
return QuestionListing(
|
||||
question: q.question ?? '',
|
||||
targetAnswer: q.answer ?? '',
|
||||
targetAnswer: answer,
|
||||
duration: 30,
|
||||
type: typeString,
|
||||
options: q.options?.map((o) => o.text).toList(),
|
||||
|
|
Loading…
Reference in New Issue