mirror of
https://github.com/Dvorinka/1356.git
synced 2026-06-04 20:12:56 +00:00
small fix, don't worry about it
This commit is contained in:
@@ -1,24 +1,30 @@
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:supabase_flutter/supabase_flutter.dart' as supabase;
|
||||
import '../../../data/models/user_model.dart' as app;
|
||||
import '../../../data/repositories/user_repository.dart';
|
||||
import '../../../bootstrap/supabase_client.dart';
|
||||
import '../../../core/errors/failure.dart';
|
||||
import '../../../core/utils/unit_conversion_utils.dart';
|
||||
|
||||
final profileControllerProvider = StateNotifierProvider<ProfileController, ProfileState>((ref) {
|
||||
final client = supabase.Supabase.instance.client;
|
||||
final repository = UserRepository(client);
|
||||
final client = supabaseClient;
|
||||
final repository = client != null ? UserRepository(client) : null;
|
||||
return ProfileController(repository);
|
||||
});
|
||||
|
||||
class ProfileController extends StateNotifier<ProfileState> {
|
||||
final UserRepository _repository;
|
||||
final UserRepository? _repository;
|
||||
|
||||
ProfileController(this._repository) : super(const ProfileState.initial());
|
||||
|
||||
Future<void> loadProfile(String userId) async {
|
||||
if (_repository == null) {
|
||||
state = const ProfileState.error('Profile is unavailable while offline.');
|
||||
return;
|
||||
}
|
||||
|
||||
state = const ProfileState.loading();
|
||||
try {
|
||||
final user = await _repository.getProfile(userId);
|
||||
final user = await _repository!.getProfile(userId);
|
||||
state = ProfileState.loaded(user);
|
||||
} on Failure catch (failure) {
|
||||
state = ProfileState.error(failure.message);
|
||||
@@ -28,15 +34,20 @@ class ProfileController extends StateNotifier<ProfileState> {
|
||||
}
|
||||
|
||||
Future<void> updateUsername(String userId, String username) async {
|
||||
if (_repository == null) {
|
||||
state = const ProfileState.error('Profile is unavailable while offline.');
|
||||
return;
|
||||
}
|
||||
|
||||
state = const ProfileState.loading();
|
||||
try {
|
||||
final isAvailable = await _repository.isUsernameAvailable(username);
|
||||
final isAvailable = await _repository!.isUsernameAvailable(username);
|
||||
if (!isAvailable) {
|
||||
state = const ProfileState.error('Username is already taken');
|
||||
return;
|
||||
}
|
||||
|
||||
final updatedUser = await _repository.updateProfile(
|
||||
final updatedUser = await _repository!.updateProfile(
|
||||
userId: userId,
|
||||
username: username,
|
||||
);
|
||||
@@ -49,9 +60,14 @@ class ProfileController extends StateNotifier<ProfileState> {
|
||||
}
|
||||
|
||||
Future<void> updateBio(String userId, String bio) async {
|
||||
if (_repository == null) {
|
||||
state = const ProfileState.error('Profile is unavailable while offline.');
|
||||
return;
|
||||
}
|
||||
|
||||
state = const ProfileState.loading();
|
||||
try {
|
||||
final updatedUser = await _repository.updateProfile(
|
||||
final updatedUser = await _repository!.updateProfile(
|
||||
userId: userId,
|
||||
bio: bio,
|
||||
);
|
||||
@@ -64,9 +80,14 @@ class ProfileController extends StateNotifier<ProfileState> {
|
||||
}
|
||||
|
||||
Future<void> updateAvatarUrl(String userId, String avatarUrl) async {
|
||||
if (_repository == null) {
|
||||
state = const ProfileState.error('Profile is unavailable while offline.');
|
||||
return;
|
||||
}
|
||||
|
||||
state = const ProfileState.loading();
|
||||
try {
|
||||
final updatedUser = await _repository.updateProfile(
|
||||
final updatedUser = await _repository!.updateProfile(
|
||||
userId: userId,
|
||||
avatarUrl: avatarUrl,
|
||||
);
|
||||
@@ -79,12 +100,17 @@ class ProfileController extends StateNotifier<ProfileState> {
|
||||
}
|
||||
|
||||
Future<void> toggleProfileVisibility(String userId) async {
|
||||
if (_repository == null) {
|
||||
state = const ProfileState.error('Profile is unavailable while offline.');
|
||||
return;
|
||||
}
|
||||
|
||||
final currentState = state;
|
||||
if (currentState.user == null) return;
|
||||
|
||||
state = const ProfileState.loading();
|
||||
try {
|
||||
final updatedUser = await _repository.updateProfile(
|
||||
final updatedUser = await _repository!.updateProfile(
|
||||
userId: userId,
|
||||
isPublicProfile: !currentState.user!.isPublicProfile,
|
||||
);
|
||||
@@ -105,16 +131,27 @@ class ProfileController extends StateNotifier<ProfileState> {
|
||||
String? instagramHandle,
|
||||
String? tiktokHandle,
|
||||
String? websiteUrl,
|
||||
Gender? gender,
|
||||
DateTime? birthDate,
|
||||
double? heightCm,
|
||||
double? weightKg,
|
||||
HeightUnit heightUnit = HeightUnit.metric,
|
||||
WeightUnit weightUnit = WeightUnit.metric,
|
||||
}) async {
|
||||
if (_repository == null) {
|
||||
state = const ProfileState.error('Profile is unavailable while offline.');
|
||||
return;
|
||||
}
|
||||
|
||||
state = const ProfileState.loading();
|
||||
try {
|
||||
final isAvailable = await _repository.isUsernameAvailable(username);
|
||||
final isAvailable = await _repository!.isUsernameAvailable(username);
|
||||
if (!isAvailable) {
|
||||
state = const ProfileState.error('Username is already taken');
|
||||
return;
|
||||
}
|
||||
|
||||
final updatedUser = await _repository.updateProfile(
|
||||
final updatedUser = await _repository!.updateProfile(
|
||||
userId: userId,
|
||||
username: username,
|
||||
bio: bio,
|
||||
@@ -123,6 +160,12 @@ class ProfileController extends StateNotifier<ProfileState> {
|
||||
instagramHandle: instagramHandle,
|
||||
tiktokHandle: tiktokHandle,
|
||||
websiteUrl: websiteUrl,
|
||||
gender: gender,
|
||||
birthDate: birthDate,
|
||||
heightCm: heightCm,
|
||||
weightKg: weightKg,
|
||||
heightUnit: heightUnit,
|
||||
weightUnit: weightUnit,
|
||||
);
|
||||
state = ProfileState.loaded(updatedUser);
|
||||
} on Failure catch (failure) {
|
||||
@@ -131,6 +174,19 @@ class ProfileController extends StateNotifier<ProfileState> {
|
||||
state = ProfileState.error(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> isProfileSetupComplete(String userId) async {
|
||||
if (_repository == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
final user = await _repository!.getProfile(userId);
|
||||
return user.username.isNotEmpty;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class ProfileState {
|
||||
|
||||
Reference in New Issue
Block a user