59 lines
2.0 KiB
Dart
59 lines
2.0 KiB
Dart
import 'dart:io';
|
|
import 'package:get/get.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
class ProfileController extends GetxController {
|
|
final RxString imagePath = ''.obs;
|
|
final RxString userName = ''.obs;
|
|
final RxString userEmail = ''.obs;
|
|
final RxString userContact = ''.obs;
|
|
final RxString userAddress = ''.obs;
|
|
|
|
void setImagePath(String path) {
|
|
imagePath.value = path;
|
|
}
|
|
|
|
Future<void> loadFromPrefs() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
// The picked photo's path was being written on every change and never read
|
|
// back, so the avatar reverted to the initial on every cold start. It is
|
|
// restored here, and dropped if the file has since gone from disk — an
|
|
// image-picker path can point into a cache the OS is free to clear.
|
|
final String savedImage = (prefs.getString('profile_image_path') ?? '')
|
|
.trim();
|
|
if (savedImage.isNotEmpty) {
|
|
bool stillThere = false;
|
|
try {
|
|
stillThere = File(savedImage).existsSync();
|
|
} catch (_) {}
|
|
imagePath.value = stillThere ? savedImage : '';
|
|
if (!stillThere) await prefs.remove('profile_image_path');
|
|
}
|
|
userName.value = (prefs.getString('user_name') ?? '').trim();
|
|
userEmail.value = (prefs.getString('user_email') ?? '').trim();
|
|
userContact.value = (prefs.getString('contactno') ?? '').trim();
|
|
userAddress.value = (prefs.getString('user_address') ?? '').trim();
|
|
}
|
|
|
|
void setProfile({
|
|
String? name,
|
|
String? email,
|
|
String? contact,
|
|
String? address,
|
|
}) {
|
|
if (name != null && name.trim().isNotEmpty) userName.value = name.trim();
|
|
if (email != null && email.trim().isNotEmpty)
|
|
userEmail.value = email.trim();
|
|
if (contact != null && contact.trim().isNotEmpty)
|
|
userContact.value = contact.trim();
|
|
if (address != null && address.trim().isNotEmpty)
|
|
userAddress.value = address.trim();
|
|
}
|
|
|
|
File? get fileOrNull {
|
|
final path = imagePath.value;
|
|
if (path.isEmpty) return null;
|
|
return File(path);
|
|
}
|
|
}
|