Files
video/app/lib/providers/profile_provider.dart
T
2026-10-15 20:36:59 +08:00

83 lines
2.2 KiB
Dart

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<VideoItem> works;
final int worksTotal;
final bool isLoading;
final bool loadFailed;
ProfileState copyWith({
UserPublicProfile? profile,
List<VideoItem>? 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<ProfileState> {
ProfileNotifier(this.userId) : super(const ProfileState()) {
load();
}
final int userId;
Future<void> 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<ProfileNotifier, ProfileState, int>(
(ref, userId) => ProfileNotifier(userId),
);
/// 我的作品(需登录,一期一次拉 50 条)
final myVideosProvider = FutureProvider.autoDispose<PagedVideos>(
(ref) => UserService.fetchMyVideos(),
);
/// 我的收藏(需登录)
final myFavoritesProvider = FutureProvider.autoDispose<PagedVideos>(
(ref) => UserService.fetchMyFavorites(),
);
/// 观看历史(需登录)
final myHistoryProvider = FutureProvider.autoDispose<List<HistoryItem>>(
(ref) => UserService.fetchMyHistory(),
);