first commit
This commit is contained in:
135
lib/controllers/account_controller/profile.dart
Normal file
135
lib/controllers/account_controller/profile.dart
Normal file
@@ -0,0 +1,135 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:in_app_review/in_app_review.dart';
|
||||
import 'package:package_info_plus/package_info_plus.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:url_launcher/url_launcher.dart';
|
||||
|
||||
import '../../domain/repository/authentication/auth_repository.dart';
|
||||
|
||||
class AccountController extends GetxController {
|
||||
var isLoading = true.obs;
|
||||
var profileName = ''.obs;
|
||||
var profileImage = ''.obs;
|
||||
var profilePhone = ''.obs;
|
||||
var appVersion = ''.obs;
|
||||
|
||||
final Dio _dio = Dio();
|
||||
var Name = ''.obs;
|
||||
var Adress = ''.obs;
|
||||
var Profile = ''.obs;
|
||||
var Number = ''.obs;
|
||||
@override
|
||||
void onInit() {
|
||||
super.onInit();
|
||||
fetchProfileFromAPI();
|
||||
loadAppVersion();
|
||||
loadUserDetails();
|
||||
_loadProfile();
|
||||
}
|
||||
|
||||
Future<void> _loadProfile() async {
|
||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
int? id = prefs.getInt('customerId');
|
||||
if (id == null) {
|
||||
Get.snackbar("Error", "Customer ID not found");
|
||||
return;
|
||||
}
|
||||
|
||||
final repo = LoginRepository();
|
||||
final fetchedProfile = await repo.fetchProfile(id.toString());
|
||||
|
||||
if (fetchedProfile != null) {
|
||||
|
||||
Name.value = fetchedProfile.firstname ?? '';
|
||||
Profile.value = fetchedProfile.profileimage ?? '';
|
||||
Number.value = fetchedProfile.contactno ?? '';
|
||||
|
||||
Adress.value = fetchedProfile.suburb ?? '';
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// Load from SharedPreferences (e.g., on app start)
|
||||
Future<void> loadUserDetails() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
|
||||
profileName.value = prefs.getString('customerFirstname') ?? '';
|
||||
profileImage.value = prefs.getString('customerProfile') ?? '';
|
||||
profilePhone.value = prefs.getString('contactno') ?? '';
|
||||
|
||||
}
|
||||
|
||||
/// 🔹 Fetch user details from API
|
||||
Future<void> fetchProfileFromAPI() async {
|
||||
try {
|
||||
isLoading(true);
|
||||
|
||||
// If you store the customerId in SharedPreferences, load it like this:
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
int? customerId = prefs.getInt('customerId') ?? 0; // fallback
|
||||
|
||||
final String url = "https://fiesta.nearle.app/live/api/v1/mob/customers/getbyid/?customerid=$customerId";
|
||||
|
||||
final response = await _dio.get(url);
|
||||
|
||||
if (response.statusCode == 200 && response.data['status'] == true) {
|
||||
final data = response.data['details'];
|
||||
print(customerId);
|
||||
print(data);
|
||||
print('rrr');
|
||||
|
||||
// Update observables
|
||||
// profileName.value = data['firstname'] ?? 'Guest';
|
||||
// profileImage.value = data['profileimage'] ??
|
||||
// 'https://i.pravatar.cc/150?img=12';
|
||||
// profilePhone.value =
|
||||
// "${data['dialcode'] ?? ''} ${data['contactno'] ?? ''}";
|
||||
|
||||
// Optionally, save to SharedPreferences for later offline use
|
||||
prefs.setString('customerFirstname', data['firstname'] ?? '');
|
||||
prefs.setString('customerProfile', data['profileimage'] ?? '');
|
||||
prefs.setString('contactno', data['contactno'] ?? '');
|
||||
} else {
|
||||
Get.snackbar('Error', 'Failed to fetch profile data');
|
||||
}
|
||||
} catch (e) {
|
||||
Get.snackbar('Error', 'Something went wrong: $e');
|
||||
} finally {
|
||||
isLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
/// 🔹 Get App Version
|
||||
Future<void> loadAppVersion() async {
|
||||
PackageInfo info = await PackageInfo.fromPlatform();
|
||||
appVersion.value = '${info.version}+${info.buildNumber}';
|
||||
}
|
||||
|
||||
Future<void> rateApp() async {
|
||||
final inAppReview = InAppReview.instance;
|
||||
|
||||
try {
|
||||
if (await inAppReview.isAvailable()) {
|
||||
await inAppReview.requestReview();
|
||||
// Popup MAY or MAY NOT show — Google's decision
|
||||
}
|
||||
} catch (e) {
|
||||
// ignore
|
||||
}
|
||||
|
||||
// ALWAYS open Play Store page (recommended for testing)
|
||||
_openStorePage();
|
||||
}
|
||||
|
||||
void _openStorePage() {
|
||||
const packageName = "com.nearle.gear";
|
||||
final url = Uri.parse(
|
||||
"https://play.google.com/store/apps/details?id=$packageName");
|
||||
|
||||
launchUrl(url, mode: LaunchMode.externalApplication);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user