Files
prosappweb/lib/providers/profile_form_provider.dart
T
Lizandro GuarnizoandClaude Sonnet 4.6 15175c1b91 replace: swap prosappweb content for prosapp_web_app (more complete version)
prosapp_web_app has chat, dashboard, calendar, support, 13 providers and
Fluro URL routing. Keep Dockerfile + nginx.conf from previous prosappweb.
Upgrade google_fonts 6.2.1 → 8.1.0 (Dart 3.12 compat fix).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-18 15:26:32 -05:00

160 lines
4.6 KiB
Dart

import 'dart:typed_data';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:flutter/material.dart';
import 'package:prosapp_web_app/models/pro_state.dart';
import 'package:prosapp_web_app/models/usuario.dart';
import 'package:prosapp_web_app/services/notifications_service.dart';
class ProfileFormProvider extends ChangeNotifier {
Usuario? user;
GlobalKey<FormState> formKey = GlobalKey<FormState>();
String? _verificationId;
void copyUserWith({
String? id,
String? email,
String? phone,
String? name,
String? nickname,
String? city,
String? picture,
String? birthday,
String? gender,
ProState? proState,
String? token,
}) {
user = Usuario(
id: id ?? user!.id,
email: email ?? user!.email,
phone: phone ?? user!.phone,
name: name ?? user!.name,
nickname: nickname ?? user!.nickname,
city: city ?? user!.city,
picture: picture ?? user!.picture,
birthday: birthday ?? user!.birthday,
gender: gender ?? user!.gender,
proState: proState ?? user!.proState,
token: token ?? user!.token,
);
notifyListeners();
}
bool _validForm() {
return formKey.currentState!.validate();
}
Future<void> updateUserInfo() async {
if (!_validForm()) return;
final docUser = FirebaseFirestore.instance
.collection('users')
.doc(user!.id)
.withConverter(
fromFirestore: (snapshot, _) =>
Usuario.fromDocument(snapshot.data()!),
toFirestore: (user, _) => user.toDocument(),
);
await docUser.set(user!);
NotificationsService.showSnackbar('Información actualizada');
}
Future<void> updateUserInfoNoValid() async {
final docUser = FirebaseFirestore.instance
.collection('users')
.doc(user!.id)
.withConverter(
fromFirestore: (snapshot, _) =>
Usuario.fromDocument(snapshot.data()!),
toFirestore: (user, _) => user.toDocument(),
);
await docUser.set(user!);
NotificationsService.showSnackbar('Información actualizada');
}
Future<Usuario> uploadPicture(Uint8List bytes) async {
try {
final storageRef = FirebaseStorage.instance
.ref()
.child('${user!.id}/PP/${user!.id}_lead');
await storageRef.putData(bytes);
final url = await storageRef.getDownloadURL();
copyUserWith(picture: url);
notifyListeners();
return user!;
} catch (e) {
print("Error al subir la imagen: $e");
rethrow;
}
}
// Agregar numero
Future<void> signUpWithPhoneNumber(String phoneNumber) async {
try {
await FirebaseAuth.instance.verifyPhoneNumber(
phoneNumber: phoneNumber,
verificationCompleted: (PhoneAuthCredential credential) async {
await FirebaseAuth.instance.signInWithCredential(credential);
NotificationsService.showSnackbar('Autenticación exitosa');
},
verificationFailed: (FirebaseAuthException e) {
NotificationsService.showSnackbar(
'Error en la verificación: ${e.message}');
},
codeSent: (String verificationId, int? resendToken) {
_verificationId = verificationId;
NotificationsService.showSnackbar(
'Código enviado al número $phoneNumber');
},
codeAutoRetrievalTimeout: (String verificationId) {
_verificationId = verificationId;
},
);
NotificationsService.showSnackbar('Código enviado');
} catch (e) {
NotificationsService.showSnackbar('Error al registrar con teléfono: $e');
}
}
Future<bool> linkPhoneNumberToExistingAccount(
String phoneNumber, String code) async {
try {
var phoneAuthCredential = PhoneAuthProvider.credential(
verificationId: _verificationId!,
smsCode: code,
);
User? user = FirebaseAuth.instance.currentUser;
if (user != null) {
await user.linkWithCredential(phoneAuthCredential);
copyUserWith(phone: phoneNumber);
notifyListeners();
NotificationsService.showSnackbar('Número vinculado exitosamente');
return true;
}
return false;
} catch (e) {
if (e is FirebaseAuthException && e.code == 'invalid-verification-code') {
NotificationsService.showSnackbar('Código de verificación inválido');
return false;
} else {
NotificationsService.showSnackbar('Error al vincular número: $e');
rethrow;
}
}
}
}