import 'package:flutter_riverpod/flutter_riverpod.dart'; import '../models/user_profile.dart'; import '../models/video_item.dart'; import '../services/user_service.dart'; /// 他人主页状态 class ProfileState { const ProfileState({ this.profile, this.works = const [], this.worksTotal = 0, this.isLoading = false, this.loadFailed = false, }); final UserPublicProfile? profile; final List works; final int worksTotal; final bool isLoading; final bool loadFailed; ProfileState copyWith({ UserPublicProfile? profile, List? works, int? worksTotal, bool? isLoading, bool? loadFailed, }) => ProfileState( profile: profile ?? this.profile, works: works ?? this.works, worksTotal: worksTotal ?? this.worksTotal, isLoading: isLoading ?? this.isLoading, loadFailed: loadFailed ?? this.loadFailed, ); } /// 他人主页 Notifier:主页信息 + 已发布作品(一期一次拉 50 条,不做分页加载) class ProfileNotifier extends StateNotifier { ProfileNotifier(this.userId) : super(const ProfileState()) { load(); } final int userId; Future load() async { state = state.copyWith(isLoading: true, loadFailed: false); try { final profile = await UserService.fetchPublicProfile(userId); final works = await UserService.fetchUserVideos(userId); state = state.copyWith( profile: profile, works: works.items, worksTotal: works.total, isLoading: false, ); } catch (_) { state = state.copyWith(isLoading: false, loadFailed: true); } } } final profileProvider = StateNotifierProvider.family( (ref, userId) => ProfileNotifier(userId), ); /// 我的作品(需登录,一期一次拉 50 条) final myVideosProvider = FutureProvider.autoDispose( (ref) => UserService.fetchMyVideos(), ); /// 我的收藏(需登录) final myFavoritesProvider = FutureProvider.autoDispose( (ref) => UserService.fetchMyFavorites(), ); /// 观看历史(需登录) final myHistoryProvider = FutureProvider.autoDispose>( (ref) => UserService.fetchMyHistory(), );