import 'package:stacked/stacked.dart'; import 'package:stacked_services/stacked_services.dart'; import '../../../app/app.locator.dart'; import '../../../models/user_model.dart'; import '../../../services/api_service.dart'; import '../../../services/authentication_service.dart'; import '../../../services/image_picker_service.dart'; import '../../../services/status_checker_service.dart'; import '../../common/enmus.dart'; import '../../common/ui_helpers.dart'; class ProfileDetailViewModel extends ReactiveViewModel with FormStateHelper implements FormViewModel { final _apiService = locator(); final _statusChecker = locator(); final _navigationService = locator(); final _imagePickerService = locator(); final _authenticationService = locator(); @override List get listenableServices => [_authenticationService]; // Current user UserModel? get _user => _authenticationService.user; UserModel? get user => _user; // First name bool _focusFirstName = false; bool get focusFirstName => _focusFirstName; // Last name bool _focusLastName = false; bool get focusLastName => _focusLastName; // Gender String? _selectedGender; String? get selectedGender => _selectedGender; // Birthday String? _selectedBirthday; String? get selectedBirthday => _selectedBirthday; // First name bool _focusPhoneNumber = false; bool get focusPhoneNumber => _focusPhoneNumber; // Email bool _focusEmail = false; bool get focusEmail => _focusEmail; // Country String _selectedCountry = 'Ethiopia'; String get selectedCountry => _selectedCountry; // Region String _selectedRegion = 'Addis Ababa'; String get selectedRegion => _selectedRegion; // Occupation bool _focusOccupation = false; bool get focusOccupation => _focusOccupation; // User data final Map _userData = {}; Map get userData => _userData; // First name void setFirstNameFocus() { _focusFirstName = true; rebuildUi(); } // Last name void setLastNameFocus() { _focusLastName = true; rebuildUi(); } // Gender void setGender(String value) { _selectedGender = value; rebuildUi(); } // Birthday void setBirthday(String value) { _selectedBirthday = value; rebuildUi(); } // Phone number void setPhoneNumberFocus() { _focusPhoneNumber = true; rebuildUi(); } // Email void setEmailFocus() { _focusEmail = true; rebuildUi(); } // Country List getCountries() => ['Ethiopia', 'Other']; void setSelectedCountry(String value) { _selectedCountry = value; if (selectedCountry != 'Ethiopia') { _selectedRegion = 'Other'; } else { _selectedRegion = 'Addis Ababa'; } rebuildUi(); } // Region List getRegions(String country) { if (country == 'Ethiopia') { return [ 'Afar', 'SNNPR', 'Amhara', 'Harari', 'Oromia', 'Sidama', 'Somali', 'Tigray', 'Gambela', 'Dire Dawa', 'Addis Ababa', 'Central Ethiopia', 'Benishangul-Gumuz', 'South West Ethiopia', ]; } else { return ['Other']; } } void setSelectedRegion(String value) { _selectedRegion = value; rebuildUi(); } // Occupation void setOccupationFocus() { _focusOccupation = true; rebuildUi(); } // User data void addUserData(Map data) { _userData.addAll(data); } void clearUserData() { _userData.clear(); } // Image picker Future openCamera() async => runBusyFuture(_openCamera(), busyObject: StateObjects.profileImage); Future _openCamera() async { String? image = await _imagePickerService.camera(); pop(); if (image != null) { await updateProfilePicture(image); await _authenticationService.saveProfileImage(image); } } Future openGallery() async => runBusyFuture(_openGallery(), busyObject: StateObjects.profileImage); Future _openGallery() async { String? image = await _imagePickerService.gallery(); pop(); if (image != null) { await updateProfilePicture(image); await _authenticationService.saveProfileImage(image); } } // Navigation void pop() => _navigationService.back(); // Remote api call // Get profile Future getProfile() async => await runBusyFuture(_getProfile()); Future _getProfile() async { if (await _statusChecker.checkConnection()) { Map response = await _apiService.getProfileData(_user?.userId); if (response['status'] == ResponseStatus.success) { addUserData(response['data']); } } } // Update profile Future updateProfile() async => await runBusyFuture(_updateProfile(), busyObject: StateObjects.profileUpdate); Future _updateProfile() async { if (await _statusChecker.checkConnection()) { Map response = await _apiService.completeProfile(_userData); if (response['status'] == ResponseStatus.success) { await _authenticationService.updateUserData(_userData); pop(); showSuccessToast(response['message']); } else { showErrorToast(response['message']); } } } // Update profile picture Future updateProfilePicture(String image) async => await runBusyFuture(_updateProfilePicture(image)); Future _updateProfilePicture(String image) async { if (await _statusChecker.checkConnection()) { Map data = {'profile_picture_url': image}; await _apiService.updateProfileImage(data: data, userId: _user?.userId); } } }