fix(mine): 支持未登录展示互动计数
This commit is contained in:
1 parent
06a2f6d7c0
commit
8d511933df
4 files changed
+334
-19
No files matched your search
@@ -18,6 +18,7 @@ class MineLogic extends ChangeNotifier {
|
||||
bool recentLoading = false;
|
||||
String? recentError;
|
||||
bool _disposed = false;
|
||||
int _requestSeq = 0;
|
||||
|
||||
void notify() {
|
||||
if (!_disposed) notifyListeners();
|
||||
@@ -25,21 +26,37 @@ class MineLogic extends ChangeNotifier {
|
||||
|
||||
/// 最近观看:取分页首屏,剧名封面仍由目录卡片按 id 映射。
|
||||
Future<void> refreshRecent() async {
|
||||
final request = ++_requestSeq;
|
||||
recentLoading = true;
|
||||
recentError = null;
|
||||
notify();
|
||||
try {
|
||||
final page = await history.recent(pageIndex: 1, pageSize: 10);
|
||||
if (_disposed || request != _requestSeq) return;
|
||||
recent = page.list;
|
||||
watchRecordTotal = page.total;
|
||||
} catch (_) {
|
||||
recentError = 'progressError';
|
||||
if (!_disposed && request == _requestSeq) {
|
||||
recentError = 'progressError';
|
||||
}
|
||||
} finally {
|
||||
recentLoading = false;
|
||||
notify();
|
||||
if (!_disposed && request == _requestSeq) {
|
||||
recentLoading = false;
|
||||
notify();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 切换账号/设备身份时先清空,避免旧身份数据回写或短暂混显。
|
||||
void resetRecent() {
|
||||
_requestSeq += 1;
|
||||
recent = const [];
|
||||
watchRecordTotal = null;
|
||||
recentLoading = false;
|
||||
recentError = null;
|
||||
notify();
|
||||
}
|
||||
|
||||
/// 页面重新可见时刷新最近观看,避免停留在旧进度。
|
||||
void syncPage({required bool active, required bool wasActive}) {
|
||||
if (active && !wasActive) refreshRecent();
|
||||
|
||||
@@ -53,25 +53,70 @@ class MyPage extends StatefulWidget {
|
||||
|
||||
class _MyPageState extends State<MyPage> {
|
||||
late final MineLogic logic;
|
||||
late final AuthController _auth;
|
||||
late final FavoriteController _favorites;
|
||||
late bool _wasSignedIn;
|
||||
bool _activationRefreshScheduled = false;
|
||||
|
||||
bool get embedded => widget.callbacks.embedded;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_auth = context.read<AuthController>();
|
||||
_favorites = context.read<FavoriteController>();
|
||||
_wasSignedIn = _auth.isSignedIn;
|
||||
_auth.addListener(_onAuthChanged);
|
||||
logic = MineBinding().create(
|
||||
history: context.read<WatchHistoryRepository>(),
|
||||
);
|
||||
if (widget.active) _scheduleFavoriteLoad();
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant MyPage oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
logic.syncPage(active: widget.active, wasActive: oldWidget.active);
|
||||
if (widget.active && !oldWidget.active) {
|
||||
_scheduleActivationRefresh();
|
||||
}
|
||||
}
|
||||
|
||||
void _scheduleFavoriteLoad() {
|
||||
if (_activationRefreshScheduled) return;
|
||||
_activationRefreshScheduled = true;
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
_activationRefreshScheduled = false;
|
||||
if (mounted) unawaited(_favorites.load());
|
||||
});
|
||||
}
|
||||
|
||||
void _scheduleActivationRefresh() {
|
||||
if (_activationRefreshScheduled) return;
|
||||
_activationRefreshScheduled = true;
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
_activationRefreshScheduled = false;
|
||||
if (!mounted || !widget.active) return;
|
||||
logic.syncPage(active: true, wasActive: false);
|
||||
unawaited(_favorites.load());
|
||||
});
|
||||
}
|
||||
|
||||
void _onAuthChanged() {
|
||||
final signedIn = _auth.isSignedIn;
|
||||
if (signedIn == _wasSignedIn) return;
|
||||
_wasSignedIn = signedIn;
|
||||
logic.resetRecent();
|
||||
_favorites.clear();
|
||||
unawaited(_reloadForIdentity());
|
||||
}
|
||||
|
||||
Future<void> _reloadForIdentity() async {
|
||||
await Future.wait([logic.refreshRecent(), _favorites.load()]);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_auth.removeListener(_onAuthChanged);
|
||||
logic.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
@@ -141,12 +186,14 @@ class _MyPageState extends State<MyPage> {
|
||||
final wallet = context.watch<WalletController>();
|
||||
final continueWatching = logic.recent.take(10).toList(growable: false);
|
||||
final followingCount =
|
||||
auth.isSignedIn &&
|
||||
favorites.error == null &&
|
||||
!(favorites.loading && favorites.total == 0)
|
||||
favorites.error == null && !(favorites.loading && favorites.total == 0)
|
||||
? favorites.total
|
||||
: null;
|
||||
final watchRecordCount = auth.isSignedIn ? logic.watchRecordTotal : null;
|
||||
final watchRecordCount =
|
||||
logic.recentError == null &&
|
||||
!(logic.recentLoading && logic.watchRecordTotal == null)
|
||||
? logic.watchRecordTotal
|
||||
: null;
|
||||
final coinBalance = auth.isSignedIn && wallet.snapshot != null
|
||||
? wallet.balance
|
||||
: null;
|
||||
@@ -216,7 +263,7 @@ class _MyPageState extends State<MyPage> {
|
||||
void _openDestination(MineDestination destination) {
|
||||
switch (destination) {
|
||||
case MineDestination.history:
|
||||
_push(const WatchRecordsPage());
|
||||
unawaited(_openHistoryPage());
|
||||
case MineDestination.downloads:
|
||||
_push(const DownloadsPage());
|
||||
case MineDestination.wallet:
|
||||
@@ -244,15 +291,22 @@ class _MyPageState extends State<MyPage> {
|
||||
callback();
|
||||
return;
|
||||
}
|
||||
_push(const WatchRecordsPage());
|
||||
unawaited(_openHistoryPage());
|
||||
}
|
||||
|
||||
void _push(Widget page) {
|
||||
Navigator.of(context).push<void>(MaterialPageRoute(builder: (_) => page));
|
||||
Future<void> _openHistoryPage() async {
|
||||
await _push(const WatchRecordsPage());
|
||||
if (mounted) unawaited(logic.refreshRecent());
|
||||
}
|
||||
|
||||
Future<void> _push(Widget page) {
|
||||
return Navigator.of(
|
||||
context,
|
||||
).push<void>(MaterialPageRoute(builder: (_) => page));
|
||||
}
|
||||
|
||||
void _pushPlaceholder() {
|
||||
_push(const _MinePlaceholderPage());
|
||||
unawaited(_push(const _MinePlaceholderPage()));
|
||||
}
|
||||
|
||||
Future<void> _openProtected(Widget page) async {
|
||||
@@ -260,7 +314,7 @@ class _MyPageState extends State<MyPage> {
|
||||
await ensureSignedIn(context);
|
||||
return;
|
||||
}
|
||||
if (mounted) _push(page);
|
||||
if (mounted) await _push(page);
|
||||
}
|
||||
|
||||
void _openVip() {
|
||||
|
||||
Reference in new issue
Block a user