Files
Vocasia-LMS-Mobile-apps--TA…/lib/providers/radeem_voucher_provider.dart

104 lines
2.7 KiB
Dart

import 'package:flutter/material.dart';
import 'package:initial_folder/models/voucher_model.dart';
import 'package:initial_folder/services/voucher_service.dart';
enum ResultState { loading, failed, success, empty }
class RadeemVoucherProvider with ChangeNotifier {
ResultState _state = ResultState.loading;
String? _message;
String? _messageCart;
String? _messageCancel;
ResultState get state => _state;
String? get message => _message;
String? get messageCart => _messageCart;
String? get messageCancel => _messageCancel;
VoucherModel? _detailKupon;
VoucherModel? get result => _detailKupon;
set detailKupon(VoucherModel? detail) {
_detailKupon = detail;
notifyListeners();
}
void clearState() {
_state = ResultState.empty;
notifyListeners();
}
Future<dynamic> radeemVoucher(int? idCourse, String voucher) async {
try {
_state = ResultState.loading;
notifyListeners();
VoucherModel? response =
await VoucherService().radeemVoucher(idCourse, voucher);
if (response.status == 200) {
_state = ResultState.success;
print("Berhasil kupon di provider ${_detailKupon}");
notifyListeners();
return _detailKupon = response;
// notifyListeners();
} else {
_state = ResultState.failed;
print("Gagal kupon di provider 1 ${_detailKupon}");
notifyListeners();
return _detailKupon = response;
}
} catch (e) {
_state = ResultState.failed;
print("Gagal kupon di provider ${e}");
_message = 'Error --> $e';
notifyListeners();
return null;
}
}
Future redeemVoucherCart(List<String> idCourse, String voucher) async {
try {
_state = ResultState.loading;
notifyListeners();
VoucherModel response =
await VoucherService().redeemVoucherCart(idCourse, voucher);
if (response.status == 200) {
_state = ResultState.success;
notifyListeners();
return true;
// notifyListeners();
} else {
_state = ResultState.failed;
notifyListeners();
return false;
}
} catch (e) {
_state = ResultState.failed;
notifyListeners();
return false;
}
}
Future<bool> deleteCoupon(_coupon) async {
try {
VoucherModel deleteCoupon = await VoucherService().cancelCoupon(_coupon);
if (deleteCoupon.status == 200) {
_state = ResultState.success;
notifyListeners();
notifyListeners();
} else if (deleteCoupon.status == 404) {
_state = ResultState.failed;
notifyListeners();
}
return true;
} catch (e) {
print("Exceptions: $e");
return false;
}
}
}