Update: 2026-06-10 02:44:54
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user