feat(player): 切集刷新互动计数统计
This commit is contained in:
1 parent
37353dac15
commit
188811fde6
11 files changed
+694
-20
No files matched your search
@@ -60,6 +60,20 @@ class Episode {
|
||||
posterUrl: posterUrl ?? this.posterUrl,
|
||||
);
|
||||
|
||||
Episode withStats({int? likeCount, int? commentCount}) => Episode(
|
||||
id: id,
|
||||
number: number,
|
||||
title: title,
|
||||
isFree: isFree,
|
||||
videoUrl: videoUrl,
|
||||
durationSec: durationSec,
|
||||
locked: locked,
|
||||
likeCount: likeCount ?? this.likeCount,
|
||||
commentCount: commentCount ?? this.commentCount,
|
||||
thumbnailsUrl: thumbnailsUrl,
|
||||
posterUrl: posterUrl,
|
||||
);
|
||||
|
||||
factory Episode.fromJson(Map<String, dynamic> json) {
|
||||
return Episode(
|
||||
id: (json['id'] ?? '').toString(),
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
// Author: fruit.com.cn
|
||||
|
||||
import 'json_numbers.dart';
|
||||
|
||||
/// 播放页轻量互动计数快照,不包含播放地址或票据信息。
|
||||
class EpisodeStats {
|
||||
const EpisodeStats({
|
||||
required this.episodeId,
|
||||
required this.dramaId,
|
||||
required this.likeCount,
|
||||
required this.commentCount,
|
||||
required this.favoriteCount,
|
||||
required this.liked,
|
||||
required this.favorited,
|
||||
});
|
||||
|
||||
final String episodeId;
|
||||
final String dramaId;
|
||||
final int likeCount;
|
||||
final int commentCount;
|
||||
final int favoriteCount;
|
||||
final bool liked;
|
||||
final bool favorited;
|
||||
|
||||
EpisodeStats copyWith({
|
||||
String? dramaId,
|
||||
int? likeCount,
|
||||
int? commentCount,
|
||||
int? favoriteCount,
|
||||
bool? liked,
|
||||
bool? favorited,
|
||||
}) {
|
||||
return EpisodeStats(
|
||||
episodeId: episodeId,
|
||||
dramaId: dramaId ?? this.dramaId,
|
||||
likeCount: likeCount ?? this.likeCount,
|
||||
commentCount: commentCount ?? this.commentCount,
|
||||
favoriteCount: favoriteCount ?? this.favoriteCount,
|
||||
liked: liked ?? this.liked,
|
||||
favorited: favorited ?? this.favorited,
|
||||
);
|
||||
}
|
||||
|
||||
factory EpisodeStats.fromJson(Map<String, dynamic> json) {
|
||||
return EpisodeStats(
|
||||
episodeId: jsonToId(json['episodeId']),
|
||||
dramaId: jsonToId(json['dramaId']),
|
||||
likeCount: jsonToInt(json['likeCount']),
|
||||
commentCount: jsonToInt(json['commentCount']),
|
||||
favoriteCount: jsonToInt(json['favoriteCount']),
|
||||
liked: json['liked'] == true,
|
||||
favorited: json['favorited'] == true,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
// Author: fruit.com.cn
|
||||
|
||||
import '../models/episode_stats.dart';
|
||||
import '../models/play_ticket.dart';
|
||||
import 'api_client.dart';
|
||||
|
||||
@@ -17,4 +18,11 @@ class PlaybackApi {
|
||||
if (data is Map<String, dynamic>) return PlayTicket.fromJson(data);
|
||||
throw const ApiException(-1, '返回数据格式异常');
|
||||
}
|
||||
|
||||
/// 轻量刷新互动计数:不签发地址,也不占用播放票据缓存。
|
||||
Future<EpisodeStats> episodeStats(String episodeId) async {
|
||||
final data = await _client.get('/app/episodes/$episodeId/stats');
|
||||
if (data is Map<String, dynamic>) return EpisodeStats.fromJson(data);
|
||||
throw const ApiException(-1, '返回数据格式异常');
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
// Author: fruit.com.cn
|
||||
|
||||
import '../models/episode_stats.dart';
|
||||
import '../models/play_ticket.dart';
|
||||
import '../remote/playback_api.dart';
|
||||
|
||||
@@ -10,6 +11,9 @@ import '../remote/playback_api.dart';
|
||||
abstract interface class PlaybackRepository {
|
||||
Future<PlayTicket> ticketFor(String episodeId, {bool forceRefresh = false});
|
||||
|
||||
/// 读取互动计数快照;与 [ticketFor] 分离,避免为了刷新计数重新签发地址。
|
||||
Future<EpisodeStats> episodeStats(String episodeId);
|
||||
|
||||
/// 当前缓存的票据是否还在有效期内(用于"回前台发现过期就重新取")。
|
||||
bool isFresh(String episodeId);
|
||||
}
|
||||
@@ -42,6 +46,10 @@ class HttpPlaybackRepository implements PlaybackRepository {
|
||||
return ticket;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<EpisodeStats> episodeStats(String episodeId) =>
|
||||
_api.episodeStats(episodeId);
|
||||
|
||||
@override
|
||||
bool isFresh(String episodeId) {
|
||||
final cached = _cache[episodeId];
|
||||
@@ -65,11 +73,22 @@ class _CachedTicket {
|
||||
|
||||
/// 测试与离线场景用:按集返回固定票据,并记录被请求过几次。
|
||||
class FakePlaybackRepository implements PlaybackRepository {
|
||||
FakePlaybackRepository({this.ticket, this.error, this.fresh = true});
|
||||
FakePlaybackRepository({
|
||||
this.ticket,
|
||||
this.error,
|
||||
this.fresh = true,
|
||||
this.stats,
|
||||
this.statsError,
|
||||
Map<String, EpisodeStats>? statsByEpisode,
|
||||
}) : statsByEpisode = statsByEpisode ?? {};
|
||||
|
||||
PlayTicket? ticket;
|
||||
Object? error;
|
||||
EpisodeStats? stats;
|
||||
Object? statsError;
|
||||
final Map<String, EpisodeStats> statsByEpisode;
|
||||
final List<String> requests = [];
|
||||
final List<String> statsRequests = [];
|
||||
final List<bool> forced = [];
|
||||
|
||||
/// 模拟"票据是否还有效":置 false 可验证回前台会重新取。
|
||||
@@ -95,4 +114,13 @@ class FakePlaybackRepository implements PlaybackRepository {
|
||||
qualities: const ['AUTO', '720P', '1080P'],
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<EpisodeStats> episodeStats(String episodeId) async {
|
||||
statsRequests.add(episodeId);
|
||||
if (statsError != null) throw statsError!;
|
||||
final value = statsByEpisode[episodeId] ?? stats;
|
||||
if (value != null) return value;
|
||||
throw StateError('episode stats not configured for $episodeId');
|
||||
}
|
||||
}
|
||||
@@ -32,6 +32,17 @@ class FavoriteController extends ChangeNotifier {
|
||||
|
||||
bool isSaved(String dramaId) => _saved.contains(dramaId);
|
||||
|
||||
/// 用统计快照校准收藏状态;已有列表卡片结构不因一次状态同步而改动。
|
||||
void applyServerState(String dramaId, {required bool saved}) {
|
||||
final changed = _saved.contains(dramaId) != saved;
|
||||
if (saved) {
|
||||
_saved.add(dramaId);
|
||||
} else {
|
||||
_saved.remove(dramaId);
|
||||
}
|
||||
if (changed) notify();
|
||||
}
|
||||
|
||||
void notify() {
|
||||
if (!_disposed) notifyListeners();
|
||||
}
|
||||
|
||||
@@ -40,6 +40,24 @@ class InteractionController extends ChangeNotifier {
|
||||
/// 该集服务端最新点赞数。
|
||||
int? episodeLikeCount(String episodeId) => _episodeLikeCounts[episodeId];
|
||||
|
||||
/// 用轻量统计快照校准当前用户的分集点赞状态与计数。
|
||||
void applyEpisodeStats(
|
||||
String episodeId, {
|
||||
required bool liked,
|
||||
required int likeCount,
|
||||
}) {
|
||||
final changed =
|
||||
_likedEpisodeIds.contains(episodeId) != liked ||
|
||||
_episodeLikeCounts[episodeId] != likeCount;
|
||||
if (liked) {
|
||||
_likedEpisodeIds.add(episodeId);
|
||||
} else {
|
||||
_likedEpisodeIds.remove(episodeId);
|
||||
}
|
||||
_episodeLikeCounts[episodeId] = likeCount;
|
||||
if (changed) _notify();
|
||||
}
|
||||
|
||||
/// 乐观切换剧集点赞,失败时回滚并以服务端返回状态校准。
|
||||
Future<OperationResult<LikeToggleResult>> toggleDramaLike(String dramaId) =>
|
||||
_toggleLike(
|
||||
|
||||
Reference in new issue
Block a user