76 lines
2.2 KiB
Dart
76 lines
2.2 KiB
Dart
import 'dart:typed_data';
|
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
import '../../../../core/supabase/supabase_providers.dart';
|
|
import '../../data/services/profile_service.dart';
|
|
import '../../domain/models/profile_model.dart';
|
|
import '../../domain/models/profile_stats_model.dart';
|
|
|
|
final profileServiceProvider = Provider<ProfileService>((ref) {
|
|
final client = ref.watch(supabaseProvider);
|
|
return ProfileService(client);
|
|
});
|
|
|
|
final myProfileProvider = FutureProvider<ProfileModel>((ref) {
|
|
return ref.watch(profileServiceProvider).fetchMyProfile();
|
|
});
|
|
|
|
final myProfileStatsProvider = FutureProvider<ProfileStatsModel>((ref) {
|
|
return ref.watch(profileServiceProvider).fetchMyStats();
|
|
});
|
|
|
|
final profileControllerProvider =
|
|
AutoDisposeAsyncNotifierProvider<ProfileController, void>(
|
|
ProfileController.new,
|
|
);
|
|
|
|
class ProfileController extends AutoDisposeAsyncNotifier<void> {
|
|
@override
|
|
Future<void> build() async {}
|
|
|
|
Future<void> updateUsername(String username) async {
|
|
state = const AsyncLoading();
|
|
state = await AsyncValue.guard(() async {
|
|
await ref.read(profileServiceProvider).updateUsername(username.trim());
|
|
ref.invalidate(myProfileProvider);
|
|
});
|
|
}
|
|
|
|
Future<void> updateBio(String bio) async {
|
|
state = const AsyncLoading();
|
|
state = await AsyncValue.guard(() async {
|
|
await ref.read(profileServiceProvider).updateBio(bio.trim());
|
|
ref.invalidate(myProfileProvider);
|
|
});
|
|
}
|
|
|
|
Future<void> uploadAvatar({
|
|
required Uint8List bytes,
|
|
required String extension,
|
|
}) async {
|
|
state = const AsyncLoading();
|
|
state = await AsyncValue.guard(() async {
|
|
await ref.read(profileServiceProvider).uploadAvatar(
|
|
bytes: bytes,
|
|
extension: extension,
|
|
);
|
|
ref.invalidate(myProfileProvider);
|
|
});
|
|
}
|
|
|
|
Future<void> saveProfile({
|
|
required String username,
|
|
required String bio,
|
|
}) async {
|
|
state = const AsyncLoading();
|
|
state = await AsyncValue.guard(() async {
|
|
await ref.read(profileServiceProvider).updateProfile(
|
|
username: username.trim(),
|
|
bio: bio.trim(),
|
|
);
|
|
ref.invalidate(myProfileProvider);
|
|
});
|
|
}
|
|
}
|