106 lines
3.2 KiB
Dart
106 lines
3.2 KiB
Dart
import 'dart:convert';
|
||
|
||
import 'package:flutter/foundation.dart';
|
||
import 'package:yumi/shared/data_sources/sources/local/data_persistence.dart';
|
||
|
||
class LastWeeklyCPSplashEntry {
|
||
const LastWeeklyCPSplashEntry({
|
||
required this.rank,
|
||
required this.leftAvatarUrl,
|
||
required this.rightAvatarUrl,
|
||
required this.leftNickname,
|
||
required this.rightNickname,
|
||
});
|
||
|
||
final int rank;
|
||
final String leftAvatarUrl;
|
||
final String rightAvatarUrl;
|
||
final String leftNickname;
|
||
final String rightNickname;
|
||
|
||
factory LastWeeklyCPSplashEntry.fromJson(Map<String, dynamic> json) {
|
||
return LastWeeklyCPSplashEntry(
|
||
rank: (json['rank'] as num?)?.toInt() ?? 0,
|
||
leftAvatarUrl: (json['leftAvatarUrl'] as String?) ?? '',
|
||
rightAvatarUrl: (json['rightAvatarUrl'] as String?) ?? '',
|
||
leftNickname: (json['leftNickname'] as String?) ?? '',
|
||
rightNickname: (json['rightNickname'] as String?) ?? '',
|
||
);
|
||
}
|
||
|
||
Map<String, dynamic> toJson() {
|
||
return {
|
||
'rank': rank,
|
||
'leftAvatarUrl': leftAvatarUrl,
|
||
'rightAvatarUrl': rightAvatarUrl,
|
||
'leftNickname': leftNickname,
|
||
'rightNickname': rightNickname,
|
||
};
|
||
}
|
||
}
|
||
|
||
class LastWeeklyCPSplashCache {
|
||
static const String _cacheKey = 'last_weekly_cp_splash_cache_v1';
|
||
|
||
/// 搜索 `api-ready-launch-splash` 可以快速找到后续要恢复接入的位置。
|
||
///
|
||
/// 当前 CP 榜接口还没 ready,因此先彻底关闭这套自定义启动页。
|
||
/// 正式恢复时保持“本地有缓存才展示、无缓存不展示”的逻辑,不要再写占位缓存。
|
||
static const bool _allowCacheDrivenSplash = false;
|
||
|
||
static bool get shouldShow {
|
||
if (!_allowCacheDrivenSplash) {
|
||
return false;
|
||
}
|
||
return loadCachedEntries().isNotEmpty;
|
||
}
|
||
|
||
static List<LastWeeklyCPSplashEntry> loadDisplayEntries() {
|
||
if (!_allowCacheDrivenSplash) {
|
||
return const [];
|
||
}
|
||
return loadCachedEntries();
|
||
}
|
||
|
||
static List<LastWeeklyCPSplashEntry> loadCachedEntries() {
|
||
final raw = DataPersistence.getString(_cacheKey);
|
||
if (raw.isEmpty) {
|
||
return const [];
|
||
}
|
||
|
||
try {
|
||
final decoded = jsonDecode(raw);
|
||
if (decoded is! List) {
|
||
return const [];
|
||
}
|
||
return decoded
|
||
.whereType<Map>()
|
||
.map(
|
||
(item) => LastWeeklyCPSplashEntry.fromJson(
|
||
Map<String, dynamic>.from(item),
|
||
),
|
||
)
|
||
.toList()
|
||
..sort((a, b) => a.rank.compareTo(b.rank));
|
||
} catch (error, stackTrace) {
|
||
debugPrint('LastWeeklyCPSplashCache parse failed: $error\n$stackTrace');
|
||
return const [];
|
||
}
|
||
}
|
||
|
||
static Future<void> refreshCacheInBackground() async {
|
||
// TODO(api-ready-launch-splash): CP 榜接口 ready 后,把 `_allowCacheDrivenSplash`
|
||
// 改为 true,并在这里补上“接口取数 -> 映射 LastWeeklyCPSplashEntry ->
|
||
// 写入 `_cacheKey`”的流程。保留“有缓存才展示、无缓存不展示”的正式逻辑。
|
||
if (!_allowCacheDrivenSplash) {
|
||
return;
|
||
}
|
||
|
||
try {
|
||
// 当前占位逻辑已下线,等待正式 CP 榜接口接入。
|
||
} catch (error, stackTrace) {
|
||
debugPrint('LastWeeklyCPSplashCache refresh failed: $error\n$stackTrace');
|
||
}
|
||
}
|
||
}
|