HydroNutrify_Ari_e41221567/lib/logic/controllers/profile_controller.dart

73 lines
2.0 KiB
Dart

import 'package:flutter/material.dart';
import 'package:mobile_monitoring/data/models/service_result.dart';
import 'package:mobile_monitoring/data/services/auth_service.dart';
import 'package:mobile_monitoring/data/services/profile_service.dart';
import 'package:mobile_monitoring/logic/controllers/base_controller.dart';
class ProfileController extends BaseController {
ProfileController({
ProfileService? profileService,
AuthService? authService,
}) : _profileService = profileService ?? ProfileService(),
_authService = authService ?? AuthService();
final ProfileService _profileService;
final AuthService _authService;
final TextEditingController nameController = TextEditingController();
bool _isLoading = false;
UserProfileInfo? _profile;
bool get isLoading => _isLoading;
UserProfileInfo? get profile => _profile;
void loadProfile() {
_profile = _profileService.getCurrentUserProfile();
nameController.text = _profile?.displayName ?? '';
notifyListeners();
}
Future<ServiceResult<void>> updateProfile() async {
_setLoading(true);
final result = await _profileService.updateUserName(nameController.text.trim());
if (result.success) {
_profile = _profileService.getCurrentUserProfile();
nameController.text = _profile?.displayName ?? '';
}
_setLoading(false);
return result;
}
Future<ServiceResult<void>> changePassword({
required String oldPassword,
required String newPassword,
}) async {
_setLoading(true);
final result = await _profileService.validateAndUpdatePassword(
oldPassword,
newPassword,
);
_setLoading(false);
return result;
}
Future<ServiceResult<void>> logout() async {
_setLoading(true);
final result = await _authService.signOut();
_setLoading(false);
return result;
}
void _setLoading(bool value) {
_isLoading = value;
notifyListeners();
}
@override
void dispose() {
nameController.dispose();
super.dispose();
}
}