import 'package:stacked/stacked.dart'; import 'package:stacked_services/stacked_services.dart'; import 'package:yimaru_app/app/app.router.dart'; import 'package:yimaru_app/services/image_picker_service.dart'; import 'package:yimaru_app/ui/common/enmus.dart'; import '../../../app/app.locator.dart'; import '../../../models/user_model.dart'; import '../../../services/api_service.dart'; import '../../../services/authentication_service.dart'; import '../../../services/status_checker_service.dart'; import '../../common/app_colors.dart'; class ProfileViewModel extends ReactiveViewModel { final _apiService = locator(); final _dialogService = 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; // 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.saveProfilePicture(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.saveProfilePicture(image); } } // Logout Future _logOut() async { await _authenticationService.logOut(); await _navigationService.replaceWithLoginView(); } // Dialog Future showAbortDialog() async { DialogResponse? response = await _dialogService.showDialog( title: 'Logout', cancelTitle: 'No', buttonTitle: 'Yes', barrierDismissible: true, cancelTitleColor: kcDarkGrey, buttonTitleColor: kcPrimaryColor, description: 'Are you sure you want to quit?', ); return response?.confirmed; } Future logOut() async { bool? response = await showAbortDialog(); if (response != null && response) { await _logOut(); } } // Navigation void pop() => _navigationService.back(); Future navigateToProfileDetail() async => await _navigationService.navigateToProfileDetailView(); Future navigateToDownloads() async => await _navigationService.navigateToDownloadsView(); Future navigateToProgress() async => await _navigationService.navigateToProgressView(); Future navigateToAccountPrivacy() async => await _navigationService.navigateToAccountPrivacyView(); Future navigateToSupport() async => await _navigationService.navigateToSupportView(); // Remote api call // Update profile 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); } } }