fix: notification setup

This commit is contained in:
akhdanre 2025-05-23 13:42:51 +07:00
parent e3d2cbb7a6
commit bfd959a5df
5 changed files with 52 additions and 34 deletions

View File

@ -11,11 +11,13 @@ class TrueFalseQuestion extends BaseQuestionModel {
}) : super(type: 'true_false');
factory TrueFalseQuestion.fromJson(Map<String, dynamic> json) {
print(json['target_answer']);
return TrueFalseQuestion(
index: json['index'],
question: json['question'],
duration: json['duration'],
targetAnswer: json['target_answer'],
targetAnswer: json['target_answer'].toString().toLowerCase() == 'true',
);
}

View File

@ -17,47 +17,58 @@ class AuthService extends GetxService {
}
Future<bool> register(RegisterRequestModel request) async {
var data = await dio.post(
APIEndpoint.register,
data: request.toJson(),
);
if (data.statusCode == 200) {
return true;
} else {
throw Exception("Registration failed");
try {
final response = await dio.post(
APIEndpoint.register,
data: request.toJson(),
);
return response.statusCode == 200;
} on DioException catch (e) {
if (e.response?.statusCode == 409) {
// Status 409 = Conflict = User already exists
throw Exception("Email sudah dipakai");
}
// Other Dio errors
final errorMessage = e.response?.data['message'] ?? "Pendaftaran gagal";
throw Exception(errorMessage);
} catch (e) {
throw Exception("Terjadi kesalahan saat mendaftar");
}
}
Future<LoginResponseModel> loginWithEmail(LoginRequestModel request) async {
final data = request.toJson();
final response = await dio.post(APIEndpoint.login, data: data);
try {
final data = request.toJson();
final response = await dio.post(APIEndpoint.login, data: data);
if (response.statusCode == 200) {
print(response.data);
final baseResponse = BaseResponseModel<LoginResponseModel>.fromJson(
response.data,
(json) => LoginResponseModel.fromJson(json),
);
return baseResponse.data!;
} else {
throw Exception("Login failed");
} on DioException catch (e) {
final errorMessage = e.response?.data['message'] ?? "Login gagal";
throw Exception(errorMessage);
}
}
Future<LoginResponseModel> loginWithGoogle(String idToken) async {
final response = await dio.post(
APIEndpoint.loginGoogle,
data: {"token_id": idToken},
);
try {
final response = await dio.post(
APIEndpoint.loginGoogle,
data: {"token_id": idToken},
);
if (response.statusCode == 200) {
final baseResponse = BaseResponseModel<LoginResponseModel>.fromJson(
response.data,
(json) => LoginResponseModel.fromJson(json),
);
return baseResponse.data!;
} else {
throw Exception("Google login failed");
} on DioException catch (e) {
final errorMessage = e.response?.data['message'] ?? "Login Google gagal";
throw Exception(errorMessage);
}
}
}

View File

@ -77,7 +77,7 @@ class DetailQuizView extends GetView<DetailQuizController> {
GlobalButton(text: "Kerjakan", onPressed: controller.goToPlayPage),
const SizedBox(height: 20),
GlobalButton(text: "buat ruangan", onPressed: () {}),
// GlobalButton(text: "buat ruangan", onPressed: () {}),
const SizedBox(height: 20),
const Divider(thickness: 1.2, color: AppColors.borderLight),

View File

@ -55,7 +55,7 @@ class LoginController extends GetxController {
final password = passwordController.text.trim();
if (email.isEmpty || password.isEmpty) {
Get.snackbar("Error", "Email and password are required");
Get.snackbar("Kesalahan", "Email dan kata sandi wajib diisi");
return;
}
@ -75,7 +75,7 @@ class LoginController extends GetxController {
Get.offAllNamed(AppRoutes.mainPage);
} catch (e, stackTrace) {
logC.e(e, stackTrace: stackTrace);
CustomNotification.error(title: "failed", message: "Check username and password");
CustomNotification.error(title: "Gagal", message: "Periksa kembali email dan kata sandi Anda");
} finally {
isLoading.value = false;
}
@ -86,13 +86,13 @@ class LoginController extends GetxController {
try {
final user = await _googleAuthService.signIn();
if (user == null) {
Get.snackbar("Error", "Google Sign-In canceled");
Get.snackbar("Kesalahan", "Masuk dengan Google dibatalkan");
return;
}
final idToken = await user.authentication.then((auth) => auth.idToken);
if (idToken == null || idToken.isEmpty) {
Get.snackbar("Error", "No ID Token received.");
Get.snackbar("Kesalahan", "Tidak menerima ID Token dari Google");
return;
}

View File

@ -37,26 +37,27 @@ class RegisterController extends GetxController {
String phone = phoneController.text.trim();
if (email.isEmpty || password.isEmpty || confirmPassword.isEmpty || name.isEmpty || birthDate.isEmpty) {
Get.snackbar("Error", "All fields are required");
CustomNotification.error(title: "Kesalahan", message: "Semua data harus diisi");
return;
}
if (!_isValidEmail(email)) {
Get.snackbar("Error", "Invalid email format");
CustomNotification.error(title: "Kesalahan", message: "Format email tidak valid");
return;
}
if (!_isValidDateFormat(birthDate)) {
Get.snackbar("Error", "Invalid date format. Use dd-mm-yyyy");
CustomNotification.error(title: "Kesalahan", message: "Format tanggal tidak valid. Gunakan format seperti ini 12-09-2003");
return;
}
if (password != confirmPassword) {
Get.snackbar("Error", "Passwords do not match");
CustomNotification.error(title: "Kesalahan", message: "Kata sandi tidak cocok");
return;
}
if (phone.isNotEmpty && (phone.length < 10 || phone.length > 13)) {
Get.snackbar("Error", "Phone number must be between 10 and 13 digits");
CustomNotification.error(title: "Kesalahan", message: "Nomor telepon harus terdiri dari 10 hingga 13 digit");
return;
}
@ -74,9 +75,13 @@ class RegisterController extends GetxController {
Get.back();
CustomFloatingLoading.hideLoadingDialog(Get.context!);
CustomNotification.success(title: "register success", message: "created account successfuly");
CustomNotification.success(title: "Pendaftaran Berhasil", message: "Akun berhasil dibuat");
} catch (e) {
Get.snackbar("Error", "Failed to register: ${e.toString()}");
CustomFloatingLoading.hideLoadingDialog(Get.context!);
String errorMessage = e.toString().replaceFirst("Exception: ", "");
CustomNotification.error(title: "Pendaftaran gagal", message: errorMessage);
}
}