95 lines
2.8 KiB
Dart
95 lines
2.8 KiB
Dart
import 'dart:convert';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:siro_rider/constant/links.dart';
|
|
import 'package:siro_rider/controller/functions/crud.dart';
|
|
|
|
class InvitesRewardsController extends GetxController {
|
|
bool isLoading = false;
|
|
String? referralCode;
|
|
int totalInvitedDrivers = 0;
|
|
int totalInvitedPassengers = 0;
|
|
List<dynamic> referrals = [];
|
|
|
|
@override
|
|
void onInit() {
|
|
super.onInit();
|
|
fetchPassengerReferrals();
|
|
}
|
|
|
|
Future<void> fetchPassengerReferrals() async {
|
|
isLoading = true;
|
|
update();
|
|
|
|
try {
|
|
var response = await CRUD().post(
|
|
link: AppLink.getPassengerReferrals,
|
|
payload: {} // Token is automatically handled by CRUD()
|
|
);
|
|
|
|
if (response != 'failure') {
|
|
var data = jsonDecode(response);
|
|
if (data['status'] == 'success') {
|
|
referralCode = data['message']['referral_code'];
|
|
totalInvitedDrivers = data['message']['total_invited_drivers'] ?? 0;
|
|
totalInvitedPassengers = data['message']['total_invited_passengers'] ?? 0;
|
|
referrals = data['message']['referrals'] ?? [];
|
|
} else {
|
|
referrals = [];
|
|
}
|
|
}
|
|
} catch (e) {
|
|
print("Error fetching passenger referrals: $e");
|
|
}
|
|
|
|
isLoading = false;
|
|
update();
|
|
}
|
|
|
|
Future<void> processScannedQRCode(String code) async {
|
|
if (code.contains('inviteCode=')) {
|
|
Uri uri = Uri.parse(code);
|
|
String? inviteCode = uri.queryParameters['inviteCode'];
|
|
|
|
if (inviteCode != null && inviteCode.isNotEmpty) {
|
|
await linkInviteCode(inviteCode);
|
|
} else {
|
|
Get.snackbar("Error".tr, "Invalid QR Code".tr);
|
|
}
|
|
} else if (code.length >= 4 && code.length <= 15) {
|
|
await linkInviteCode(code);
|
|
} else {
|
|
Get.snackbar("Error".tr, "Invalid QR Code format".tr);
|
|
}
|
|
}
|
|
|
|
Future<void> linkInviteCode(String inviteCode) async {
|
|
Get.dialog(const Center(child: CircularProgressIndicator()), barrierDismissible: false);
|
|
|
|
try {
|
|
var response = await CRUD().post(
|
|
link: AppLink.addUnifiedInvite,
|
|
payload: {
|
|
"inviter_code": inviteCode,
|
|
}
|
|
);
|
|
|
|
Get.back(); // close loading
|
|
|
|
if (response != 'failure') {
|
|
var data = jsonDecode(response);
|
|
if (data['status'] == 'success') {
|
|
Get.snackbar("Success".tr, "You have been successfully referred!".tr, backgroundColor: Colors.green, colorText: Colors.white);
|
|
} else {
|
|
Get.snackbar("Notice".tr, data['message'] ?? "Could not add invite".tr);
|
|
}
|
|
} else {
|
|
Get.snackbar("Error".tr, "Network error occurred".tr);
|
|
}
|
|
} catch (e) {
|
|
Get.back(); // close loading
|
|
Get.snackbar("Error".tr, "Network error occurred".tr);
|
|
}
|
|
}
|
|
}
|