Compare commits

...

6 Commits

Author SHA1 Message Date
roxy
f2e9ac3c60 bug fix 2026-05-12 10:56:18 +08:00
roxy
92bbbcb0eb svg资源释放 2026-05-11 14:08:43 +08:00
roxy
829914c58c 即时红包领取层弹窗加个本地兜底 2026-05-11 13:39:49 +08:00
roxy
39c5fbf208 即时红包领取层 2026-05-11 13:11:42 +08:00
roxy
2a1b63de24 内存泄露闪退以及切房公屏显示问题 2026-05-11 11:31:48 +08:00
roxy
fec672dfb6 红包bug 修复 2026-05-11 10:21:47 +08:00
30 changed files with 889 additions and 234 deletions

View File

@ -502,7 +502,7 @@
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 11;
CURRENT_PROJECT_VERSION = 12;
DEVELOPMENT_TEAM = S9X2AJ2US9;
ENABLE_BITCODE = NO;
INFOPLIST_FILE = Runner/Info.plist;
@ -511,7 +511,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.3.0;
MARKETING_VERSION = 1.3.1;
PRODUCT_BUNDLE_IDENTIFIER = com.org.yumiparty;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
@ -693,7 +693,7 @@
CODE_SIGN_ENTITLEMENTS = Runner/RunnerDebug.entitlements;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 11;
CURRENT_PROJECT_VERSION = 12;
DEVELOPMENT_TEAM = F33K8VUZ62;
ENABLE_BITCODE = NO;
INFOPLIST_FILE = Runner/Info.plist;
@ -702,7 +702,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.3.0;
MARKETING_VERSION = 1.3.1;
PRODUCT_BUNDLE_IDENTIFIER = com.org.yumiparty;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
@ -722,7 +722,7 @@
CODE_SIGN_ENTITLEMENTS = Runner/RunnerRelease.entitlements;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 11;
CURRENT_PROJECT_VERSION = 12;
DEVELOPMENT_TEAM = F33K8VUZ62;
ENABLE_BITCODE = NO;
INFOPLIST_FILE = Runner/Info.plist;
@ -731,7 +731,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.3.0;
MARKETING_VERSION = 1.3.1;
PRODUCT_BUNDLE_IDENTIFIER = com.org.yumiparty;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";

View File

@ -1,11 +1,13 @@
import 'package:fluro/fluro.dart' as fluro;
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:yumi/app/routes/sc_router_init.dart';
import 'package:yumi/modules/country/country_route.dart';
import 'package:yumi/modules/index/main_route.dart';
import 'package:yumi/modules/chat/chat_route.dart';
import 'package:yumi/modules/store/store_route.dart';
import 'package:yumi/modules/index/index_page.dart';
import 'package:yumi/services/room/rc_room_manager.dart';
import 'package:yumi/modules/user/settings/settings_route.dart';
import 'package:yumi/modules/user/task/task_route.dart';
import 'package:yumi/modules/user/vip/vip_route.dart';
@ -32,7 +34,7 @@ class SCRoutes {
handler: fluro.Handler(
handlerFunc:
(BuildContext? context, Map<String, List<String>> params) =>
SCIndexPage(),
_buildHomePage(context),
),
);
@ -57,6 +59,13 @@ class SCRoutes {
routerProvider.initRouter(router);
}
}
static Widget _buildHomePage(BuildContext? context) {
return ChangeNotifierProvider<SocialChatRoomManager>.value(
value: resolveSocialChatRoomManager(context),
child: const SCIndexPage(),
);
}
}
// PS使

View File

@ -26,6 +26,7 @@ import 'app/routes/sc_routes.dart';
import 'app/routes/sc_lk_application.dart';
import 'shared/tools/sc_deep_link_handler.dart';
import 'shared/tools/sc_deviceId_utils.dart';
import 'shared/tools/sc_gift_vap_svga_manager.dart';
import 'shared/data_sources/sources/local/user_manager.dart';
import 'modules/splash/splash_page.dart';
import 'services/general/sc_app_general_manager.dart';
@ -45,6 +46,7 @@ import 'services/auth/user_profile_manager.dart';
import 'ui_kit/theme/socialchat_theme.dart';
import 'ui_kit/widgets/room/anim/room_gift_seat_flight_overlay.dart';
import 'ui_kit/widgets/room/effect/vapp_svga_layer_widget.dart';
import 'ui_kit/widgets/svga/sc_svga_asset_widget.dart';
bool _isCrashlyticsReady = false;
@ -105,6 +107,16 @@ void _configureImageCache() {
imageCache.maximumSize = SCGlobalConfig.recommendedImageCacheEntries;
}
void _releaseVisualMemoryCaches({bool clearLiveImages = false}) {
SCGiftVapSvgaManager().clearMemoryCache();
SCSvgaAssetWidget.clearMemoryCache();
final imageCache = PaintingBinding.instance.imageCache;
imageCache.clear();
if (clearLiveImages) {
imageCache.clearLiveImages();
}
}
void _installErrorHandlers() {
FlutterError.onError = (details) {
FlutterError.presentError(details);
@ -229,9 +241,8 @@ class RootAppWithProviders extends StatelessWidget {
),
// Provider -
ChangeNotifierProvider<SocialChatRoomManager>(
lazy: true,
create: (context) => SocialChatRoomManager(),
ChangeNotifierProvider<SocialChatRoomManager>.value(
value: socialChatRoomManager,
),
// Provider -
@ -303,6 +314,14 @@ class _YumiApplicationState extends State<YumiApplication>
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
if (state == AppLifecycleState.paused ||
state == AppLifecycleState.inactive ||
state == AppLifecycleState.hidden) {
SCGiftVapSvgaManager().stopPlayback();
_releaseVisualMemoryCaches(
clearLiveImages: state == AppLifecycleState.paused,
);
}
if (state == AppLifecycleState.detached) {
unawaited(
context.read<RtcProvider>().releaseRtcEngineForAppTermination(),
@ -310,6 +329,13 @@ class _YumiApplicationState extends State<YumiApplication>
}
}
@override
void didHaveMemoryPressure() {
super.didHaveMemoryPressure();
SCGiftVapSvgaManager().stopPlayback();
_releaseVisualMemoryCaches(clearLiveImages: true);
}
Future<void> _initLink() async {
//
await _deepLinkHandler.initDeepLinks(

View File

@ -1,3 +1,4 @@
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
@ -364,7 +365,7 @@ class SCLoginWithAccountPageState extends State<SCLoginWithAccountPage>
}
///
_login() async {
Future<void> _login() async {
String account = accountController.text;
String pass = passController.text;
if (account.isEmpty || pass.isEmpty) {
@ -394,15 +395,47 @@ class SCLoginWithAccountPageState extends State<SCLoginWithAccountPage>
}
AccountStorage().setCurrentUser(user);
SCLoadingManager.hide();
DataPersistence.setString("Login_Account", account);
DataPersistence.setString("Login_Pwd", pass);
try {
await Future.wait([
DataPersistence.setString("Login_Account", account),
DataPersistence.setString("Login_Pwd", pass),
]);
} catch (_) {}
if (mounted) {
SCNavigatorUtils.push(context, SCRoutes.home, clearStack: true);
}
} catch (e) {
SCLoadingManager.hide();
//
rethrow;
final message = _loginErrorMessage(e);
if (message.isNotEmpty) {
SCTts.show(message);
}
}
}
String _loginErrorMessage(Object error) {
if (error is DioException) {
final responseData = error.response?.data;
if (responseData is Map) {
final serverMessage = responseData["errorMsg"]?.toString().trim();
if (serverMessage != null && serverMessage.isNotEmpty) {
return serverMessage;
}
}
final dioError = error.error?.toString().trim();
if (dioError != null && dioError.isNotEmpty) {
return dioError.replaceFirst("Exception: ", "");
}
final message = error.message?.trim();
if (message != null && message.isNotEmpty) {
return message;
}
}
final message = error.toString().replaceFirst("Exception: ", "").trim();
if (message.isNotEmpty) {
return message;
}
return "Login failed. Please try again.";
}
}

View File

@ -640,7 +640,9 @@ class _SCEditProfilePageState extends State<SCEditProfilePage> {
authType = SCAuthType.GOOGLE.name;
idToken = googleUid;
}
} catch (e) {}
} catch (_) {
// Fallback auth lookup is best effort; the form will ask to sign in again below.
}
}
if (authType.isEmpty || idToken.isEmpty) {
SCLoadingManager.hide();

View File

@ -563,7 +563,8 @@ class _SCIndexPageState extends State<SCIndexPage>
return;
}
await SCEntryPopupCoordinator.openFirstPartyRoomRandomGame(context);
} catch (error) {
} catch (_) {
// The entry popup is optional; failure should not block the home page.
} finally {
_isOpeningFirstRegisterRoomGame = false;
}

View File

@ -21,6 +21,7 @@ class _AllChatPageState extends State<AllChatPage> {
bool _isDisposed = false;
final List<Msg> _msgList = [];
late RtmProvider provider;
late final RoomNewMsgListener _newMsgListener = _onNewMsg;
@override
void initState() {
@ -28,7 +29,7 @@ class _AllChatPageState extends State<AllChatPage> {
_controller.addListener(_scrollListener);
provider = Provider.of<RtmProvider>(context, listen: false);
final msgList = provider.roomAllMsgList;
provider.msgAllListener = _onNewMsg;
provider.msgAllListener = _newMsgListener;
_msgList.addAll(msgList);
// 使
_initScrollPosition();
@ -81,7 +82,9 @@ class _AllChatPageState extends State<AllChatPage> {
@override
void dispose() {
_isDisposed = true;
provider.msgAllListener = null;
if (identical(provider.msgAllListener, _newMsgListener)) {
provider.msgAllListener = null;
}
_controller.removeListener(_scrollListener);
_controller.dispose();
super.dispose();

View File

@ -21,6 +21,7 @@ class _ChatPageState extends State<ChatPage> {
bool _isDisposed = false;
List<Msg> _msgList = [];
late RtmProvider provider;
late final RoomNewMsgListener _newMsgListener = _onNewMsg;
@override
void initState() {
@ -28,7 +29,7 @@ class _ChatPageState extends State<ChatPage> {
_controller.addListener(_scrollListener);
provider = Provider.of<RtmProvider>(context, listen: false);
var msgList = provider.roomChatMsgList;
provider.msgChatListener = _onNewMsg;
provider.msgChatListener = _newMsgListener;
_msgList.addAll(msgList ??= []);
// 使
_initScrollPosition();
@ -80,7 +81,9 @@ class _ChatPageState extends State<ChatPage> {
@override
void dispose() {
_isDisposed = true;
provider.msgChatListener = null;
if (identical(provider.msgChatListener, _newMsgListener)) {
provider.msgChatListener = null;
}
_controller.removeListener(_scrollListener);
_controller.dispose();
super.dispose();

View File

@ -21,6 +21,7 @@ class _GiftChatPageState extends State<GiftChatPage> {
bool _isDisposed = false;
final List<Msg> _msgList = [];
late RtmProvider provider;
late final RoomNewMsgListener _newMsgListener = _onNewMsg;
@override
void initState() {
@ -28,7 +29,7 @@ class _GiftChatPageState extends State<GiftChatPage> {
_controller.addListener(_scrollListener);
provider = Provider.of<RtmProvider>(context, listen: false);
final msgList = provider.roomGiftMsgList;
provider.msgGiftListener = _onNewMsg;
provider.msgGiftListener = _newMsgListener;
_msgList.addAll(msgList);
// 使
_initScrollPosition();
@ -81,7 +82,9 @@ class _GiftChatPageState extends State<GiftChatPage> {
@override
void dispose() {
_isDisposed = true;
provider.msgGiftListener = null;
if (identical(provider.msgGiftListener, _newMsgListener)) {
provider.msgGiftListener = null;
}
_controller.removeListener(_scrollListener);
_controller.dispose();
super.dispose();

View File

@ -52,6 +52,7 @@ class VoiceRoomPage extends StatefulWidget {
class _VoiceRoomPageState extends State<VoiceRoomPage>
with SingleTickerProviderStateMixin {
static const int _chatTabCount = 3;
static const Duration _luckyGiftComboWindow = Duration(seconds: 3);
static const Duration _luckyGiftQueueDrainWindow = Duration(seconds: 3);
static const int _maxLuckyGiftTrackedAnimations = 5;
@ -65,7 +66,6 @@ class _VoiceRoomPageState extends State<VoiceRoomPage>
static const Duration _giftVisualBatchDedupeWindow = Duration(seconds: 4);
late TabController _tabController;
final List<Widget> _pages = [AllChatPage(), ChatPage(), GiftChatPage()];
late StreamSubscription _subscription;
final RoomGiftSeatFlightController _giftSeatFlightController =
RoomGiftSeatFlightController();
@ -80,7 +80,7 @@ class _VoiceRoomPageState extends State<VoiceRoomPage>
@override
void initState() {
super.initState();
_tabController = TabController(length: _pages.length, vsync: this);
_tabController = TabController(length: _chatTabCount, vsync: this);
_enableRoomVisualEffects();
_tabController.addListener(_handleTabChange);
_subscription = eventBus.on<SCGiveRoomLuckPageDisposeEvent>().listen((
@ -277,6 +277,7 @@ class _VoiceRoomPageState extends State<VoiceRoomPage>
@override
Widget build(BuildContext context) {
final chatRoomKey = _chatRoomKey(context);
return PopScope(
canPop: false,
onPopInvokedWithResult: (bool didPop, Object? result) {
@ -334,7 +335,7 @@ class _VoiceRoomPageState extends State<VoiceRoomPage>
children: [
Column(
children: [
_buildChatView(),
_buildChatView(chatRoomKey),
const RoomBottomWidget(showGiftComboButton: false),
],
),
@ -373,7 +374,7 @@ class _VoiceRoomPageState extends State<VoiceRoomPage>
}
///
Widget _buildChatView() {
Widget _buildChatView(String chatRoomKey) {
return Expanded(
child: Stack(
alignment: AlignmentDirectional.bottomEnd,
@ -418,7 +419,7 @@ class _VoiceRoomPageState extends State<VoiceRoomPage>
SCGlobalConfig.businessLogicStrategy
.getVoiceRoomTabDividerColor(),
controller: _tabController,
tabs: List<Widget>.generate(_pages.length, _buildImageTab),
tabs: List<Widget>.generate(_chatTabCount, _buildImageTab),
),
Expanded(
child: Container(
@ -436,7 +437,7 @@ class _VoiceRoomPageState extends State<VoiceRoomPage>
removeTop: true,
child: TabBarView(
controller: _tabController,
children: _pages,
children: _buildChatPages(chatRoomKey),
),
),
),
@ -449,6 +450,28 @@ class _VoiceRoomPageState extends State<VoiceRoomPage>
);
}
String _chatRoomKey(BuildContext context) {
final roomProfile =
context.read<RtcProvider>().currenRoom?.roomProfile?.roomProfile;
final roomId = roomProfile?.id?.trim() ?? "";
final groupId = roomProfile?.roomAccount?.trim() ?? "";
return "$roomId|$groupId";
}
List<Widget> _buildChatPages(String chatRoomKey) {
return <Widget>[
KeyedSubtree(
key: ValueKey("all_chat_$chatRoomKey"),
child: const AllChatPage(),
),
KeyedSubtree(key: ValueKey("chat_$chatRoomKey"), child: ChatPage()),
KeyedSubtree(
key: ValueKey("gift_chat_$chatRoomKey"),
child: const GiftChatPage(),
),
];
}
Widget _buildImageTab(int index) {
return Tab(
height: 32.w,

View File

@ -1,5 +1,3 @@
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svga/flutter_svga.dart';
@ -566,6 +564,7 @@ class _GiftWallSvgaEffectOverlay extends StatefulWidget {
class _GiftWallSvgaEffectOverlayState extends State<_GiftWallSvgaEffectOverlay>
with SingleTickerProviderStateMixin {
late final SVGAAnimationController _controller;
MovieEntity? _retainedMovieEntity;
bool _loaded = false;
bool _finished = false;
@ -585,9 +584,15 @@ class _GiftWallSvgaEffectOverlayState extends State<_GiftWallSvgaEffectOverlay>
Future<void> _loadAndPlay() async {
try {
final movieEntity = await _loadMovieEntity(widget.sourceUrl);
if (!mounted || _finished) return;
final movieEntity = await SCGiftVapSvgaManager().acquireSvgaEntity(
widget.sourceUrl,
);
if (!mounted || _finished) {
SCGiftVapSvgaManager().releaseSvgaEntity(movieEntity);
return;
}
setState(() {
_retainedMovieEntity = movieEntity;
_loaded = true;
_controller.videoItem = movieEntity;
});
@ -599,28 +604,6 @@ class _GiftWallSvgaEffectOverlayState extends State<_GiftWallSvgaEffectOverlay>
}
}
Future<MovieEntity> _loadMovieEntity(String sourceUrl) async {
final cache = SCGiftVapSvgaManager().videoItemCache;
final cached = cache[sourceUrl];
if (cached != null) return cached;
final pathType = SCPathUtils.getPathType(sourceUrl);
late final MovieEntity movieEntity;
if (pathType == PathType.network) {
movieEntity = await SVGAParser.shared.decodeFromURL(sourceUrl);
} else if (pathType == PathType.asset) {
movieEntity = await SVGAParser.shared.decodeFromAssets(sourceUrl);
} else if (pathType == PathType.file) {
final bytes = await File(sourceUrl).readAsBytes();
movieEntity = await SVGAParser.shared.decodeFromBuffer(bytes);
} else {
throw Exception("Unsupported SVGA source: $sourceUrl");
}
movieEntity.autorelease = false;
cache[sourceUrl] = movieEntity;
return movieEntity;
}
void _finish() {
if (_finished) return;
_finished = true;
@ -631,6 +614,8 @@ class _GiftWallSvgaEffectOverlayState extends State<_GiftWallSvgaEffectOverlay>
void dispose() {
_controller.removeStatusListener(_handleStatusChanged);
_controller.dispose();
SCGiftVapSvgaManager().releaseSvgaEntity(_retainedMovieEntity);
_retainedMovieEntity = null;
super.dispose();
}

View File

@ -2708,13 +2708,7 @@ class RealTimeCommunicationManager extends ChangeNotifier {
return;
}
_startRoomStatePolling();
final currentContext = context;
if (currentContext != null && currentContext.mounted) {
Provider.of<SocialChatRoomManager>(
currentContext,
listen: false,
).fetchContributionLevelData(roomId);
}
socialChatRoomManager.fetchContributionLevelData(roomId);
failureType = RoomStartupFailureType.rtc;
final rtcTokenResult = await rtcTokenFuture;
@ -3400,13 +3394,7 @@ class RealTimeCommunicationManager extends ChangeNotifier {
void _clearRoomContributionLevelData([BuildContext? targetContext]) {
final currentContext = targetContext ?? context;
if (currentContext == null || !currentContext.mounted) {
return;
}
Provider.of<SocialChatRoomManager>(
currentContext,
listen: false,
).clearContributionLevelData();
resolveSocialChatRoomManager(currentContext).clearContributionLevelData();
}
@override

View File

@ -1048,6 +1048,9 @@ class RealTimeMessagingManager extends ChangeNotifier {
lastMsgID: null,
);
final messages = result.data ?? const <V2TimMessage>[];
if (!_isCurrentVoiceRoomGroup(normalizedGroupId)) {
return;
}
for (final message in messages.reversed) {
_addRoomHistoryMessage(normalizedGroupId, message);
}
@ -1722,6 +1725,11 @@ class RealTimeMessagingManager extends ChangeNotifier {
'';
}
bool _isCurrentVoiceRoomGroup(String groupId) {
final currentGroupId = _currentVoiceRoomGroupId();
return currentGroupId.isNotEmpty && currentGroupId == groupId.trim();
}
bool _shouldRouteUntrackedRoomRedPacketBroadcast(
String groupId,
V2TimMessage message,
@ -1815,7 +1823,8 @@ class RealTimeMessagingManager extends ChangeNotifier {
if (!_isCurrentVisibleVoiceRoom(payloadRoomId)) {
return;
}
if (_isCurrentUserRoomRedPacketPayload(payload)) {
if (_isCurrentUserRoomRedPacketPayload(payload) &&
_isDelayedRoomRedPacketPayload(payload)) {
return;
}
final dialogData = RoomRedPacketUiData.fromJson(payloadMap);
@ -1956,8 +1965,11 @@ class RealTimeMessagingManager extends ChangeNotifier {
///
var fData = data["data"];
final payload = _broadcastPayloadMap(fData);
final packetId = _payloadText(payload['packetId']);
final packetRoomId = _payloadText(payload["roomId"]);
if (isRegionBroadcastGroup &&
!_isDelayedRoomRedPacketPayload(fData)) {
return;
}
if (!_isSameRoomRedPacketRegion(fData)) {
return;
}
@ -2554,6 +2566,7 @@ class RealTimeMessagingManager extends ChangeNotifier {
Future<void> sendRoomRedPacketBroadcast(
Map<String, dynamic> payload, {
String roomGroupId = '',
bool includeRegionBroadcast = false,
}) async {
if (payload.isEmpty) {
return;
@ -2564,13 +2577,15 @@ class RealTimeMessagingManager extends ChangeNotifier {
targetGroupIds.add(normalizedRoomGroupId);
}
var regionGroupId = roomRedPacketBroadcastGroupId?.trim() ?? '';
if (regionGroupId.isEmpty) {
await syncRoomRedPacketBroadcastGroup();
regionGroupId = roomRedPacketBroadcastGroupId?.trim() ?? '';
}
if (regionGroupId.isNotEmpty) {
targetGroupIds.add(regionGroupId);
if (includeRegionBroadcast) {
var regionGroupId = roomRedPacketBroadcastGroupId?.trim() ?? '';
if (regionGroupId.isEmpty) {
await syncRoomRedPacketBroadcastGroup();
regionGroupId = roomRedPacketBroadcastGroupId?.trim() ?? '';
}
if (regionGroupId.isNotEmpty) {
targetGroupIds.add(regionGroupId);
}
}
if (targetGroupIds.isEmpty) {
@ -2620,6 +2635,18 @@ class RealTimeMessagingManager extends ChangeNotifier {
return _isSameRegionBroadcastPayload(payload);
}
bool _isDelayedRoomRedPacketPayload(dynamic payload) {
final map = _broadcastPayloadMap(payload);
final packetMode = _payloadText(map['packetMode']).toUpperCase();
if (packetMode == 'DELAYED') {
return true;
}
if (packetMode == 'IMMEDIATE') {
return false;
}
return _payloadText(map['claimStartTime']).isNotEmpty;
}
bool _isSameRegionBroadcastPayload(dynamic payload) {
final map = _broadcastPayloadMap(payload);
final packetRegion = map['regionCode']?.toString().trim();

View File

@ -32,8 +32,12 @@ class SocialChatAuthenticationManager extends ChangeNotifier {
}
Future<void> _initializeAuthentication() async {
await SCGoogleAuthService.initialize();
await _verifyExistingSession();
try {
await SCGoogleAuthService.initialize();
await _verifyExistingSession();
} catch (_) {
// Login buttons should remain usable even if silent Google restore fails.
}
}
Future<void> _verifyExistingSession() async {
@ -51,10 +55,11 @@ class SocialChatAuthenticationManager extends ChangeNotifier {
if (authType == SCAuthType.GOOGLE.name) {
this.authType = authType;
try {
_user ??= await SCGoogleAuthService.signInWithGoogle();
if (_user == null) {
final signedInUser = await SCGoogleAuthService.signInWithGoogle();
if (signedInUser == null) {
return;
}
_user = signedInUser;
uid = _user!.uid;
SCLoadingManager.show();
if (uid.isNotEmpty) {

View File

@ -1,4 +1,5 @@
import 'package:flutter/cupertino.dart';
import 'package:provider/provider.dart';
import 'package:yumi/app_localizations.dart';
import 'package:yumi/ui_kit/components/sc_tts.dart';
import 'package:yumi/shared/tools/sc_loading_manager.dart';
@ -9,6 +10,25 @@ import 'package:yumi/shared/business_logic/models/res/sc_edit_room_info_res.dart
import 'package:yumi/shared/business_logic/models/res/my_room_res.dart';
import 'package:yumi/shared/business_logic/models/res/sc_room_contribute_level_res.dart';
final SocialChatRoomManager socialChatRoomManager = SocialChatRoomManager();
SocialChatRoomManager resolveSocialChatRoomManager(BuildContext? context) {
if (context != null && context.mounted) {
try {
final manager = Provider.of<SocialChatRoomManager?>(
context,
listen: false,
);
if (manager != null) {
return manager;
}
} catch (_) {
// A stale route context can be outside the provider tree after logout.
}
}
return socialChatRoomManager;
}
class SocialChatRoomManager extends ChangeNotifier {
MyRoomRes? myRoom;
SCRoomContributeLevelRes? roomContributeLevelRes;

View File

@ -18,6 +18,8 @@ class DataPersistence {
static bool _isInitialized = false;
static Completer<void>? _initializationCompleter;
static bool get isInitialized => _isInitialized && _prefs != null;
// SharedPreferences
static Future<void> initialize() async {
if (_isInitialized) return;
@ -48,9 +50,17 @@ class DataPersistence {
}
}
static Future<void> _ensureInitialized() async {
if (isInitialized) {
return;
}
await initialize();
_checkInitialized();
}
//
static Future<bool> setString(String key, String value) async {
_checkInitialized();
await _ensureInitialized();
return await _prefs!.setString(key, value);
}
@ -62,7 +72,7 @@ class DataPersistence {
//
static Future<bool> setInt(String key, int value) async {
_checkInitialized();
await _ensureInitialized();
return await _prefs!.setInt(key, value);
}
@ -74,7 +84,7 @@ class DataPersistence {
//
static Future<bool> setBool(String key, bool value) async {
_checkInitialized();
await _ensureInitialized();
return await _prefs!.setBool(key, value);
}
@ -86,7 +96,7 @@ class DataPersistence {
//
static Future<bool> setDouble(String key, double value) async {
_checkInitialized();
await _ensureInitialized();
return await _prefs!.setDouble(key, value);
}

View File

@ -584,6 +584,9 @@ class OverlayManager {
if (message.isRegionBroadcast) {
return _shouldDisplayRegionBroadcastMessage(context, message);
}
if (message.type == 4) {
return _isCurrentRoomFloatingMessage(context, message);
}
if (SCGlobalConfig.isFloatingBroadcastRoomOnly) {
return _isCurrentRoomFloatingMessage(context, message);
}

View File

@ -38,7 +38,10 @@ class AccountStorage {
}
var userJson = DataPersistence.getCurrentUser();
if (userJson.isNotEmpty) {
_currentUser = SocialChatLoginRes.fromJson(jsonDecode(userJson));
_currentUser = _decodeCurrentUser(userJson);
if (_currentUser == null) {
unawaited(_clearPersistedSession(clearDurable: false));
}
}
return _currentUser;
}
@ -46,16 +49,10 @@ class AccountStorage {
///
void setCurrentUser(SocialChatLoginRes user) {
_currentUser = user;
setToken(_currentUser!.token ?? "");
token = _currentUser!.token ?? "";
String userJson = jsonEncode(_currentUser?.toJson());
if (userJson.isNotEmpty) {
DataPersistence.setCurrentUser(userJson);
unawaited(
DurableAuthStorage.saveSession(
token: _currentUser!.token ?? "",
currentUserJson: userJson,
),
);
unawaited(_persistSession(token: token, currentUserJson: userJson));
}
}
@ -130,54 +127,110 @@ class AccountStorage {
Future<void> restoreDurableSessionIfNeeded() async {
final localToken = DataPersistence.getToken().trim();
final localUserJson = DataPersistence.getCurrentUser().trim();
if (localToken.isNotEmpty && localUserJson.isNotEmpty) {
final localUser = _decodeCurrentUser(localUserJson);
if (localToken.isNotEmpty && localUser != null) {
token = localToken;
_currentUser = localUser;
return;
}
final session = await DurableAuthStorage.readSession();
if (session == null) {
if (localToken.isNotEmpty || localUserJson.isNotEmpty) {
unawaited(_clearPersistedSession(clearDurable: false));
}
return;
}
token = session.token;
await DataPersistence.setToken(session.token);
await DataPersistence.setCurrentUser(session.currentUserJson);
try {
_currentUser = SocialChatLoginRes.fromJson(
jsonDecode(session.currentUserJson),
);
} catch (_) {
_currentUser = null;
_currentUser = _decodeCurrentUser(session.currentUserJson);
if (_currentUser == null) {
token = "";
await _clearPersistedSession();
return;
}
await _persistSession(
token: session.token,
currentUserJson: session.currentUserJson,
);
}
void setToken(String tk) {
token = tk;
DataPersistence.setToken(token);
unawaited(_persistToken(token));
}
void _cleanUser() {
token = "";
_currentUser = null;
DataPersistence.setToken("");
DataPersistence.setCurrentUser("");
unawaited(_clearPersistedSession());
unawaited(DataPersistence.clearPendingChannelAuth());
unawaited(DurableAuthStorage.clearSession());
}
///退
void logout(BuildContext context) {
_cleanUser();
SCHeartbeatUtils.cancelTimer();
Provider.of<RtcProvider>(
context,
listen: false,
).exitCurrentVoiceRoomSession(true);
Provider.of<RtmProvider>(context, listen: false).logout();
try {
Provider.of<RtcProvider>(
context,
listen: false,
).exitCurrentVoiceRoomSession(true);
} catch (_) {}
try {
Provider.of<RtmProvider>(context, listen: false).logout();
} catch (_) {}
SCNavigatorUtils.pushLoginIfNeeded(context, clearStack: true);
SCRoomUtils.closeAllDialogs();
try {
SCRoomUtils.closeAllDialogs();
} catch (_) {}
SCMessageUtils.redPacketFutureCache.clear();
SCGlobalConfig.resetVisualEffectSwitchesToRecommendedDefaults();
OverlayManager().dispose();
try {
OverlayManager().dispose();
} catch (_) {}
}
SocialChatLoginRes? _decodeCurrentUser(String userJson) {
if (userJson.trim().isEmpty) {
return null;
}
try {
final decoded = jsonDecode(userJson);
if (decoded is Map) {
return SocialChatLoginRes.fromJson(Map<String, dynamic>.from(decoded));
}
} catch (_) {}
return null;
}
Future<void> _persistToken(String value) async {
try {
await DataPersistence.setToken(value);
} catch (_) {}
}
Future<void> _persistSession({
required String token,
required String currentUserJson,
}) async {
try {
await DataPersistence.setToken(token);
await DataPersistence.setCurrentUser(currentUserJson);
} catch (_) {}
await DurableAuthStorage.saveSession(
token: token,
currentUserJson: currentUserJson,
);
}
Future<void> _clearPersistedSession({bool clearDurable = true}) async {
try {
await DataPersistence.setToken("");
await DataPersistence.setCurrentUser("");
} catch (_) {}
if (clearDurable) {
await DurableAuthStorage.clearSession();
}
}
}

View File

@ -107,6 +107,7 @@ Future<bool> _isCurrentSessionStillValid(DioException e) async {
class BaseNetworkClient {
final Dio dio;
static const String silentErrorToastKey = 'silentErrorToast';
static const String suppressAuthLogoutKey = 'suppressAuthLogout';
static const String baseUrlOverrideKey = 'baseUrlOverride';
BaseNetworkClient() : dio = Dio() {
@ -292,6 +293,13 @@ class BaseNetworkClient {
SCLoadingManager.hide();
final bool silentErrorToast =
e.requestOptions.extra[BaseNetworkClient.silentErrorToastKey] == true;
final handledBusinessError = await _handleBusinessErrorResponse(
e,
silentErrorToast: silentErrorToast,
);
if (handledBusinessError != null) {
return handledBusinessError;
}
switch (e.type) {
case DioExceptionType.connectionTimeout:
return DioException(requestOptions: e.requestOptions, error: '连接超时');
@ -300,50 +308,10 @@ class BaseNetworkClient {
case DioExceptionType.receiveTimeout:
return DioException(requestOptions: e.requestOptions, error: '接收超时');
case DioExceptionType.badResponse:
final responseData = e.response?.data;
final errorCode =
responseData is Map ? responseData["errorCode"] : null;
final errorMsg =
responseData is Map
? responseData["errorMsg"]
: responseData?.toString();
if (errorCode == SCErroCode.userNotRegistered.code) {
//
SCTts.show("Please register an account first.");
BuildContext? context = navigatorKey.currentContext;
if (context != null) {
SCNavigatorUtils.push(
context,
LoginRouter.editProfile,
replace: false,
);
}
} else if (errorCode == SCErroCode.authUnauthorized.code) {
//token过期
final shouldLogout =
!SCNavigatorUtils.inLoginPage &&
!await _isCurrentSessionStillValid(e);
final BuildContext? context = navigatorKey.currentContext;
if (context != null && context.mounted && shouldLogout) {
AccountStorage().logout(context);
SCNavigatorUtils.inLoginPage = true;
}
} else {
if (errorMsg.toString().endsWith("balance not made")) {
BuildContext? context = navigatorKey.currentContext;
if (context != null) {
SCRoomUtils.goRecharge(context);
}
} else {
if (!silentErrorToast) {
SCTts.show(errorMsg);
}
}
}
return DioException(
requestOptions: e.requestOptions,
response: e.response,
error: '服务器错误: $errorCode ${errorMsg ?? ""}'.trim(),
error: e.error ?? e.message ?? 'Server fail',
);
case DioExceptionType.cancel:
return DioException(
@ -362,6 +330,95 @@ class BaseNetworkClient {
);
}
}
Future<DioException?> _handleBusinessErrorResponse(
DioException e, {
required bool silentErrorToast,
}) async {
final responseData = e.response?.data;
if (responseData == null) {
return null;
}
final errorCode = _readErrorCode(responseData);
final errorMsg = _readErrorMessage(responseData);
if (errorCode == null && errorMsg == null) {
return null;
}
if (errorCode == SCErroCode.userNotRegistered.code) {
// Google/Apple
SCTts.show("Please register an account first.");
final BuildContext? context = navigatorKey.currentContext;
if (context != null && context.mounted) {
SCNavigatorUtils.push(context, LoginRouter.editProfile, replace: false);
}
} else if (errorCode == SCErroCode.authUnauthorized.code) {
// token过期
if (e.requestOptions.extra[BaseNetworkClient.suppressAuthLogoutKey] ==
true) {
return DioException(
requestOptions: e.requestOptions,
response: e.response,
type: e.type,
error: '服务器错误: ${errorCode ?? ""} ${errorMsg ?? ""}'.trim(),
message: e.message,
);
}
final shouldLogout =
!SCNavigatorUtils.inLoginPage &&
!await _isCurrentSessionStillValid(e);
final BuildContext? context = navigatorKey.currentContext;
if (context != null && context.mounted && shouldLogout) {
AccountStorage().logout(context);
SCNavigatorUtils.inLoginPage = true;
}
} else {
final message = errorMsg ?? "";
if (message.endsWith("balance not made")) {
final BuildContext? context = navigatorKey.currentContext;
if (context != null && context.mounted) {
SCRoomUtils.goRecharge(context);
}
} else if (!silentErrorToast && message.isNotEmpty) {
SCTts.show(message);
}
}
return DioException(
requestOptions: e.requestOptions,
response: e.response,
type: e.type,
error: '服务器错误: ${errorCode ?? ""} ${errorMsg ?? ""}'.trim(),
message: e.message,
);
}
int? _readErrorCode(dynamic responseData) {
if (responseData is! Map) {
return null;
}
final value = responseData["errorCode"] ?? responseData["code"];
if (value is int) {
return value;
}
if (value is num) {
return value.toInt();
}
if (value is String) {
return int.tryParse(value);
}
return null;
}
String? _readErrorMessage(dynamic responseData) {
if (responseData is Map) {
return responseData["errorMsg"]?.toString() ??
responseData["message"]?.toString();
}
final message = responseData?.toString().trim();
return message == null || message.isEmpty ? null : message;
}
}
class NotSuccessException implements Exception {

View File

@ -213,6 +213,10 @@ class SCAccountRepository implements SocialChatUserRepository {
final result = await http.post(
"58f9485b5b9f401c6a14bc1e6534122c7a085bb5f6a613094188ca56fe8bc03d",
data: parm,
extra: const {
BaseNetworkClient.silentErrorToastKey: true,
BaseNetworkClient.suppressAuthLogoutKey: true,
},
fromJson: (json) => {},
);
return result;
@ -352,6 +356,10 @@ class SCAccountRepository implements SocialChatUserRepository {
final result = await http.get(
"d28c7d4eeb945fb765c8206c8f79d77f",
queryParams: {"roomId": roomId},
extra: const {
BaseNetworkClient.silentErrorToastKey: true,
BaseNetworkClient.suppressAuthLogoutKey: true,
},
fromJson: (json) => {},
);
return result;

View File

@ -23,7 +23,8 @@ class SCGiftVapSvgaManager {
static const int _maxConsecutiveEntryTasksBeforeGift = 2;
static const Duration _taskWatchdogTimeout = Duration(seconds: 20);
static const Duration _entryTaskTtl = Duration(seconds: 6);
Map<String, MovieEntity> videoItemCache = {};
final Map<String, MovieEntity> _videoItemCache = <String, MovieEntity>{};
final Map<MovieEntity, int> _svgaEntityUseCounts = <MovieEntity, int>{};
static SCGiftVapSvgaManager? _inst;
static const int _maxPreloadConcurrency = 1;
static const int _maxPreloadQueueLength = 12;
@ -79,6 +80,44 @@ class SCGiftVapSvgaManager {
return SCPathUtils.getFileExtension(path).toLowerCase() == ".svga";
}
Future<MovieEntity> acquireSvgaEntity(String path) async {
final entity = await _loadSvgaEntity(path);
_retainSvgaEntity(entity);
return entity;
}
void releaseSvgaEntity(MovieEntity? entity) {
if (entity == null) {
return;
}
final count = _svgaEntityUseCounts[entity] ?? 0;
if (count <= 1) {
_svgaEntityUseCounts.remove(entity);
if (!_videoItemCache.containsValue(entity) &&
!_isSvgaEntityInUse(entity)) {
_disposeSvgaEntity(entity);
} else {
_trimSvgaCache();
}
return;
}
_svgaEntityUseCounts[entity] = count - 1;
}
void evictSvgaEntity(String path, {bool includeCurrent = false}) {
final entity = _videoItemCache.remove(path);
if (entity == null) {
return;
}
if (includeCurrent || !_isSvgaEntityInUse(entity)) {
_disposeSvgaEntity(entity);
}
}
void _retainSvgaEntity(MovieEntity entity) {
_svgaEntityUseCounts[entity] = (_svgaEntityUseCounts[entity] ?? 0) + 1;
}
static int resolveEffectPriority({
int priority = 0,
int type = giftEffectType,
@ -116,7 +155,7 @@ class SCGiftVapSvgaManager {
bool _isPreloadedOrLoading(String path) {
if (_needsSvgaController(path)) {
return videoItemCache.containsKey(path) ||
return _videoItemCache.containsKey(path) ||
_svgaLoadTasks.containsKey(path);
}
final pathType = SCPathUtils.getPathType(path);
@ -170,8 +209,9 @@ class SCGiftVapSvgaManager {
'start preload path=$path active=$_activePreloadCount '
'queueRemaining=${_preloadQueue.length}',
);
_warmupPath(path).whenComplete(() {
_activePreloadCount--;
_warmupPath(path).catchError((_) {}).whenComplete(() {
_activePreloadCount =
_activePreloadCount > 0 ? _activePreloadCount - 1 : 0;
_log(
'finish preload path=$path active=$_activePreloadCount '
'queueRemaining=${_preloadQueue.length}',
@ -255,17 +295,20 @@ class SCGiftVapSvgaManager {
}
MovieEntity? _touchCachedSvgaEntity(String path) {
final cached = videoItemCache.remove(path);
final cached = _videoItemCache.remove(path);
if (cached != null) {
videoItemCache[path] = cached;
_videoItemCache[path] = cached;
}
return cached;
}
void _cacheSvgaEntity(String path, MovieEntity entity) {
videoItemCache.remove(path);
videoItemCache[path] = entity;
_trimResolvedCache(videoItemCache, _maxSvgaCacheEntries);
final previous = _videoItemCache.remove(path);
if (previous != null && !identical(previous, entity)) {
_disposeSvgaEntityIfUnused(previous);
}
_videoItemCache[path] = entity;
_trimSvgaCache();
}
String? _touchCachedPlayablePath(String path) {
@ -282,6 +325,22 @@ class SCGiftVapSvgaManager {
_trimResolvedCache(_playablePathCache, _maxPlayablePathCacheEntries);
}
void _trimSvgaCache() {
while (_videoItemCache.length > _maxSvgaCacheEntries) {
final removableEntry = _videoItemCache.entries.firstWhere(
(entry) => !_isSvgaEntityInUse(entry.value),
orElse: () => _videoItemCache.entries.first,
);
if (_isSvgaEntityInUse(removableEntry.value)) {
_videoItemCache.remove(removableEntry.key);
_videoItemCache[removableEntry.key] = removableEntry.value;
break;
}
_videoItemCache.remove(removableEntry.key);
_disposeSvgaEntity(removableEntry.value);
}
}
void _trimResolvedCache<T>(Map<String, T> cache, int maxEntries) {
while (cache.length > maxEntries) {
final oldestKey = cache.keys.first;
@ -554,7 +613,7 @@ class SCGiftVapSvgaManager {
} else {
_finishCurrentTask();
}
} catch (e, s) {
} catch (_) {
_finishCurrentTask();
}
}
@ -728,6 +787,41 @@ class SCGiftVapSvgaManager {
SCRoomEffectScheduler().clearHighCostTasks(reason: 'stop_playback');
}
void clearMemoryCache({bool includeCurrent = false}) {
_preloadQueue.clear();
_queuedPreloadPaths.clear();
_activePreloadCount = 0;
final entries = _videoItemCache.entries.toList(growable: false);
for (final entry in entries) {
final entity = entry.value;
if (!includeCurrent && _isSvgaEntityInUse(entity)) {
continue;
}
_videoItemCache.remove(entry.key);
_disposeSvgaEntity(entity);
}
_playablePathCache.clear();
}
bool _isSvgaEntityInUse(MovieEntity entity) {
return identical(_rsc?.videoItem, entity) ||
(_svgaEntityUseCounts[entity] ?? 0) > 0;
}
void _disposeSvgaEntityIfUnused(MovieEntity entity) {
if (_isSvgaEntityInUse(entity)) {
return;
}
_disposeSvgaEntity(entity);
}
void _disposeSvgaEntity(MovieEntity entity) {
try {
_svgaEntityUseCounts.remove(entity);
entity.dispose();
} catch (_) {}
}
//
void dispose() {
_log('dispose queue=$_queueSummary currentPath=${_currentTask?.path}');
@ -735,7 +829,7 @@ class SCGiftVapSvgaManager {
stopPlayback();
_svgaLoadTasks.clear();
_playablePathTasks.clear();
videoItemCache.clear();
clearMemoryCache(includeCurrent: true);
_playablePathCache.clear();
_rgc?.dispose();
_rgc = null;

View File

@ -55,14 +55,17 @@ class SCHeartbeatUtils {
}
///
static void scheduleAnchorHeartbeat(String roomId) async {
static void scheduleAnchorHeartbeat(String roomId) {
cancelAnchorTimer();
scheduleHeartbeat(_c, true, roomId: roomId);
SCAccountRepository().anchorHeartbeat(roomId).whenComplete(() {
_a ??= Timer.periodic(Duration(seconds: 60), (timer) {
SCAccountRepository().anchorHeartbeat(roomId).catchError((_) => {});
});
});
unawaited(scheduleHeartbeat(_c, true, roomId: roomId));
SCAccountRepository()
.anchorHeartbeat(roomId)
.then((_) {
_a ??= Timer.periodic(Duration(seconds: 60), (timer) {
SCAccountRepository().anchorHeartbeat(roomId).catchError((_) => {});
});
})
.catchError((_) {});
}
static void cancelAnchorTimer() {

View File

@ -48,6 +48,8 @@ class SVGAHeadwearWidget extends StatefulWidget {
class _SVGAHeadwearWidgetState extends State<SVGAHeadwearWidget>
with SingleTickerProviderStateMixin {
SVGAAnimationController? _animationController;
MovieEntity? _retainedMovieEntity;
String? _retainedResource;
bool _isLoading = true;
bool _isNetworkResource = false;
bool _hasError = false;
@ -82,41 +84,53 @@ class _SVGAHeadwearWidgetState extends State<SVGAHeadwearWidget>
});
try {
//
MovieEntity? videoItem;
if (widget.useCache) {
videoItem = SCGiftVapSvgaManager().videoItemCache[widget.resource];
}
final videoItem =
_retainedResource == widget.resource && _retainedMovieEntity != null
? _retainedMovieEntity!
: await SCGiftVapSvgaManager().acquireSvgaEntity(
widget.resource,
);
final acquiredNewEntity = !identical(videoItem, _retainedMovieEntity);
if (!mounted) {
if (acquiredNewEntity) {
SCGiftVapSvgaManager().releaseSvgaEntity(videoItem);
}
return;
}
//
if (videoItem == null) {
videoItem =
setState(() {
_releaseRetainedMovieEntity(except: videoItem);
_retainedMovieEntity = videoItem;
_retainedResource = widget.resource;
_animationController?.videoItem = videoItem;
_isLoading = false;
});
} else {
final videoItem =
_isNetworkResource
? await SVGAParser.shared.decodeFromURL(widget.resource)
: await SVGAParser.shared.decodeFromAssets(widget.resource);
videoItem.autorelease =false;
//
if (widget.useCache) {
SCGiftVapSvgaManager().videoItemCache[widget.resource] = videoItem;
if (!mounted) {
videoItem.dispose();
return;
}
}
if (mounted) {
setState(() {
_animationController?.videoItem = videoItem;
_isLoading = false;
});
}
//
if (widget.loops == 0) {
_animationController?.repeat();
} else {
_animationController?.repeat(count: widget.loops);
}
//
if (widget.loops == 0) {
_animationController?.repeat();
} else {
_animationController?.repeat(count: widget.loops);
}
if (widget.onFinishLoading != null) {
widget.onFinishLoading!();
}
if (widget.onFinishLoading != null) {
widget.onFinishLoading!();
}
} catch (e) {
if (mounted) {
@ -157,14 +171,16 @@ class _SVGAHeadwearWidgetState extends State<SVGAHeadwearWidget>
_animationController?.stop();
if (widget.clearsAfterStop) {
_animationController?.videoItem = null;
_releaseRetainedMovieEntity();
}
}
//
void reload() {
if (widget.useCache) {
//
SCGiftVapSvgaManager().videoItemCache.remove(widget.resource);
_animationController?.videoItem = null;
_releaseRetainedMovieEntity();
SCGiftVapSvgaManager().evictSvgaEntity(widget.resource);
}
_loadAnimation();
}
@ -173,9 +189,20 @@ class _SVGAHeadwearWidgetState extends State<SVGAHeadwearWidget>
void dispose() {
stop();
_animationController?.dispose();
_releaseRetainedMovieEntity();
super.dispose();
}
void _releaseRetainedMovieEntity({MovieEntity? except}) {
final entity = _retainedMovieEntity;
if (entity == null || identical(entity, except)) {
return;
}
_retainedMovieEntity = null;
_retainedResource = null;
SCGiftVapSvgaManager().releaseSvgaEntity(entity);
}
@override
Widget build(BuildContext context) {
if (_isLoading && !widget.autoPlay) {

View File

@ -270,6 +270,8 @@ class _RoomEntranceWidgetState extends State<RoomEntranceWidget>
bool _isLoading = false;
bool _hasError = false;
String? _currentResource;
MovieEntity? _retainedMovieEntity;
String? _retainedResource;
// SVGAImage组件的key便
GlobalKey _svgaKey = GlobalKey();
@ -351,45 +353,79 @@ class _RoomEntranceWidgetState extends State<RoomEntranceWidget>
_hasError = false;
});
MovieEntity? acquiredMovieEntity;
try {
final isNetworkResource = resource.startsWith('http');
MovieEntity? videoItem;
if (widget.useCache) {
videoItem = SCGiftVapSvgaManager().videoItemCache[resource];
}
final videoItem =
_retainedResource == resource && _retainedMovieEntity != null
? _retainedMovieEntity!
: await SCGiftVapSvgaManager().acquireSvgaEntity(resource);
final acquiredNewEntity = !identical(videoItem, _retainedMovieEntity);
if (acquiredNewEntity) {
acquiredMovieEntity = videoItem;
}
if (!mounted) {
if (acquiredNewEntity) {
SCGiftVapSvgaManager().releaseSvgaEntity(videoItem);
acquiredMovieEntity = null;
}
return;
}
if (videoItem == null) {
videoItem =
//
await _applyDynamicData(videoItem, dynamicData);
if (!mounted) {
if (acquiredNewEntity) {
SCGiftVapSvgaManager().releaseSvgaEntity(videoItem);
acquiredMovieEntity = null;
}
return;
}
setState(() {
_releaseRetainedMovieEntity(except: videoItem);
_retainedMovieEntity = videoItem;
_retainedResource = resource;
_animationController?.videoItem = videoItem;
_isLoading = false;
});
acquiredMovieEntity = null;
} else {
final isNetworkResource = resource.startsWith('http');
final videoItem =
isNetworkResource
? await SVGAParser.shared.decodeFromURL(resource)
: await SVGAParser.shared.decodeFromAssets(resource);
videoItem.autorelease = false;
if (widget.useCache) {
SCGiftVapSvgaManager().videoItemCache[resource] = videoItem;
if (!mounted) {
videoItem.dispose();
return;
}
}
if (mounted) {
//
await _applyDynamicData(videoItem, dynamicData);
if (!mounted) {
videoItem.dispose();
return;
}
setState(() {
_animationController?.videoItem = videoItem;
_isLoading = false;
});
}
//
_startAnimation();
//
_startAnimation();
if (widget.onFinishLoading != null) {
widget.onFinishLoading!();
}
if (widget.onFinishLoading != null) {
widget.onFinishLoading!();
}
} catch (e) {
SCGiftVapSvgaManager().releaseSvgaEntity(acquiredMovieEntity);
if (mounted) {
setState(() {
_animationController?.videoItem = null;
_releaseRetainedMovieEntity();
_isLoading = false;
_hasError = true;
});
@ -543,6 +579,7 @@ class _RoomEntranceWidgetState extends State<RoomEntranceWidget>
_animationController?.stop();
if (widget.clearsAfterStop) {
_animationController?.videoItem = null;
_releaseRetainedMovieEntity();
setState(() {
_currentResource = null;
});
@ -553,7 +590,9 @@ class _RoomEntranceWidgetState extends State<RoomEntranceWidget>
void reload() {
if (_currentResource != null) {
if (widget.useCache) {
SCGiftVapSvgaManager().videoItemCache.remove(_currentResource);
_animationController?.videoItem = null;
_releaseRetainedMovieEntity();
SCGiftVapSvgaManager().evictSvgaEntity(_currentResource!);
}
_loadAnimation(
@ -568,11 +607,22 @@ class _RoomEntranceWidgetState extends State<RoomEntranceWidget>
void dispose() {
stop();
_animationController?.dispose();
_releaseRetainedMovieEntity();
//
_queueManager.clearCurrentPlayer();
super.dispose();
}
void _releaseRetainedMovieEntity({MovieEntity? except}) {
final entity = _retainedMovieEntity;
if (entity == null || identical(entity, except)) {
return;
}
_retainedMovieEntity = null;
_retainedResource = null;
SCGiftVapSvgaManager().releaseSvgaEntity(entity);
}
@override
Widget build(BuildContext context) {
//

View File

@ -26,6 +26,8 @@ class RoomRedPacketOpenDialog extends StatefulWidget {
static final Map<String, num> _claimedAmountByPacketId = {};
static final Set<String> _unavailablePacketIds = {};
static final ValueNotifier<int> claimedStateNotifier = ValueNotifier<int>(0);
static String? _activeDialogPacketId;
static DateTime? _activeDialogRequestedAt;
final RoomRedPacketUiData data;
final List<RoomRedPacketUiData>? dataList;
@ -66,6 +68,12 @@ class RoomRedPacketOpenDialog extends StatefulWidget {
}
}
final nextPacketId = packets[initialIndex].packetId.trim();
if (_isDuplicateDialogRequest(nextPacketId)) {
return;
}
_activeDialogPacketId = nextPacketId;
_activeDialogRequestedAt = DateTime.now();
SmartDialog.dismiss(tag: dialogTag);
SmartDialog.show(
tag: dialogTag,
@ -208,6 +216,17 @@ class RoomRedPacketOpenDialog extends StatefulWidget {
}
}
static bool _isDuplicateDialogRequest(String packetId) {
if (packetId.isEmpty || _activeDialogPacketId != packetId) {
return false;
}
final requestedAt = _activeDialogRequestedAt;
final isRecent =
requestedAt != null &&
DateTime.now().difference(requestedAt) < const Duration(seconds: 1);
return isRecent || SmartDialog.checkExist(tag: dialogTag);
}
@override
State<RoomRedPacketOpenDialog> createState() =>
_RoomRedPacketOpenDialogState();

View File

@ -29,6 +29,8 @@ import 'package:yumi/ui_kit/components/sc_tts.dart';
import 'package:yumi/ui_kit/widgets/room/room_msg_item.dart';
import 'package:yumi/ui_kit/widgets/room/red_packet/room_red_packet_error.dart';
import 'package:yumi/ui_kit/widgets/room/red_packet/room_red_packet_history_page.dart';
import 'package:yumi/ui_kit/widgets/room/red_packet/room_red_packet_models.dart';
import 'package:yumi/ui_kit/widgets/room/red_packet/room_red_packet_open_dialog.dart';
import 'package:yumi/ui_kit/widgets/room/red_packet/room_red_packet_rule_page.dart';
class RoomRedPacketSendPanel extends StatefulWidget {
@ -664,7 +666,8 @@ class _RoomRedPacketSendPanelState extends State<RoomRedPacketSendPanel> {
currentUser?.userNickname ?? result.userName ?? 'Lucky Pack';
final senderAvatar = currentUser?.userAvatar ?? result.userAvatar ?? '';
final claimStartTime = (result.claimStartTime ?? '').trim();
if (_isDelayedRedPacketResult(result) && claimStartTime.isEmpty) {
final isDelayedPacket = _isDelayedRedPacketResult(result);
if (isDelayedPacket && claimStartTime.isEmpty) {
debugPrint(
'[RoomRedPacket][SendFallback] skip delayed local UI because server '
'claimStartTime is empty packetId=$packetId',
@ -718,6 +721,7 @@ class _RoomRedPacketSendPanelState extends State<RoomRedPacketSendPanel> {
rtmProvider.sendRoomRedPacketBroadcast(
payload,
roomGroupId: roomProfile?.roomAccount ?? '',
includeRegionBroadcast: isDelayedPacket,
),
);
OverlayManager().addMessage(
@ -730,15 +734,35 @@ class _RoomRedPacketSendPanelState extends State<RoomRedPacketSendPanel> {
toUserId: packetId,
coins: result.totalAmount ?? _selectedAmount,
number: result.totalCount ?? _selectedCount,
broadcastScope: SCFloatingMessage.broadcastScopeRegion,
broadcastScope:
isDelayedPacket ? SCFloatingMessage.broadcastScopeRegion : '',
priority: 1000,
),
);
if (!isDelayedPacket) {
_showImmediateSentRedPacketDialog(payload);
}
debugPrint(
'[RoomRedPacket][SendFallback] inserted local chat/floating packetId=$packetId roomId=$roomId',
);
}
void _showImmediateSentRedPacketDialog(Map<String, dynamic> payload) {
final dialogData = RoomRedPacketUiData.fromJson(payload);
if (!dialogData.hasPacketId ||
!dialogData.isOpenable ||
RoomRedPacketOpenDialog.isClaimedLocally(dialogData.packetId)) {
return;
}
WidgetsBinding.instance.addPostFrameCallback((_) {
final currentContext = navigatorKey.currentState?.context;
if (currentContext == null || !currentContext.mounted) {
return;
}
RoomRedPacketOpenDialog.show(currentContext, dialogData);
});
}
void _showInsufficientBalanceRechargeSheet() {
SmartDialog.dismiss(tag: RoomRedPacketSendPanel.dialogTag);
_RoomRedPacketRechargeSheet.show(context);

View File

@ -52,6 +52,8 @@ class _SCNetworkSvgaWidgetState extends State<SCNetworkSvgaWidget>
with SingleTickerProviderStateMixin {
late final SVGAAnimationController _controller;
String? _loadedResource;
MovieEntity? _retainedMovieEntity;
String? _retainedResource;
bool _hasError = false;
@override
@ -78,9 +80,10 @@ class _SCNetworkSvgaWidgetState extends State<SCNetworkSvgaWidget>
final resource = widget.resource.trim();
if (resource.isEmpty || !SCNetworkSvgaWidget.isSvga(resource)) {
setState(() {
_controller.videoItem = null;
_releaseRetainedMovieEntity();
_hasError = true;
_loadedResource = null;
_controller.videoItem = null;
});
return;
}
@ -89,37 +92,53 @@ class _SCNetworkSvgaWidgetState extends State<SCNetworkSvgaWidget>
_hasError = false;
});
MovieEntity? acquiredMovieEntity;
try {
final cache = SCGiftVapSvgaManager().videoItemCache;
var movieEntity = cache[resource];
if (movieEntity == null) {
movieEntity =
resource.startsWith("http")
? await SVGAParser.shared.decodeFromURL(resource)
: await SVGAParser.shared.decodeFromAssets(resource);
movieEntity.autorelease = false;
cache[resource] = movieEntity;
final movieEntity =
_retainedResource == resource && _retainedMovieEntity != null
? _retainedMovieEntity!
: await SCGiftVapSvgaManager().acquireSvgaEntity(resource);
final acquiredNewEntity = !identical(movieEntity, _retainedMovieEntity);
if (acquiredNewEntity) {
acquiredMovieEntity = movieEntity;
}
if (!mounted || widget.resource.trim() != resource) {
if (acquiredNewEntity) {
SCGiftVapSvgaManager().releaseSvgaEntity(movieEntity);
acquiredMovieEntity = null;
}
return;
}
if (widget.movieConfigurer != null) {
movieEntity.dynamicItem.reset();
await widget.movieConfigurer!(movieEntity);
}
if (!mounted || widget.resource.trim() != resource) {
if (acquiredNewEntity) {
SCGiftVapSvgaManager().releaseSvgaEntity(movieEntity);
acquiredMovieEntity = null;
}
return;
}
setState(() {
_releaseRetainedMovieEntity(except: movieEntity);
_retainedMovieEntity = movieEntity;
_retainedResource = resource;
_loadedResource = resource;
_controller.videoItem = movieEntity;
});
acquiredMovieEntity = null;
_syncPlayback(restartIfActive: true);
} catch (error) {
SCGiftVapSvgaManager().releaseSvgaEntity(acquiredMovieEntity);
if (!mounted || widget.resource.trim() != resource) {
return;
}
setState(() {
_controller.videoItem = null;
_releaseRetainedMovieEntity();
_hasError = true;
_loadedResource = null;
_controller.videoItem = null;
});
}
}
@ -145,9 +164,20 @@ class _SCNetworkSvgaWidgetState extends State<SCNetworkSvgaWidget>
@override
void dispose() {
_controller.dispose();
_releaseRetainedMovieEntity();
super.dispose();
}
void _releaseRetainedMovieEntity({MovieEntity? except}) {
final entity = _retainedMovieEntity;
if (entity == null || identical(entity, except)) {
return;
}
_retainedMovieEntity = null;
_retainedResource = null;
SCGiftVapSvgaManager().releaseSvgaEntity(entity);
}
@override
Widget build(BuildContext context) {
final videoItem = _controller.videoItem;

View File

@ -8,9 +8,11 @@ typedef SCSvgaMovieConfigurer =
FutureOr<void> Function(MovieEntity movieEntity);
class SCSvgaAssetWidget extends StatefulWidget {
static const int _maxCacheEntries = 8;
static final Map<String, MovieEntity> _cache = <String, MovieEntity>{};
static final Map<String, Future<MovieEntity>> _loadingTasks =
<String, Future<MovieEntity>>{};
static final Map<MovieEntity, int> _activeUseCounts = <MovieEntity, int>{};
const SCSvgaAssetWidget({
super.key,
@ -49,8 +51,26 @@ class SCSvgaAssetWidget extends StatefulWidget {
return _obtainMovieEntity(assetPath);
}
static void clearMemoryCache({bool includeActive = false}) {
final entries = _cache.entries.toList(growable: false);
for (final entry in entries) {
final entity = entry.value;
if (!includeActive && _isActive(entity)) {
continue;
}
_cache.remove(entry.key);
_disposeMovieEntity(entity);
}
}
static Future<MovieEntity> _acquireMovieEntity(String assetPath) async {
final entity = await _obtainMovieEntity(assetPath);
_retainMovieEntity(entity);
return entity;
}
static Future<MovieEntity> _obtainMovieEntity(String assetPath) async {
final cached = _cache[assetPath];
final cached = _touchCachedMovieEntity(assetPath);
if (cached != null) {
return cached;
}
@ -71,7 +91,7 @@ class SCSvgaAssetWidget extends StatefulWidget {
);
}
entity.autorelease = false;
_cache[assetPath] = entity;
_cacheMovieEntity(assetPath, entity);
return entity;
}();
@ -83,6 +103,77 @@ class SCSvgaAssetWidget extends StatefulWidget {
}
}
static MovieEntity? _touchCachedMovieEntity(String assetPath) {
final cached = _cache.remove(assetPath);
if (cached != null) {
_cache[assetPath] = cached;
}
return cached;
}
static void _cacheMovieEntity(String assetPath, MovieEntity entity) {
final previous = _cache.remove(assetPath);
if (previous != null && !identical(previous, entity)) {
_disposeMovieEntityIfInactive(previous);
}
_cache[assetPath] = entity;
_trimCache();
}
static void _trimCache() {
while (_cache.length > _maxCacheEntries) {
final removableEntry = _cache.entries.firstWhere(
(entry) => !_isActive(entry.value),
orElse: () => _cache.entries.first,
);
if (_isActive(removableEntry.value)) {
_cache.remove(removableEntry.key);
_cache[removableEntry.key] = removableEntry.value;
break;
}
_cache.remove(removableEntry.key);
_disposeMovieEntity(removableEntry.value);
}
}
static void _retainMovieEntity(MovieEntity entity) {
_activeUseCounts[entity] = (_activeUseCounts[entity] ?? 0) + 1;
}
static void _releaseMovieEntity(MovieEntity? entity) {
if (entity == null) {
return;
}
final count = _activeUseCounts[entity] ?? 0;
if (count <= 1) {
_activeUseCounts.remove(entity);
if (!_cache.containsValue(entity)) {
_disposeMovieEntity(entity);
} else {
_trimCache();
}
return;
}
_activeUseCounts[entity] = count - 1;
}
static bool _isActive(MovieEntity entity) =>
(_activeUseCounts[entity] ?? 0) > 0;
static void _disposeMovieEntityIfInactive(MovieEntity entity) {
if (_isActive(entity)) {
return;
}
_disposeMovieEntity(entity);
}
static void _disposeMovieEntity(MovieEntity entity) {
try {
_activeUseCounts.remove(entity);
entity.dispose();
} catch (_) {}
}
@override
State<SCSvgaAssetWidget> createState() => _SCSvgaAssetWidgetState();
}
@ -91,6 +182,8 @@ class _SCSvgaAssetWidgetState extends State<SCSvgaAssetWidget>
with SingleTickerProviderStateMixin {
late final SVGAAnimationController _controller;
String? _loadedAssetPath;
MovieEntity? _retainedMovieEntity;
String? _retainedAssetPath;
bool _hasError = false;
@override
@ -131,25 +224,44 @@ class _SCSvgaAssetWidgetState extends State<SCSvgaAssetWidget>
setState(() {
_hasError = false;
});
MovieEntity? acquiredMovieEntity;
try {
final movieEntity = await SCSvgaAssetWidget._obtainMovieEntity(assetPath);
final movieEntity =
_retainedAssetPath == assetPath && _retainedMovieEntity != null
? _retainedMovieEntity!
: await SCSvgaAssetWidget._acquireMovieEntity(assetPath);
final acquiredNewEntity = !identical(movieEntity, _retainedMovieEntity);
if (acquiredNewEntity) {
acquiredMovieEntity = movieEntity;
}
if (widget.movieConfigurer != null) {
movieEntity.dynamicItem.reset();
await widget.movieConfigurer!(movieEntity);
}
if (!mounted || widget.assetPath != assetPath) {
if (acquiredNewEntity) {
SCSvgaAssetWidget._releaseMovieEntity(movieEntity);
acquiredMovieEntity = null;
}
return;
}
setState(() {
_releaseRetainedMovieEntity(except: movieEntity);
_retainedMovieEntity = movieEntity;
_retainedAssetPath = assetPath;
_loadedAssetPath = assetPath;
_controller.videoItem = movieEntity;
});
acquiredMovieEntity = null;
_syncPlayback(restartIfActive: true);
} catch (error) {
SCSvgaAssetWidget._releaseMovieEntity(acquiredMovieEntity);
if (!mounted || widget.assetPath != assetPath) {
return;
}
setState(() {
_controller.videoItem = null;
_releaseRetainedMovieEntity();
_hasError = true;
});
}
@ -185,9 +297,20 @@ class _SCSvgaAssetWidgetState extends State<SCSvgaAssetWidget>
void dispose() {
_controller.removeStatusListener(_handleAnimationStatusChanged);
_controller.dispose();
_releaseRetainedMovieEntity();
super.dispose();
}
void _releaseRetainedMovieEntity({MovieEntity? except}) {
final entity = _retainedMovieEntity;
if (entity == null || identical(entity, except)) {
return;
}
_retainedMovieEntity = null;
_retainedAssetPath = null;
SCSvgaAssetWidget._releaseMovieEntity(entity);
}
@override
Widget build(BuildContext context) {
if (_hasError) {

View File

@ -16,7 +16,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
# In Windows, build-name is used as the major, minor, and patch parts
# of the product and file versions while build-number is used as the build suffix.
version: 1.3.0+11
version: 1.3.1+12
environment:

View File

@ -0,0 +1,26 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:yumi/shared/data_sources/sources/local/data_persistence.dart';
import 'package:yumi/shared/data_sources/sources/local/user_manager.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
tearDown(() {
DataPersistence.reset();
});
test('corrupted cached user session is ignored and cleared', () async {
SharedPreferences.setMockInitialValues({
'token': 'stale-token',
'currentUser': '{bad json',
});
await DataPersistence.initialize();
expect(AccountStorage().getCurrentUser(), isNull);
await Future<void>.delayed(Duration.zero);
expect(DataPersistence.getToken(), isEmpty);
expect(DataPersistence.getCurrentUser(), isEmpty);
});
}